Exemplo n.º 1
0
        private void CreateRoom(Client client, GameSetting setting)
        {
            lock (this)
            {
                //检查游戏设置是否正常
                GameMode mode = Engine.GetMode(setting.GameMode);
                if (!RId2Room.ContainsKey(client.GameRoom) && mode.Name == setting.GameMode && mode.CardPackage.Count > 0 && mode.GeneralPackage.Count > 0 &&
                    setting.GeneralPackage.Count > 0 && setting.CardPackage.Count > 0)
                {
                    if (!mode.PlayerNum.Contains(setting.PlayerNum))
                    {
                        setting.PlayerNum = mode.PlayerNum[0];
                    }

                    List <string> general_p = new List <string>();
                    foreach (string general in setting.GeneralPackage)
                    {
                        if (mode.GeneralPackage.Contains(general))
                        {
                            setting.GeneralPackage = mode.GeneralPackage;
                            break;
                        }
                    }
                    foreach (string card in setting.CardPackage)
                    {
                        if (!mode.GeneralPackage.Contains(card))
                        {
                            setting.CardPackage = mode.CardPackage;
                            break;
                        }
                    }

                    Debug(string.Format("创建room的hall当前线程为{0}", Thread.CurrentThread.ManagedThreadId));

                    int  room_id = ++room_serial;
                    Room room    = new Room(this, room_id, client, setting);
                    RId2Room.Add(room_id, room);

                    //通知form更新
                    ClientList.Instance().AddClient(client.UserID, client.Profile.NickName, room_id);
                    UpdateUsers(ClientList.Instance().GetUserList(0));
                }
                else
                {
                    //设置错误,通知客户端
                    MyData data = new MyData
                    {
                        Description = PacketDescription.Hall2Cient,
                        Protocol    = protocol.CreateRoom,
                    };
                    client.SendSwitchReply(data);
                }
            }
        }
Exemplo n.º 2
0
        public void InterHall(Client client, bool reconnect)
        {
            //update user profile
            //bring to hall
            bool proceed = client.GetProfile(!reconnect);

            if (!proceed)
            {
                return;
            }

            ClientList.Instance().AddClient(client.UserID, client.Profile.NickName);
            //更新到form1
            UpdateUsers(ClientList.Instance().GetUserList(0));

            MyData data = new MyData();

            if (!reconnect)
            {
                //发送当前已登录的其他玩家信息和游戏房间信息
                DataSet ds = new DataSet();
                ds.Tables.Add(ClientList.Instance().GetUserList(0).Copy());
                ds.Tables.Add(RoomList.Instance().GetRoomList().Copy());

                data = new MyData
                {
                    Description = PacketDescription.Hall2Cient,
                    Protocol    = protocol.GetProfile,
                    Body        = new List <string> {
                        JsonUntity.DataSet2Json(ds)
                    }
                };
                client.SendProfileReply(data);
            }

            //通知其他客户端更新
            data = new MyData()
            {
                Description = PacketDescription.Hall2Cient,
                Protocol    = protocol.UpdateHallJoin,
                Body        = new List <string> {
                    client.UserID.ToString(), client.Profile.NickName, "0"
                }
            };
            foreach (Client other in UId2ClientTable.Values)
            {
                if (other != client)
                {
                    other.SendProfileReply(data);
                }
            }
        }
Exemplo n.º 3
0
        internal void OnDisconnected(MsgPackSession session, CloseReason value)
        {
            lock (this)
            {
                OutPut(session.SessionID + " disconnected:" + value);

                Client client = Session2ClientTable[session];
                client.Connected -= OnConnected;
                //删除session对应
                Session2ClientTable.Remove(session);
                session = null;
                //删除uid对应
                if (client != null)
                {
                    client.OnDisconnected();

                    if (UId2ClientTable.ContainsValue(client))
                    {
                        UId2ClientTable.Remove(client.UserID);
                    }
                    //广播离线信息
                    ClientList.Instance().RemoveClient(client.UserID);
                    //更新到form1
                    UpdateUsers(ClientList.Instance().GetUserList(0));

                    MyData data = new MyData()
                    {
                        Description = PacketDescription.Hall2Cient,
                        Protocol    = protocol.UpdateHallLeave,
                        Body        = new List <string> {
                            client.UserID.ToString()
                        }
                    };
                    foreach (Client other in UId2ClientTable.Values)
                    {
                        other.SendProfileReply(data);
                    }
                }
            }
        }
Exemplo n.º 4
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;
            }
        }