コード例 #1
0
        public void OnConnected(object sender, EventArgs e)
        {
            lock (this)
            {
                if (sender is Client temp)
                {
                    //同名已连接用户强制离线
                    bool same = HandleSameAccount(temp.UserID);

                    //Add the client to the Hashtable
                    UId2ClientTable.Add(temp.UserID, temp);
                    OutPut("Client Connected:" + temp.UserID);

                    //check last game is over
                    bool reconnect = false;
                    if (temp.GameRoom > 0)
                    {
                        if (RId2Room.ContainsKey(temp.GameRoom) && RId2Room[temp.GameRoom].GameStarted)
                        {
                            reconnect = true;
                        }
                        else
                        {
                            //更新用户room id
                            ClientDBOperation.UpdateGameRoom(temp.UserName, -1);
                        }
                    }

                    InterHall(temp, reconnect);
                }
            }
        }
コード例 #2
0
        public void StartGame(Room room)
        {
            //更新房间号至数据库,以备断线重连
            foreach (Client client in room.Clients)
            {
                if (client.UserID > 0)
                {
                    ClientDBOperation.UpdateGameRoom(client.UserName, client.GameRoom);
                }
            }

            Thread thread = new Thread(room.Run);

            Room2Thread.Add(room, thread);
            thread.Start();
        }
コード例 #3
0
        public void StartGame(Room room)
        {
            //更新房间号至数据库,以备断线重连
            foreach (Client client in room.Clients)
            {
                if (client.UserID > 0)
                {
                    ClientDBOperation.UpdateGameRoom(client.UserName, client.GameRoom);
                }
            }

            if (!Room2Thread.TryGetValue(room, out Thread _thread))
            {
                Thread thread = new Thread(room.Run);
                if (Room2Thread.TryAdd(room, thread))
                {
                    thread.Start();
                }
            }
        }
コード例 #4
0
        public void OnConnected(object sender, EventArgs e)
        {
            try
            {
                if (sender is Client temp)
                {
                    //同名已连接用户强制离线
                    bool same = HandleSameAccount(temp.UserID);
                    //Add the client to the Hashtable
                    UId2ClientTable.TryAdd(temp.UserID, temp);

                    OutPut("Client Connected:" + temp.UserID);

                    //check last game is over
                    bool reconnect = false;
                    if (temp.GameRoom > 0)
                    {
                        if (RId2Room.TryGetValue(temp.GameRoom, out Room room) && room.GameStarted)
                        {
                            reconnect = true;
                        }
                        else
                        {
                            //更新用户room id
                            ClientDBOperation.UpdateGameRoom(temp.UserName, -1);
                        }
                    }

                    InterHall(temp, reconnect);
                }
            }
            catch (Exception error)
            {
                LogHelper.WriteLog(null, error);
                Debug(string.Format("error at client connected {0} {1} {2}", error.Message, error.Source, error.HelpLink));
            }
        }
コード例 #5
0
        public void RemoveRoom(Room room, Client host, List <Client> clients)
        {
            lock (this)
            {
                Debug("remove at " + Thread.CurrentThread.ManagedThreadId.ToString());

                //更新该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);
                        }
                    }
                }

                RId2Room.Remove(room.RoomId);
                if (Room2Thread.ContainsKey(room))
                {
                    Thread thread = Room2Thread[room];
                    thread.Abort();
                    Room2Thread.Remove(room);
                    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()
                    }
                };
                foreach (Client client in UId2ClientTable.Values)
                {
                    client.SendProfileReply(data);
                }
            }
        }
コード例 #6
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));
            }
        }