private void ShowLobby(string sessionName, bool isHost) { LobbyWindow lobbyWindow = new LobbyWindow(this.battleshipClient, sessionName, this.joinSessionIdCache, isHost); lobbyWindow.Show(); this.Close(); }
public void OnMessageReceived(Message message) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { List <byte> content = new List <byte>(message.GetContent()); switch (message.GetId()) { case Message.ID.CHAT_MESSAGE: { if (message.GetState() == Message.State.OK) { int messageLength = content[0]; string chatMessage = Encoding.UTF8.GetString(content.GetRange(1, messageLength).ToArray()); string playerName = Encoding.UTF8.GetString(content.GetRange(messageLength + 1, content.Count - (messageLength + 1)).ToArray()); txb_Chat.Text += $"{playerName}: {chatMessage}" + Environment.NewLine; txb_Chat.ScrollToEnd(); } else if (message.GetState() == Message.State.ERROR) { MessageBox.Show(Encoding.UTF8.GetString(content.ToArray())); } break; } case Message.ID.END_GAME: { if (message.GetState() == Message.State.OK) { this.game.Stop(); MessageBox.Show(Encoding.UTF8.GetString(content.ToArray())); LobbyWindow lobbyWindow = new LobbyWindow(this.battleshipClient, this.sessionName, this.sessionId, this.isHost); lobbyWindow.Show(); this.Hide(); //base.OnClosed(null); } break; } case Message.ID.REMOVE_PLAYER: { if (message.GetState() == Message.State.OK) { this.game.Stop(); MessageBox.Show("Match ended, player enemy disconnected!"); LobbyWindow lobbyWindow = new LobbyWindow(this.battleshipClient, this.sessionName, this.sessionId, this.isHost); lobbyWindow.Show(); this.Hide(); //base.OnClosed(null); } break; } case Message.ID.LEAVE_SESSION: { if (message.GetState() == Message.State.OK) { this.game.Stop(); MessageBox.Show("Match ended, player enemy disconnected!"); LobbyWindow lobbyWindow = new LobbyWindow(this.battleshipClient, this.sessionName, this.sessionId, this.isHost); lobbyWindow.Show(); //this.Hide(); base.OnClosed(null); } break; } case Message.ID.START_MATCH: { if (message.GetState() == Message.State.OK) { txb_Chat.Text += "Match started!" + Environment.NewLine; if (this.isHost) { txb_Chat.Text += "Your turn!" + Environment.NewLine; } txb_Chat.ScrollToEnd(); this.game.StartMatch(); } break; } case Message.ID.SUBMIT_MOVE: { if (message.GetState() == Message.State.OK) { int indexX = content[0]; int indexY = content[1]; bool isHit = (content[2] == 1); string username = Encoding.UTF8.GetString(content.GetRange(3, content.Count - 3).ToArray()); txb_Chat.Text += ((isHit) ? "Hit!" : "Missed!") + Environment.NewLine; if (isHit && UserLogin.Username == username || !isHit && UserLogin.Username != username) { txb_Chat.Text += "Your turn!" + Environment.NewLine; } else { txb_Chat.Text += "Enemy turn!" + Environment.NewLine; } txb_Chat.ScrollToEnd(); if (isHit) { this.mediaPlayer.Open(new Uri(Asset.HitSound)); this.mediaPlayer.Play(); } else { this.mediaPlayer.Open(new Uri(Asset.MissSound)); this.mediaPlayer.Play(); } this.game.SubmitMove(indexX, indexY, isHit, username); } else if (message.GetState() == Message.State.ERROR) { MessageBox.Show(Encoding.UTF8.GetString(content.ToArray())); this.game.OnSubmitMoveFailed(); } break; } default: { break; } } })); }