コード例 #1
0
        public static MyData Unpack(byte[] bytes)
        {
            using (MemoryStream raw = new MemoryStream(bytes))
            {
                GZipInputStream zipFile = new GZipInputStream(raw);
                using (MemoryStream re = new MemoryStream(1000))
                {
                    int    count;
                    byte[] data = new byte[1000];
                    while ((count = zipFile.Read(data, 0, data.Length)) != 0)
                    {
                        re.Write(data, 0, count);
                    }
                    byte[] overarr = re.ToArray();
                    re.Close();
                    zipFile.Close();
                    raw.Close();

                    //将byte数组转为string
                    string result = Encoding.UTF8.GetString(overarr);
                    return(JsonUntity.Json2Object <MyData>(result));
                }
            }

            /*
             * string str = Encoding.UTF8.GetString(bytes);
             * return JsonUntity.Json2Object<MyData>(str);
             */
        }
コード例 #2
0
        private void RoomSwitch(Client client, MyData data)
        {
            switch (data.Protocol)
            {
            case protocol.CreateRoom:
                CreateRoom(client, JsonUntity.Json2Object <GameSetting>(data.Body[0]));
                break;

            case protocol.JoinRoom:
                int    room_id = -1;
                string pass    = string.Empty;
                if (data.Body.Count > 0)
                {
                    room_id = int.Parse(data.Body[0]);
                    pass    = data.Body[1].ToString();
                }
                else
                {
                    room_id = client.GameRoom;
                }

                Room room   = RId2Room.ContainsKey(room_id) ? RId2Room[room_id] : null;
                bool result = false;
                if (room != null)
                {
                    //if (JoinRoom != null)
                    //{
                    //    Delegate[] delArray = JoinRoom.GetInvocationList();
                    //    foreach (Delegate del in delArray)
                    //    {
                    //        Room target = (Room)del.Target;
                    //        if (target != null && target.RoomId == room_id)
                    //        {
                    //            JoinRoomDelegate method = (JoinRoomDelegate)del;
                    //            result = method(client, room_id, pass);
                    //        }
                    //    }
                    //}
                    //JoinRoom?.Invoke(client, room_id, pass);
                    result = room.OnClientRequestInter(client, room_id, pass);
                }
                if (!result)
                {
                    Debug(string.Format("{0} join request fail at hall", client.UserName));
                    data = new MyData
                    {
                        Description = PacketDescription.Hall2Cient,
                        Protocol    = protocol.JoinRoom,
                    };
                    client.SendSwitchReply(data);
                }
                else
                {
                    ClientList.Instance().AddClient(client.UserID, client.Profile.NickName, room_id);
                    UpdateUsers(ClientList.Instance().GetUserList(0));
                }
                break;

            case protocol.LeaveRoom:
                if (RId2Room.ContainsKey(client.GameRoom))
                {
                    client.RequestLeaveRoom();

                    //更新form
                    ClientList.Instance().AddClient(client.UserID, client.Profile.NickName);
                    UpdateUsers(ClientList.Instance().GetUserList(0));
                }
                break;

            case protocol.UpdateRoom:
                client.RequstReady(bool.Parse(data.Body[0]));
                break;

            case protocol.RoleReserved:
                if (client.UserRight >= 2 && RId2Room.ContainsKey(client.GameRoom))
                {
                    room = RId2Room[client.GameRoom];
                    if (!room.GameStarted && room.Setting.GameMode == "Classic")
                    {
                        client.RoleReserved = data.Body[0];
                    }
                }
                break;

            case protocol.KickOff:
                room = RId2Room.ContainsKey(client.GameRoom) ? RId2Room[client.GameRoom] : null;
                if (room != null && room.Host == client && !room.GameStarted)
                {
                    int    victim_id = int.Parse(data.Body[0]);
                    Client victim    = GetClient(victim_id);
                    if (victim != null || victim_id < 0)
                    {
                        victim.RequestLeaveRoom(true);

                        //更新form
                        ClientList.Instance().AddClient(client.UserID, client.Profile.NickName);
                        UpdateUsers(ClientList.Instance().GetUserList(0));
                    }
                }
                break;

            case protocol.ConfigChange:
                room = RId2Room.ContainsKey(client.GameRoom) ? RId2Room[client.GameRoom] : null;
                if (room != null && room.Host == client && !room.GameStarted)
                {
                    room.ChangeSetting(JsonUntity.Json2Object <GameSetting>(data.Body[0]));
                }
                break;

            default:
                break;
            }
        }
コード例 #3
0
        private void RoomSwitch(Client client, MyData data)
        {
            try
            {
                Room room = null;
                switch (data.Protocol)
                {
                case Protocol.CreateRoom:
                    CreateRoom(client, JsonUntity.Json2Object <GameSetting>(data.Body[0]));
                    break;

                case Protocol.JoinRoom:
                    int    room_id = -1;
                    string pass    = string.Empty;
                    if (data.Body.Count > 0)
                    {
                        room_id = int.Parse(data.Body[0]);
                        pass    = data.Body[1].ToString();
                    }
                    else
                    {
                        room_id = client.GameRoom;
                    }

                    bool result = false;
                    if (RId2Room.TryGetValue(room_id, out room))
                    {
                        //if (JoinRoom != null)
                        //{
                        //    Delegate[] delArray = JoinRoom.GetInvocationList();
                        //    foreach (Delegate del in delArray)
                        //    {
                        //        Room target = (Room)del.Target;
                        //        if (target != null && target.RoomId == room_id)
                        //        {
                        //            JoinRoomDelegate method = (JoinRoomDelegate)del;
                        //            result = method(client, room_id, pass);
                        //        }
                        //    }
                        //}
                        //JoinRoom?.Invoke(client, room_id, pass);
                        result = room.OnClientRequestInter(client, room_id, pass);
                    }
                    if (!result)
                    {
                        Debug(string.Format("{0} join request fail at hall", client.UserName));
                        data = new MyData
                        {
                            Description = PacketDescription.Hall2Cient,
                            Protocol    = Protocol.JoinRoom,
                        };
                        client.SendSwitchReply(data);
                    }
                    break;

                case Protocol.LeaveRoom:
                    if (RId2Room.TryGetValue(client.GameRoom, out room))
                    {
                        client.RequestLeaveRoom();
                    }
                    break;

                case Protocol.UpdateRoom:
                    client.RequstReady(bool.Parse(data.Body[0]));
                    break;

                case Protocol.RoleReserved:
                    if (client.UserRight >= 2 && RId2Room.TryGetValue(client.GameRoom, out room))
                    {
                        if (!room.GameStarted && room.Setting.GameMode == "Classic")
                        {
                            client.RoleReserved = data.Body[0];
                        }
                    }
                    break;

                case Protocol.GeneralReserved:
                    if (client.UserRight >= 2 && RId2Room.TryGetValue(client.GameRoom, out room) && !room.GameStarted &&
                        (room.Setting.GameMode == "Classic" || room.Setting.GameMode == "Hegemony") && data.Body.Count <= 2)
                    {
                        client.GeneralReserved = data.Body;
                    }
                    break;

                case Protocol.KickOff:
                    if (RId2Room.TryGetValue(client.GameRoom, out room) && room != null && room.Host == client && !room.GameStarted)
                    {
                        int victim_id = int.Parse(data.Body[0]);
                        if (!UId2ClientTable.TryGetValue(victim_id, out Client victim) || victim != null || victim_id < 0)
                        {
                            victim.RequestLeaveRoom(true);
                        }
                    }
                    break;

                case Protocol.ConfigChange:
                    if (RId2Room.TryGetValue(client.GameRoom, out room) && room != null && room.Host == client && !room.GameStarted)
                    {
                        GameSetting setting = JsonUntity.Json2Object <GameSetting>(data.Body[0]);
                        GameMode    mode    = Engine.GetMode(room.Setting.GameMode);

                        if (room.Setting.GameMode == setting.GameMode && room.Setting.PlayerNum == setting.PlayerNum && setting.GeneralPackage.Count > 0 && setting.CardPackage.Count > 0)
                        {
                            foreach (string general in setting.GeneralPackage)
                            {
                                if (!mode.GeneralPackage.Contains(general))
                                {
                                    return;
                                }
                            }

                            foreach (string card in setting.CardPackage)
                            {
                                if (!mode.CardPackage.Contains(card))
                                {
                                    return;
                                }
                            }

                            room.ChangeSetting(setting);
                        }
                    }
                    break;

                default:
                    break;
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(null, e);
                Debug(string.Format("error on switch room {0} {1}", e.Message, e.TargetSite));
            }
        }
コード例 #4
0
        public static MyData Unpack(byte[] bytes)
        {
            string str = System.Text.Encoding.UTF8.GetString(bytes);

            return(JsonUntity.Json2Object <MyData>(str));
        }