public static void OnTimeConfirm(GameSession client, GameClientPacket packet)
 {
     if (client != null)
     {
         //Logger.Debug("OnTimeConfirm "+client.Name);
     }
 }
 public static void OnStartDuel(GameSession client, GameClientPacket packet)
 {
     if (client.Game != null)
     {
         client.Game.StartDuel(client);
     }
 }
 public static void OnSurrender(GameSession client, GameClientPacket packet)
 {
     if (client.Game != null)
     {
         client.Game.Surrender(client, 0);
     }
 }
 public static void OnClose(GameSession client, GameClientPacket packet)
 {
     using (PacketWriter packet_write = new PacketWriter(GameServerPacket.GamePacketByteLength)) {
         packet_write.Write((byte)RoomMessage.STOP_CLIENT);
         client.Send(packet_write.Content);
     }
     client.CloseAsync();
 }
        public static void OnKick(GameSession client, GameClientPacket packet)
        {
            int pos = packet.ReadByte();

            if (client.Game != null)
            {
                client.Game.KickPlayer(client, pos);
            }
        }
        public static void OnTpResult(GameSession client, GameClientPacket packet)
        {
            bool tp = packet.ReadByte() != 0;

            if (client.Game != null)
            {
                client.Game.TpResult(client, tp);
            }
        }
        public static void OnHandResult(GameSession client, GameClientPacket packet)
        {
            int res = packet.ReadByte();

            if (client.Game != null)
            {
                client.Game.HandResult(client, res);
            }
        }
 public static void OnNotReady(GameSession client, GameClientPacket packet)
 {
     if (!client.IsAuthentified)
     {
         return;
     }
     if (client.Game != null)
     {
         client.Game.SetReady(client, false);
     }
 }
 public static void OnMoveToObserver(GameSession client, GameClientPacket packet)
 {
     if (!client.IsAuthentified)
     {
         return;
     }
     if (client.Game != null)
     {
         client.Game.MoveToObserver(client);
     }
 }
 public bool OnCheck()
 {
     byte[] data;
     if (m_client.GetPacketData(GameServerPacket.GamePacketByteLength, out data))
     {
         GameClientPacket packet = new GameClientPacket(data);
         if (GameEvent.Handler(this, packet) == CtosMessage.PlayerInfo)
         {
             return(true);
         }
     }
     Logger.Debug("first msg isn't PlayerInfo");
     CloseAsync();
     return(false);
 }
        public static void OnUpdateDeck(GameSession client, GameClientPacket packet)
        {
            if (client.Game == null || client.Type == (int)PlayerType.Observer || client.Type == (int)PlayerType.Undefined)
            {
                return;
            }
            Deck deck = new Deck();
            int  main = packet.ReadInt32();
            int  side = packet.ReadInt32();

            for (int i = 0; i < main; i++)
            {
                deck.AddMain(packet.ReadInt32());
            }
            for (int i = 0; i < side; i++)
            {
                deck.AddSide(packet.ReadInt32());
            }
            if (client.Game.State == GameState.Lobby)
            {
                client.Deck = deck;
                client.Game.IsReady[client.Type] = false;
            }
            else if (client.Game.State == GameState.Side)
            {
                if (client.Game.IsReady[client.Type])
                {
                    return;
                }
                if (!client.Deck.Check(deck))
                {
                    using (GameServerPacket error = new GameServerPacket(StocMessage.ErrorMsg)){
                        error.Write((byte)3);
                        error.Write(0);
                        client.Send(error);
                    }
                    return;
                }
                client.Deck = deck;
                client.Game.IsReady[client.Type] = true;
                client.Game.ServerMessage(string.Format(Messages.MSG_READY, client.Name));
                client.Send(GameServerPacket.EmtryMessage(StocMessage.DuelStart));
                client.Game.MatchSide();
            }
        }
 public static void OnResponse(GameSession client, GameClientPacket packet)
 {
     if (client.Game == null || client.Game.State != GameState.Duel)
     {
         return;
     }
     if (client.State != PlayerState.Response)
     {
         return;
     }
     byte[] resp = packet.ReadToEnd();
     if (resp.Length > 64)
     {
         return;
     }
     client.State = PlayerState.None;
     client.Game.SetResponse(resp);
 }
        public static void OnCreateGame(GameSession client, GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(client.Name) || client.Type != (int)PlayerType.Undefined)
            {
                return;
            }
            GameRoom room = null;

            byte[]         data = packet.ReadBytes(StructTransformer.SizeOf(typeof(CtosCreateGame)));
            CtosCreateGame roomconfig;
            GameConfig     config = null;

            try
            {
                roomconfig = (CtosCreateGame)StructTransformer.BytesToStruct(data, typeof(CtosCreateGame));
                config     = roomconfig.Build();
            }
            catch (Exception e)
            {
                Logger.Warn(e);
            }
            if (config == null)
            {
                client.CloseAsync();
                return;
            }
            room = RoomManager.CreateOrGetGame(config);

            if (room == null)
            {
                client.LobbyError(Messages.MSG_FULL);
                return;
            }
            client.Game = room;
            lock (room.AsyncRoot)
            {
                room.AddPlayer(client);
            }
            //IsAuthentified = CheckAuth();
            if (!client.IsAuthentified)
            {
                client.LobbyError(Messages.ERR_AUTH_FAIL);
            }
        }
        public static void OnChat(GameSession client, GameClientPacket packet)
        {
            if (!client.IsAuthentified)
            {
                return;
            }
            string msg = packet.ReadUnicode(256);

            if (client.Game == null)
            {
                return;
            }
            if (!client.OnChatCommand(msg))
            {
                using (GameServerPacket chat = new GameServerPacket(StocMessage.Chat)){
                    chat.Write((short)client.Type);
                    chat.WriteUnicode(msg, msg.Length + 1);
                    client.Game.SendToAllBut(chat, client);
                }
            }
        }
        public void OnReceive(object obj)
        {
            if (m_close)
            {
                return;
            }
            //Threading
            bool next = true;

            while (next)
            {
                byte[] data;
                next = m_client.GetPacketData(GameServerPacket.GamePacketByteLength, out data);
                if (data != null && data.Length > 0)
                {
                    //Handle game events
                    GameClientPacket packet = new GameClientPacket(data);
                    GameEvent.Handler(this, packet);
                }
            }
        }
        public static void OnPlayerInfo(GameSession client, GameClientPacket packet)
        {
            if (client.Name != null)
            {
                return;
            }
            string name = packet.ReadUnicode(20);

            Logger.Debug("player name:" + name);
            if (name == "client")
            {
                client.LobbyError("[err]404");
                return;
            }
            if (string.IsNullOrEmpty(name))
            {
                client.LobbyError(Messages.ERR_NO_NAME);
            }
            client.Name           = name;
            client.IsAuthentified = client.CheckAuth(name);
        }
        public static void OnJoinGame(GameSession client, GameClientPacket packet)
        {
            if (string.IsNullOrEmpty(client.Name) || client.Type != (int)PlayerType.Undefined)
            {
                Logger.Debug("join room fail:" + client.Name);
                return;
            }
            int version = packet.ReadInt16();

            if (version < Program.Config.ClientVersion)
            {
                client.LobbyError(Messages.ERR_LOW_VERSION);
                return;
            }
            else if (version > Program.Config.ClientVersion)
            {
                client.ServerMessage(Messages.MSG_HIGH_VERSION);
            }
            int gameid = packet.ReadInt32();            //gameid

            packet.ReadInt16();

            string joinCommand = packet.ReadUnicode(60);

            GameRoom room = null;

            //IsAuthentified = CheckAuth();
            if (!client.IsAuthentified)
            {
                client.LobbyError(Messages.ERR_AUTH_FAIL);
                return;
            }
            if (!RoomManager.CheckRoomPassword(joinCommand))
            {
                client.LobbyError(Messages.ERR_PASSWORD);
                return;
            }
            GameConfig config = GameConfigBuilder.Build(joinCommand);

            room = RoomManager.CreateOrGetGame(config);
            if (room == null)
            {
                client.LobbyError(Messages.MSG_FULL);
                return;
            }
            if (!room.IsOpen)
            {
                client.LobbyError(Messages.MSG_GAMEOVER);
                return;
            }
            if (room != null && room.Config != null)
            {
                if (room.Config.NoCheckDeck)
                {
                    client.ServerMessage(Messages.MSG_NOCHECKDECK);
                }
                if (room.Config.NoShuffleDeck)
                {
                    client.ServerMessage(Messages.MSG_NOSHUFFLEDECK);
                }
                if (room.Config.EnablePriority)
                {
                    client.ServerMessage(Messages.MSG_ENABLE_PROIORITY);
                }
            }
            client.Game = room;
            lock (room.AsyncRoot)
            {
                room.AddPlayer(client);
            }
        }
 public static void OnLeaveGame(GameSession client, GameClientPacket packet)
 {
     client.CloseAsync();
 }