Exemple #1
0
        public void AddPlayer(Player player)
        {
            int pos = GetAvailablePlayerPos();
            if (Program.Config.STDOUT == true)
                Console.WriteLine("::::join-slot|{1}|{0}", player.Name, pos);


            if (State != GameState.Lobby)
            {
                player.Type = (int)PlayerType.Observer;
                if (State == GameState.End)
                    return;
                SendJoinGame(player);
                player.SendTypeChange();
                player.Send(new GameServerPacket(StocMessage.DuelStart));
                Observers.Add(player);


                if (State == GameState.Duel)
                    InitNewSpectator(player);
                return;
            }

            if (HostPlayer == null)
                HostPlayer = player;

           
            if (pos != -1)
            {
                GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
                enter.Write(player.Name, 20);
                enter.Write((byte)pos);
                SendToAll(enter);

                Players[pos] = player;
                IsReady[pos] = false;
                player.Type = pos;

                
            }
            else
            {
                GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange);
                watch.Write((short)(Observers.Count + 1));
                if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
                SendToAll(watch);

                player.Type = (int)PlayerType.Observer;
                Observers.Add(player);
               
            }

            SendJoinGame(player);
            player.SendTypeChange();

            for (int i = 0; i < Players.Length; i++)
            {
                if (Players[i] != null)
                {
                    GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
                    enter.Write(Players[i].Name, 20);
                    enter.Write((byte)i);
                    player.Send(enter);

                    if (IsReady[i])
                    {
                        GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
                        change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
                        player.Send(change);
                    }
                }
            }

            if (Observers.Count > 0)
            {
                GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
                nwatch.Write((short)Observers.Count);
                player.Send(nwatch);
                if (Program.Config.STDOUT == true)
                Console.WriteLine("::::spectator|{0}", Observers.Count);
            }
        }
Exemple #2
0
        public void SetReady(Player 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)
                    result = player.Deck.Check(Banlist, ocg, tcg);
                if (result != 0)
                {
                    GameServerPacket rechange = new GameServerPacket(StocMessage.HsPlayerChange);
                    rechange.Write((byte)((player.Type << 4) + (int)(PlayerChange.NotReady)));
                    player.Send(rechange);
                    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);
                    return;
                }
            }

            IsReady[player.Type] = ready;

            GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
            change.Write((byte)((player.Type << 4) + (int)(ready ? PlayerChange.Ready : PlayerChange.NotReady)));
            if (Program.Config.STDOUT == true)
            Console.WriteLine("::::lock-slot|{1}|{0}", ready, player.Type);
            SendToAll(change);
        }
Exemple #3
0
        private void InitSpectatorLocation(Player 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);

                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);

                    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);

                    foreach (ClientCard material in card.Overlay)
                    {
                        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);

                        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);
                    }

                    if (facedown)
                    {
                        ms.Position = pos;
                        writer.Write(new byte[len - 4]);
                    }
                }

                if (loc == CardLocation.MonsterZone)
                {
                    result = m_duel.QueryFieldCard(index, loc, 0x81fff, false);
                    ms = new MemoryStream(result);
                    reader = new BinaryReader(ms);
                    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]);
                        }
                    }
                }

                GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
                update.Write((byte)index);
                update.Write((byte)loc);
                update.Write(result);
                player.Send(update);
            }
        }
Exemple #4
0
        private void InitNewSpectator(Player player)
        {
            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);

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);
            packet.Write((byte)(m_swapped ? 0x11 : 0x10));
            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);

            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);

            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);

            GameServerPacket turn = new GameServerPacket(GameMessage.NewTurn);
            turn.Write((byte)0);
            player.Send(turn);
            if (CurrentPlayer == 1)
            {
                turn = new GameServerPacket(GameMessage.NewTurn);
                turn.Write((byte)0);
                player.Send(turn);
            }

            InitSpectatorLocation(player, CardLocation.MonsterZone);
            InitSpectatorLocation(player, CardLocation.SpellZone);
            InitSpectatorLocation(player, CardLocation.Grave);
            InitSpectatorLocation(player, CardLocation.Removed);
        }
Exemple #5
0
 private void SendDuelingPlayers(Player player)
 {
     for (int i = 0; i < Players.Length; i++)
     {
         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.Write(Players[id].Name, 20);
         enter.Write((byte)i);
         player.Send(enter);
     }
 }
Exemple #6
0
        private void SendJoinGame(Player player)
        {
            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);
        }
Exemple #7
0
		public void AddPlayer(Player player)
		{
			if (State != GameState.Lobby)
			{
				if (State == GameState.End)
					return;
				//断线重连
//				if(CanPause && IsPause){
//					for(int i=0;i<PlayerNames.Length;i++){
//						if(PlayerNames[i] == player.Name){
//							if(Players[i] == null){
//								//重新加入游戏
//								player.Type = i;
//								Players[i] = player;
//								AutoEndTrun = Program.Config.AutoEndTurn;
//								GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
//								enter.Write(player.Name, 20);
//								enter.Write((byte)i);
//								SendToAll(enter);
//								SendJoinGame(player);
//								player.SendTypeChange();
//								//player.Send(new GameServerPacket(StocMessage.DuelStart));
//								InitNewSpectator(player, i);
//								IsPause = false;
//								return;
//							}
//							break;
//						}
//					}
//				}
				player.Type = (int)PlayerType.Observer;
				SendJoinGame(player);
				player.SendTypeChange();
				player.Send(new GameServerPacket(StocMessage.DuelStart));
				Observers.Add(player);
				if (State == GameState.Duel){
					//中途观战
					InitNewSpectator(player);
				}else if(State == GameState.Side){
					player.ServerMessage(Messages.MSG_WATCH_SIDE);
				}
				return;
			}

			if (HostPlayer == null)
				HostPlayer = player;

			int pos = GetAvailablePlayerPos();
			if (pos != -1)
			{
				GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
				enter.WriteUnicode(player.Name, 20);
				enter.Write((byte)pos);
				SendToAll(enter);
				PlayerNames[pos] = player.Name;
				Players[pos] = player;
				IsReady[pos] = false;
				player.Type = pos;
			}
			else
			{
				GameServerPacket watch = new GameServerPacket(StocMessage.HsWatchChange);
				watch.Write((short)(Observers.Count + 1));
				SendToAll(watch);

				player.Type = (int)PlayerType.Observer;
				Observers.Add(player);
				if(player.IsAuthentified){
					SendToAll(GameManager.getMessage("[Server] "+player.Name+" watch game.", PlayerType.White));
				}
			}

			SendJoinGame(player);
			player.SendTypeChange();

			for (int i = 0; i < Players.Length; i++)
			{
				if (Players[i] != null)
				{
					GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
					enter.WriteUnicode(Players[i].Name, 20);
					enter.Write((byte)i);
					player.Send(enter);
					if (IsReady[i])
					{
						GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
						change.Write((byte)((i << 4) + (int)PlayerChange.Ready));
						player.Send(change);
					}
				}
			}

			if (Observers.Count > 0)
			{
				GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
				nwatch.Write((short)Observers.Count);
				player.Send(nwatch);
			}
		}