Ejemplo n.º 1
0
		public void OnPacket(GameServerPacket packet)
		{
			StocMessage id = packet.ReadStoc();
			if (id == StocMessage.GameMsg)
			{
				GameMessage msg = packet.ReadGameMsg();
				
				if (_messages.ContainsKey(msg)){
					_messages[msg](packet);
					#if DEBUG
					Logger.WriteLine("GameMessage deal "+msg);
					#endif
				}
				else{
					#if DEBUG
					Logger.WriteLine("GameMessage don't deal "+msg);
					#endif
				}
				return;
			}
			if (_packets.ContainsKey(id)){
				_packets[id](packet);
				#if DEBUG
				Logger.WriteLine("id deal "+id);
				#endif
			}else{
				#if DEBUG
				Logger.WriteLine("id don't deal "+id);
				#endif
			}
		}
Ejemplo n.º 2
0
        private GameServerPacket InternalReceive()
        {
            _lastAction = DateTime.Now;
            int len = _reader.ReadInt16();
            GameServerPacket packet = new GameServerPacket(_reader.ReadBytes(len));

            return(packet);
        }
Ejemplo n.º 3
0
 public void OnPacket(GameServerPacket packet)
 {
     StocMessage id = packet.ReadStoc();
     if (id == StocMessage.GameMsg)
     {
         GameMessage msg = packet.ReadGameMsg();
         if (_messages.ContainsKey(msg))
             _messages[msg](packet);
         return;
     }
     if (_packets.ContainsKey(id))
         _packets[id](packet);
 }
Ejemplo n.º 4
0
 private void InternalTick()
 {
     lock (_sendQueue)
     {
         while (_sendQueue.Count > 0)
         {
             InternalSend(_sendQueue.Dequeue());
         }
     }
     while (_client.Available > 1)
     {
         GameServerPacket packet = InternalReceive();
         lock (_receiveQueue)
         {
             _receiveQueue.Enqueue(packet);
         }
     }
 }
Ejemplo n.º 5
0
		private void OnSelectDisfield(GameServerPacket packet)
		{
			OnSelectPlace(packet);
		}
Ejemplo n.º 6
0
		private void OnSelectChain(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			int count = packet.ReadByte();
			packet.ReadByte(); // specount
			bool forced = packet.ReadByte() != 0;
			packet.ReadInt32(); // hint1
			packet.ReadInt32(); // hint2

			IList<ClientCard> cards = new List<ClientCard>();
			IList<int> descs = new List<int>();

			for (int i = 0; i < count; ++i)
			{
				packet.ReadInt32(); // card id
				int con = GetLocalPlayer(packet.ReadByte());
				int loc = packet.ReadByte();
				int seq = packet.ReadByte();
				int sseq = 0; //packet.ReadByte();

				int desc = packet.ReadInt32();
				cards.Add(_duel.GetCard(con, loc, seq, sseq));
				descs.Add(desc);
			}

			if (cards.Count == 0)
			{
				Connection.Send(CtosMessage.Response, -1);
				return;
			}

			if (cards.Count == 1 && forced)
			{
				Connection.Send(CtosMessage.Response, 0);
				return;
			}

			Connection.Send(CtosMessage.Response, _ai.OnSelectChain(cards, descs, forced));
		}
Ejemplo n.º 7
0
		private void OnSelectCounter(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			int type = packet.ReadInt16();
			int quantity = packet.ReadByte();

			IList<ClientCard> cards = new List<ClientCard>();
			IList<int> counters = new List<int>();
			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				packet.ReadInt32(); // card id
				int player = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation) packet.ReadByte();
				int seq = packet.ReadByte();
				int num = packet.ReadByte();
				cards.Add(_duel.GetCard(player, loc, seq));
				counters.Add(num);
			}

			IList<int> used = _ai.OnSelectCounter(type, quantity, cards, counters);
			byte[] result = new byte[used.Count];
			for (int i = 0; i < quantity; ++i)
				result[i] = (byte) used[i];
			GameClientPacket reply = new GameClientPacket(CtosMessage.Response);
			reply.Write(result);
			Connection.Send(reply);
		}
Ejemplo n.º 8
0
		private void OnDamage(GameServerPacket packet)
		{
			int player = GetLocalPlayer(packet.ReadByte());
			int final = _duel.LifePoints[player] - packet.ReadInt32();
			if (final < 0) final = 0;
			_duel.LifePoints[player] = final;
		}
Ejemplo n.º 9
0
		private void OnNewTurn(GameServerPacket packet)
		{
			_duel.Turn++;
			_duel.Player = GetLocalPlayer(packet.ReadByte());
			_ai.OnNewTurn();
		}
Ejemplo n.º 10
0
		private void OnSelectOption(GameServerPacket packet)
		{
			IList<int> options = new List<int>();
			packet.ReadByte(); // player
			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
				options.Add(packet.ReadInt32());
			Connection.Send(CtosMessage.Response, _ai.OnSelectOption(options));
		}
Ejemplo n.º 11
0
		private void OnSelectPosition(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			int cardId = packet.ReadInt32();
			int pos = packet.ReadByte();
			if (pos == 0x1 || pos == 0x2 || pos == 0x4 || pos == 0x8)
			{
				Connection.Send(CtosMessage.Response, pos);
				return;
			}
			IList<CardPosition> positions = new List<CardPosition>();
			if ((pos & (int)CardPosition.FaceUpAttack) != 0)
				positions.Add(CardPosition.FaceUpAttack);
			if ((pos & (int)CardPosition.FaceDownAttack) != 0)
				positions.Add(CardPosition.FaceDownAttack);
			if ((pos & (int)CardPosition.FaceUpDefence) != 0)
				positions.Add(CardPosition.FaceUpDefence);
			if ((pos & (int)CardPosition.FaceDownDefence) != 0)
				positions.Add(CardPosition.FaceDownDefence);
			Connection.Send(CtosMessage.Response, (int)_ai.OnSelectPosition(cardId, positions));
		}
Ejemplo n.º 12
0
		private void OnChainSorting(GameServerPacket packet)
		{
			Connection.Send(CtosMessage.Response, -1);
		}
Ejemplo n.º 13
0
		private void OnUpdateCard(GameServerPacket packet)
		{
			int player = GetLocalPlayer(packet.ReadByte());
			int loc = packet.ReadByte();
			int seq = packet.ReadByte();

			packet.ReadInt32(); // ???

			ClientCard card = _duel.GetCard(player, (CardLocation)loc, seq);
			if (card == null) return;

			card.Update(packet,_duel);
		}
Ejemplo n.º 14
0
		private void OnChaining(GameServerPacket packet)
		{
			packet.ReadInt32(); // card id
			int pcc = GetLocalPlayer(packet.ReadByte());
			int pcl = packet.ReadByte();
			int pcs = packet.ReadSByte();
			int subs = packet.ReadSByte();
			ClientCard card = _duel.GetCard(pcc, pcl, pcs, subs);
			int cc = GetLocalPlayer(packet.ReadByte());
			_ai.OnChaining(card, cc);
		}
Ejemplo n.º 15
0
		private void OnChainEnd(GameServerPacket packet)
		{
			_ai.OnChainEnd();
		}
Ejemplo n.º 16
0
		private void OnPosChange(GameServerPacket packet)
		{
			packet.ReadInt32(); // card id
			int pc = GetLocalPlayer(packet.ReadByte());
			int pl = packet.ReadByte();
			int ps = packet.ReadSByte();
			packet.ReadSByte(); // pp
			int cp = packet.ReadSByte();
			ClientCard card = _duel.GetCard(pc, (CardLocation)pl, ps);
			if (card != null)
				card.Position = cp;
		}
Ejemplo n.º 17
0
		private void OnMove(GameServerPacket packet)
		{
			int cardId = packet.ReadInt32();
			int pc = GetLocalPlayer(packet.ReadByte());
			int pl = packet.ReadByte();
			int ps = packet.ReadSByte();
			packet.ReadSByte(); // pp
			int cc = GetLocalPlayer(packet.ReadByte());
			int cl = packet.ReadByte();
			int cs = packet.ReadSByte();
			int cp = packet.ReadSByte();
			packet.ReadInt32(); // reason

			ClientCard card = _duel.GetCard(pc, (CardLocation)pl, ps);

			if ((pl & (int)CardLocation.Overlay) != 0)
			{
				pl = pl & 0x7f;
				card = _duel.GetCard(pc, (CardLocation)pl, ps);
				if (card != null)
					card.Overlays.Remove(cardId);
			}
			else
				_duel.RemoveCard((CardLocation)pl, card, pc, ps);

			if ((cl & (int)CardLocation.Overlay) != 0)
			{
				cl = cl & 0x7f;
				card = _duel.GetCard(cc, (CardLocation)cl, cs);
				if (card != null)
					card.Overlays.Add(cardId);
			}
			else
			{
				_duel.AddCard((CardLocation)cl, cardId, cc, cs, cp);
				if ((pl & (int)CardLocation.Overlay) == 0 && card != null)
				{
					ClientCard newcard = _duel.GetCard(cc, (CardLocation)cl, cs);
					if (newcard != null)
						newcard.Overlays.AddRange(card.Overlays);
				}
			}
		}
Ejemplo n.º 18
0
		private void OnLpUpdate(GameServerPacket packet)
		{
			int player = GetLocalPlayer(packet.ReadByte());
			_duel.LifePoints[player] = packet.ReadInt32();
		}
Ejemplo n.º 19
0
		private void OnSelectEffectYn(GameServerPacket packet)
		{
			packet.ReadByte(); // player

			int cardId = packet.ReadInt32();
			int player = GetLocalPlayer(packet.ReadByte());
			CardLocation loc = (CardLocation)packet.ReadByte();
			int seq = packet.ReadByte();
			packet.ReadByte();

			ClientCard card = _duel.GetCard(player, loc, seq);
			if (card == null)
			{
				Connection.Send(CtosMessage.Response, 0);
				return;
			}

			if (card.Id == 0) card.SetId(cardId);

			int reply = _ai.OnSelectEffectYn(card) ? (1) : (0);
			Connection.Send(CtosMessage.Response, reply);
		}
Ejemplo n.º 20
0
		private void OnSelectTribute(GameServerPacket packet)
		{
			InternalOnSelectCard(packet, _ai.OnSelectTribute);
		}
Ejemplo n.º 21
0
		private void OnSelectIdleCmd(GameServerPacket packet)
		{
			packet.ReadByte(); // player

			_duel.MainPhase = new MainPhase();
			MainPhase main = _duel.MainPhase;
			int count;
			for (int k = 0; k < 5; k++)
			{
				count = packet.ReadByte();
				for (int i = 0; i < count; ++i)
				{
					packet.ReadInt32(); // card id
					int con = GetLocalPlayer(packet.ReadByte());
					CardLocation loc = (CardLocation)packet.ReadByte();
					int seq = packet.ReadByte();
					ClientCard card = _duel.GetCard(con, loc, seq);
					if (card == null) continue;
					card.ActionIndex[k] = i;
					switch (k)
					{
						case 0:
							main.SummonableCards.Add(card);
							break;
						case 1:
							main.SpecialSummonableCards.Add(card);
							break;
						case 2:
							main.ReposableCards.Add(card);
							break;
						case 3:
							main.MonsterSetableCards.Add(card);
							break;
						case 4:
							main.SpellSetableCards.Add(card);
							break;
					}
				}
			}
			count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				packet.ReadInt32(); // card id
				int con = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation)packet.ReadByte();
				int seq = packet.ReadByte();
				int desc = packet.ReadInt32();

				ClientCard card = _duel.GetCard(con, loc, seq);
				if (card == null) continue;
				card.ActionIndex[5] = i;
				if (card.ActionActivateIndex.ContainsKey(desc))
					card.ActionActivateIndex.Remove(desc);
				card.ActionActivateIndex.Add(desc, i);
				main.ActivableCards.Add(card);
				main.ActivableDescs.Add(desc);
			}

			main.CanBattlePhase = packet.ReadByte() != 0;
			main.CanEndPhase = packet.ReadByte() != 0;
			packet.ReadByte(); // CanShuffle

			Connection.Send(CtosMessage.Response, _ai.OnSelectIdleCmd(main).ToValue());
		}
Ejemplo n.º 22
0
		private void OnAnnounceAttrib(GameServerPacket packet)
		{
			IList<CardAttribute> attributes = new List<CardAttribute>();
			packet.ReadByte(); // player
			int count = packet.ReadByte();
			int available = packet.ReadInt32();
			int filter = 0x1;
			for (int i = 0; i < 7; ++i)
			{
				if ((available & filter) != 0)
					attributes.Add((CardAttribute) filter);
				filter <<= 1;
			}
			attributes = _ai.OnAnnounceAttrib(count, attributes);
			int reply = 0;
			for (int i = 0; i < count; ++i)
				reply += (int)attributes[i];
			Connection.Send(CtosMessage.Response, reply);
		}
Ejemplo n.º 23
0
		private void OnSelectPlace(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			packet.ReadByte(); // min
			int field = ~packet.ReadInt32();

			byte[] resp = new byte[3];

			bool pendulumZone = false;
            
			int filter;
			if ((field & 0x1f) != 0)
			{
				resp[0] = (byte)GetLocalPlayer(0);
				resp[1] = 0x4;
				filter = field & 0x1f;
			}
			else if ((field & 0x1f00) != 0)
			{
				resp[0] = (byte)GetLocalPlayer(0);
				resp[1] = 0x8;
				filter = (field >> 8) & 0x1f;
			}
			else if ((field & 0xc000) != 0)
			{
				resp[0] = (byte)GetLocalPlayer(0);
				resp[1] = 0x8;
				filter = (field >> 14) & 0x3;
				pendulumZone = true;
			}
			else if ((field & 0x1f0000) != 0)
			{
				resp[0] = (byte)GetLocalPlayer(1);
				resp[1] = 0x4;
				filter = (field >> 16) & 0x1f;
			}
			else if ((field & 0x1f000000) != 0)
			{
				resp[0] = (byte) GetLocalPlayer(1);
				resp[1] = 0x8;
				filter = (field >> 24) & 0x1f;
			}
			else
			{
				resp[0] = (byte) GetLocalPlayer(1);
				resp[1] = 0x8;
				filter = (field >> 30) & 0x3;
				pendulumZone = true;
			}

			if (!pendulumZone)
			{
				if ((filter & 0x4) != 0) resp[2] = 2;
				else if ((filter & 0x2) != 0) resp[2] = 1;
				else if ((filter & 0x8) != 0) resp[2] = 3;
				else if ((filter & 0x1) != 0) resp[2] = 0;
				else if ((filter & 0x10) != 0) resp[2] = 4;
			}
			else
			{
				if ((filter & 0x1) != 0) resp[2] = 6;
				if ((filter & 0x2) != 0) resp[2] = 7;
			}

			GameClientPacket reply = new GameClientPacket(CtosMessage.Response);
			reply.Write(resp);
			Connection.Send(reply);
		}
Ejemplo n.º 24
0
		private void OnAnnounceNumber(GameServerPacket packet)
		{
			IList<int> numbers = new List<int>();
			packet.ReadByte(); // player
			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
				numbers.Add(packet.ReadInt32());
			Connection.Send(CtosMessage.Response, _ai.OnAnnounceNumber(numbers));
		}
Ejemplo n.º 25
0
		private void OnSelectSum(GameServerPacket packet)
		{
			packet.ReadByte(); // mode
			packet.ReadByte(); // player
			int sumval = packet.ReadInt32();
			int min = packet.ReadByte();
			int max = packet.ReadByte();

			IList<ClientCard> cards = new List<ClientCard>();
			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				int cardId = packet.ReadInt32();
				int player = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation)packet.ReadByte();
				int seq = packet.ReadByte();
				ClientCard card = _duel.GetCard(player, loc, seq);
				if (card != null)
				{
					if (cardId != 0 && card.Id != cardId)
						card.SetId(cardId);
					cards.Add(card);
				}
				packet.ReadInt32();
			}

			IList<ClientCard> selected = _ai.OnSelectSum(cards, sumval, min, max);

			byte[] result = new byte[selected.Count + 1];
			result[0] = (byte)selected.Count;
			for (int i = 0; i < selected.Count; ++i)
			{
				int id = 0;
				for (int j = 0; j < count; ++j)
				{
					if (cards[j] == null) continue;
					if (cards[j].Equals(selected[i]))
					{
						id = j;
						break;
					}
				}
				result[i + 1] = (byte)id;
			}

			GameClientPacket reply = new GameClientPacket(CtosMessage.Response);
			reply.Write(result);
			Connection.Send(reply);
		}
Ejemplo n.º 26
0
		private void OnNewPhase(GameServerPacket packet)
		{
			_duel.Phase = (DuelPhase)packet.ReadByte();
			_ai.OnNewPhase();
		}
Ejemplo n.º 27
0
		private void OnSelectYesNo(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			int reply = _ai.OnSelectYesNo(packet.ReadInt32()) ? (1) : (0);
			Connection.Send(CtosMessage.Response, reply);
		}
Ejemplo n.º 28
0
		private void OnUpdateData(GameServerPacket packet)
		{
			int player = GetLocalPlayer(packet.ReadByte());
			CardLocation loc = (CardLocation)packet.ReadByte();

			IList<ClientCard> cards = null;
			switch (loc)
			{
				case CardLocation.Hand:
					cards = _duel.Fields[player].Hand;
					break;
				case CardLocation.MonsterZone:
					cards = _duel.Fields[player].MonsterZone;
					break;
				case CardLocation.SpellZone:
					cards = _duel.Fields[player].SpellZone;
					break;
				case CardLocation.Grave:
					cards = _duel.Fields[player].Graveyard;
					break;
				case CardLocation.Removed:
					cards = _duel.Fields[player].Banished;
					break;
				case CardLocation.Deck:
					cards = _duel.Fields[player].Deck;
					break;
				case CardLocation.Extra:
					cards = _duel.Fields[player].ExtraDeck;
					break;
			}
			if (cards != null)
			{
				foreach (ClientCard card in cards)
				{
					try{
						int len = packet.ReadInt32();
						if (len < 8) continue;
						long pos = packet.GetPosition();
						card.Update(packet, _duel);
						packet.SetPosition(pos + len - 4);
					}catch(Exception){
						
					}
				}
			}
		}
Ejemplo n.º 29
0
		private void OnAnnounceCard(GameServerPacket packet)
		{
			Connection.Send(CtosMessage.Response, _ai.OnAnnounceCard());
		}
Ejemplo n.º 30
0
		private void OnSelectBattleCmd(GameServerPacket packet)
		{
			packet.ReadByte(); // player
			_duel.BattlePhase = new BattlePhase();
			BattlePhase battle = _duel.BattlePhase;

			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				packet.ReadInt32(); // card id
				int con = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation)packet.ReadByte();
				int seq = packet.ReadByte();
				int desc = packet.ReadInt32();

				ClientCard card = _duel.GetCard(con, loc, seq);
				if (card != null)
				{
					card.ActionIndex[0] = i;
					battle.ActivableCards.Add(card);
					battle.ActivableDescs.Add(desc);
				}
			}

			count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				packet.ReadInt32(); // card id
				int con = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation)packet.ReadByte();
				int seq = packet.ReadByte();
				packet.ReadByte(); // diratt

				ClientCard card = _duel.GetCard(con, loc, seq);
				if (card != null)
				{
					card.ActionIndex[1] = i;
					battle.AttackableCards.Add(_duel.GetCard(con, loc, seq));
				}
			}

			battle.CanMainPhaseTwo = packet.ReadByte() != 0;
			battle.CanEndPhase = packet.ReadByte() != 0;

			Connection.Send(CtosMessage.Response, _ai.OnSelectBattleCmd(battle).ToValue());
		}
Ejemplo n.º 31
0
 private GameServerPacket InternalReceive()
 {
     _lastAction = DateTime.Now;
     int len = _reader.ReadInt16();
     GameServerPacket packet = new GameServerPacket(_reader.ReadBytes(len));
     return packet;
 }
Ejemplo n.º 32
0
		private void InternalOnSelectCard(GameServerPacket packet, Func<IList<ClientCard>, int, int, bool, IList<ClientCard>> func)
		{
			packet.ReadByte(); // player
			bool cancelable = packet.ReadByte() != 0;
			int min = packet.ReadByte();
			int max = packet.ReadByte();

			IList<ClientCard> cards = new List<ClientCard>();
			int count = packet.ReadByte();
			for (int i = 0; i < count; ++i)
			{
				int id = packet.ReadInt32();
				int player = GetLocalPlayer(packet.ReadByte());
				CardLocation loc = (CardLocation)packet.ReadByte();
				int seq = packet.ReadByte();
				packet.ReadByte(); // pos
				ClientCard card;
				if (((int)loc & (int)CardLocation.Overlay) != 0)
					card = new ClientCard(id, CardLocation.Overlay);
				else
					card = _duel.GetCard(player, loc, seq);
				if (card == null) continue;
				if (card.Id == 0)
					card.SetId(id);
				cards.Add(card);
			}

			IList<ClientCard> selected = func(cards, min, max, cancelable);

			if (selected.Count == 0 && cancelable)
			{
				Connection.Send(CtosMessage.Response, -1);
				return;
			}

			byte[] result = new byte[selected.Count + 1];
			result[0] = (byte)selected.Count;
			for (int i = 0; i < selected.Count; ++i)
			{
				int id = 0;
				for (int j = 0; j < count; ++j)
				{
					if (cards[j] == null) continue;
					if (cards[j].Equals(selected[i]))
					{
						id = j;
						break;
					}
				}
				result[i + 1] = (byte)id;
			}

			GameClientPacket reply = new GameClientPacket(CtosMessage.Response);
			reply.Write(result);
			Connection.Send(reply);
		}