/// <summary> /// /// </summary> /// <param name="obj"></param> private void PlayAI(object obj) { object[] param = (object[])obj; Guid gameID = (Guid)param[0]; int playerIndex = (int)param[1]; if (gameStates.ContainsKey(gameID)) { GameState game = gameStates[gameID]; PlayerInformation player = game.Players[playerIndex]; if (game.StepsMade < 12 * game.Players.Count) { player.TimeOutsCounter++; player.AIPlayer = new Yacht.AIPlayerBehavior(); YachtStep step = player.AIPlayer.Play(player.ScoreCard); step.PlayerIndex = playerIndex; step.StepNumber = game.StepsMade; MakeStep(gameID, step); player.AIPlayer = null; CheckForInactivePlayer(gameID, playerIndex, player); } } }
/// <summary> /// Handle game step for both AI and client /// </summary> /// <param name="gameID"></param> /// <param name="playerIndex"></param> private void HandlePlayerStep(Guid gameID, int playerIndex) { if (gameStates.ContainsKey(gameID)) { GameState game = gameStates[gameID]; PlayerInformation player = game.Players[playerIndex]; if (game.CurrentPlayer == playerIndex && game.StepsMade < 12 * game.Players.Count) { // If ai player makes a move if (player.AIPlayer != null) { YachtStep step = player.AIPlayer.Play(player.ScoreCard); step.PlayerIndex = playerIndex; step.StepNumber = game.StepsMade; MakeStep(gameID, step); } else { // If client reset or create the timer if (player.Timer != null) { player.Timer.Change(25000, -1); } else { player.Timer = new Timer(PlayAI, new object[] { gameID, playerIndex }, 25000, -1); } } } } }
/// <summary> /// The method gives the server information about a step that the client has done. /// </summary> /// <param name="gameID">The game identifier.</param> /// <param name="sessionID">the client's identifier.</param> /// <param name="scoreIndex">The cell index of the score card.</param> /// <param name="scoreValue">The value of the score.</param> /// <param name="playerIndex">The index of player in the players list.</param> /// <param name="step">How many step have been made.</param> public void GameStep(Guid gameID, int sessionID, int scoreIndex, byte scoreValue, int playerIndex, int step) { if (gameStates.ContainsKey(gameID)) { // Get the game state. GameState game = gameStates[gameID]; // Get the player information PlayerInformation player = game.Players[playerIndex]; if (subscribers[sessionID].GameID == game.GameID) { YachtStep currentStep = new YachtStep(scoreIndex, scoreValue, playerIndex, step); MakeStep(gameID, currentStep); } if (gameStates[gameID].TimerToDelete != null) { gameStates[gameID].TimerToDelete.Dispose(); gameStates[gameID].TimerToDelete = null; } } }
/// <summary> /// Updates a game according to a step performed. /// </summary> /// <param name="gameID">ID of the game to update.</param> /// <param name="step">The step to perform in the game.</param> private void MakeStep(Guid gameID, YachtStep step) { if (gameStates.ContainsKey(gameID)) { GameState game = gameStates[gameID]; PlayerInformation player = game.Players[step.PlayerIndex]; // Check if the step is valid if (IsStepValid(game, step.PlayerIndex, step.StepNumber)) { // Reset the timer for deeming a player inactive if (player.Timer != null) { player.Timer.Change(Timeout.Infinite, Timeout.Infinite); if (player.AIPlayer == null) { player.TimeOutsCounter = 0; } } MakeMove(gameID, step); byte[] bytes = GetBytes <GameState>(gameStates[gameID]); NotifyUpdate(gameID, null, bytes, ServiceConstants.GameStateMessageString); PlayerInformation previousPlayer; if (step.PlayerIndex == 0) { previousPlayer = game.Players[3]; } else { previousPlayer = game.Players[step.PlayerIndex - 1]; } if (previousPlayer.TimeOutsCounter == 1) { sendToastMessage(subscribers[previousPlayer.PlayerID].ChannelUri, "Yacht - Network player made his step"); } CheckIfGameEnded(gameID); CheckNextPlayerAsync checkNextPlayerDelegate = HandlePlayerStep; checkNextPlayerDelegate.BeginInvoke(gameID, game.CurrentPlayer, null, null); } } }
/// <summary> /// Makes a move in the game. /// </summary> /// <param name="gameID">The game identifier.</param> /// <param name="move">The move to apply on the game</param> private void MakeMove(Guid gameID, YachtStep move) { if (gameStates.ContainsKey(gameID)) { GameState game = gameStates[gameID]; PlayerInformation player = game.Players[move.PlayerIndex]; player.ScoreCard[move.ScoreLine] = move.Score; player.TotalScore += move.Score; game.CurrentPlayer = (game.CurrentPlayer + 1) % game.Players.Count; game.StepsMade++; } }