added updating scores on lobby list receive

This commit is contained in:
Sem van der Hoeven
2020-10-23 13:58:44 +02:00
parent 5c2dea3ab4
commit da5528c7b2
3 changed files with 41 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ namespace Client
public delegate void CanvasDataReceived(double[][] coordinates, Color color);
public delegate void CanvasReset();
public delegate void LobbyCallback(int id);
class Client : ObservableObject
{
@@ -48,6 +49,7 @@ namespace Client
private ClientData data = ClientData.Instance;
public CanvasDataReceived CanvasDataReceived;
public CanvasReset CReset;
public HandleIncomingPlayer UpdateUserScores;
public Lobby[] Lobbies { get; set; }
public Client(string username)
@@ -190,7 +192,6 @@ namespace Client
case JSONConvert.CANVAS_WRITING:
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
// we hebben gedrawed, dus stuur dat we weer kunnen drawen
break;
}
@@ -216,11 +217,16 @@ namespace Client
private void updateGameLobby()
{
Debug.WriteLine("[CLIENT] updating game lobby");
foreach (var item in Lobbies)
{
Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count);
if (item.ID == data.Lobby?.ID)
IncomingPlayer?.Invoke(item);
{
//IncomingPlayer?.Invoke(item);
UpdateUserScores?.Invoke(item as Lobby);
}
}
}

View File

@@ -112,7 +112,7 @@ namespace Client
private void updateLobbies()
{
Debug.WriteLine("updating lobbies...");
Debug.WriteLine("[VIEWMODEL] updating lobbies...");
Lobby[] lobbiesArr = client.Lobbies;
Application.Current.Dispatcher.Invoke(delegate
{
@@ -122,6 +122,11 @@ namespace Client
foreach (Lobby l in lobbiesArr)
{
_lobbies.Add(l);
Lobby clientLobby = ClientData.Instance.Lobby;
if (l.ID == clientLobby?.ID)
{
clientLobby = l;
}
}
});

View File

@@ -91,6 +91,7 @@ namespace Client.ViewModels
data.Client.RandomWord = HandleRandomWord;
data.Client.IncomingMsg = HandleIncomingMsg;
data.Client.IncomingPlayer = HandleIncomingPlayer;
data.Client.UpdateUserScores = UpdateUserScores;
}
public ICommand OnKeyDown { get; set; }
@@ -255,9 +256,34 @@ namespace Client.ViewModels
Players.Clear();
foreach (var item in lobby.Users)
{
Players.Add(item.Username);
Players.Add(item.Username + "\n" + item.Score);
}
});
}
private void UpdateUserScores(Lobby newLobby) {
Debug.WriteLine("[GAME] updating user scores");
List<User> newUsers = newLobby.Users;
// go over all users in current lobby
foreach (User user in data.Lobby?.Users)
{
// check with all users in new lobby
foreach (User newUser in newUsers)
{
// and update the score
if (newUser.Username == user.Username)
{
Debug.WriteLine($"[GAME] setting score of {user.Username} to {newUser.Score}. it was {user.Score}");
user.Score = newUser.Score;
break;
}
}
}
// update all the scores in the player list
HandleIncomingPlayer(newLobby);
}
}
}