full functionality

This commit is contained in:
Baipyrus 2022-07-01 16:23:16 +02:00
parent af07caaffc
commit 30e22b76a9
5 changed files with 206 additions and 9 deletions

7
.gitignore vendored
View File

@ -1,2 +1,5 @@
bin/*
obj/*
client/bin/*
client/obj/*
server/bin/*
server/obj/*

View File

@ -14,6 +14,8 @@ namespace SchiffeVersenken {
private static int[] schiffe = {5, 4, 4, 3, 3, 3, 2, 2, 2, 2};
private static bool lastShot = false, isMultiplayer = false;
private static int multiplayerState = 0;
public static string lobbyCode = "";
private static bool fullLobby = false;
private static void Main() {
// Konsolen Schriftgröße 13 ist zu empfehlen, 16 ist aber Standard.
@ -122,7 +124,7 @@ namespace SchiffeVersenken {
Connection.Send(Connection.self, $"Shoot {ux},{uy}");
// multiplayerShot(ux, uy);
while (multiplayerState < 2)
Thread.Sleep(100);
Thread.Sleep(250);
multiplayerState = 0;
zuege++;
}
@ -133,6 +135,7 @@ namespace SchiffeVersenken {
}
private static void confirmShot(Socket other, string rest) {
// Handle confirming a hit
string[] lmSplit = rest.Split(':');
string[] koordsSplit = lmSplit[0].Split(';');
int x = int.TryParse(koordsSplit[0], out x) ? x : 0;
@ -154,7 +157,7 @@ namespace SchiffeVersenken {
}
public static void receiveShot(Socket other, string rest) {
// Handle data / Game Logic
// Handle being shot at
multiplayerState++;
string[] coordSplit = rest.Split(',');
int x = int.Parse(coordSplit[0]);
@ -166,11 +169,77 @@ namespace SchiffeVersenken {
Connection.Send(Connection.self, $"Response {x};{y}:{hit};{winCon}");
}
private static void readLobbyCode(Socket other, string rest) {
lobbyCode = rest;
Console.WriteLine($"Eine Lobby mit Code '{rest}' wurde erstellt.");
}
private static void startMultiplayer(Socket other) {
fullLobby = true;
}
private static bool multiplayerConnect() {
// Ask for user input for IP address and port number
Console.WriteLine("Angabe der IP Adresse und Portnummer des anderen Spielers");
Console.Write("( Format 'IPv4:Port' ): ");
string ipIn = Console.ReadLine();
if (ipIn.ToLower() == "exit")
return false;
Connection.ClientConnect(ipIn);
// Handle lobby-connect
while (true) {
Console.Clear();
Console.WriteLine("################################");
Console.WriteLine("Wählen der Verbindungsart:");
Console.WriteLine("0) Zurück");
Console.WriteLine("1) Lobby erstellen");
Console.WriteLine("2) Lobby beitreten");
Console.WriteLine("################################");
Console.WriteLine();
Console.Write("Wahl (0-2): ");
string input = Console.ReadLine();
// Handle user input with switch statement
bool ready = true;
switch (input) {
default:
Console.Write("Ungültige Eingabe oder Fehler. Bitte erneut versuchen. ");
Console.ReadKey(true);
ready = false;
break;
case "0":
return false;
case "1":
Connection.Send(Connection.self, "NewLobby");
break;
case "2":
// Ask for user input for lobby code
Console.Write("Angabe des Lobby Codes auf dem Server: ");
string lcIn = Console.ReadLine();
if (lcIn.ToLower() == "exit") {
ready = false;
break;
}
Connection.Send(Connection.self, $"JoinLobby {lcIn}");
break;
}
if (ready)
break;
}
while (!fullLobby)
Thread.Sleep(250);
return true;
}
private static void multiplayerInitialize() {
// Verbindung über Sockets aufgbauen
Connection.Initialize();
Connection.messageEvents.Add(new MessageEvent("Response", confirmShot));
Connection.messageEvents.Add(new MessageEvent("Shoot", receiveShot));
Connection.messageEvents.Add(new MessageEvent("Created", readLobbyCode));
Connection.messageEvents.Add(new MessageEvent("FullLobby", startMultiplayer));
while (Connection.self == null || !Connection.self.Connected) {
Console.Clear();
@ -184,7 +253,7 @@ namespace SchiffeVersenken {
Console.WriteLine("3) Mit Server verbinden");
Console.WriteLine("################################");
Console.WriteLine();
Console.Write("Wahl (0-2): ");
Console.Write("Wahl (0-3): ");
string input = Console.ReadLine();
// Handle user input with switch statement
@ -202,7 +271,6 @@ namespace SchiffeVersenken {
string ipIn = Console.ReadLine();
if (ipIn.ToLower() == "exit")
return;
Connection.ClientConnect(ipIn);
break;
case "2":
@ -211,8 +279,9 @@ namespace SchiffeVersenken {
Connection.ClientListen();
break;
case "3":
Console.WriteLine("Not yet implemented!");
Console.ReadKey(true);
// Connect player to server and find/create lobby
if (!multiplayerConnect())
return;
break;
}
}

View File

@ -2,7 +2,7 @@
<!-- Reference to local copy of https://gitlab1.ptb.de/waltem01/csharpsocketserver -->
<ItemGroup>
<ProjectReference Include="..\SocketTest\Net.Sockets\SocketLib\SocketLib.csproj" />
<ProjectReference Include="..\..\SocketTest\Net.Sockets\SocketLib\SocketLib.csproj" />
</ItemGroup>
<PropertyGroup>

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Reference to local copy of https://gitlab1.ptb.de/waltem01/csharpsocketserver -->
<ItemGroup>
<ProjectReference Include="..\..\SocketTest\Net.Sockets\SocketLib\SocketLib.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>
</Project>

105
server/Server.cs Normal file
View File

@ -0,0 +1,105 @@
using System.Net.Sockets;
using SocketLib;
// Initialize global variables and Server
List<Lobby> lobbies = new List<Lobby>();
Connection.Initialize();
Connection.disconnectMethod = removeFromLobby;
// Create onEvent methods
Connection.messageEvents.Add(new MessageEvent("NewLobby", onNewLobby));
Connection.messageEvents.Add(new MessageEvent("JoinLobby", onJoinLobby));
// Redirect other messages
Connection.messageEvents.Add(new MessageEvent("Response", responseRedirect));
Connection.messageEvents.Add(new MessageEvent("Shoot", shootRedirect));
// Start the Server itself
Console.WriteLine("Server Started . . . ");
Console.WriteLine($"Using {Connection.ipAddress}:{Connection.port}");
Connection.ServerStart();
void responseRedirect(Socket other, string rest) {
redirect(other, $"Response {rest}");
}
void shootRedirect(Socket other, string rest) {
redirect(other, $"Shoot {rest}");
}
void redirect(Socket other, string message) {
foreach (Lobby l in lobbies)
if (l.users.Contains(other)) {
foreach (Socket u in l.users)
if (!u.Equals(other))
Connection.Send(u, message);
}
}
void onNewLobby(Socket other) {
// Create a new Lobby
Random rng = new Random();
string lobbyCode = "";
while (lobbyCode.Length < 6) {
if (rng.Next(2) == 0)
lobbyCode += (char)rng.Next(48, 58);
else
lobbyCode += (char)rng.Next(65, 91);
}
lobbies.Add(new Lobby(other, lobbyCode));
Connection.Send(other, $"Created {lobbyCode}");
}
void onJoinLobby(Socket other, string lobbyCode) {
// Add user to lobby or else give feedback
string response = "";
Lobby? temp = null;
foreach (Lobby l in lobbies) {
if (l.users.Contains(other)) {
response = $"Already in a Lobby";
break;
} else if (l.code == lobbyCode && l.users.Count < 4) {
l.users.Add(other);
response = $"Joined {lobbyCode}";
temp = l;
break;
}
}
// Handle feedback for Lobbies
if (response == "")
response = "No such lobby code.";
Connection.Send(other, response);
if (temp != null)
if (temp.users.Count == 2)
for (int i = 0; i < temp.users.Count; i++)
Connection.Send(temp.users[i], "FullLobby");
}
void removeFromLobby(Socket self) {
// Remove client from lobby, and lobby from list
foreach (Lobby l in lobbies) {
if (l.users.Contains(self)) {
l.users.Remove(self);
Console.WriteLine("Removed Client");
}
if (l.users.Count == 0) {
lobbies.Remove(l);
Console.WriteLine("Closed Lobby.");
}
}
}
// Custom Lobby class
class Lobby {
// Keep track of own lobby code and list of users
public List<Socket> users;
public string code;
// Initialize Lobby with current user in list and given lobby code
public Lobby(Socket other, string lobbyCode) {
users = new List<Socket>();
users.Add(other);
code = lobbyCode;
}
}