Merge remote-tracking branch 'origin/setupBranch' into setupBranch
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Client.Views;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
@@ -17,9 +18,9 @@ namespace Client
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
MainWindow startWindow = new MainWindow();
|
||||
ViewModel VM = new ViewModel();
|
||||
startWindow.DataContext = VM;
|
||||
LoginScreen startWindow = new LoginScreen();
|
||||
//ViewModel VM = new ViewModel();
|
||||
//startWindow.DataContext = VM;
|
||||
startWindow.Show();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,12 @@ namespace Client
|
||||
private int totalBufferReceived = 0;
|
||||
public int Port = 5555;
|
||||
public bool Connected = false;
|
||||
//TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username)
|
||||
public string Username { get; }
|
||||
private string username;
|
||||
public Callback OnSuccessfullConnect;
|
||||
|
||||
public Client()
|
||||
public Client(string username)
|
||||
{
|
||||
this.username = username;
|
||||
this.tcpClient = new TcpClient();
|
||||
Debug.WriteLine("Starting connect to server");
|
||||
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
|
||||
@@ -31,6 +32,8 @@ namespace Client
|
||||
Debug.Write("finished connecting to server");
|
||||
this.tcpClient.EndConnect(ar);
|
||||
this.stream = tcpClient.GetStream();
|
||||
OnSuccessfullConnect?.Invoke();
|
||||
SendMessage(JSONConvert.ConstructUsernameMessage(username));
|
||||
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
|
||||
}
|
||||
|
||||
@@ -72,10 +75,10 @@ namespace Client
|
||||
Array.Copy(message, 1, payload, 0, message.Length - 1);
|
||||
switch (id)
|
||||
{
|
||||
case 0x01:
|
||||
case JSONConvert.LOGIN:
|
||||
// json log in username data
|
||||
break;
|
||||
case 0x02:
|
||||
case JSONConvert.MESSAGE:
|
||||
// json message data
|
||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||
string textUsername = combo.Item1;
|
||||
@@ -84,11 +87,11 @@ namespace Client
|
||||
|
||||
break;
|
||||
|
||||
case 0x03:
|
||||
case JSONConvert.LOBBY:
|
||||
// lobby data
|
||||
//TODO fill lobby with the data received
|
||||
break;
|
||||
case 0x04:
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
break;
|
||||
default:
|
||||
@@ -100,11 +103,13 @@ namespace Client
|
||||
|
||||
public void SendMessage(byte[] message)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
|
||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
|
||||
}
|
||||
|
||||
private void OnWriteComplete(IAsyncResult ar)
|
||||
{
|
||||
Debug.WriteLine("[CLIENT] finished writing");
|
||||
stream.EndWrite(ar);
|
||||
}
|
||||
}
|
||||
|
||||
59
Client/ClientData.cs
Normal file
59
Client/ClientData.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
class ClientData
|
||||
{
|
||||
private static ClientData _instance;
|
||||
private static readonly object padlock = new object();
|
||||
|
||||
public static ClientData Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (padlock)
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new ClientData();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private User _user;
|
||||
private Client _client;
|
||||
private Lobby _lobby;
|
||||
|
||||
private ClientData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public User User
|
||||
{
|
||||
get { return _user; }
|
||||
set { _user = value; }
|
||||
}
|
||||
|
||||
public Client Client
|
||||
{
|
||||
get { return _client; }
|
||||
set { _client = value; }
|
||||
}
|
||||
|
||||
public Lobby Lobby
|
||||
{
|
||||
get { return _lobby; }
|
||||
set { _lobby = value; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ namespace Client
|
||||
|
||||
private int _numbers;
|
||||
private bool _status;
|
||||
private bool _canStartGame;
|
||||
|
||||
//Test code
|
||||
public int Numbers
|
||||
@@ -37,11 +38,18 @@ namespace Client
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanStartGame
|
||||
{
|
||||
get { return _canStartGame; }
|
||||
set { _canStartGame = value; }
|
||||
}
|
||||
|
||||
|
||||
public Model()
|
||||
{
|
||||
_status = false;
|
||||
_numbers = 0;
|
||||
_canStartGame = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ using System.ComponentModel;
|
||||
using System.Text;
|
||||
using System.Windows.Input;
|
||||
using SharedClientServer;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Collections.ObjectModel;
|
||||
using Client.Views;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -12,19 +16,38 @@ namespace Client
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public ICommand OnHostButtonClick { get; set; }
|
||||
public ICommand JoinSelectedLobby { get; set; }
|
||||
|
||||
public Lobby SelectedLobby { get; set; }
|
||||
|
||||
public ViewModel()
|
||||
{
|
||||
_model = new Model();
|
||||
ButtonCommand = new RelayCommand(() =>
|
||||
{
|
||||
Client client = new Client();
|
||||
});
|
||||
|
||||
_lobbies = new List<Lobby>();
|
||||
_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));
|
||||
|
||||
OnHostButtonClick = new RelayCommand(() =>
|
||||
{
|
||||
Debug.WriteLine("Host button clicked");
|
||||
});
|
||||
|
||||
JoinSelectedLobby = new RelayCommand(startGameInLobby, true);
|
||||
}
|
||||
|
||||
private void startGameInLobby()
|
||||
{
|
||||
if (SelectedLobby != null)
|
||||
{
|
||||
ClientData.Instance.Lobby = SelectedLobby;
|
||||
|
||||
_model.CanStartGame = false;
|
||||
GameWindow window = new GameWindow();
|
||||
window.Show();
|
||||
}
|
||||
}
|
||||
|
||||
private void ClickCheck()
|
||||
@@ -35,17 +58,12 @@ namespace Client
|
||||
_model.Numbers = _model.Numbers + 5;
|
||||
}
|
||||
|
||||
public ICommand ButtonCommand { get; set; }
|
||||
|
||||
|
||||
private Model _model;
|
||||
public Model Model
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_model == null)
|
||||
_model = new Model();
|
||||
|
||||
return _model;
|
||||
}
|
||||
|
||||
@@ -55,8 +73,8 @@ namespace Client
|
||||
}
|
||||
}
|
||||
|
||||
private List<Lobby> _lobbies;
|
||||
public List<Lobby> Lobbies
|
||||
private ObservableCollection<Lobby> _lobbies;
|
||||
public ObservableCollection<Lobby> Lobbies
|
||||
{
|
||||
get { return _lobbies; }
|
||||
set { _lobbies = value; }
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Client.Views
|
||||
/// </summary>
|
||||
public partial class GameWindow : Window
|
||||
{
|
||||
|
||||
ClientData data = ClientData.Instance;
|
||||
public GameWindow()
|
||||
{
|
||||
DataContext = new ViewModelGame();
|
||||
@@ -110,7 +110,7 @@ namespace Client.Views
|
||||
*/
|
||||
private void WriteToChat(string message)
|
||||
{
|
||||
string user = "Monkey";
|
||||
string user = data.User.Username;
|
||||
SentMessage.AppendText($"{user}: {message}\n");
|
||||
}
|
||||
}
|
||||
|
||||
29
Client/Views/LoginScreen.xaml
Normal file
29
Client/Views/LoginScreen.xaml
Normal file
@@ -0,0 +1,29 @@
|
||||
<Window x:Class="Client.Views.LoginScreen"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Client.Views"
|
||||
mc:Ignorable="d"
|
||||
Title="LoginScreen" Height="450" Width="800">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="60"/>
|
||||
<RowDefinition Height="50"/>
|
||||
<RowDefinition Height="30"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Label Grid.Row="0" FontSize="40" Content="Welcome to Scrubl.io"/>
|
||||
<Label Grid.Row="1" FontSize="30" Content="Enter a username:"/>
|
||||
<Label Grid.Row="2" FontSize="15" Content="(max amount of characters for the username is 10)"/>
|
||||
|
||||
<TextBox Name="usernameTextbox" Grid.Row="1" Grid.Column="1" MaxLength="10" FontSize="30" VerticalAlignment="Center" HorizontalAlignment="Left" Width="250"/>
|
||||
<Button Content="ENTER" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Width="100" Height="40" Click="Button_EnterUsername"/>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
45
Client/Views/LoginScreen.xaml.cs
Normal file
45
Client/Views/LoginScreen.xaml.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Client.Views
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for LoginScreen.xaml
|
||||
/// </summary>
|
||||
public partial class LoginScreen : Window
|
||||
{
|
||||
ClientData data = ClientData.Instance;
|
||||
public LoginScreen()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_EnterUsername(object sender, RoutedEventArgs e)
|
||||
{
|
||||
User user = new User(usernameTextbox.Text);
|
||||
Client client = new Client(user.Username);
|
||||
client.OnSuccessfullConnect = () =>
|
||||
{
|
||||
// because we need to start the main window on a UI thread, we need to let the dispatcher handle it, which will execute the code on the ui thread
|
||||
Application.Current.Dispatcher.Invoke(delegate {
|
||||
data.User = user;
|
||||
data.Client = client;
|
||||
MainWindow startWindow = new MainWindow();
|
||||
startWindow.Show();
|
||||
this.Close();
|
||||
});
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition/>
|
||||
@@ -34,9 +34,8 @@
|
||||
<Label Grid.Row="0" Content="This client information:" FontSize="17"/>
|
||||
|
||||
<Label Grid.Row="1" Grid.Column="0" Content="Your username:" FontSize="15" VerticalAlignment="Center"/>
|
||||
<TextBox Name="usernameTextbox" Grid.Row="1" Grid.Column="1" MaxLength="10" FontSize="15" VerticalAlignment="Center"/>
|
||||
|
||||
<Label Grid.Row="2" Grid.Column="0" Content="Which color you want to be:" FontSize="15" VerticalAlignment="Center"/>
|
||||
<Label Grid.Row="2" Grid.Column="0" Content="Select your color:" FontSize="15" VerticalAlignment="Center"/>
|
||||
<ComboBox Name="colorSelection" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" FontSize="15">
|
||||
<ComboBoxItem Content="BLUE"/>
|
||||
<ComboBoxItem Content="RED"/>
|
||||
@@ -48,11 +47,12 @@
|
||||
</ComboBox>
|
||||
|
||||
<Label Grid.Row="3" Name="testLabel" FontSize="15" VerticalAlignment="Center"/>
|
||||
<Label Name="usernameLabel" Content="place username here" Grid.Column="1" HorizontalAlignment="Center" Margin="0,12,0,0" VerticalAlignment="Top" Grid.Row="1"/>
|
||||
|
||||
</Grid>
|
||||
|
||||
|
||||
<ListView Name="LobbyList" Grid.Row="1" Grid.Column="0" Margin="10, 10, 10, 10" ItemsSource="{Binding Path=Lobbies}">
|
||||
<ListView Name="LobbyList" Grid.Row="1" Grid.Column="0" SelectedItem="{Binding SelectedLobby}" Margin="10, 10, 10, 10" ItemsSource="{Binding Path=Lobbies}">
|
||||
<ListView.View>
|
||||
<GridView x:Name="grdList">
|
||||
<GridViewColumn Header="Lobby ID" DisplayMemberBinding="{Binding ID}" Width="70"/>
|
||||
@@ -67,8 +67,8 @@
|
||||
<RowDefinition Height="50"/>
|
||||
<RowDefinition Height="50"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Button Name="joinButton" Grid.Row="0" Content="join a selected lobby" Click="Button_Click" Width="200" Height="40" HorizontalAlignment="Left" Margin="10, 0, 0, 0"/>
|
||||
<Button Name="hostButton" Grid.Row="1" Content="host a new lobby" Command="{Binding ...}" Width="200" Height="40" HorizontalAlignment="left" Margin="10, 0, 0, 0"/>
|
||||
<Button Name="joinButton" Grid.Row="0" Content="join a selected lobby" Command="{Binding JoinSelectedLobby}" IsEnabled="{Binding Model.CanStartGame}" Width="200" Height="40" HorizontalAlignment="Left" Margin="10, 0, 0, 0"/>
|
||||
<Button Name="hostButton" Grid.Row="1" Content="host a new lobby" Command="{Binding OnHostButtonClick}" IsEnabled="{Binding Model.CanStartGame}" Width="200" Height="40" HorizontalAlignment="left" Margin="10, 0, 0, 0"/>
|
||||
</Grid>
|
||||
|
||||
</Grid>
|
||||
|
||||
@@ -21,9 +21,13 @@ namespace Client
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
ClientData data = ClientData.Instance;
|
||||
public MainWindow()
|
||||
{
|
||||
this.DataContext = new ViewModel();
|
||||
InitializeComponent();
|
||||
|
||||
usernameLabel.Content = data.User.Username;
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
@@ -33,13 +37,13 @@ namespace Client
|
||||
if(lobbySelected != null)
|
||||
{
|
||||
testLabel.Content = lobbySelected.ID;
|
||||
usernameTextbox.IsEnabled = false;
|
||||
colorSelection.IsEnabled = false;
|
||||
joinButton.IsEnabled = false;
|
||||
hostButton.IsEnabled = false;
|
||||
|
||||
GameWindow window = new GameWindow();
|
||||
window.Show();
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,12 @@ namespace Server.Models
|
||||
{
|
||||
class ServerClient : ObservableObject
|
||||
{
|
||||
public string Username { get; set; }
|
||||
private TcpClient tcpClient;
|
||||
private NetworkStream stream;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] totalBuffer = new byte[1024];
|
||||
private int totalBufferReceived = 0;
|
||||
public User User { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -24,8 +24,10 @@ namespace Server.Models
|
||||
/// <param name="client">the TcpClient object to use</param>
|
||||
public ServerClient(TcpClient client)
|
||||
{
|
||||
Debug.WriteLine("[SERVERCLIENT] making new instance and starting");
|
||||
tcpClient = client;
|
||||
stream = tcpClient.GetStream();
|
||||
Debug.WriteLine("[SERVERCLIENT] starting read");
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
}
|
||||
|
||||
@@ -35,6 +37,10 @@ namespace Server.Models
|
||||
/// <param name="ar">the async result status</param>
|
||||
private void OnRead(IAsyncResult ar)
|
||||
{
|
||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||
return;
|
||||
|
||||
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
@@ -84,24 +90,27 @@ namespace Server.Models
|
||||
/// <param name="message">the incoming message</param>
|
||||
private void HandleIncomingMessage(byte[] message)
|
||||
{
|
||||
Debug.WriteLine($"Got message from {Username} : {message}");
|
||||
byte id = message[0];
|
||||
byte[] payload = new byte[message.Length - 1];
|
||||
Array.Copy(message,1,payload,0,message.Length-1);
|
||||
Debug.WriteLine($"Got message : {Encoding.ASCII.GetString(message)}");
|
||||
byte id = message[4];
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message,5,payload,0,message.Length-5);
|
||||
Debug.WriteLine("[SERVERCLIENT] GOT STRING" + Encoding.ASCII.GetString(payload));
|
||||
switch(id)
|
||||
{
|
||||
|
||||
case 0x01:
|
||||
case JSONConvert.LOGIN:
|
||||
// json log in username data
|
||||
string uName = JSONConvert.GetUsernameLogin(message);
|
||||
string uName = JSONConvert.GetUsernameLogin(payload);
|
||||
|
||||
if (uName != null)
|
||||
{
|
||||
Username = uName;
|
||||
Debug.WriteLine("[SERVERCLIENT] set username to " + Username);
|
||||
User = new User(uName);
|
||||
User.Username = uName;
|
||||
Debug.WriteLine("[SERVERCLIENT] set username to " + uName);
|
||||
|
||||
}
|
||||
break;
|
||||
case 0x02:
|
||||
case JSONConvert.MESSAGE:
|
||||
// json message data
|
||||
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
|
||||
string textUsername = combo.Item1;
|
||||
@@ -110,10 +119,10 @@ namespace Server.Models
|
||||
// todo handle sending to all except this user the username and message to display in chat
|
||||
break;
|
||||
|
||||
case 0x03:
|
||||
case JSONConvert.LOBBY:
|
||||
// lobby data
|
||||
break;
|
||||
case 0x04:
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
// todo send canvas data to all other serverclients in lobby
|
||||
break;
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace Server.Models
|
||||
public List<Lobby> lobbies;
|
||||
private Dictionary<Lobby, List<ServerClient>> serverClientsInlobbies;
|
||||
public Action newClientAction;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// use a padlock object to make sure the singleton is thread-safe
|
||||
@@ -97,7 +98,7 @@ namespace Server.Models
|
||||
{
|
||||
foreach (ServerClient sc in serverClients)
|
||||
{
|
||||
if (sc.Username != username) sc.sendMessage(message);
|
||||
if (sc.User.Username != username) sc.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,14 +117,14 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
|
||||
public void AddToLobby(Lobby lobby, string username)
|
||||
public void AddToLobby(Lobby lobby, User user)
|
||||
{
|
||||
foreach (Lobby l in lobbies)
|
||||
{
|
||||
if (l == lobby)
|
||||
{
|
||||
bool succ;
|
||||
l.AddUser(username, out succ);
|
||||
l.AddUser(user, out succ);
|
||||
if (!succ)
|
||||
{
|
||||
// TODO send lobby full message
|
||||
@@ -131,7 +132,7 @@ namespace Server.Models
|
||||
{
|
||||
foreach(ServerClient sc in serverClients)
|
||||
{
|
||||
if (sc.Username == username)
|
||||
if (sc.User.Username == user.Username)
|
||||
{
|
||||
serverClientsInlobbies[l].Add(sc);
|
||||
break;
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
|
||||
namespace SharedClientServer
|
||||
{
|
||||
public delegate void Callback();
|
||||
class ClientServerUtil
|
||||
{
|
||||
// creates a message array to send to the server or to clients
|
||||
|
||||
@@ -1,16 +1,31 @@
|
||||
using Newtonsoft.Json;
|
||||
using Client;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace SharedClientServer
|
||||
{
|
||||
class JSONConvert
|
||||
{
|
||||
public const byte LOGIN = 0x01;
|
||||
public const byte MESSAGE = 0x02;
|
||||
public const byte LOBBY = 0x03;
|
||||
public const byte CANVAS = 0x04;
|
||||
|
||||
enum LobbyIdentifier
|
||||
{
|
||||
HOST,
|
||||
ADD,
|
||||
LEAVE,
|
||||
REQUEST
|
||||
}
|
||||
public static (string,string) GetUsernameAndMessage(byte[] json)
|
||||
{
|
||||
string msg = Encoding.ASCII.GetString(json);
|
||||
dynamic payload = JsonConvert.DeserializeObject(msg);
|
||||
|
||||
return (payload.username, payload.message);
|
||||
}
|
||||
|
||||
@@ -19,5 +34,41 @@ namespace SharedClientServer
|
||||
dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json));
|
||||
return payload.username;
|
||||
}
|
||||
|
||||
public static byte[] ConstructUsernameMessage(string uName)
|
||||
{
|
||||
return GetMessageToSend(LOGIN, new
|
||||
{
|
||||
username = uName
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] ConstructLobbyDataMessage(Lobby lobby)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// constructs a message that can be sent to the clients or server
|
||||
/// </summary>
|
||||
/// <param name="identifier">the identifier for what kind of message it is</param>
|
||||
/// <param name="payload">the json payload</param>
|
||||
/// <returns>a byte array containing a message that can be sent to clients or server</returns>
|
||||
public static byte[] GetMessageToSend(byte identifier, dynamic payload)
|
||||
{
|
||||
// convert the dynamic to bytes
|
||||
byte[] payloadBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(payload));
|
||||
// make the array that holds the message and copy the payload into it with the first spot containing the identifier
|
||||
byte[] res = new byte[payloadBytes.Length + 5];
|
||||
// put the payload in the res array
|
||||
Array.Copy(payloadBytes, 0, res, 5, payloadBytes.Length);
|
||||
// put the identifier at the start of the payload part
|
||||
res[4] = identifier;
|
||||
// put the length of the payload at the start of the res array
|
||||
Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Client
|
||||
succes = false;
|
||||
if (_users.Count < _maxPlayers)
|
||||
{
|
||||
_users.Add(new User(username, 0));
|
||||
_users.Add(new User(username, 0, false));
|
||||
succes = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,20 @@ namespace SharedClientServer
|
||||
{
|
||||
private string _username;
|
||||
private int _score;
|
||||
private bool _host;
|
||||
|
||||
public User(string username, int score)
|
||||
public User(string username, int score, bool host)
|
||||
{
|
||||
_username = username;
|
||||
_score = score;
|
||||
_host = host;
|
||||
}
|
||||
|
||||
public User(string username)
|
||||
{
|
||||
_username = username;
|
||||
_score = 0;
|
||||
_host = false;
|
||||
}
|
||||
|
||||
public string Username
|
||||
@@ -26,5 +35,11 @@ namespace SharedClientServer
|
||||
get { return _score; }
|
||||
set { _score = value; }
|
||||
}
|
||||
|
||||
public bool Host
|
||||
{
|
||||
get { return _host; }
|
||||
set { _host = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user