Merge pull request #13 from SemvdH/feature/timer

Feature/timer
This commit is contained in:
SemvdH
2020-10-23 15:45:12 +02:00
committed by GitHub
4 changed files with 88 additions and 6 deletions

View File

@@ -209,6 +209,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));

View File

@@ -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<System.Timers.Timer, int> lobbyTimers;
public User User { get; set; }
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
private Callback OnMessageReceivedOk;
@@ -34,6 +36,7 @@ namespace Server.Models
/// <param name="client">the TcpClient object to use</param>
public ServerClient(TcpClient client)
{
lobbyTimers = new Dictionary<System.Timers.Timer, int>();
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)

View File

@@ -142,7 +142,7 @@ namespace Server.Models
{
foreach (ServerClient sc in serverClientsInlobbies[l])
{
Debug.WriteLine("[SERVERCLIENT] Sending message");
Debug.WriteLine("[SERVERCLIENT] Sending message to lobby");
sc.sendMessage(message);
}
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)
{
foreach (Lobby l in lobbies)
@@ -261,6 +277,10 @@ namespace Server.Models
break;
}
}
if (l.Users.Count != 0)
{
l.Users[0].Host = true;
}
break;
}
}

View File

@@ -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;