Example #1
0
        public void SendErrorMessage(string message) {
            _isError = true;

            GameServerPacket join = new GameServerPacket(StocMessage.JoinGame);
            join.Write(0U);
            join.Write((byte)0);
            join.Write((byte)0);
            join.Write(false);
            join.Write(false);
            join.Write(false);
            // C++ padding: 5 bytes + 3 bytes = 8 bytes
            for (int i = 0; i < 3; i++)
                join.Write((byte)0);
            join.Write(8000);
            join.Write(5);
            join.Write(1);
            join.Write(0);
            Send(join);

            GameServerPacket packet = new GameServerPacket(StocMessage.TypeChange);
            packet.Write((byte)(0));
            Send(packet);

            GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
            enter.Write("[Error occurred]:", 20);
            enter.Write((byte)0);
            Send(enter);

            enter = new GameServerPacket(StocMessage.HsPlayerEnter);
            enter.Write(message, 20);
            enter.Write((byte)1);
            Send(enter);
        }
Example #2
0
        private void OnMove(CoreMessage msg)
        {
            byte[] raw = msg.Reader.ReadBytes(16);
            int    pc  = raw[4];
            int    pl  = raw[5];
            int    cc  = raw[8];
            int    cl  = raw[9];
            int    cs  = raw[10];
            int    cp  = raw[11];

            SendToPlayer(msg, cc);
            GameServerPacket packet = new GameServerPacket(msg.Message);

            packet.Write(raw);
            if (!Convert.ToBoolean((cl & ((int)CardLocation.Grave + (int)CardLocation.Overlay))) && Convert.ToBoolean((cl & ((int)CardLocation.Deck + (int)CardLocation.Hand))) ||
                Convert.ToBoolean((cp & (int)CardPosition.FaceDown)))
            {
                packet.SetPosition(2);
                packet.Write(0);
            }
            Game.SendToAllBut(packet, cc);

            if (cl != 0 && (cl & 0x80) == 0 && (cl != pl || pc != cc))
            {
                Game.RefreshSingle(cc, cl, cs);
            }
        }
Example #3
0
        private void OnDraw(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);
            int player = msg.Reader.ReadByte();
            int count  = msg.Reader.ReadByte();

            packet.Write((byte)player);
            packet.Write((byte)count);

            for (int i = 0; i < count; i++)
            {
                uint code = msg.Reader.ReadUInt32();
                if ((code & 0x80000000) != 0)
                {
                    packet.Write(code);
                }
                else
                {
                    packet.Write(0);
                }
            }

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);
        }
Example #4
0
        public void Surrender(Player player, int reason, bool force = false)
        {
            if (!force)
            {
                if (State != GameState.Duel)
                {
                    return;
                }
            }
            if (player.Type == (int)PlayerType.Observer)
            {
                return;
            }
            GameServerPacket win = new GameServerPacket(GameMessage.Win);
            int team             = player.Type;

            if (IsTag)
            {
                team = player.Type >= 2 ? 1 : 0;
            }
            else if (State == GameState.Hand)
            {
                team = 1 - team;
            }
            win.Write((byte)(1 - team));
            win.Write((byte)reason);
            SendToAll(win);

            MatchSaveResult(1 - team);

            EndDuel(reason == 4);
        }
Example #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 (_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);
         //padding
         enter.Write((byte)0);
         player.Send(enter);
     }
 }
Example #6
0
        public void CustomMessage(Player player, string msg)
        {
            string           finalmsg = msg;
            GameServerPacket packet   = new GameServerPacket(StocMessage.Chat);

            packet.Write((short)PlayerType.Yellow);
            packet.Write(finalmsg, finalmsg.Length + 1);
            SendToAllBut(packet, player);
        }
Example #7
0
        private void HandleError(string error)
        {
            GameServerPacket packet = new GameServerPacket(StocMessage.Chat);

            packet.Write((short)PlayerType.Observer);
            packet.Write(error, error.Length + 1);
            SendToAll(packet);

            File.WriteAllText("lua_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", error);
        }
Example #8
0
        private void OnSet(CoreMessage msg)
        {
            msg.Reader.ReadBytes(4);
            byte[]           raw    = msg.Reader.ReadBytes(4);
            GameServerPacket packet = new GameServerPacket(GameMessage.Set);

            packet.Write(0);
            packet.Write(raw);
            Game.SendToAll(packet);
        }
Example #9
0
        public void RefreshGrave(int player, int flag = 0x81fff, bool useCache = true)
        {
            byte[]           result = _duel.QueryFieldCard(player, CardLocation.Grave, flag, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);

            update.Write((byte)player);
            update.Write((byte)CardLocation.Grave);
            update.Write(result);
            SendToAll(update);
        }
Example #10
0
        public void RefreshExtra(int player, int flag = 0x81fff, bool useCache = true)
        {
            byte[]           result = _duel.QueryFieldCard(player, CardLocation.Extra, flag, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);

            update.Write((byte)player);
            update.Write((byte)CardLocation.Extra);
            update.Write(result);
            CurPlayers[player].Send(update);
        }
Example #11
0
        private void InitNewSpectator(Player player)
        {
            int deck1 = _duel.QueryFieldCount(0, CardLocation.Deck);
            int deck2 = _duel.QueryFieldCount(1, CardLocation.Deck);

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

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);

            packet.Write((byte)(_swapped ? 0x11 : 0x10));
            packet.Write(LifePoints[0]);
            packet.Write(LifePoints[1]);
            packet.Write((short)(deck1 + hand1));
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)(deck2 + hand2));
            packet.Write((short)_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);
        }
Example #12
0
        public void MoveToDuelist(Player player)
        {
            if (State != GameState.Lobby)
            {
                return;
            }
            int pos = GetAvailablePlayerPos();

            if (pos == -1)
            {
                return;
            }
            if (player.Type != (int)PlayerType.Observer)
            {
                if (!IsTag || IsReady[player.Type])
                {
                    return;
                }

                pos = (player.Type + 1) % 4;
                while (Players[pos] != null)
                {
                    pos = (pos + 1) % 4;
                }

                GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
                change.Write((byte)((player.Type << 4) + pos));
                SendToAll(change);

                Players[player.Type] = null;
                Players[pos]         = player;
                player.Type          = pos;
                player.SendTypeChange();
            }
            else
            {
                Observers.Remove(player);
                Players[pos] = player;
                player.Type  = pos;

                GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
                enter.Write(player.Name, 20);
                enter.Write((byte)pos);
                //padding
                enter.Write((byte)0);
                SendToAll(enter);

                GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
                nwatch.Write((short)Observers.Count);
                SendToAll(nwatch);

                player.SendTypeChange();
            }
        }
Example #13
0
        public void WaitForResponse(int player)
        {
            _lastresponse            = player;
            CurPlayers[player].State = PlayerState.Response;
            SendToAllBut(new GameServerPacket(GameMessage.Waiting), player);
            TimeStart();
            GameServerPacket packet = new GameServerPacket(StocMessage.TimeLimit);

            packet.Write((byte)player);
            packet.Write((byte)0); // C++ padding
            packet.Write((short)_timelimit[player]);
            SendToPlayers(packet);
        }
Example #14
0
        private void OnSelectCard(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);

            int player = msg.Reader.ReadByte();

            packet.Write((byte)player);
            packet.Write(msg.Reader.ReadBytes(3));

            int count = msg.Reader.ReadByte();

            packet.Write((byte)count);

            for (int i = 0; i < count; i++)
            {
                int code = msg.Reader.ReadInt32();
                int pl   = msg.Reader.ReadByte();
                int loc  = msg.Reader.ReadByte();
                int seq  = msg.Reader.ReadByte();
                int pos  = msg.Reader.ReadByte();
                packet.Write(pl == player ? code : 0);
                packet.Write((byte)pl);
                packet.Write((byte)loc);
                packet.Write((byte)seq);
                packet.Write((byte)pos);
            }

            Game.WaitForResponse(player);
            Game.CurPlayers[player].Send(packet);
        }
Example #15
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 (player.Deck != null)
                {
                    result = Config.NoCheckDeck ? 0 : 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)));
            SendToAll(change);
        }
Example #16
0
 public void RemovePlayer(Player player)
 {
     if (player.Equals(HostPlayer) && State == GameState.Lobby)
     {
         _server.Stop();
     }
     else if (player.Type == (int)PlayerType.Observer)
     {
         Observers.Remove(player);
         if (State == GameState.Lobby)
         {
             GameServerPacket nwatch = new GameServerPacket(StocMessage.HsWatchChange);
             nwatch.Write((short)Observers.Count);
             SendToAll(nwatch);
         }
         player.Disconnect();
     }
     else if (State == GameState.Lobby)
     {
         Players[player.Type] = null;
         IsReady[player.Type] = false;
         GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
         change.Write((byte)((player.Type << 4) + (int)PlayerChange.Leave));
         SendToAll(change);
         player.Disconnect();
     }
     else
     {
         Surrender(player, 4, true);
     }
 }
Example #17
0
        public void Chat(Player player, string msg)
        {
            GameServerPacket packet = new GameServerPacket(StocMessage.Chat);

            packet.Write((short)player.Type);
            if (player.Type == (int)PlayerType.Observer)
            {
                msg = "[" + player.Name + "]: " + msg;
                CustomMessage(player, msg);
            }
            else
            {
                packet.Write(msg, msg.Length + 1);
                SendToAllBut(packet, player);
            }
        }
Example #18
0
        public void MoveToObserver(Player player)
        {
            if (State != GameState.Lobby)
            {
                return;
            }
            if (player.Type == (int)PlayerType.Observer)
            {
                return;
            }
            if (IsReady[player.Type])
            {
                return;
            }
            Players[player.Type] = null;
            IsReady[player.Type] = false;
            Observers.Add(player);

            GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);

            change.Write((byte)((player.Type << 4) + (int)PlayerChange.Observe));
            SendToAll(change);

            player.Type = (int)PlayerType.Observer;
            player.SendTypeChange();
        }
Example #19
0
        public void SendTypeChange()
        {
            GameServerPacket packet = new GameServerPacket(StocMessage.TypeChange);

            packet.Write((byte)(Type + (Game.HostPlayer.Equals(this) ? (int)PlayerType.Host : 0)));
            Send(packet);
        }
Example #20
0
        private void SendToAll(CoreMessage msg)
        {
            byte[]           buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);

            packet.Write(buffer);
            Game.SendToAll(packet);
        }
Example #21
0
        public void RefreshSingle(int player, int location, int sequence, int flag = 0x781fff)
        {
            byte[] result = _duel.QueryCard(player, location, sequence, flag);

            if (location == (int)CardLocation.Removed && (result[15] & (int)CardPosition.FaceDown) != 0)
            {
                return;
            }

            GameServerPacket update = new GameServerPacket(GameMessage.UpdateCard);

            update.Write((byte)player);
            update.Write((byte)location);
            update.Write((byte)sequence);
            update.Write(result);
            CurPlayers[player].Send(update);

            if (IsTag)
            {
                if ((location & (int)CardLocation.Onfield) != 0)
                {
                    SendToTeam(update, player);
                    if ((result[15] & (int)CardPosition.FaceUp) != 0)
                    {
                        SendToTeam(update, 1 - player);
                    }
                }
                else
                {
                    CurPlayers[player].Send(update);
                    if ((location & 0x90) != 0)
                    {
                        SendToAllBut(update, player);
                    }
                }
            }
            else
            {
                if ((location & 0x90) != 0 || ((location & 0x2c) != 0 && (result[15] & (int)CardPosition.FaceUp) != 0))
                {
                    SendToAllBut(update, player);
                }
            }
        }
Example #22
0
        private void OnShuffleHand(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);
            int player = msg.Reader.ReadByte();
            int count  = msg.Reader.ReadByte();

            packet.Write((byte)player);
            packet.Write((byte)count);

            msg.Reader.ReadBytes(count * 4);
            for (int i = 0; i < count; i++)
            {
                packet.Write(0);
            }

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);
            Game.RefreshHand(player, 0x181fff, false);
        }
Example #23
0
        private void OnUpdateDeck(GameClientPacket packet)
        {
            if (Type == (int)PlayerType.Observer)
            {
                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 (Game.State == GameState.Lobby)
            {
                Deck = deck;
                Game.IsReady[Type] = false;
            }
            else if (Game.State == GameState.Side)
            {
                if (Game.IsReady[Type])
                {
                    return;
                }
                if (!Deck.Check(deck))
                {
                    GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg);
                    error.Write((byte)3);
                    error.Write(0);
                    Send(error);
                    return;
                }
                Deck = deck;
                Game.IsReady[Type] = true;
                Send(new GameServerPacket(StocMessage.DuelStart));
                Game.MatchSide();
            }
        }
Example #24
0
        private void SendToPlayer(CoreMessage msg, int player)
        {
            if (player != 0 && player != 1)
            {
                return;
            }
            byte[]           buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);

            packet.Write(buffer);
            Game.CurPlayers[player].Send(packet);
        }
Example #25
0
        public void SendErrorMessage(string message)
        {
            _isError = true;

            GameServerPacket join = new GameServerPacket(StocMessage.JoinGame);

            join.Write(0U);
            join.Write((byte)0);
            join.Write((byte)0);
            join.Write(false);
            join.Write(false);
            join.Write(false);
            // C++ padding: 5 bytes + 3 bytes = 8 bytes
            for (int i = 0; i < 3; i++)
            {
                join.Write((byte)0);
            }
            join.Write(8000);
            join.Write(5);
            join.Write(1);
            join.Write(0);
            Send(join);

            GameServerPacket packet = new GameServerPacket(StocMessage.TypeChange);

            packet.Write((byte)(0));
            Send(packet);

            GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);

            enter.Write("[Error occurred]:", 20);
            enter.Write((byte)0);
            Send(enter);

            enter = new GameServerPacket(StocMessage.HsPlayerEnter);
            enter.Write(message, 20);
            enter.Write((byte)1);
            Send(enter);
        }
Example #26
0
        public void RefreshSpells(int player, int flag = 0x681fff, bool useCache = true)
        {
            byte[]           result = _duel.QueryFieldCard(player, CardLocation.SpellZone, flag, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);

            update.Write((byte)player);
            update.Write((byte)CardLocation.SpellZone);
            update.Write(result);
            SendToTeam(update, player);

            update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.SpellZone);

            MemoryStream ms     = new MemoryStream(result);
            BinaryReader reader = new BinaryReader(ms);
            BinaryWriter writer = new BinaryWriter(ms);

            for (int i = 0; i < 8; i++)
            {
                int len = reader.ReadInt32();
                if (len == 4)
                {
                    continue;
                }
                long   pos = ms.Position;
                byte[] raw = reader.ReadBytes(len - 4);
                if ((raw[11] & (int)CardPosition.FaceDown) != 0)
                {
                    ms.Position = pos;
                    writer.Write(new byte[len - 4]);
                }
            }
            update.Write(result);

            SendToTeam(update, 1 - player);
            SendToObservers(update);
        }
Example #27
0
        public void RefreshHand(int player, int flag = 0x181fff, bool useCache = true)
        {
            byte[]           result = _duel.QueryFieldCard(player, CardLocation.Hand, flag | 0x100000, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);

            update.Write((byte)player);
            update.Write((byte)CardLocation.Hand);
            update.Write(result);
            CurPlayers[player].Send(update);

            update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.Hand);

            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);
                if (raw[len - 8] == 0)
                {
                    ms.Position = pos;
                    writer.Write(new byte[len - 4]);
                }
            }
            update.Write(result);

            SendToAllBut(update, player);
        }
Example #28
0
        private void OnConfirmCards(CoreMessage msg)
        {
            int player = msg.Reader.ReadByte();
            int count  = msg.Reader.ReadByte();

            msg.Reader.ReadBytes(count * 7);

            byte[]           buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);

            packet.Write(buffer);
            if ((CardLocation)buffer[7] == CardLocation.Hand)
            {
                Game.SendToAll(packet);
            }
            else
            {
                Game.CurPlayers[player].Send(packet);
            }
        }
Example #29
0
        private void OnHint(CoreMessage msg)
        {
            int type   = msg.Reader.ReadByte();
            int player = msg.Reader.ReadByte();

            msg.Reader.ReadInt32();

            byte[]           buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);

            packet.Write(buffer);

            switch (type)
            {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                Game.CurPlayers[player].Send(packet);
                break;

            case 6:
            case 7:
            case 8:
            case 9:
                Game.SendToAllBut(packet, player);
                break;

            case 10:
                if (Game.IsTag)
                {
                    Game.CurPlayers[player].Send(packet);
                }
                else
                {
                    Game.SendToAll(packet);
                }
                break;
            }
        }
Example #30
0
        private void OnTagSwap(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(GameMessage.TagSwap);

            int player = msg.Reader.ReadByte();

            packet.Write((byte)player);
            packet.Write(msg.Reader.ReadBytes(2));
            int count = msg.Reader.ReadByte();

            packet.Write((byte)count);
            packet.Write(msg.Reader.ReadBytes(4));

            for (int i = 0; i < count; i++)
            {
                uint code = msg.Reader.ReadUInt32();
                if ((code & 0x80000000) != 0)
                {
                    packet.Write(code);
                }
                else
                {
                    packet.Write(0);
                }
            }

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);

            Game.RefreshExtra(player);
            Game.RefreshMonsters(0, 0x81fff, false);
            Game.RefreshMonsters(1, 0x81fff, false);
            Game.RefreshSpells(0, 0x681fff, false);
            Game.RefreshSpells(1, 0x681fff, false);
            Game.RefreshHand(0, 0x181fff, false);
            Game.RefreshHand(1, 0x181fff, false);
        }
Example #31
0
        public void TpResult(Player player, bool result)
        {
            if (State != GameState.Starting)
            {
                return;
            }
            if (player.Type != _startplayer)
            {
                return;
            }

            _swapped = false;
            if (result && player.Type == (IsTag ? 2 : 1) || !result && player.Type == 0)
            {
                _swapped = true;
                if (IsTag)
                {
                    Player temp = Players[0];
                    Players[0] = Players[2];
                    Players[2] = temp;

                    temp       = Players[1];
                    Players[1] = Players[3];
                    Players[3] = temp;

                    Players[0].Type = 0;
                    Players[1].Type = 1;
                    Players[2].Type = 2;
                    Players[3].Type = 3;
                }
                else
                {
                    Player temp = Players[0];
                    Players[0]      = Players[1];
                    Players[1]      = temp;
                    Players[0].Type = 0;
                    Players[1].Type = 1;
                }
            }
            CurPlayers[0] = Players[0];
            CurPlayers[1] = Players[IsTag ? 3 : 1];

            State = GameState.Duel;
            int seed = Environment.TickCount;

            _duel = Duel.Create((uint)seed);
            Random rand = new Random(seed);

            _duel.SetAnalyzer(_analyser.Analyse);
            _duel.SetErrorHandler(HandleError);

            _duel.InitPlayers(Config.StartLp, Config.StartHand, Config.DrawCount);

            int opt = 0;

            if (Config.EnablePriority)
            {
                opt += 0x08;
            }
            if (Config.NoShuffleDeck)
            {
                opt += 0x10;
            }
            if (IsTag)
            {
                opt += 0x20;
            }

            Replay = new Replay((uint)seed, IsTag);
            Replay.Writer.WriteUnicode(Players[0].Name, 20);
            Replay.Writer.WriteUnicode(Players[1].Name, 20);
            if (IsTag)
            {
                Replay.Writer.WriteUnicode(Players[2].Name, 20);
                Replay.Writer.WriteUnicode(Players[3].Name, 20);
            }
            Replay.Writer.Write(Config.StartLp);
            Replay.Writer.Write(Config.StartHand);
            Replay.Writer.Write(Config.DrawCount);
            Replay.Writer.Write(opt);

            for (int i = 0; i < Players.Length; i++)
            {
                Player dplayer = Players[i == 2 ? 3 : (i == 3 ? 2 : i)];
                int    pid     = i;
                if (IsTag)
                {
                    pid = i >= 2 ? 1 : 0;
                }
                if (!Config.NoShuffleDeck)
                {
                    List <int> cards = ShuffleCards(rand, dplayer.Deck.Main);
                    Replay.Writer.Write(cards.Count);
                    foreach (int id in cards)
                    {
                        if (IsTag && (i == 1 || i == 3))
                        {
                            _duel.AddTagCard(id, pid, CardLocation.Deck);
                        }
                        else
                        {
                            _duel.AddCard(id, pid, CardLocation.Deck);
                        }
                        Replay.Writer.Write(id);
                    }
                }
                else
                {
                    Replay.Writer.Write(dplayer.Deck.Main.Count);
                    for (int j = dplayer.Deck.Main.Count - 1; j >= 0; j--)
                    {
                        int id = dplayer.Deck.Main[j];
                        if (IsTag && (i == 1 || i == 3))
                        {
                            _duel.AddTagCard(id, pid, CardLocation.Deck);
                        }
                        else
                        {
                            _duel.AddCard(id, pid, CardLocation.Deck);
                        }
                        Replay.Writer.Write(id);
                    }
                }
                Replay.Writer.Write(dplayer.Deck.Extra.Count);
                foreach (int id in dplayer.Deck.Extra)
                {
                    if (IsTag && (i == 1 || i == 3))
                    {
                        _duel.AddTagCard(id, pid, CardLocation.Extra);
                    }
                    else
                    {
                        _duel.AddCard(id, pid, CardLocation.Extra);
                    }
                    Replay.Writer.Write(id);
                }
            }

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);

            packet.Write((byte)0);
            packet.Write(Config.StartLp);
            packet.Write(Config.StartLp);
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Deck));
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Deck));
            packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Extra));
            SendToTeam(packet, 0);

            packet.SetPosition(2);
            packet.Write((byte)1);
            SendToTeam(packet, 1);

            packet.SetPosition(2);
            if (_swapped)
            {
                packet.Write((byte)0x11);
            }
            else
            {
                packet.Write((byte)0x10);
            }
            SendToObservers(packet);

            RefreshExtra(0);
            RefreshExtra(1);

            _duel.Start(opt);

            TurnCount     = 0;
            LifePoints[0] = Config.StartLp;
            LifePoints[1] = Config.StartLp;
            TimeReset();

            Process();
        }
Example #32
0
        private void OnDraw(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);
            int player = msg.Reader.ReadByte();
            int count = msg.Reader.ReadByte();
            packet.Write((byte)player);
            packet.Write((byte)count);

            for (int i = 0; i < count; i++)
            {
                uint code = msg.Reader.ReadUInt32();
                if ((code & 0x80000000) != 0)
                    packet.Write(code);
                else
                    packet.Write(0);
            }

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);
        }
Example #33
0
        private void OnMove(CoreMessage msg)
        {
            byte[] raw = msg.Reader.ReadBytes(16);
            int pc = raw[4];
            int pl = raw[5];
            int cc = raw[8];
            int cl = raw[9];
            int cs = raw[10];
            int cp = raw[11];

            SendToPlayer(msg, cc);
            GameServerPacket packet = new GameServerPacket(msg.Message);
            packet.Write(raw);
            if (!Convert.ToBoolean((cl & ((int)CardLocation.Grave + (int)CardLocation.Overlay))) && Convert.ToBoolean((cl & ((int)CardLocation.Deck + (int)CardLocation.Hand)))
                || Convert.ToBoolean((cp & (int)CardPosition.FaceDown)))
            {
                packet.SetPosition(2);
                packet.Write(0);
            }
            Game.SendToAllBut(packet, cc);

            if (cl != 0 && (cl & 0x80) == 0 && (cl != pl || pc != cc))
                Game.RefreshSingle(cc, cl, cs);
        }
Example #34
0
        private void OnConfirmCards(CoreMessage msg)
        {
            int player = msg.Reader.ReadByte();
            int count = msg.Reader.ReadByte();
            msg.Reader.ReadBytes(count * 7);

            byte[] buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);
            packet.Write(buffer);
            if ((CardLocation)buffer[7] == CardLocation.Hand)
                Game.SendToAll(packet);
            else
                Game.CurPlayers[player].Send(packet);
        }
Example #35
0
        private void OnHint(CoreMessage msg)
        {
            int type = msg.Reader.ReadByte();
            int player = msg.Reader.ReadByte();
            msg.Reader.ReadInt32();

            byte[] buffer = msg.CreateBuffer();
            GameServerPacket packet = new GameServerPacket(msg.Message);
            packet.Write(buffer);

            switch (type)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    Game.CurPlayers[player].Send(packet);
                    break;
                case 6:
                case 7:
                case 8:
                case 9:
                    Game.SendToAllBut(packet, player);
                    break;
                case 10:
                    if (Game.IsTag)
                        Game.CurPlayers[player].Send(packet);
                    else
                        Game.SendToAll(packet);
                    break;
            }
        }
Example #36
0
 public void SendTypeChange() {
     GameServerPacket packet = new GameServerPacket(StocMessage.TypeChange);
     packet.Write((byte)(Type + (Game.HostPlayer.Equals(this) ? (int)PlayerType.Host : 0)));
     Send(packet);
 }
Example #37
0
        private void InitNewSpectator(Player player) {
            int deck1 = _duel.QueryFieldCount(0, CardLocation.Deck);
            int deck2 = _duel.QueryFieldCount(1, CardLocation.Deck);

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

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);
            packet.Write((byte)(_swapped ? 0x11 : 0x10));
            packet.Write(LifePoints[0]);
            packet.Write(LifePoints[1]);
            packet.Write((short)(deck1 + hand1));
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)(deck2 + hand2));
            packet.Write((short)_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);
        }
Example #38
0
        public void Surrender(Player player, int reason, bool force = false) {
            if (!force)
                if (State != GameState.Duel)
                    return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            GameServerPacket win = new GameServerPacket(GameMessage.Win);
            int team = player.Type;
            if (IsTag)
                team = player.Type >= 2 ? 1 : 0;
            else if (State == GameState.Hand)
                team = 1 - team;
            win.Write((byte)(1 - team));
            win.Write((byte)reason);
            SendToAll(win);

            MatchSaveResult(1 - team);

            EndDuel(reason == 4);
        }
Example #39
0
        public void TpResult(Player player, bool result) {
            if (State != GameState.Starting)
                return;
            if (player.Type != _startplayer)
                return;

            _swapped = false;
            if (result && player.Type == (IsTag ? 2 : 1) || !result && player.Type == 0) {
                _swapped = true;
                if (IsTag) {
                    Player temp = Players[0];
                    Players[0] = Players[2];
                    Players[2] = temp;

                    temp = Players[1];
                    Players[1] = Players[3];
                    Players[3] = temp;

                    Players[0].Type = 0;
                    Players[1].Type = 1;
                    Players[2].Type = 2;
                    Players[3].Type = 3;
                } else {
                    Player temp = Players[0];
                    Players[0] = Players[1];
                    Players[1] = temp;
                    Players[0].Type = 0;
                    Players[1].Type = 1;
                }
            }
            CurPlayers[0] = Players[0];
            CurPlayers[1] = Players[IsTag ? 3 : 1];

            State = GameState.Duel;
            int seed = Environment.TickCount;
            _duel = Duel.Create((uint)seed);
            Random rand = new Random(seed);

            _duel.SetAnalyzer(_analyser.Analyse);
            _duel.SetErrorHandler(HandleError);

            _duel.InitPlayers(Config.StartLp, Config.StartHand, Config.DrawCount);

            int opt = 0;
            if (Config.EnablePriority)
                opt += 0x08;
            if (Config.NoShuffleDeck)
                opt += 0x10;
            if (IsTag)
                opt += 0x20;

            Replay = new Replay((uint)seed, IsTag);
            Replay.Writer.WriteUnicode(Players[0].Name, 20);
            Replay.Writer.WriteUnicode(Players[1].Name, 20);
            if (IsTag) {
                Replay.Writer.WriteUnicode(Players[2].Name, 20);
                Replay.Writer.WriteUnicode(Players[3].Name, 20);
            }
            Replay.Writer.Write(Config.StartLp);
            Replay.Writer.Write(Config.StartHand);
            Replay.Writer.Write(Config.DrawCount);
            Replay.Writer.Write(opt);

            for (int i = 0; i < Players.Length; i++) {
                Player dplayer = Players[i == 2 ? 3 : (i == 3 ? 2 : i)];
                int pid = i;
                if (IsTag)
                    pid = i >= 2 ? 1 : 0;
                if (!Config.NoShuffleDeck) {
                    List<int> cards = ShuffleCards(rand, dplayer.Deck.Main);
                    Replay.Writer.Write(cards.Count);
                    foreach (int id in cards) {
                        if (IsTag && (i == 1 || i == 3))
                            _duel.AddTagCard(id, pid, CardLocation.Deck);
                        else
                            _duel.AddCard(id, pid, CardLocation.Deck);
                        Replay.Writer.Write(id);
                    }
                } else {
                    Replay.Writer.Write(dplayer.Deck.Main.Count);
                    for (int j = dplayer.Deck.Main.Count - 1; j >= 0; j--) {
                        int id = dplayer.Deck.Main[j];
                        if (IsTag && (i == 1 || i == 3))
                            _duel.AddTagCard(id, pid, CardLocation.Deck);
                        else
                            _duel.AddCard(id, pid, CardLocation.Deck);
                        Replay.Writer.Write(id);
                    }
                }
                Replay.Writer.Write(dplayer.Deck.Extra.Count);
                foreach (int id in dplayer.Deck.Extra) {
                    if (IsTag && (i == 1 || i == 3))
                        _duel.AddTagCard(id, pid, CardLocation.Extra);
                    else
                        _duel.AddCard(id, pid, CardLocation.Extra);
                    Replay.Writer.Write(id);
                }
            }

            GameServerPacket packet = new GameServerPacket(GameMessage.Start);
            packet.Write((byte)0);
            packet.Write(Config.StartLp);
            packet.Write(Config.StartLp);
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Deck));
            packet.Write((short)_duel.QueryFieldCount(0, CardLocation.Extra));
            packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Deck));
            packet.Write((short)_duel.QueryFieldCount(1, CardLocation.Extra));
            SendToTeam(packet, 0);

            packet.SetPosition(2);
            packet.Write((byte)1);
            SendToTeam(packet, 1);

            packet.SetPosition(2);
            if (_swapped)
                packet.Write((byte)0x11);
            else
                packet.Write((byte)0x10);
            SendToObservers(packet);

            RefreshExtra(0);
            RefreshExtra(1);

            _duel.Start(opt);

            TurnCount = 0;
            LifePoints[0] = Config.StartLp;
            LifePoints[1] = Config.StartLp;
            TimeReset();

            Process();
        }
Example #40
0
        public void HandResult(Player player, int result) {
            if (State != GameState.Hand)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (result < 1 || result > 3)
                return;
            if (IsTag && player.Type != 0 && player.Type != 2)
                return;
            int type = player.Type;
            if (IsTag && player.Type == 2)
                type = 1;
            if (_handResult[type] != 0)
                return;
            _handResult[type] = result;
            if (_handResult[0] != 0 && _handResult[1] != 0) {
                GameServerPacket packet = new GameServerPacket(StocMessage.HandResult);
                packet.Write((byte)_handResult[0]);
                packet.Write((byte)_handResult[1]);
                SendToTeam(packet, 0);
                SendToObservers(packet);

                packet = new GameServerPacket(StocMessage.HandResult);
                packet.Write((byte)_handResult[1]);
                packet.Write((byte)_handResult[0]);
                SendToTeam(packet, 1);

                if (_handResult[0] == _handResult[1]) {
                    _handResult[0] = 0;
                    _handResult[1] = 0;
                    SendHand();
                    return;
                }
                if ((_handResult[0] == 1 && _handResult[1] == 2) ||
                    (_handResult[0] == 2 && _handResult[1] == 3) ||
                    (_handResult[0] == 3 && _handResult[1] == 1))
                    _startplayer = IsTag ? 2 : 1;
                else
                    _startplayer = 0;
                State = GameState.Starting;
                Players[_startplayer].Send(new GameServerPacket(StocMessage.SelectTp));
                TpTimer = DateTime.UtcNow;
            }
        }
Example #41
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 (player.Deck != null) {
                    result = Config.NoCheckDeck ? 0 : 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)));
            SendToAll(change);
        }
Example #42
0
 public void CustomMessage(Player player, string msg) {
     string finalmsg = msg;
     GameServerPacket packet = new GameServerPacket(StocMessage.Chat);
     packet.Write((short)PlayerType.Yellow);
     packet.Write(finalmsg, finalmsg.Length + 1);
     SendToAllBut(packet, player);
 }
Example #43
0
 public void Chat(Player player, string msg) {
     GameServerPacket packet = new GameServerPacket(StocMessage.Chat);
     packet.Write((short)player.Type);
     if (player.Type == (int)PlayerType.Observer) {
         msg = "[" + player.Name + "]: " + msg;
         CustomMessage(player, msg);
     } else {
         packet.Write(msg, msg.Length + 1);
         SendToAllBut(packet, player);
     }
 }
Example #44
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);
        }
Example #45
0
 private void SendDuelingPlayers(Player player) {
     for (int i = 0; i < Players.Length; i++) {
         GameServerPacket enter = new GameServerPacket(StocMessage.HsPlayerEnter);
         int id = i;
         if (_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);
         //padding
         enter.Write((byte)0);
         player.Send(enter);
     }
 }
Example #46
0
        public void RefreshSpells(int player, int flag = 0x681fff, bool useCache = true) {
            byte[] result = _duel.QueryFieldCard(player, CardLocation.SpellZone, flag, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.SpellZone);
            update.Write(result);
            SendToTeam(update, player);

            update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.SpellZone);

            MemoryStream ms = new MemoryStream(result);
            BinaryReader reader = new BinaryReader(ms);
            BinaryWriter writer = new BinaryWriter(ms);
            for (int i = 0; i < 8; i++) {
                int len = reader.ReadInt32();
                if (len == 4)
                    continue;
                long pos = ms.Position;
                byte[] raw = reader.ReadBytes(len - 4);
                if ((raw[11] & (int)CardPosition.FaceDown) != 0) {
                    ms.Position = pos;
                    writer.Write(new byte[len - 4]);
                }
            }
            update.Write(result);

            SendToTeam(update, 1 - player);
            SendToObservers(update);
        }
Example #47
0
        private void OnUpdateDeck(GameClientPacket packet) {
            if (Type == (int)PlayerType.Observer)
                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 (Game.State == GameState.Lobby) {
                Deck = deck;
                Game.IsReady[Type] = false;
            } else if (Game.State == GameState.Side) {
                if (Game.IsReady[Type])
                    return;
                if (!Deck.Check(deck)) {
                    GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg);
                    error.Write((byte)3);
                    error.Write(0);
                    Send(error);
                    return;
                }
                Deck = deck;
                Game.IsReady[Type] = true;
                Send(new GameServerPacket(StocMessage.DuelStart));
                Game.MatchSide();
            }
        }
Example #48
0
        public void RefreshHand(int player, int flag = 0x181fff, bool useCache = true) {
            byte[] result = _duel.QueryFieldCard(player, CardLocation.Hand, flag | 0x100000, useCache);
            GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.Hand);
            update.Write(result);
            CurPlayers[player].Send(update);

            update = new GameServerPacket(GameMessage.UpdateData);
            update.Write((byte)player);
            update.Write((byte)CardLocation.Hand);

            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);
                if (raw[len - 8] == 0) {
                    ms.Position = pos;
                    writer.Write(new byte[len - 4]);
                }
            }
            update.Write(result);

            SendToAllBut(update, player);
        }
Example #49
0
 public void RefreshGrave(int player, int flag = 0x81fff, bool useCache = true) {
     byte[] result = _duel.QueryFieldCard(player, CardLocation.Grave, flag, useCache);
     GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
     update.Write((byte)player);
     update.Write((byte)CardLocation.Grave);
     update.Write(result);
     SendToAll(update);
 }
Example #50
0
 public void RefreshExtra(int player, int flag = 0x81fff, bool useCache = true) {
     byte[] result = _duel.QueryFieldCard(player, CardLocation.Extra, flag, useCache);
     GameServerPacket update = new GameServerPacket(GameMessage.UpdateData);
     update.Write((byte)player);
     update.Write((byte)CardLocation.Extra);
     update.Write(result);
     CurPlayers[player].Send(update);
 }
Example #51
0
        private void OnSelectCard(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);

            int player = msg.Reader.ReadByte();
            packet.Write((byte)player);
            packet.Write(msg.Reader.ReadBytes(3));

            int count = msg.Reader.ReadByte();
            packet.Write((byte)count);

            for (int i = 0; i < count; i++)
            {
                int code = msg.Reader.ReadInt32();
                int pl = msg.Reader.ReadByte();
                int loc = msg.Reader.ReadByte();
                int seq = msg.Reader.ReadByte();
                int pos = msg.Reader.ReadByte();
                packet.Write(pl == player ? code : 0);
                packet.Write((byte)pl);
                packet.Write((byte)loc);
                packet.Write((byte)seq);
                packet.Write((byte)pos);
            }

            Game.WaitForResponse(player);
            Game.CurPlayers[player].Send(packet);
        }
Example #52
0
        public void RefreshSingle(int player, int location, int sequence, int flag = 0x781fff) {
            byte[] result = _duel.QueryCard(player, location, sequence, flag);

            if (location == (int)CardLocation.Removed && (result[15] & (int)CardPosition.FaceDown) != 0)
                return;

            GameServerPacket update = new GameServerPacket(GameMessage.UpdateCard);
            update.Write((byte)player);
            update.Write((byte)location);
            update.Write((byte)sequence);
            update.Write(result);
            CurPlayers[player].Send(update);

            if (IsTag) {
                if ((location & (int)CardLocation.Onfield) != 0) {
                    SendToTeam(update, player);
                    if ((result[15] & (int)CardPosition.FaceUp) != 0)
                        SendToTeam(update, 1 - player);
                } else {
                    CurPlayers[player].Send(update);
                    if ((location & 0x90) != 0)
                        SendToAllBut(update, player);
                }
            } else {
                if ((location & 0x90) != 0 || ((location & 0x2c) != 0 && (result[15] & (int)CardPosition.FaceUp) != 0))
                    SendToAllBut(update, player);
            }
        }
Example #53
0
        private void OnShuffleHand(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(msg.Message);
            int player = msg.Reader.ReadByte();
            int count = msg.Reader.ReadByte();
            packet.Write((byte)player);
            packet.Write((byte)count);

            msg.Reader.ReadBytes(count * 4);
            for (int i = 0; i < count; i++)
                packet.Write(0);

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);
            Game.RefreshHand(player, 0x181fff, false);
        }
Example #54
0
 public void WaitForResponse(int player) {
     _lastresponse = player;
     CurPlayers[player].State = PlayerState.Response;
     SendToAllBut(new GameServerPacket(GameMessage.Waiting), player);
     TimeStart();
     GameServerPacket packet = new GameServerPacket(StocMessage.TimeLimit);
     packet.Write((byte)player);
     packet.Write((byte)0); // C++ padding
     packet.Write((short)_timelimit[player]);
     SendToPlayers(packet);
 }
Example #55
0
 private void OnSet(CoreMessage msg)
 {
     msg.Reader.ReadBytes(4);
     byte[] raw = msg.Reader.ReadBytes(4);
     GameServerPacket packet = new GameServerPacket(GameMessage.Set);
     packet.Write(0);
     packet.Write(raw);
     Game.SendToAll(packet);
 }
Example #56
0
        public void EndDuel(bool force) {
            if (State == GameState.Duel) {
                if (!Replay.Disabled) {
                    Replay.End();
                    byte[] replayData = Replay.GetContent();
                    GameServerPacket packet = new GameServerPacket(StocMessage.Replay);
                    packet.Write(replayData);
                    SendToAll(packet);
                }

                State = GameState.End;
                _duel.End();
            }

            if (_swapped) {
                _swapped = false;
                if (IsTag) {
                    Player temp = Players[0];
                    Players[0] = Players[2];
                    Players[2] = temp;

                    temp = Players[1];
                    Players[1] = Players[3];
                    Players[3] = temp;

                    Players[0].Type = 0;
                    Players[1].Type = 1;
                    Players[2].Type = 2;
                    Players[3].Type = 3;
                } else {
                    Player temp = Players[0];
                    Players[0] = Players[1];
                    Players[1] = temp;
                    Players[0].Type = 0;
                    Players[1].Type = 1;
                }
            }

            if (IsMatch && !force && !MatchIsEnd()) {
                IsReady[0] = false;
                IsReady[1] = false;
                State = GameState.Side;
                SideTimer = DateTime.UtcNow;
                SendToPlayers(new GameServerPacket(StocMessage.ChangeSide));
                SendToObservers(new GameServerPacket(StocMessage.WaitingSide));
            } else {
                if (State == GameState.Hand) {
                    State = GameState.End;
                    End();
                } else {
                    ApplyRanking();
                    End();
                }
            }
        }
Example #57
0
        private void OnTagSwap(CoreMessage msg)
        {
            GameServerPacket packet = new GameServerPacket(GameMessage.TagSwap);

            int player = msg.Reader.ReadByte();
            packet.Write((byte)player);
            packet.Write(msg.Reader.ReadBytes(2));
            int count = msg.Reader.ReadByte();
            packet.Write((byte)count);
            packet.Write(msg.Reader.ReadBytes(4));

            for (int i = 0; i < count; i++)
            {
                uint code = msg.Reader.ReadUInt32();
                if ((code & 0x80000000) != 0)
                    packet.Write(code);
                else
                    packet.Write(0);
            }

            SendToPlayer(msg, player);
            Game.SendToAllBut(packet, player);

            Game.RefreshExtra(player);
            Game.RefreshMonsters(0, 0x81fff, false);
            Game.RefreshMonsters(1, 0x81fff, false);
            Game.RefreshSpells(0, 0x681fff, false);
            Game.RefreshSpells(1, 0x681fff, false);
            Game.RefreshHand(0, 0x181fff, false);
            Game.RefreshHand(1, 0x181fff, false);
        }
Example #58
0
 private void SendToAll(CoreMessage msg)
 {
     byte[] buffer = msg.CreateBuffer();
     GameServerPacket packet = new GameServerPacket(msg.Message);
     packet.Write(buffer);
     Game.SendToAll(packet);
 }
Example #59
0
 private void SendToPlayer(CoreMessage msg, int player)
 {
     if (player != 0 && player != 1)
         return;
     byte[] buffer = msg.CreateBuffer();
     GameServerPacket packet = new GameServerPacket(msg.Message);
     packet.Write(buffer);
     Game.CurPlayers[player].Send(packet);
 }
Example #60
0
        public void MoveToObserver(Player player) {
            if (State != GameState.Lobby)
                return;
            if (player.Type == (int)PlayerType.Observer)
                return;
            if (IsReady[player.Type])
                return;
            Players[player.Type] = null;
            IsReady[player.Type] = false;
            Observers.Add(player);

            GameServerPacket change = new GameServerPacket(StocMessage.HsPlayerChange);
            change.Write((byte)((player.Type << 4) + (int)PlayerChange.Observe));
            SendToAll(change);

            player.Type = (int)PlayerType.Observer;
            player.SendTypeChange();
        }