Merge branch 'master' into setupBranch
This commit is contained in:
@@ -70,9 +70,10 @@ namespace Client
|
|||||||
OnSuccessfullConnect?.Invoke();
|
OnSuccessfullConnect?.Invoke();
|
||||||
OnLobbyUpdate = updateGameLobby;
|
OnLobbyUpdate = updateGameLobby;
|
||||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||||
|
|
||||||
} catch (Exception e)
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Can't connect, retrying...");
|
Debug.WriteLine("Can't connect, retrying...");
|
||||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||||
@@ -113,7 +114,8 @@ namespace Client
|
|||||||
|
|
||||||
ar.AsyncWaitHandle.WaitOne();
|
ar.AsyncWaitHandle.WaitOne();
|
||||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||||
} catch (IOException e)
|
}
|
||||||
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[CLIENT] server not responding! got error: " + e.Message);
|
Debug.WriteLine("[CLIENT] server not responding! got error: " + e.Message);
|
||||||
OnServerDisconnect?.Invoke();
|
OnServerDisconnect?.Invoke();
|
||||||
@@ -139,11 +141,16 @@ namespace Client
|
|||||||
string textUsername = combo.Item1;
|
string textUsername = combo.Item1;
|
||||||
string textMsg = combo.Item2;
|
string textMsg = combo.Item2;
|
||||||
|
|
||||||
if(textUsername != data.User.Username)
|
if (textUsername != data.User.Username)
|
||||||
{
|
{
|
||||||
IncomingMsg?.Invoke(textUsername, textMsg);
|
IncomingMsg?.Invoke(textUsername, textMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (textMsg == data.User.RandomWord && !string.IsNullOrEmpty(data.User.RandomWord))
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"[CLIENT] word has been guessed! {data.User.Username} + Word: {data.User.RandomWord}");
|
||||||
|
}
|
||||||
|
|
||||||
//TODO display username and message in chat window
|
//TODO display username and message in chat window
|
||||||
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
|
||||||
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
|
||||||
@@ -191,29 +198,42 @@ namespace Client
|
|||||||
|
|
||||||
case JSONConvert.CANVAS_WRITING:
|
case JSONConvert.CANVAS_WRITING:
|
||||||
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
|
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case JSONConvert.RANDOMWORD:
|
case JSONConvert.RANDOMWORD:
|
||||||
//Flag byte for receiving the random word.
|
//Flag byte for receiving the random word.
|
||||||
int lobbyId = JSONConvert.GetLobbyID(payload);
|
int lobbyId = JSONConvert.GetLobbyID(payload);
|
||||||
string randomWord = JSONConvert.GetRandomWord(payload);
|
data.User.RandomWord = JSONConvert.GetRandomWord(payload);
|
||||||
|
data.User.TurnToDraw = true; // Dit is test code, dit kan weg zodra alles lopende is.
|
||||||
if (data.Lobby?.ID == lobbyId)
|
if (data.Lobby?.ID == lobbyId && data.User.TurnToDraw)
|
||||||
RandomWord?.Invoke(randomWord);
|
RandomWord?.Invoke(data.User.RandomWord);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED,null));
|
break;
|
||||||
|
}
|
||||||
|
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED, null));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Updates the current lobby with the joining players,
|
||||||
|
* their player score is also tracked and should always be zero.
|
||||||
|
*/
|
||||||
private void updateGameLobby()
|
private void updateGameLobby()
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[CLIENT] updating game lobby");
|
Debug.WriteLine("[CLIENT] updating game lobby");
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -22,7 +23,9 @@ namespace Server.Models
|
|||||||
private NetworkStream stream;
|
private NetworkStream stream;
|
||||||
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 string _randomWord = "";
|
||||||
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 +37,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();
|
||||||
@@ -145,6 +149,11 @@ namespace Server.Models
|
|||||||
message = textMsg
|
message = textMsg
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (textMsg == _randomWord && !string.IsNullOrEmpty(_randomWord))
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"[SERVERCLIENT] word has been guessed! {User.Username} + Word: {_randomWord}");
|
||||||
|
}
|
||||||
|
|
||||||
//Sends the incomming message to be broadcast to all of the clients inside the current lobby.
|
//Sends the incomming message to be broadcast to all of the clients inside the current lobby.
|
||||||
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet));
|
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, packet));
|
||||||
break;
|
break;
|
||||||
@@ -186,14 +195,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 +241,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)
|
||||||
@@ -234,11 +275,11 @@ namespace Server.Models
|
|||||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
OnMessageReceivedOk = () =>
|
OnMessageReceivedOk = () =>
|
||||||
{
|
{
|
||||||
|
_randomWord = JSONConvert.SendRandomWord("WordsForGame.json");
|
||||||
serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
|
serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
|
||||||
{
|
{
|
||||||
id = serverCom.GetLobbyForUser(User).ID,
|
id = serverCom.GetLobbyForUser(User).ID,
|
||||||
word = JSONConvert.SendRandomWord("WordsForGame.json")
|
word = _randomWord
|
||||||
}));
|
}));
|
||||||
OnMessageReceivedOk = null;
|
OnMessageReceivedOk = null;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -142,7 +142,23 @@ 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);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
sc.sendMessage(message);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace SharedClientServer
|
|||||||
private bool _host;
|
private bool _host;
|
||||||
private bool _turnToDraw;
|
private bool _turnToDraw;
|
||||||
private string _message;
|
private string _message;
|
||||||
|
private string _randomWord;
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public User(string username, int score, bool host, bool turnToDraw)
|
public User(string username, int score, bool host, bool turnToDraw)
|
||||||
@@ -90,5 +91,10 @@ namespace SharedClientServer
|
|||||||
get { return _turnToDraw; }
|
get { return _turnToDraw; }
|
||||||
set { _turnToDraw = value; }
|
set { _turnToDraw = value; }
|
||||||
}
|
}
|
||||||
|
public string RandomWord
|
||||||
|
{
|
||||||
|
get { return _randomWord; }
|
||||||
|
set { _randomWord = value; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user