[ADD] added sending the lobbies data from the server to the clients on startup
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using static SharedClientServer.JSONConvert;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -18,6 +19,8 @@ namespace Client
|
||||
public bool Connected = false;
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
public Callback OnLobbiesListReceived;
|
||||
public Lobby[] Lobbies { get; set; }
|
||||
|
||||
public Client(string username)
|
||||
{
|
||||
@@ -70,9 +73,9 @@ namespace Client
|
||||
|
||||
private void handleData(byte[] message)
|
||||
{
|
||||
byte id = message[0];
|
||||
byte[] payload = new byte[message.Length - 1];
|
||||
Array.Copy(message, 1, payload, 0, message.Length - 1);
|
||||
byte id = message[4];
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
switch (id)
|
||||
{
|
||||
case JSONConvert.LOGIN:
|
||||
@@ -89,6 +92,14 @@ namespace Client
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
// lobby data
|
||||
LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload);
|
||||
switch (lobbyIdentifier)
|
||||
{
|
||||
case LobbyIdentifier.LIST:
|
||||
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
|
||||
OnLobbiesListReceived?.Invoke();
|
||||
break;
|
||||
}
|
||||
//TODO fill lobby with the data received
|
||||
break;
|
||||
case JSONConvert.CANVAS:
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Collections.ObjectModel;
|
||||
using Client.Views;
|
||||
using System.Linq;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -21,14 +22,17 @@ namespace Client
|
||||
|
||||
public Lobby SelectedLobby { get; set; }
|
||||
|
||||
private Client client;
|
||||
|
||||
public ViewModel()
|
||||
{
|
||||
_model = new Model();
|
||||
_lobbies = new ObservableCollection<Lobby>();
|
||||
|
||||
_lobbies.Add(new Lobby(50, 3, 8));
|
||||
_lobbies.Add(new Lobby(69, 1, 9));
|
||||
_lobbies.Add(new Lobby(420, 7, 7));
|
||||
client = ClientData.Instance.Client;
|
||||
client.OnLobbiesListReceived = updateLobbies;
|
||||
//_lobbies.Add(new Lobby(50, 3, 8));
|
||||
//_lobbies.Add(new Lobby(69, 1, 9));
|
||||
//_lobbies.Add(new Lobby(420, 7, 7));
|
||||
|
||||
OnHostButtonClick = new RelayCommand(() =>
|
||||
{
|
||||
@@ -38,6 +42,20 @@ namespace Client
|
||||
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
|
||||
}
|
||||
|
||||
private void updateLobbies()
|
||||
{
|
||||
Lobby[] lobbiesArr = client.Lobbies;
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
{
|
||||
foreach (Lobby lobby in lobbiesArr)
|
||||
{
|
||||
_lobbies.Add(lobby);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void startGameInLobby()
|
||||
{
|
||||
if (SelectedLobby != null)
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace Client.Views
|
||||
Application.Current.Dispatcher.Invoke(delegate {
|
||||
data.User = user;
|
||||
data.Client = client;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
|
||||
MainWindow startWindow = new MainWindow();
|
||||
startWindow.Show();
|
||||
this.Close();
|
||||
|
||||
Reference in New Issue
Block a user