[ADDED] lobby now secured when the game starts. start button is still for everyone available, not working yet
This commit is contained in:
@@ -11,6 +11,9 @@ using System.Collections.ObjectModel;
|
||||
using Client.Views;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
using System.Data;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Client
|
||||
{
|
||||
@@ -79,8 +82,16 @@ namespace Client
|
||||
{
|
||||
// lobby die je wilt joinen verwijderen
|
||||
// nieuwe binnengekregen lobby toevoegen
|
||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||
if (SelectedLobby != null)
|
||||
{
|
||||
if (SelectedLobby.PlayersIn == SelectedLobby.MaxPlayers || !SelectedLobby.LobbyJoineble)
|
||||
{
|
||||
return;
|
||||
}
|
||||
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
|
||||
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnLobbyJoinSuccess()
|
||||
@@ -168,5 +179,7 @@ namespace Client
|
||||
get { return _lobbies; }
|
||||
set { _lobbies = value; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,21 +13,15 @@ namespace Client.ViewModels
|
||||
{
|
||||
class ViewModelGame : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
private ClientData data = ClientData.Instance;
|
||||
private GameWindow window;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private Point currentPoint = new Point();
|
||||
private Color color;
|
||||
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
private string _username;
|
||||
|
||||
private string _message;
|
||||
|
||||
public string Message
|
||||
{
|
||||
get
|
||||
@@ -39,7 +33,43 @@ namespace Client.ViewModels
|
||||
_message = value;
|
||||
}
|
||||
}
|
||||
|
||||
public User User
|
||||
{
|
||||
get { return data.User; }
|
||||
set
|
||||
{
|
||||
data.User = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ViewModelGame(GameWindow window)
|
||||
{
|
||||
this.window = window;
|
||||
if (_payload == null)
|
||||
{
|
||||
_message = "";
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//_message = data.Message;
|
||||
//_username = data.User.Username;
|
||||
//Messages.Add($"{data.User.Username}: {Message}");
|
||||
}
|
||||
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
||||
ButtonStartGame = new RelayCommand(BeginGame);
|
||||
data.Client.CanvasDataReceived = UpdateCanvasWithNewData;
|
||||
}
|
||||
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
public ICommand OnKeyDown { get; set; }
|
||||
public ICommand ButtonStartGame { get; set; }
|
||||
|
||||
public void BeginGame()
|
||||
{
|
||||
data.Client.SendMessage(JSONConvert.ConstructGameStartData(data.Lobby.ID));
|
||||
}
|
||||
|
||||
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
|
||||
{
|
||||
@@ -83,26 +113,6 @@ namespace Client.ViewModels
|
||||
color = colorSelected;
|
||||
}
|
||||
|
||||
|
||||
public ViewModelGame(GameWindow window)
|
||||
{
|
||||
this.window = window;
|
||||
if (_payload == null)
|
||||
{
|
||||
_message = "";
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//_message = data.Message;
|
||||
//_username = data.User.Username;
|
||||
//Messages.Add($"{data.User.Username}: {Message}");
|
||||
}
|
||||
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
||||
data.Client.CanvasDataReceived = UpdateCanvasWithNewData;
|
||||
}
|
||||
|
||||
|
||||
private void UpdateCanvasWithNewData(double[] coordinates)
|
||||
{
|
||||
Application.Current.Dispatcher.Invoke(delegate
|
||||
|
||||
@@ -35,9 +35,23 @@
|
||||
|
||||
</Grid>
|
||||
|
||||
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="2" Margin="84,10,10,10" Content="RESET"/>
|
||||
<Grid Grid.Row="0" Grid.Column="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="160"/>
|
||||
<ColumnDefinition Width="160"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="100"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" FontSize="20" Content="Pick a color -->"/>
|
||||
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
|
||||
|
||||
<Label Grid.Row="0" Grid.Column="2" FontSize="20" Content="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
|
||||
|
||||
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/>
|
||||
</Grid>
|
||||
|
||||
<Button Name="StartGame" Grid.Row="0" Grid.Column="2" Content="Start Game" FontSize="20" Command="{Binding ButtonStartGame}" IsEnabled="{Binding User.Host}"/>
|
||||
|
||||
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
|
||||
|
||||
<Border Grid.Row="1" Grid.Column="1" Margin ="10,10,10,10" BorderBrush="Black" BorderThickness ="2.5">
|
||||
<Canvas Name="CanvasForPaint" MouseDown="CanvasForPaint_MouseDown" MouseMove="CanvasForPaint_MouseMove">
|
||||
|
||||
@@ -57,7 +57,8 @@
|
||||
<GridView x:Name="grdList">
|
||||
<GridViewColumn Header="Lobby ID" DisplayMemberBinding="{Binding ID}" Width="70"/>
|
||||
<GridViewColumn Header="Players in" DisplayMemberBinding="{Binding PlayersIn}" Width="70"/>
|
||||
<GridViewColumn Header="max players available" DisplayMemberBinding="{Binding MaxPlayers}"/>
|
||||
<GridViewColumn Header="max players available" DisplayMemberBinding="{Binding MaxPlayers}" Width="150"/>
|
||||
<GridViewColumn Header="joineble" DisplayMemberBinding="{Binding LobbyJoineble}"/>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
|
||||
@@ -134,8 +134,9 @@ namespace Server.Models
|
||||
LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload);
|
||||
handleLobbyMessage(payload,l);
|
||||
break;
|
||||
|
||||
case JSONConvert.CANVAS:
|
||||
Debug.WriteLine("GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
|
||||
Debug.WriteLine("[SERVERCLIENT] GOT A MESSAGE FROM THE CLIENT ABOUT THE CANVAS!!!");
|
||||
dynamic canvasData = new {
|
||||
coordinatesLine = JSONConvert.getCoordinates(payload)
|
||||
};
|
||||
@@ -144,6 +145,21 @@ namespace Server.Models
|
||||
// canvas data
|
||||
// todo send canvas data to all other serverclients in lobby
|
||||
break;
|
||||
|
||||
case JSONConvert.GAME:
|
||||
Debug.WriteLine("[SERVERCLIENT] Got a message about the game logic");
|
||||
string command = JSONConvert.GetGameCommand(payload);
|
||||
switch (command)
|
||||
{
|
||||
case "startGame":
|
||||
int lobbyID = JSONConvert.GetStartGameLobbyID(payload);
|
||||
serverCom.CloseALobby(lobbyID);
|
||||
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.WriteLine("[SERVER] Received weird identifier: " + id);
|
||||
break;
|
||||
|
||||
@@ -183,5 +183,16 @@ namespace Server.Models
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseALobby(int lobbyID)
|
||||
{
|
||||
foreach (Lobby lobby in lobbies)
|
||||
{
|
||||
if (lobby.ID == lobbyID)
|
||||
{
|
||||
lobby.LobbyJoineble = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace SharedClientServer
|
||||
public const byte MESSAGE = 0x02;
|
||||
public const byte LOBBY = 0x03;
|
||||
public const byte CANVAS = 0x04;
|
||||
public const byte GAME = 0x05;
|
||||
|
||||
public enum LobbyIdentifier
|
||||
{
|
||||
@@ -146,7 +147,7 @@ namespace SharedClientServer
|
||||
return GetMessageToSend(CANVAS, new
|
||||
{
|
||||
coordinatesLine = coordinates
|
||||
}); ;
|
||||
});
|
||||
}
|
||||
|
||||
public static double[] getCoordinates(byte[] payload)
|
||||
@@ -159,6 +160,28 @@ namespace SharedClientServer
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
public static byte[] ConstructGameStartData(int lobbyID)
|
||||
{
|
||||
string startGame = "startGame";
|
||||
return GetMessageToSend(GAME, new
|
||||
{
|
||||
command = startGame,
|
||||
lobbyToStart = lobbyID
|
||||
}); ;
|
||||
}
|
||||
|
||||
public static string GetGameCommand(byte[] payload)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
||||
return json.command;
|
||||
}
|
||||
|
||||
public static int GetStartGameLobbyID(byte[] payload)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payload));
|
||||
return json.lobbyToStart;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// constructs a message that can be sent to the clients or server
|
||||
/// </summary>
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Client
|
||||
private int _id;
|
||||
private int _playersIn;
|
||||
private int _maxPlayers;
|
||||
private bool _lobbyJoineble;
|
||||
//private List<string> _usernames;
|
||||
private List<User> _users;
|
||||
|
||||
@@ -34,6 +35,7 @@ namespace Client
|
||||
_maxPlayers = maxPlayers;
|
||||
//_usernames = new List<string>();
|
||||
_users = new List<User>();
|
||||
_lobbyJoineble = true;
|
||||
}
|
||||
|
||||
public void AddUser(string username, out bool succes)
|
||||
@@ -41,7 +43,7 @@ namespace Client
|
||||
succes = false;
|
||||
if (_users.Count < _maxPlayers)
|
||||
{
|
||||
_users.Add(new User(username, 0, false));
|
||||
_users.Add(new User(username, 0, false, false));
|
||||
succes = true;
|
||||
}
|
||||
}
|
||||
@@ -87,6 +89,11 @@ namespace Client
|
||||
set { _users = value; }
|
||||
}
|
||||
|
||||
public bool LobbyJoineble
|
||||
{
|
||||
get { return _lobbyJoineble; }
|
||||
set { _lobbyJoineble = value; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,16 @@ namespace SharedClientServer
|
||||
private string _username;
|
||||
private int _score;
|
||||
private bool _host;
|
||||
private bool _turnToDraw;
|
||||
private string _message;
|
||||
|
||||
[JsonConstructor]
|
||||
public User(string username, int score, bool host)
|
||||
public User(string username, int score, bool host, bool turnToDraw)
|
||||
{
|
||||
_username = username;
|
||||
_score = score;
|
||||
_host = host;
|
||||
_turnToDraw = turnToDraw;
|
||||
}
|
||||
|
||||
public User(string username)
|
||||
@@ -25,6 +27,7 @@ namespace SharedClientServer
|
||||
_username = username;
|
||||
_score = 0;
|
||||
_host = false;
|
||||
_turnToDraw = false;
|
||||
}
|
||||
|
||||
public string Username
|
||||
@@ -44,5 +47,11 @@ namespace SharedClientServer
|
||||
get { return _host; }
|
||||
set { _host = value; }
|
||||
}
|
||||
|
||||
public bool TurnToDraw
|
||||
{
|
||||
get { return _turnToDraw; }
|
||||
set { _turnToDraw = value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user