[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.Diagnostics;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using static SharedClientServer.JSONConvert;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
@@ -18,6 +19,8 @@ namespace Client
|
|||||||
public bool Connected = false;
|
public bool Connected = false;
|
||||||
private string username;
|
private string username;
|
||||||
public Callback OnSuccessfullConnect;
|
public Callback OnSuccessfullConnect;
|
||||||
|
public Callback OnLobbiesListReceived;
|
||||||
|
public Lobby[] Lobbies { get; set; }
|
||||||
|
|
||||||
public Client(string username)
|
public Client(string username)
|
||||||
{
|
{
|
||||||
@@ -70,9 +73,9 @@ namespace Client
|
|||||||
|
|
||||||
private void handleData(byte[] message)
|
private void handleData(byte[] message)
|
||||||
{
|
{
|
||||||
byte id = message[0];
|
byte id = message[4];
|
||||||
byte[] payload = new byte[message.Length - 1];
|
byte[] payload = new byte[message.Length - 5];
|
||||||
Array.Copy(message, 1, payload, 0, message.Length - 1);
|
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||||
switch (id)
|
switch (id)
|
||||||
{
|
{
|
||||||
case JSONConvert.LOGIN:
|
case JSONConvert.LOGIN:
|
||||||
@@ -89,6 +92,14 @@ namespace Client
|
|||||||
|
|
||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// 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
|
//TODO fill lobby with the data received
|
||||||
break;
|
break;
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using System.Diagnostics;
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using Client.Views;
|
using Client.Views;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Client
|
namespace Client
|
||||||
{
|
{
|
||||||
@@ -21,14 +22,17 @@ namespace Client
|
|||||||
|
|
||||||
public Lobby SelectedLobby { get; set; }
|
public Lobby SelectedLobby { get; set; }
|
||||||
|
|
||||||
|
private Client client;
|
||||||
|
|
||||||
public ViewModel()
|
public ViewModel()
|
||||||
{
|
{
|
||||||
_model = new Model();
|
_model = new Model();
|
||||||
_lobbies = new ObservableCollection<Lobby>();
|
_lobbies = new ObservableCollection<Lobby>();
|
||||||
|
client = ClientData.Instance.Client;
|
||||||
_lobbies.Add(new Lobby(50, 3, 8));
|
client.OnLobbiesListReceived = updateLobbies;
|
||||||
_lobbies.Add(new Lobby(69, 1, 9));
|
//_lobbies.Add(new Lobby(50, 3, 8));
|
||||||
_lobbies.Add(new Lobby(420, 7, 7));
|
//_lobbies.Add(new Lobby(69, 1, 9));
|
||||||
|
//_lobbies.Add(new Lobby(420, 7, 7));
|
||||||
|
|
||||||
OnHostButtonClick = new RelayCommand(() =>
|
OnHostButtonClick = new RelayCommand(() =>
|
||||||
{
|
{
|
||||||
@@ -38,6 +42,20 @@ namespace Client
|
|||||||
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
|
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()
|
private void startGameInLobby()
|
||||||
{
|
{
|
||||||
if (SelectedLobby != null)
|
if (SelectedLobby != null)
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ namespace Client.Views
|
|||||||
Application.Current.Dispatcher.Invoke(delegate {
|
Application.Current.Dispatcher.Invoke(delegate {
|
||||||
data.User = user;
|
data.User = user;
|
||||||
data.Client = client;
|
data.Client = client;
|
||||||
|
client.SendMessage(JSONConvert.ConstructLobbyRequestMessage());
|
||||||
MainWindow startWindow = new MainWindow();
|
MainWindow startWindow = new MainWindow();
|
||||||
startWindow.Show();
|
startWindow.Show();
|
||||||
this.Close();
|
this.Close();
|
||||||
|
|||||||
@@ -123,6 +123,13 @@ namespace Server.Models
|
|||||||
case JSONConvert.LOBBY:
|
case JSONConvert.LOBBY:
|
||||||
// lobby data
|
// lobby data
|
||||||
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
||||||
|
switch(l)
|
||||||
|
{
|
||||||
|
case LobbyIdentifier.REQUEST:
|
||||||
|
Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies...");
|
||||||
|
sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case JSONConvert.CANVAS:
|
case JSONConvert.CANVAS:
|
||||||
// canvas data
|
// canvas data
|
||||||
@@ -132,7 +139,6 @@ namespace Server.Models
|
|||||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//TODO implement ways to handle the message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ namespace Server.Models
|
|||||||
listener = new TcpListener(IPAddress.Any, port);
|
listener = new TcpListener(IPAddress.Any, port);
|
||||||
serverClients = new List<ServerClient>();
|
serverClients = new List<ServerClient>();
|
||||||
lobbies = new List<Lobby>();
|
lobbies = new List<Lobby>();
|
||||||
|
lobbies.Add(new Lobby(1,1,1));
|
||||||
|
lobbies.Add(new Lobby(2, 2, 2));
|
||||||
|
lobbies.Add(new Lobby(3, 3, 3));
|
||||||
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
serverClientsInlobbies = new Dictionary<Lobby, List<ServerClient>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using Client;
|
using Client;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SharedClientServer
|
namespace SharedClientServer
|
||||||
@@ -79,6 +81,14 @@ namespace SharedClientServer
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Lobby[] GetLobbiesFromMessage(byte[] json)
|
||||||
|
{
|
||||||
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
JArray lobbiesArray = payload.lobbies;
|
||||||
|
Debug.WriteLine(lobbiesArray.ToString());
|
||||||
|
return lobbiesArray.ToObject<Lobby[]>();
|
||||||
|
}
|
||||||
|
|
||||||
public static int GetLobbyID(byte[] json)
|
public static int GetLobbyID(byte[] json)
|
||||||
{
|
{
|
||||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||||
|
|||||||
Reference in New Issue
Block a user