[ADDITION] added a new property to the user (may be removed later on), and made a check for the random word
This commit is contained in:
@@ -20,7 +20,7 @@ namespace Client
|
|||||||
public delegate void CanvasDataReceived(double[][] coordinates, Color color);
|
public delegate void CanvasDataReceived(double[][] coordinates, Color color);
|
||||||
public delegate void CanvasReset();
|
public delegate void CanvasReset();
|
||||||
public delegate void LobbyCallback(int id);
|
public delegate void LobbyCallback(int id);
|
||||||
|
|
||||||
|
|
||||||
class Client : ObservableObject
|
class Client : ObservableObject
|
||||||
{
|
{
|
||||||
@@ -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,10 +114,11 @@ 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,31 @@ 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;
|
||||||
}
|
}
|
||||||
SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE_RECEIVED,null));
|
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");
|
||||||
@@ -228,7 +237,7 @@ namespace Client
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendMessage(byte[] message)
|
public void SendMessage(byte[] message)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ 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;
|
||||||
public User User { get; set; }
|
public User User { get; set; }
|
||||||
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
|
||||||
@@ -145,6 +146,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;
|
||||||
@@ -234,11 +240,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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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