[ADDED] player list in lobby updates on join! Not on disconnect yet. Tried to set the word label doesn't work yet.

This commit is contained in:
Dogukan
2020-10-22 23:54:07 +02:00
parent d37696d4bd
commit 1c04ab95c0
6 changed files with 51 additions and 18 deletions

View File

@@ -27,6 +27,7 @@ namespace Client
public LobbyJoinCallback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost;
public Callback OnServerDisconnect;
public Callback OnClientJoinLobby;
public LobbyCallback OnLobbyCreated;
public LobbyCallback OnLobbyLeave;
private ClientData data = ClientData.Instance;
@@ -62,6 +63,7 @@ namespace Client
{
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
return;
try
{
int amountReceived = stream.EndRead(ar);
@@ -138,6 +140,7 @@ namespace Client
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
OnLobbiesListReceived?.Invoke();
OnLobbiesReceivedAndWaitingForHost?.Invoke();
OnClientJoinLobby?.Invoke();
break;
case LobbyIdentifier.HOST:
// we receive this when the server has made us a host of a new lobby
@@ -147,8 +150,19 @@ namespace Client
OnLobbyCreated?.Invoke(lobbyCreatedID);
break;
case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
OnClientJoinLobby = () =>
{
foreach (var item in Lobbies)
{
Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count);
if (item.ID == data.Lobby.ID)
ViewModels.ViewModelGame.HandleIncomingPlayer(item);
}
};
break;
case LobbyIdentifier.LEAVE:
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
@@ -181,7 +195,6 @@ namespace Client
{
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
}
private void OnWriteComplete(IAsyncResult ar)

View File

@@ -2,6 +2,7 @@
using Client.Views;
using GalaSoft.MvvmLight.Command;
using SharedClientServer;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
@@ -24,6 +25,8 @@ namespace Client.ViewModels
public static ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
public static ObservableCollection<string> Players { get; } = new ObservableCollection<string>();
private dynamic _payload;
public static string Word
@@ -46,6 +49,7 @@ namespace Client.ViewModels
_message = value;
}
}
public ICommand OnKeyDown { get; set; }
public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window)
@@ -106,7 +110,6 @@ namespace Client.ViewModels
internal void AddMessage(string message)
{
Messages.Add($"{data.User.Username}: {message}");
_payload = new
{
username = data.User.Username,
@@ -131,6 +134,10 @@ namespace Client.ViewModels
public void LeaveGame(object sender, CancelEventArgs e)
{
Debug.WriteLine("Leaving...");
Application.Current.Dispatcher.Invoke(delegate
{
Players.Remove(data.User.Username);
});
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID));
}
@@ -141,7 +148,22 @@ namespace Client.ViewModels
public static void HandleRandomWord(string randomWord)
{
Debug.WriteLine("[CLIENT] Reached the handle random word method!");
Word = "NegerPik";
Application.Current.Dispatcher.Invoke(delegate
{
Word = randomWord;
});
}
public static void HandleIncomingPlayer(Lobby lobby)
{
Application.Current.Dispatcher.Invoke(delegate
{
Players.Clear();
foreach (var item in lobby.Users)
{
Players.Add(item.Username);
}
});
}
}
}

View File

@@ -22,21 +22,14 @@
<Grid Grid.Column="0" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListBox Name="PlayerList" ItemsSource="{Binding Path=Players}" Margin="10,0,0,10" FontSize="20"/>
</Grid>
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="2" Margin="84,10,10,10" Content="RESET"/>
<Label Name="GuessWord" Grid.Row="0" Grid.Column="1" Content="{Binding Path=Word, UpdateSourceTrigger=PropertyChanged}" Margin="140,0,109,0"/>
<Label Name="GuessWord" Grid.Row="0" Grid.Column="1" Content="{Binding Path=Word, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="140,0,109,0"/>
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
@@ -49,9 +42,9 @@
</Border>
<Grid Grid.Column="2" Grid.Row="1">
<ListBox Name ="TextBox" ItemsSource="{Binding Path=Messages}" Margin="0,0,0,69"/>
<ListBox Name ="TextBox" ItemsSource="{Binding Path=Messages}" Margin="0,0,10,69" />
<TextBox Name="ChatBox" Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,465,0,0">
<TextBox Name="ChatBox" Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,465,10,0">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding OnKeyDown}"/>
</TextBox.InputBindings>

View File

@@ -11,6 +11,7 @@
<RowDefinition Height="60"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
@@ -22,8 +23,9 @@
<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"/>
<TextBox Name="usernameTextbox" Grid.Row="1" Grid.Column="1" MaxLength="69" 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"/>
<Label Grid.Row="3" Grid.Column="0" Content="Tip of the century: base64 != UTF8!" FontSize="20" FontWeight="Bold"/>
</Grid>
</Window>

View File

@@ -1,4 +1,5 @@
using SharedClientServer;
using Client.ViewModels;
using SharedClientServer;
using System;
using System.Collections.Generic;
using System.Text;

View File

@@ -59,6 +59,8 @@ namespace Server.Models
throw new OutOfMemoryException("buffer is too small!");
}
ar.AsyncWaitHandle.WaitOne();
// copy the received bytes into the buffer
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
// add the bytes we received to the total amount
@@ -90,7 +92,6 @@ namespace Server.Models
}
ar.AsyncWaitHandle.WaitOne();
// start reading for a new message
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
}
@@ -194,6 +195,7 @@ namespace Server.Models
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
OnMessageReceivedOk = () =>
{
serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
{
id = serverCom.GetLobbyForUser(User).ID,