@@ -209,6 +209,17 @@ namespace Client
|
|||||||
default:
|
default:
|
||||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
|
case JSONConvert.GAME:
|
||||||
|
switch (JSONConvert.GetGameCommand(payload))
|
||||||
|
{
|
||||||
|
case JSONConvert.GameCommand.TIMER_ELAPSED:
|
||||||
|
int lobbyElapsedID = JSONConvert.GetLobbyID(payload);
|
||||||
|
|
||||||
|
//todo set next round
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED,null));
|
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED,null));
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Timers;
|
||||||
using static SharedClientServer.JSONConvert;
|
using static SharedClientServer.JSONConvert;
|
||||||
|
|
||||||
namespace Server.Models
|
namespace Server.Models
|
||||||
@@ -23,6 +24,7 @@ namespace Server.Models
|
|||||||
private byte[] buffer = new byte[2048];
|
private byte[] buffer = new byte[2048];
|
||||||
private byte[] totalBuffer = new byte[2048];
|
private byte[] totalBuffer = new byte[2048];
|
||||||
private int totalBufferReceived = 0;
|
private int totalBufferReceived = 0;
|
||||||
|
private Dictionary<System.Timers.Timer, int> lobbyTimers;
|
||||||
public User User { get; set; }
|
public User User { get; set; }
|
||||||
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
||||||
private Callback OnMessageReceivedOk;
|
private Callback OnMessageReceivedOk;
|
||||||
@@ -34,6 +36,7 @@ namespace Server.Models
|
|||||||
/// <param name="client">the TcpClient object to use</param>
|
/// <param name="client">the TcpClient object to use</param>
|
||||||
public ServerClient(TcpClient client)
|
public ServerClient(TcpClient client)
|
||||||
{
|
{
|
||||||
|
lobbyTimers = new Dictionary<System.Timers.Timer, int>();
|
||||||
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
|
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
|
||||||
tcpClient = client;
|
tcpClient = client;
|
||||||
stream = tcpClient.GetStream();
|
stream = tcpClient.GetStream();
|
||||||
@@ -186,14 +189,35 @@ namespace Server.Models
|
|||||||
|
|
||||||
case JSONConvert.GAME:
|
case JSONConvert.GAME:
|
||||||
Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic");
|
Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic");
|
||||||
string command = JSONConvert.GetGameCommand(payload);
|
GameCommand command = JSONConvert.GetGameCommand(payload);
|
||||||
switch (command)
|
switch (command)
|
||||||
{
|
{
|
||||||
case "startGame":
|
case GameCommand.START_GAME:
|
||||||
int lobbyID = JSONConvert.GetStartGameLobbyID(payload);
|
int lobbyID = JSONConvert.GetStartGameLobbyID(payload);
|
||||||
serverCom.CloseALobby(lobbyID);
|
serverCom.CloseALobby(lobbyID);
|
||||||
|
//todo start a timer for this lobby
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] making timer for lobby " + lobbyID);
|
||||||
|
System.Timers.Timer lobbyTimer = new System.Timers.Timer(60 * 1000);
|
||||||
|
this.lobbyTimers.Add(lobbyTimer, lobbyID);
|
||||||
|
lobbyTimer.Elapsed += LobbyTimer_Elapsed;
|
||||||
|
lobbyTimer.Start();
|
||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
break;
|
break;
|
||||||
|
case GameCommand.TIMER_ELAPSED:
|
||||||
|
|
||||||
|
break;
|
||||||
|
case GameCommand.NEXT_ROUND:
|
||||||
|
// The next round has been started, so we can start the timer again
|
||||||
|
lobbyID = JSONConvert.GetLobbyID(payload);
|
||||||
|
foreach (System.Timers.Timer timer in lobbyTimers.Keys)
|
||||||
|
{
|
||||||
|
if (lobbyTimers[timer] == lobbyID)
|
||||||
|
{
|
||||||
|
timer.Start();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -211,6 +235,17 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LobbyTimer_Elapsed(object sender, ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
System.Timers.Timer timer = sender as System.Timers.Timer;
|
||||||
|
int lobbyID = lobbyTimers[timer];
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] timer elapsed for lobby " + lobbyID);
|
||||||
|
serverCom.SendToLobby(lobbyID, JSONConvert.ConstructGameTimerElapsedMessage(lobbyID));
|
||||||
|
timer.Stop();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
|
private void handleLobbyMessage(byte[] payload, LobbyIdentifier l)
|
||||||
{
|
{
|
||||||
switch (l)
|
switch (l)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ namespace Server.Models
|
|||||||
{
|
{
|
||||||
foreach (ServerClient sc in serverClientsInlobbies[l])
|
foreach (ServerClient sc in serverClientsInlobbies[l])
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[SERVERCLIENT] Sending message");
|
Debug.WriteLine("[SERVERCLIENT] Sending message to lobby");
|
||||||
sc.sendMessage(message);
|
sc.sendMessage(message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -150,6 +150,22 @@ namespace Server.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendToLobby(int lobbyID, byte[] message)
|
||||||
|
{
|
||||||
|
foreach (Lobby l in lobbies)
|
||||||
|
{
|
||||||
|
if (l.ID == lobbyID)
|
||||||
|
{
|
||||||
|
foreach (ServerClient sc in serverClientsInlobbies[l])
|
||||||
|
{
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] Sending message to lobby");
|
||||||
|
sc.sendMessage(message);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void SendCanvasDataToLobby(Lobby lobby, string username, byte[] message)
|
public void SendCanvasDataToLobby(Lobby lobby, string username, byte[] message)
|
||||||
{
|
{
|
||||||
foreach (Lobby l in lobbies)
|
foreach (Lobby l in lobbies)
|
||||||
@@ -261,6 +277,10 @@ namespace Server.Models
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (l.Users.Count != 0)
|
||||||
|
{
|
||||||
|
l.Users[0].Host = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,13 @@ namespace SharedClientServer
|
|||||||
REQUEST
|
REQUEST
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum GameCommand
|
||||||
|
{
|
||||||
|
START_GAME,
|
||||||
|
TIMER_ELAPSED,
|
||||||
|
NEXT_ROUND
|
||||||
|
}
|
||||||
|
|
||||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||||
{
|
{
|
||||||
string msg = Encoding.UTF8.GetString(json);
|
string msg = Encoding.UTF8.GetString(json);
|
||||||
@@ -205,15 +212,24 @@ namespace SharedClientServer
|
|||||||
|
|
||||||
public static byte[] ConstructGameStartData(int lobbyID)
|
public static byte[] ConstructGameStartData(int lobbyID)
|
||||||
{
|
{
|
||||||
string startGame = "startGame";
|
|
||||||
return GetMessageToSend(GAME, new
|
return GetMessageToSend(GAME, new
|
||||||
{
|
{
|
||||||
command = startGame,
|
command = GameCommand.START_GAME,
|
||||||
lobbyToStart = lobbyID
|
lobbyToStart = lobbyID
|
||||||
}); ;
|
}); ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetGameCommand(byte[] payload)
|
public static byte[] ConstructGameTimerElapsedMessage(int lobbyID)
|
||||||
|
{
|
||||||
|
return GetMessageToSend(GAME, new
|
||||||
|
{
|
||||||
|
command = GameCommand.TIMER_ELAPSED,
|
||||||
|
id = lobbyID
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GameCommand GetGameCommand(byte[] payload)
|
||||||
{
|
{
|
||||||
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
|
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
|
||||||
return json.command;
|
return json.command;
|
||||||
|
|||||||
Reference in New Issue
Block a user