Example #1
0
        public void RemoveRoom(Room room, Client host, List <Client> clients)
        {
            try
            {
                //更新该room下用户的房间号为0
                foreach (Client client in clients)
                {
                    if (client.UserID > 0)
                    {
                        client.GameRoom = 0;
                        ClientDBOperation.UpdateGameRoom(client.UserName, 0);
                    }
                }

                //若房主存在,重建一个新的房间
                if (host != null)
                {
                    CreateRoom(host, room.Setting);
                    int id = host.GameRoom;

                    //Debug(string.Format("host {0} {1}", host.Profile.NickName, host.GameRoom));

                    if (id > 0 && id != room.RoomId)
                    {
                        Room new_room = GetRoom(id);
                        foreach (Client client in clients)
                        {
                            if (client == host || client.Status != Client.GameStatus.online)
                            {
                                continue;
                            }
                            new_room.OnClientRequestInter(client, id, room.Setting.PassWord);
                        }
                    }
                }

                if (!RId2Room.TryRemove(room.RoomId, out Room remove))
                {
                    Debug(string.Format("remove room {0} failed", room.RoomId));
                }

                if (Room2Thread.TryGetValue(room, out Thread thread))
                {
                    thread.Abort();
                    Room2Thread.TryRemove(room, out Thread _trhead);
                    thread = null;
                }
                RoomList.Instance().RemoveRoom(room.RoomId);
                int room_id = room.RoomId;
                room.Dispose();
                room = null;

                MyData data = new MyData
                {
                    Description = PacketDescription.Hall2Cient,
                    Protocol    = Protocol.UPdateRoomList,
                    Body        = new List <string>
                    {
                        room_id.ToString()
                    }
                };

                List <Client> all = new List <Client>(UId2ClientTable.Values);
                foreach (Client client in all)
                {
                    client.SendProfileReply(data);
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(null, e);
                Debug(string.Format("error at remove room {0} {1} {2}", e.Message, e.TargetSite, e.Source));
            }
        }
Example #2
0
        private void MessageForward(Client sourcer, MyData data)
        {
            try
            {
                MyData message = new MyData
                {
                    Description = PacketDescription.Hall2Cient,
                    Protocol    = data.Protocol
                };
                message.Body = data.Body;

                List <Client> clients = new List <Client>(UId2ClientTable.Values);
                switch (data.Protocol)
                {
                case Protocol.Message2Hall:
                    foreach (Client client in clients)
                    {
                        if (client.GameRoom <= 0)
                        {
                            client.SendMessage(message);
                        }
                    }
                    break;

                case Protocol.Message2Client:
                    if (UId2ClientTable.TryGetValue(int.Parse(data.Body[2]), out Client destination) && destination != null)
                    {
                        destination.SendMessage(message);
                    }
                    break;

                case Protocol.Message2Room:
                    message.Description = PacketDescription.Room2Cient;
                    Room room = GetRoom(sourcer.GameRoom);
                    if (room != null)
                    {
                        if (!room.GameStarted || !room.Setting.SpeakForbidden || (data.Body.Count == 3 && data.Body[1] != "Speech"))
                        {
                            if (!room.GameStarted)
                            {
                                foreach (Client dest in room.Clients)
                                {
                                    if (dest.GameRoom == room.RoomId)
                                    {
                                        dest.SendMessage(message);
                                    }
                                }
                            }
                            else
                            {
                                //假如双方有分冷暖阵营,则只有同阵营之间可以通讯
                                Game3v3Camp camp = room.GetPlayers(sourcer.UserID)[0].Camp;
                                foreach (Client dest in room.Clients)
                                {
                                    if (dest.GameRoom == room.RoomId && room.GetPlayers(dest.UserID)[0].Camp == camp)
                                    {
                                        dest.SendMessage(message);
                                    }
                                }
                            }
                        }
                    }
                    break;

                case Protocol.MessageSystem:
                    if (sourcer.UserRight >= 3)
                    {
                        foreach (Client client in clients)
                        {
                            client.SendMessage(message);
                        }
                    }
                    break;

                default:
                    return;
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(null, e);
                Debug(string.Format("error on message forward {0} {1}", e.Message, e.TargetSite));
            }
        }
Example #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.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));
            }
        }
Example #4
0
        public void OnRequesting(MsgPackSession session, BinaryRequestInfo requestInfo)
        {
            MyData data = null;

            if (Session2ClientTable.TryGetValue(session, out Client client))
            {
                try
                {
                    data = PacketTranslator.Unpack(requestInfo.Body);
                }
                catch (Exception e)
                {
                    LogHelper.WriteLog(null, e);
                    Debug(string.Format("error at parse client request {0} {1}", e.Message, e.TargetSite));
                    Debug(string.Format("error messsage {0} {1}", requestInfo?.ToString(), requestInfo.Body?.ToString()));
                    session.Close();
                    return;
                }
            }
            else
            {
                Debug(string.Format("found no client {0}", session.LocalEndPoint.ToString()));
                session.Close();
                return;
            }
            TransferType request = PacketTranslator.GetTransferType(requestInfo.Key);

            //OutPut(string.Format("请求协议为{0}, 内容为{1}", data.Protocol.ToString(), data.Body[0]));
            if (client.UserName == null && !request.Equals(TransferType.TypeLogin))
            {
                OutPut(string.Format("{0} 未登录客户端,关闭连接", client.IP));
                session.Close();
                return;
            }

            if (data.Description == PacketDescription.Client2Hall)
            {
                switch (request)
                {
                case TransferType.TypeLogin:                              //登录相关
                    switch (data.Protocol)
                    {
                    case Protocol.Login:
                        client.CheckUserName(data);
                        break;

                    case Protocol.Register:                               //用户注册
                        client.Register(data);
                        break;

                    case Protocol.PasswordChange:                         //密码修改
                        break;

                    default:
                        return;
                    }
                    break;

                case TransferType.TypeMessage:                            //公共事件 比如 聊天
                    MessageForward(client, data);
                    break;

                case TransferType.TypeSwitch:                               //切换room和hall
                    RoomSwitch(client, data);
                    break;

                case TransferType.TypeUserProfile:                          //修改个人信息
                    client.UpDateProfile(data);
                    break;

                default:
                    Debug(string.Format("{0} 对聊天大厅无效的请求", client.UserName));
                    break;;
                }
            }
            else if (data.Description == PacketDescription.Client2Room)
            {
                switch (request)
                {
                case TransferType.TypeMessage:                            //公共事件 比如 聊天
                    MessageForward(client, data);
                    break;

                case TransferType.TypeGameControll:                       //游戏内各操作
                {
                    if (RId2Room.TryGetValue(client.GameRoom, out Room room))
                    {
                        room.ProcessClientPacket(client, data);
                    }
                    //client.ControlGame(data);
                    break;
                }

                case TransferType.TypeSwitch:                               //切换room和hall
                    RoomSwitch(client, data);
                    break;

                default:
                    Debug(string.Format("{0} 对ROOM无效的请求", client.UserName));
                    break;;
                }
            }
            else
            {
                OutPut("无效的客户端请求");
            }
        }