Exemplo n.º 1
0
        public static void StreamHandler(object a, StreamArgs e)
        {
            NetworkStream stream = e.Stream;

            try
            {
                GameRequest sq = stream.DeSerialize <GameRequest>();
                if (sq.MapNumber > mapNumber)
                {
                    Debug.WriteLine("yes: " + mapNumber + " ; " + sq.MapNumber);
                    mapNumber = sq.MapNumber;
                    mapName   = "level" + mapNumber;
                    mapArray  = loader.GetMapCharArray(mapName);
                }
                if (sq.Player.PlayerID == PlayerID.Player1)
                {
                    player1Y = sq.y;
                    player1X = sq.x;
                    GameRequest gR = new GameRequest {
                        Player = new Player {
                            PlayerID = PlayerID.Player2
                        }, x = player2X, y = player2Y, Running = true, MapName = mapName, Map = mapArray, MapNumber = mapNumber
                    };
                    Debug.WriteLine(sq.MapNumber);
                    gR.Serialize <GameRequest>(stream);
                }
                if (sq.Player.PlayerID == PlayerID.Player2)
                {
                    player2Y = sq.y;
                    player2X = sq.x;
                    GameRequest gR = new GameRequest {
                        Player = new Player {
                            PlayerID = PlayerID.Player1
                        }, x = player1X, y = player1Y, Running = true, MapName = mapName, Map = mapArray, MapNumber = mapNumber
                    };
                    Debug.WriteLine(sq.MapNumber);
                    gR.Serialize <GameRequest>(stream);
                }
                e.Canceled = false;
            }
            catch (Exception ex)
            {
                if (!e.Canceled)
                {
                    writer.Error(e.Player + " : Caught error deserializing stream or reading SpecialRequest object.");
                }
                e.Canceled = true;
            }
        }