private void HandleGameFinishedNotification(GameFinishedNotification gameFinishedNotification) { bool isVictory = gameFinishedNotification.WinnerPlayerId == PlayerIndex; string finishText = isVictory ? "VICTORY" : "DEFEAT"; Debug.Log($"Game is over! Result: {finishText}"); endGamePanelController.gameObject.SetActive(true); endGamePanelController.Show(isVictory); }
public void SuddenlyEndGameOnPlayerDisconnect(AsyncUserToken userToken) { ServerSideTokenIdentity identity = (ServerSideTokenIdentity)userToken.info; lock (identity.MatchmakingLock) { if (identity.MatchmakingStatus != UserMatchmakingStatus.GAME) { return; } GameWrapper gameWrapper = identity.GameWrapper; var game = gameWrapper.Game; if (game.Players.Length != 2) { // Only for 2 player games return; } lock (gameWrapper.@lock) { GameFinishedNotification gameFinishedNotification = new GameFinishedNotification(); for (int i = 0; i < gameWrapper.Tokens.Length; i++) { var token = gameWrapper.Tokens[i]; if (token == userToken) { gameFinishedNotification.WinnerPlayerId = 1 - i; break; } } foreach (var token in gameWrapper.Tokens) { if (token != userToken) { Config.GameServer.Send(token, gameFinishedNotification); } } TerminateGame(gameWrapper); } } }
// This works only for 2 player game because only that is implemented in first iteration // 2 players can't loose health as cause of single action in current state of the game private void CheckIfPlayerDied(GameWrapper gameWrapper) { if (gameWrapper == null) { throw new ArgumentNullException(nameof(gameWrapper)); } lock (gameWrapper.@lock) { var game = gameWrapper.Game; if (game.Players.Length != 2) { throw new ArgumentException("Meyhod works only for 2 player games"); } int?winner = null; for (int i = 0; i < game.Players.Length && winner == null; i++) { if (game.Players[i].Health <= 0) { winner = 1 - i; } } if (winner != null) { GameFinishedNotification gameFinishedNotification = new GameFinishedNotification { WinnerPlayerId = (int)winner }; foreach (var token in gameWrapper.Tokens) { Config.GameServer.Send(token, gameFinishedNotification); } TerminateGame(gameWrapper); } } }