From bf09dba8503c55483507d274e1592df968d75dae Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 23 Oct 2020 15:44:18 +0200 Subject: [PATCH] [ADD] added timer start on next round message receive --- Client/Client.cs | 11 ++++++++ Server/Models/ServerClient.cs | 39 ++++++++++++++++++++++++++-- Server/Models/ServerCommunication.cs | 16 ++++++++++++ SharedClientServer/JSONConvert.cs | 22 +++++++++++++--- 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index d3788f3..fd54caa 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -210,6 +210,17 @@ namespace Client default: Debug.WriteLine("[CLIENT] Received weird identifier: " + id); 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)); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 57bb6cb..8af3a86 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -11,6 +11,7 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Timers; using static SharedClientServer.JSONConvert; namespace Server.Models @@ -23,6 +24,7 @@ namespace Server.Models private byte[] buffer = new byte[2048]; private byte[] totalBuffer = new byte[2048]; private int totalBufferReceived = 0; + private Dictionary lobbyTimers; public User User { get; set; } private ServerCommunication serverCom = ServerCommunication.INSTANCE; private Callback OnMessageReceivedOk; @@ -34,6 +36,7 @@ namespace Server.Models /// the TcpClient object to use public ServerClient(TcpClient client) { + lobbyTimers = new Dictionary(); Debug.WriteLine("[SERVERCLIENT] making new instance and starting"); tcpClient = client; stream = tcpClient.GetStream(); @@ -186,14 +189,35 @@ namespace Server.Models case JSONConvert.GAME: Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic"); - string command = JSONConvert.GetGameCommand(payload); + GameCommand command = JSONConvert.GetGameCommand(payload); switch (command) { - case "startGame": + case GameCommand.START_GAME: int lobbyID = JSONConvert.GetStartGameLobbyID(payload); 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())); 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; @@ -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) { switch (l) diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index e70d376..3dbdb8e 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -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) { foreach (Lobby l in lobbies) diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 17e22f4..1f11b5a 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -35,6 +35,13 @@ namespace SharedClientServer REQUEST } + public enum GameCommand + { + START_GAME, + TIMER_ELAPSED, + NEXT_ROUND + } + public static (string,string) GetUsernameAndMessage(byte[] json) { string msg = Encoding.UTF8.GetString(json); @@ -205,15 +212,24 @@ namespace SharedClientServer public static byte[] ConstructGameStartData(int lobbyID) { - string startGame = "startGame"; + return GetMessageToSend(GAME, new { - command = startGame, + command = GameCommand.START_GAME, 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)); return json.command;