Exemplo n.º 1
0
        public static TTTPacket Decode(string xmlStr)
        {
            TTTPacket     inPacket   = null;
            XmlSerializer serializer = new XmlSerializer(typeof(TTTPacket));

            using (StringReader textReader = new StringReader(xmlStr))
            {
                inPacket = (TTTPacket)serializer.Deserialize(textReader);
            }
            return(inPacket);
        }
Exemplo n.º 2
0
        public static string Encode(TTTPacket packet)
        {
            string        result     = null;
            XmlSerializer serializer = new XmlSerializer(typeof(TTTPacket));

            using (StringWriter textWriter = new StringWriter())
            {
                serializer.Serialize(textWriter, packet);
                result = textWriter.ToString();
            }
            return(result.Replace("\r\n", ""));
        }
Exemplo n.º 3
0
        private void PacketUsage(TTTPacket packet, GameRoom room)
        {
            bool isGameOver;

            if (room.game.IsWrongStep(packet.ButtonNumber - 1))
            {
                return;
            }
            room.Turn += 1;

            room.game.Turn(packet.ButtonNumber - 1, packet.Unit);
            isGameOver = room.game.IsGameOver();
            if (isGameOver)
            {
                packet.GameResult = room.game.Result;
            }
            packet.Matrix = room.game.GetMatrix();

            NetworkStream ns1, ns2;

            if (room.Turn == 1)
            {
                ns1 = room.player1.User.GetStream();
                ns2 = room.player2.User.GetStream();
            }
            else
            {
                ns1 = room.player2.User.GetStream();
                ns2 = room.player1.User.GetStream();
            }

            StreamWriter sw = new StreamWriter(ns2);

            sw.WriteLine(XmlPacketDecoder.Encode(packet));
            sw.Flush();

            packet.PlayerTurn = ~packet.PlayerTurn;

            sw = new StreamWriter(ns1);
            sw.WriteLine(XmlPacketDecoder.Encode(packet));
            sw.Flush();
            if (isGameOver)
            {
                RemoveFromGame(room.player1, room.player2);
                gameRooms.Remove(room);
                Broadcast();
            }
        }
Exemplo n.º 4
0
        public void SendInitPackage(Client wanter, Client wanted)
        {
            AddToGame(wanter, wanted);
            TTTPacket    packet = new TTTPacket(PlayerTurn.Turn, "X", 0, null, null);
            StreamWriter sw     = new StreamWriter(wanter.User.GetStream());

            sw.WriteLine(XmlPacketDecoder.Encode(packet));
            sw.Flush();

            packet = new TTTPacket(PlayerTurn.Wait, "O", 0, null, null);
            sw     = new StreamWriter(wanted.User.GetStream());
            sw.WriteLine(XmlPacketDecoder.Encode(packet));
            sw.Flush();

            gameRooms.Add(new GameRoom(wanter, wanted, GameType.XO));
        }