Пример #1
0
        private void SocketHandler_MakeMove(Messages.MakeMove msg, NetworkStream stream,
                                            BinaryReader streamReader, BinaryWriter streamWriter)
        {
            //Get the game the message is referencing.
            var game = FindGame(msg.PlayerID);

            if (game == null)
            {
                Try(() => { throw new NullReferenceException("Couldn't find game with ID " + msg.PlayerID); },
                    "sending game state to player",
                    null, streamWriter);
                return;
            }

            //Update the game.
            //Hold onto the old game state in case something goes wrong.
            var oldBoardState = game.BoardState;
            var oldMatchState = game.MatchState;

            game.BoardState = msg.NewBoardState;
            game.AllMoves.Add(msg.Move);
            game.MatchState = msg.NewMatchState;

            //We need to let the other player know next time he checks into this server.
            if (msg.NewMatchState.IsGameOver())
            {
                lock (lock_finishedGames)
                {
                    finishedGamesByUnacknowledgedPlayer.Add(game.GetOtherPlayer(msg.PlayerID), game);
                }
            }

            //Send an acknowledgement. If it fails, undo the changes.
            Try(() => Messages.Base.Write(new Messages.Acknowledge(), streamWriter),
                "sending move acknowledgement to client",
                e =>
            {
                lock (lock_finishedGames)
                    finishedGamesByUnacknowledgedPlayer.Remove(game.GetOtherPlayer(msg.PlayerID));

                game.BoardState = oldBoardState;
                game.AllMoves.RemoveAt(game.AllMoves.Count - 1);
                game.MatchState = oldMatchState;
            });
        }
Пример #2
0
        public static Base Read(BinaryReader reader)
        {
            Base b = null;

            var type = (Types)reader.ReadByte();

            switch (type)
            {
            case Types.Error: b = new Error(null); break;

            case Types.Acknowledge: b = new Acknowledge(); break;

            case Types.FindGame: b = new FindGame(null, 0); break;

            case Types.SuccessfullyInQueue: b = new SuccessfullyInQueue(0); break;

            case Types.CheckOpponentFound: b = new CheckOpponentFound(0); break;

            case Types.FoundOpponent: b = new FoundOpponent(null, 0, false); break;

            case Types.NewBoard: b = new NewBoard(null, MatchStates.Tie); break;

            case Types.GetGameState: b = new GetGameState(0, 0); break;

            case Types.GameState: b = new GameState(null, null, MatchStates.Tie, 0, 0); break;

            case Types.MakeMove: b = new MakeMove(0, null, null, MatchStates.Tie); break;

            case Types.ForfeitGame: b = new ForfeitGame(0); break;

            default: throw new NotImplementedException(type.ToString());
            }

            b.Deserialize(reader);

            return(b);
        }