Пример #1
0
 public static void OnClose(GameSession client, GameClientPacket packet)
 {
     using (PacketWriter packet_write = new PacketWriter(GameServerPacket.GamePacketByteLength)) {
         packet_write.Write((byte)RoomMessage.STOP_CLIENT);
         client.Send(packet_write.Content);
     }
     client.CloseAsync();
 }
Пример #2
0
 private void SendJoinGame(GameSession player)
 {
     using (GameServerPacket join = new GameServerPacket(StocMessage.JoinGame))
     {
         join.Write(Banlist == null ? 0U : Banlist.Hash);
         join.Write((byte)Config.Rule);
         join.Write((byte)Config.Mode);
         join.Write(Config.EnablePriority);
         join.Write(Config.NoCheckDeck);
         join.Write(Config.NoShuffleDeck);
         // C++ padding: 5 bytes + 3 bytes = 8 bytes
         for (int i = 0; i < 3; i++)
             join.Write((byte)0);
         join.Write(Config.StartLp);
         join.Write((byte)Config.StartHand);
         join.Write((byte)Config.DrawCount);
         join.Write((short)Config.GameTimer);
         player.Send(join);
     }
     if (State != GameState.Lobby)
         SendDuelingPlayers(player);
 }
Пример #3
0
		public static void OnUpdateDeck(GameSession client, GameClientPacket packet){
			if (client.Game==null||client.Type == (int)PlayerType.Observer||client.Type == (int)PlayerType.Undefined)
				return;
			Deck deck = new Deck();
			int main = packet.ReadInt32();
			int side = packet.ReadInt32();
			for (int i = 0; i < main; i++)
				deck.AddMain(packet.ReadInt32());
			for (int i = 0; i < side; i++)
				deck.AddSide(packet.ReadInt32());
			if (client.Game.State == GameState.Lobby)
			{
				client.Deck = deck;
				client.Game.IsReady[client.Type] = false;
			}
			else if (client.Game.State == GameState.Side)
			{
				if (client.Game.IsReady[client.Type])
					return;
				if (!client.Deck.Check(deck))
				{
					using(GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg)){
						error.Write((byte)3);
						error.Write(0);
						client.Send(error);
					}
					return;
				}
				client.Deck = deck;
				client.Game.IsReady[client.Type] = true;
				client.Game.ServerMessage(string.Format(Messages.MSG_READY, client.Name));
				client.Send(GameServerPacket.EmtryMessage(StocMessage.DuelStart));
				client.Game.MatchSide();
			}
		}
Пример #4
0
 private void SendDuelingPlayers(GameSession player, bool isNow = true)
 {
     for (int i = 0; i < Players.Length; i++)
     {
         using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
         {
             int id = i;
             if (m_swapped)
             {
                 if (IsTag)
                 {
                     if (i == 0 || id == 1)
                         id = i + 2;
                     else
                         id = i - 2;
                 }
                 else
                     id = 1 - i;
             }
             enter.WriteUnicode(Players[id] == null ? "???" : Players[id].Name, 20);
             enter.Write((byte)i);
             player.Send(enter, isNow);
         }
     }
 }
Пример #5
0
        private void InitSpectatorLocation(GameSession player, CardLocation loc)
        {
            for (int index = 0; index < 2; index++)
            {
                int flag = loc == CardLocation.MonsterZone ? 0x91fff : 0x81fff;
                byte[] result = m_duel.QueryFieldCard(index, loc, flag, false);

                using (MemoryStream ms = new MemoryStream(result))
                {
                    BinaryReader reader = new BinaryReader(ms);
                    BinaryWriter writer = new BinaryWriter(ms);
                    while (ms.Position < ms.Length)
                    {
                        int len = reader.ReadInt32();
                        if (len == 4)
                            continue;
                        long pos = ms.Position;
                        reader.ReadBytes(len - 4);
                        long endPos = ms.Position;

                        ms.Position = pos;
                        ClientCard card = new ClientCard();
                        card.Update(reader);
                        ms.Position = endPos;

                        bool facedown = ((card.Position & (int)CardPosition.FaceDown) != 0);

                        using (GameServerPacket move = new GameServerPacket(GameMessage.Move))
                        {
                            move.Write(facedown ? 0 : card.Code);
                            move.Write(0);
                            move.Write((byte)card.Controler);
                            move.Write((byte)card.Location);
                            move.Write((byte)card.Sequence);
                            move.Write((byte)card.Position);
                            move.Write(0);
                            player.Send(move, false);
                        }
                        foreach (ClientCard material in card.Overlay)
                        {
                            using (GameServerPacket xyzcreate = new GameServerPacket(GameMessage.Move))
                            {
                                xyzcreate.Write(material.Code);
                                xyzcreate.Write(0);
                                xyzcreate.Write((byte)index);
                                xyzcreate.Write((byte)CardLocation.Grave);
                                xyzcreate.Write((byte)0);
                                xyzcreate.Write((byte)0);
                                xyzcreate.Write(0);
                                player.Send(xyzcreate, false);
                            }

                            using (GameServerPacket xyzmove = new GameServerPacket(GameMessage.Move))
                            {
                                xyzmove.Write(material.Code);
                                xyzmove.Write((byte)index);
                                xyzmove.Write((byte)CardLocation.Grave);
                                xyzmove.Write((byte)0);
                                xyzmove.Write((byte)0);
                                xyzmove.Write((byte)material.Controler);
                                xyzmove.Write((byte)material.Location);
                                xyzmove.Write((byte)material.Sequence);
                                xyzmove.Write((byte)material.Position);
                                xyzmove.Write(0);
                                player.Send(xyzmove, false);
                            }
                        }

                        if (facedown)
                        {
                            ms.Position = pos;
                            writer.Write(new byte[len - 4]);
                        }
                    }
                    writer.Close();
                    reader.Close();
                }
                if (loc == CardLocation.MonsterZone)
                {
                    result = m_duel.QueryFieldCard(index, loc, 0x81fff, false);
                    using (MemoryStream ms = new MemoryStream(result))
                    {
                        BinaryReader reader = new BinaryReader(ms);
                        BinaryWriter writer = new BinaryWriter(ms);
                        while (ms.Position < ms.Length)
                        {
                            int len = reader.ReadInt32();
                            if (len == 4)
                                continue;
                            long pos = ms.Position;
                            byte[] raw = reader.ReadBytes(len - 4);

                            bool facedown = ((raw[11] & (int)CardPosition.FaceDown) != 0);
                            if (facedown)
                            {
                                ms.Position = pos;
                                writer.Write(new byte[len - 4]);
                            }
                        }
                        reader.Close();
                        writer.Close();
                    }
                }
                using (GameServerPacket update = new GameServerPacket(GameMessage.UpdateData))
                {
                    update.Write((byte)index);
                    update.Write((byte)loc);
                    update.Write(result);
                    player.Send(update);
                }
            }
        }
Пример #6
0
        private void InitNewSpectator(GameSession player, int pos = -1)
        {
            if (m_duel == null)
            {
                return;
            }
            int deck1 = m_duel.QueryFieldCount(0, CardLocation.Deck);
            int deck2 = m_duel.QueryFieldCount(1, CardLocation.Deck);

            int hand1 = m_duel.QueryFieldCount(0, CardLocation.Hand);
            int hand2 = m_duel.QueryFieldCount(1, CardLocation.Hand);

            using (GameServerPacket packet = new GameServerPacket(GameMessage.Start))
            {
                if (pos < 0)
                {
                    packet.Write((byte)(m_swapped ? 0x11 : 0x10));
                }
                else {
                    packet.Write((byte)pos);
                }
                packet.Write(LifePoints[0]);
                packet.Write(LifePoints[1]);
                packet.Write((short)(deck1 + hand1));
                packet.Write((short)m_duel.QueryFieldCount(0, CardLocation.Extra));
                packet.Write((short)(deck2 + hand2));
                packet.Write((short)m_duel.QueryFieldCount(1, CardLocation.Extra));
                player.Send(packet);
            }
            using (GameServerPacket draw = new GameServerPacket(GameMessage.Draw))
            {
                draw.Write((byte)0);
                draw.Write((byte)hand1);
                for (int i = 0; i < hand1; i++)
                    draw.Write(0);
                player.Send(draw, false);
            }
            using (GameServerPacket draw = new GameServerPacket(GameMessage.Draw))
            {
                draw.Write((byte)1);
                draw.Write((byte)hand2);
                for (int i = 0; i < hand2; i++)
                    draw.Write(0);
                player.Send(draw);
            }
            //回合数
            using (GameServerPacket turn0 = new GameServerPacket(GameMessage.NewTurn))
            {
                turn0.Write((byte)0);
                using (GameServerPacket turn1 = new GameServerPacket(GameMessage.NewTurn))
                {
                    turn1.Write((byte)1);
                    for (int i = 0; i < TurnCount; i++)
                    {
                        if (i % 2 == 0)
                        {
                            player.Send(turn0, false);
                        }
                        else {
                            player.Send(turn1, false);
                        }
                    }
                }
            }
            //			if (CurrentPlayer == 1)
            //			{
            //				GameServerPacket turn = new GameServerPacket(GameMessage.NewTurn);
            //				turn.Write((byte)0);
            //				player.Send(turn);
            //			}
            player.PeekSend();
            InitSpectatorLocation(player, CardLocation.MonsterZone);
            InitSpectatorLocation(player, CardLocation.SpellZone);
            InitSpectatorLocation(player, CardLocation.Grave);
            InitSpectatorLocation(player, CardLocation.Removed);
        }
Пример #7
0
        public void AddPlayer(GameSession player)
        {
            //			if(IsJoin(player)){
            ////				/玩家已经在游戏
            //				player.LobbyError(Messages.MSG_PLAYER_INGAME);
            //				return;
            //			}
            Logger.Debug("add " + player.Name + " to " + Name);
            if (State != GameState.Lobby)
            {
                if (State == GameState.End)
                    return;
                player.Type = (int)PlayerType.Observer;
                SendJoinGame(player);
                player.SendTypeChange();
                player.Send(GameServerPacket.EmtryMessage(StocMessage.DuelStart));
                lock (Observers)
                    Observers.Add(player);
                if (State == GameState.Duel)
                {
                    //中途观战
                    InitNewSpectator(player);
                }
                else if (State == GameState.Side)
                {
                    player.ServerMessage(Messages.MSG_WATCH_SIDE);
                }
                ServerApi.OnPlayerEnter(player, this);
                return;
            }
            if (HostPlayer == null)
                HostPlayer = player;
            int pos = GetAvailablePlayerPos();
            if (pos != -1)
            {
                Players[pos] = player;
                IsReady[pos] = false;

                player.Type = pos;
                using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
                {
                    enter.WriteUnicode(player.Name, 20);
                    enter.Write((byte)pos);
                    SendToAll(enter);
                }
                //	Server.OnPlayEvent(PlayerStatu.PlayerReady, player);
            }
            else
            {
                using (GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange))
                {
                    watch.Write((short)(Observers.Count + 1));
                    SendToAll(watch);
                }
                player.Type = (int)PlayerType.Observer;
                lock (Observers)
                    Observers.Add(player);
                //				if(player.IsAuthentified){
                //					ServerMessage("[Server] "+player.Name+" watch game.", PlayerType.White);
                //				}
            }
            SendJoinGame(player);
            player.SendTypeChange();

            for (int i = 0; i < Players.Length; i++)
            {
                if (Players[i] != null)
                {
                    using (GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter))
                    {
                        enter.WriteUnicode(Players[i].Name, 20);
                        enter.Write((byte)i);
                        player.Send(enter, false);
                    }
                    if (IsReady[i])
                    {
                        using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange))
                        {
                            change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
                            player.Send(change, false);
                        }
                    }
                }
            }
            bool _watch = false;
            lock (Observers)
            {
                _watch = Observers.Count > 0;
            }
            if (_watch)
            {
                using (GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange))
                {
                    nwatch.Write((short)Observers.Count);
                    player.Send(nwatch, false);
                }
            }
            player.PeekSend();
            ServerApi.OnPlayerEnter(player, this);
        }
Пример #8
0
        public void SetReady(GameSession player, bool ready)
        {
            if (State != GameState.Lobby)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (IsReady[player.Type] == ready)
                return;

            if (ready)
            {
                bool ocg = Config.Rule == 0 || Config.Rule == 2;
                bool tcg = Config.Rule == 1 || Config.Rule == 2;
                int result = 1;
                if (Config.NoCheckDeck)
                    result = 0;
                else if (player.Deck != null)
                {
                    if (player.Name.StartsWith("[AI]"))
                    {
                        result = 0;
                    }
                    else {
                        result = player.Deck.Check(Banlist, ocg, tcg);
                    }
                }
                if (result != 0)
                {
                    using (GameServerPacket rechange = new GameServerPacket(StocMessage.HsPlayerChange))
                    {
                        rechange.Write((byte)((player.Type << 4) + (int)(PlayerChange.NotReady)));
                        player.Send(rechange);
                    }
                    using (GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg))
                    {
                        error.Write((byte)2); // ErrorMsg.DeckError
                                              // C++ padding: 1 byte + 3 bytes = 4 bytes
                        for (int i = 0; i < 3; i++)
                            error.Write((byte)0);
                        error.Write(result);
                        player.Send(error);
                    }
                    Logger.Debug("check deck fail:" + player.Name);
                    return;
                }
            }

            IsReady[player.Type] = ready;

            using (GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange))
            {
                change.Write((byte)((player.Type << 4) + (int)(ready ? PlayerChange.Ready : PlayerChange.NotReady)));
                SendToAll(change);
            }
        }