static void PostMoveCompleted(string data, int matchId) { var response = JsonSerializer.DeseriaizeFromJSON<MoveResponse>(data); bool newGame = false; int gameId; int oldGameId; Match match; Player tttdPlayer; CentralServerSession session; StatusFlag flag = CentralServerCommunicationChannel.ParseStatus(response.StatusFlag); using (IGameDataService dataService = new GameDataService()) { match = dataService.GetMatch(matchId, null); session = dataService.GetCentralServerSession(null, null, match.CurrentGameId.Value); tttdPlayer = dataService.GetPlayer(match.PlayerOneId); oldGameId = match.CurrentGameId.Value; if (response.NewGameId != null && response.NewGameId > 0 && response.NewGameId != session.CentralServerGameId) { int newGameId = TicTacToeHost.Instance.ConfigureGame(matchId); gameId = newGameId; newGame = true; session = dataService.CreateCentralServerSession(newGameId, response.NewGameId); } else { gameId = match.CurrentGameId.Value; } dataService.Save(); } if (response.YourTurn //|| (newGame && response.X == null && response.Y == null) ) { using (IGameDataService dataService = new GameDataService()) { dataService.SetPlayerTurn(gameId, match.PlayerOneId); dataService.Save(); } } else if(flag == StatusFlag.ChallengeMove || flag == StatusFlag.ChallengeWin) { using (IGameDataService dataService = new GameDataService()) { dataService.EndGame(oldGameId, null); dataService.Save(); } } else if (response.X >= 0 && response.Y >= 0) { if (flag == StatusFlag.AcceptLoss) { using (IGameDataService dataService = new GameDataService()) { dataService.EndGame(oldGameId, match.PlayerOneId); dataService.Save(); } } if (newGame) { using (IGameDataService dataService = new GameDataService()) { dataService.SetPlayerTurn(gameId, match.PlayerTwoId); dataService.Save(); } } Move move = new Move() { GameId = gameId, PlayerId = match.PlayerTwoId }; GameState state = TicTacToeHost.Instance.GetGameState(gameId, match.PlayerTwoId); if (state.Mode == PlayMode.DeathMatch) { move.OriginX = response.X; move.OriginY = response.Y; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (state.GameBoard[x][y] == null || state.GameBoard[x][y] == 0) { move.X = x; move.Y = y; } } } } else { move.X = response.X.Value; move.Y = response.Y.Value; } MoveResult opponentMoveResult = TicTacToeHost.Instance.Move(move, false); GameState postMoveState = TicTacToeHost.Instance.GetGameState(gameId, match.PlayerTwoId); if (opponentMoveResult != MoveResult.Valid || flag == StatusFlag.WinningMove || postMoveState.YouWon) { MoveRequest challengeRequest = new MoveRequest(); challengeRequest.GameId = session.CentralServerGameId.Value; challengeRequest.PlayerName = tttdPlayer.PlayerName; challengeRequest.X = 0; challengeRequest.Y = 0; bool challenging = false; if (opponentMoveResult != MoveResult.Valid) { challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.ChallengeMove); challenging = true; } else { if (flag == StatusFlag.WinningMove && !postMoveState.YouWon) { challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.ChallengeWin); challenging = true; } else { challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.AcceptLoss); } } if (challenging) { using (IGameDataService dataService = new GameDataService()) { dataService.EndGame(gameId, null); dataService.Save(); } } CentralServerCommunicationChannel.Instance.PostMove(challengeRequest, match.CurrentGameId.Value, match.MatchId); } } }
public MoveResult Move(Move move) { return this.host.Move(move); }
public MoveResult Move(Move move, bool raiseEvent) { MoveResult validationResult = this.ValidateMove(move); bool canMove = true; if (validationResult != MoveResult.Valid) canMove = false; Logger.Instance.Log("MoveAttempt", string.Format("GameId:{1}|PlayerId:{2}|ValidationResult:{0}", validationResult.ToString(), move.GameId, move.PlayerId), JsonSerializer.SerializeToJSON(move)); if (!canMove) return validationResult; Match match; GameState currentState = TicTacToeHost.Instance.GetGameState(move.GameId, move.PlayerId); using (IGameDataService gameDataService = new GameDataService()) { match = gameDataService.GetMatch(null, move.GameId); gameDataService.Move(move.GameId, move.PlayerId, move.OriginX, move.OriginY, move.X, move.Y); gameDataService.Save(); } this.UpdateGame(move.GameId); if (validationResult == MoveResult.Valid && raiseEvent) { GameConfiguration config = GameConfigCache.Instance.GetConfig(match.MatchId); if (config.GameType == GameType.Network) this.PlayerMove(this, new MoveEventArgs() { MatchId = match.MatchId, PlayerId = move.PlayerId, Mode = currentState.Mode, OriginX = move.OriginX, OriginY = move.OriginY, X = move.X, Y = move.Y }); } return validationResult; }
public MoveResult ValidateMove(Move move) { GameState state = this.GetGameState(move.GameId, move.PlayerId); if (state == null) return MoveResult.InvalidGame; else if (state.YourTurn == false) return MoveResult.NotPlayerTurn; else { if (move.OriginX != null || move.OriginY != null) { if ((state.Mode != PlayMode.DeathMatch) || (move.OriginX == null || move.OriginY == null) || (move.OriginX < 0 || move.OriginX > 2) || (move.OriginY < 0 || move.OriginY > 2) || (state.GameBoard[move.OriginX.Value][move.OriginY.Value] != 1)) { return MoveResult.InvalidOrigin; } } if ((move.X < 0 || move.X > 2) || (move.Y < 0 || move.Y > 2) || (state.GameBoard[move.X][move.Y] != null)) { return MoveResult.InvalidDestination; } return MoveResult.Valid; } //If there isn't a rule to catch by now, we just need to fail. return MoveResult.Failed; }
public MoveResult Move(Move move) { return this.Move(move, true); }