10 Commits

Author SHA1 Message Date
Dogukan
7df48ab322 [FIX] Callbacks weren't properly called 2020-10-23 14:49:20 +02:00
Sem van der Hoeven
80d970da35 [FIX] made login impossible if no name was entered 2020-10-23 14:13:22 +02:00
SemvdH
2c7bf3e217 Merge pull request #11 from SemvdH/setupBranch
merge Setup branch into master
2020-10-23 14:06:36 +02:00
SemvdH
92f57a87bc Merge pull request #10 from SemvdH/update-scores
added updating scores on lobby list receive
2020-10-23 13:59:28 +02:00
Sem van der Hoeven
da5528c7b2 added updating scores on lobby list receive 2020-10-23 13:58:44 +02:00
Lars
c16f7cf68f Merge branch 'master' into setupBranch 2020-10-23 13:11:56 +02:00
SemvdH
5c2dea3ab4 Merge pull request #9 from SemvdH/feature/playerList
Feature/player list
2020-10-23 12:53:43 +02:00
Lars
24eb43239b [FIX] nog when you click host, you are able to click the start game button 2020-10-22 22:02:41 +02:00
Lars
335d17838c Merge branch 'master' into setupBranch 2020-10-22 20:04:30 +02:00
Lars
242c435ac7 added something little, not that very important tho 2020-10-22 17:46:45 +02:00
6 changed files with 54 additions and 31 deletions

View File

@@ -21,6 +21,7 @@ namespace Client
public delegate void CanvasReset();
public delegate void LobbyCallback(int id);
class Client : ObservableObject
{
@@ -48,6 +49,7 @@ namespace Client
private ClientData data = ClientData.Instance;
public CanvasDataReceived CanvasDataReceived;
public CanvasReset CReset;
public HandleIncomingPlayer UpdateUserScores;
public Lobby[] Lobbies { get; set; }
public Client(string username)
@@ -92,7 +94,6 @@ namespace Client
throw new OutOfMemoryException("buffer too small");
}
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, amountReceived);
totalBufferReceived += amountReceived;
@@ -190,7 +191,6 @@ namespace Client
case JSONConvert.CANVAS_WRITING:
CanvasDataReceived?.Invoke(JSONConvert.getCoordinates(payload), JSONConvert.getCanvasDrawingColor(payload));
// we hebben gedrawed, dus stuur dat we weer kunnen drawen
break;
}
@@ -216,11 +216,16 @@ namespace Client
private void updateGameLobby()
{
Debug.WriteLine("[CLIENT] updating game lobby");
foreach (var item in Lobbies)
{
Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count);
if (item.ID == data.Lobby?.ID)
IncomingPlayer?.Invoke(item);
{
//IncomingPlayer?.Invoke(item);
UpdateUserScores?.Invoke(item as Lobby);
}
}
}

View File

@@ -61,6 +61,7 @@ namespace Client
Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username);
client.SendMessage(JSONConvert.ConstructLobbyHostMessage());
client.OnLobbyCreated = becomeHostForLobby;
}
private void becomeHostForLobby(int id)
@@ -68,6 +69,7 @@ namespace Client
Debug.WriteLine($"got host succes with data {id} ");
wantToBeHost = true;
wantToBeHostId = id;
ClientData.Instance.User.Host = true;
client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived;
}
@@ -112,7 +114,7 @@ namespace Client
private void updateLobbies()
{
Debug.WriteLine("updating lobbies...");
Debug.WriteLine("[VIEWMODEL] updating lobbies...");
Lobby[] lobbiesArr = client.Lobbies;
Application.Current.Dispatcher.Invoke(delegate
{
@@ -122,6 +124,11 @@ namespace Client
foreach (Lobby l in lobbiesArr)
{
_lobbies.Add(l);
Lobby clientLobby = ClientData.Instance.Lobby;
if (l.ID == clientLobby?.ID)
{
clientLobby = l;
}
}
});

View File

@@ -48,19 +48,12 @@ namespace Client.ViewModels
}
private string _randomWord;
public string RandomWord
{
get { return _randomWord; }
set { _randomWord = value; }
}
public static string Word
{
get;
set;
}
public bool IsHost
{
get { return data.User.Host; }
@@ -69,17 +62,6 @@ namespace Client.ViewModels
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}");
}
buffer = new double[maxLines][];
linesQueue = new Queue<double[][]>();
@@ -91,6 +73,7 @@ namespace Client.ViewModels
data.Client.RandomWord = HandleRandomWord;
data.Client.IncomingMsg = HandleIncomingMsg;
data.Client.IncomingPlayer = HandleIncomingPlayer;
data.Client.UpdateUserScores = UpdateUserScores;
}
public ICommand OnKeyDown { get; set; }
@@ -227,7 +210,7 @@ namespace Client.ViewModels
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
}
public static void HandleIncomingMsg(string username, string message)
public void HandleIncomingMsg(string username, string message)
{
Application.Current.Dispatcher.Invoke(delegate
{
@@ -240,12 +223,12 @@ namespace Client.ViewModels
data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID));
}
public static void HandleRandomWord(string randomWord)
public void HandleRandomWord(string randomWord)
{
Debug.WriteLine("[CLIENT] Reached the handle random word method!");
Application.Current.Dispatcher.Invoke(delegate
{
Word = randomWord;
RandomWord = randomWord;
});
}
public void HandleIncomingPlayer(Lobby lobby)
@@ -255,9 +238,34 @@ namespace Client.ViewModels
Players.Clear();
foreach (var item in lobby.Users)
{
Players.Add(item.Username);
Players.Add(item.Username + "\n" + item.Score);
}
});
}
private void UpdateUserScores(Lobby newLobby) {
Debug.WriteLine("[GAME] updating user scores");
List<User> newUsers = newLobby.Users;
// go over all users in current lobby
foreach (User user in data.Lobby?.Users)
{
// check with all users in new lobby
foreach (User newUser in newUsers)
{
// and update the score
if (newUser.Username == user.Username)
{
Debug.WriteLine($"[GAME] setting score of {user.Username} to {newUser.Score}. it was {user.Score}");
user.Score = newUser.Score;
break;
}
}
}
// update all the scores in the player list
HandleIncomingPlayer(newLobby);
}
}
}

View File

@@ -38,9 +38,7 @@
<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged_1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="22" Width="100"/>
<Label Name="GuessWord" Grid.Row="0" Grid.Column="1" Content="{Binding Path=RandomWord, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Label Grid.Row="0" Grid.Column="2" FontSize="20" Content="" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Label Name="GuessWord" Grid.Row="0" Grid.Column="2" Content="{Binding Path=RandomWord, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"/>
<Button Name="CanvasReset" Click="CanvasReset_Click" Grid.Row="0" Grid.Column="3" Content="RESET"/>
</Grid>

View File

@@ -24,7 +24,7 @@
<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="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"/>
<Button Name="LoginButton" 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>

View File

@@ -27,8 +27,12 @@ namespace Client.Views
private void Button_EnterUsername(object sender, RoutedEventArgs e)
{
User user = new User(usernameTextbox.Text);
string name = usernameTextbox.Text;
if (name == string.Empty) return;
User user = new User(name);
Client client = new Client(user.Username);
LoginButton.IsEnabled = false;
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
@@ -42,6 +46,7 @@ namespace Client.Views
});
};
}
}
}