Merge branch 'lobbies' into setupBranch
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
|
||||
using Client;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Server.Models
|
||||
{
|
||||
@@ -121,6 +123,8 @@ namespace Server.Models
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
// lobby data
|
||||
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
||||
handleLobbyMessage(payload,l);
|
||||
break;
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
@@ -130,7 +134,30 @@ namespace Server.Models
|
||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||
break;
|
||||
}
|
||||
//TODO implement ways to handle the message
|
||||
}
|
||||
|
||||
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
|
||||
{
|
||||
switch (l)
|
||||
{
|
||||
case LobbyIdentifier.REQUEST:
|
||||
Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies...");
|
||||
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
case LobbyIdentifier.HOST:
|
||||
// add new lobby and add this serverclient to it
|
||||
int createdLobbyID = ServerCommunication.INSTANCE.HostForLobby(this.User);
|
||||
Debug.WriteLine("[SERVERCLIENT] created lobby");
|
||||
sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(createdLobbyID));
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
case LobbyIdentifier.JOIN:
|
||||
int id = JSONConvert.GetLobbyID(payload);
|
||||
ServerCommunication.INSTANCE.JoinLobby(this.User,id);
|
||||
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -32,7 +32,10 @@ namespace Server.Models
|
||||
listener = new TcpListener(IPAddress.Any, port);
|
||||
serverClients = new List<ServerClient>();
|
||||
lobbies = new List<Lobby>();
|
||||
Lobby temp = new Lobby(1, 7, 8);
|
||||
lobbies.Add(temp);
|
||||
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
||||
serverClientsInlobbies.Add(temp, new List<ServerClient>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -117,6 +120,18 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public Lobby GetLobbyForUser(User user)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l.Users.Contains(user))
|
||||
{
|
||||
return l;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void AddToLobby(Lobby lobby, User user)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
@@ -125,6 +140,7 @@ namespace Server.Models
|
||||
{
|
||||
bool succ;
|
||||
l.AddUser(user, out succ);
|
||||
Debug.WriteLine("[SERVERCOMM] added user to lobby, now contains " + l.PlayersIn);
|
||||
if (!succ)
|
||||
{
|
||||
// TODO send lobby full message
|
||||
@@ -144,5 +160,28 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int HostForLobby(User user)
|
||||
{
|
||||
Lobby lobby = new Lobby( lobbies.Count + 1,0, 8);
|
||||
lobbies.Add(lobby);
|
||||
serverClientsInlobbies.Add(lobby, new List<ServerClient>());
|
||||
user.Host = true;
|
||||
AddToLobby(lobby, user);
|
||||
return lobby.ID;
|
||||
}
|
||||
|
||||
public void JoinLobby(User user, int id)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l.ID == id)
|
||||
{
|
||||
AddToLobby(l, user);
|
||||
Debug.WriteLine($"{user.Username} joined lobby with id {id}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user