added methods to send to all in lobby

This commit is contained in:
Sem van der Hoeven
2020-10-13 13:28:18 +02:00
parent 52e83f111a
commit 1ae1b628f3
11 changed files with 193 additions and 6 deletions

View File

@@ -8,6 +8,15 @@ namespace SharedClientServer
{
class ClientServerUtil
{
// creates a message array to send to the server or to clients
public byte[] createPayload(byte id, string payload)
{
byte[] stringAsBytes = Encoding.ASCII.GetBytes(payload);
byte[] res = new byte[stringAsBytes.Length + 1];
res[0] = id;
Array.Copy(stringAsBytes, 0, res, 1, stringAsBytes.Length);
return res;
}
}
}

View File

@@ -13,5 +13,12 @@ namespace SharedClientServer
dynamic payload = JsonConvert.DeserializeObject(msg);
return (payload.username, payload.message);
}
public static string GetUsernameLogin(byte[] json)
{
string msg = Encoding.ASCII.GetString(json);
dynamic payload = JsonConvert.DeserializeObject(msg);
return payload.username;
}
}
}

View File

@@ -13,12 +13,24 @@ namespace Client
private int _id;
private int _playersIn;
private int _maxPlayers;
private List<string> _usernames;
public void AddUsername(string username, out bool success)
{
success = false;
if (_usernames.Count < _maxPlayers)
{
_usernames.Add(username);
success = true;
}
}
public Lobby(int id, int playersIn, int maxPlayers)
{
_id = id;
_playersIn = playersIn;
_maxPlayers = maxPlayers;
_usernames = new List<string>();
}
public int ID

View File

@@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientServerUtil.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JSONConvert.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Lobby.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObservableObject.cs" />
</ItemGroup>
</Project>