private InsertResult createInsertResult(int insertionRowIndex, int column, MOVE_RESULT move) { InsertResult result = new InsertResult(); result.Row_index = insertionRowIndex; result.Move_result = move; return(result); }
public InsertResult Insert(int column, string playerName, int gameId) { InsertResult result = new InsertResult(); int insertionRowIndex = -1; PlayingGame currentGame; if (currentGames.TryGetValue(gameId, out currentGame)) { // game is found if (playerName == currentGame.Player1) { insertionRowIndex = currentGame.Board.Insert(Color.Red, column); bool isWinner = currentGame.Board.Winner(insertionRowIndex, column); // if player 1 is winner if (isWinner) { updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Win); annoucePlayerWinner(playerName, currentGame.Player2, currentGame.GameStartTime); disconnectClientsAndRemoveGame(currentGame, gameId); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Win)); } // if there is tie if (currentGame.Board.Tied()) { updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Draw); announceDraw(playerName, currentGame.Player2, currentGame.GameStartTime); disconnectClientsAndRemoveGame(currentGame, gameId); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Draw)); } //if not win or draw, update turn updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Nothing); updateTurn(playerName, currentGame); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Nothing)); } else if (playerName == currentGame.Player2) { insertionRowIndex = currentGame.Board.Insert(Color.Black, column); bool isWinner = currentGame.Board.Winner(insertionRowIndex, column); if (isWinner) { // if player 2 winner updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Win); annoucePlayerWinner(playerName, currentGame.Player1, currentGame.GameStartTime); disconnectClientsAndRemoveGame(currentGame, gameId); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Win)); } if (currentGame.Board.Tied()) { // if tie updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Draw); announceDraw(playerName, currentGame.Player1, currentGame.GameStartTime); disconnectClientsAndRemoveGame(currentGame, gameId); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Draw)); } //if not win or draw, update turn updateCellForAnotherPlayer(playerName, insertionRowIndex, column, currentGame, MOVE_RESULT.Nothing); updateTurn(playerName, currentGame); return(createInsertResult(insertionRowIndex, column, MOVE_RESULT.Nothing)); } } else { throwGameNotFoundFault(gameId); } return(result); }