public static bool DestroyRoom(uint roomId, string reason = "")
        {
#if DEBUG
            Logger.Instance.Log("Destroying room " + roomId);
#endif
            if (rooms.Any(x => x.roomId == roomId))
            {
                BaseRoom room = rooms.First(x => x.roomId == roomId);
                room.StopRoom();
                WebSocketListener.DestroyRoom(room);

                if (string.IsNullOrEmpty(reason))
                {
                    reason = "Room destroyed!";
                }

                NetOutgoingMessage outMsg = HubListener.ListenerServer.CreateMessage();

                outMsg.Write((byte)CommandType.Disconnect);
                outMsg.Write(reason);

                room.BroadcastPacket(outMsg, NetDeliveryMethod.ReliableOrdered);
                rooms.Remove(room);

                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static void AddClient(BaseRoom room, Client client)
        {
            if (room.GetRoomInfo().players == 0 && room.noHost)
            {
                room.ForceTransferHost(client.playerInfo);
            }

            room.roomClients.Add(client);
        }
        public static uint CreateRoom(RoomSettings settings, PlayerInfo host)
        {
            BaseRoom room = Activator.CreateInstance(roomClass, GetNextFreeID(), settings, host) as BaseRoom;

            rooms.Add(room);
            room.StartRoom();
            WebSocketListener.AddRoom(room);
#if DEBUG
            Logger.Instance.Log($"New room created! Host={host.playerName}({host.playerId}) Settings: name={settings.Name}, password={settings.Password}, usePassword={settings.UsePassword}, maxPlayers={settings.MaxPlayers}, noFail={settings.NoFail}, songSelecionType={settings.SelectionType}");
#endif
            return(room.roomId);
        }
Ejemplo n.º 4
0
        public static uint CreateRoom(RoomSettings settings)
        {
            BaseRoom room = Activator.CreateInstance(roomClass, GetNextFreeID(), settings, new PlayerInfo("server", long.MaxValue)) as BaseRoom;

            room.noHost = true;
            rooms.Add(room);
            room.StartRoom();
            WebSocketListener.AddRoom(room);
#if DEBUG
            Logger.Instance.Log($"New room created! Settings: name={settings.Name}, password={settings.Password}, usePassword={settings.UsePassword}, maxPlayers={settings.MaxPlayers}, songSelecionType={settings.SelectionType}");
#endif
            return(room.roomId);
        }
        public static bool ClientJoined(Client client, uint roomId, string password)
        {
#if DEBUG
            Logger.Instance.Log($"Client joining room {roomId}");
#endif

            NetOutgoingMessage outMsg = HubListener.ListenerServer.CreateMessage();

            if (rooms.Any(x => x.roomId == roomId))
            {
                BaseRoom room     = rooms.First(x => x.roomId == roomId);
                RoomInfo roomInfo = room.GetRoomInfo();

                if (roomInfo.players < roomInfo.maxPlayers || roomInfo.maxPlayers == 0)
                {
                    if (roomInfo.usePassword)
                    {
                        if (room.roomSettings.Password == password)
                        {
                            outMsg.Write((byte)CommandType.JoinRoom);
                            outMsg.Write((byte)JoinResult.Success);

                            client.playerConnection.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                            Program.networkBytesOutNow += outMsg.LengthBytes;

                            client.joinedRoomID = room.roomId;
                            if (room.roomClients.Any(x => x.playerInfo == client.playerInfo))
                            {
                                Client loggedIn = room.roomClients.Find(x => x.playerInfo == client.playerInfo);
                                if (loggedIn.playerConnection != client.playerConnection)
                                {
                                    loggedIn.KickClient("You logged in from another location");
                                }
                                else
                                {
                                    room.roomClients.Remove(loggedIn);
                                }
                            }
                            AddClient(room, client);
                            return(true);
                        }
                        else
                        {
                            outMsg.Write((byte)CommandType.JoinRoom);
                            outMsg.Write((byte)JoinResult.IncorrectPassword);

                            client.playerConnection.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                            Program.networkBytesOutNow += outMsg.LengthBytes;
                            return(false);
                        }
                    }
                    else
                    {
                        outMsg.Write((byte)CommandType.JoinRoom);
                        outMsg.Write((byte)JoinResult.Success);

                        client.playerConnection.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                        Program.networkBytesOutNow += outMsg.LengthBytes;

                        client.joinedRoomID = room.roomId;
                        if (room.roomClients.Any(x => x.playerInfo == client.playerInfo))
                        {
                            Client loggedIn = room.roomClients.Find(x => x.playerInfo.Equals(client.playerInfo));
                            if (!loggedIn.playerConnection.Equals(client.playerConnection))
                            {
                                loggedIn.KickClient("You logged in from another location");
                            }
                            else
                            {
                                room.roomClients.Remove(loggedIn);
                            }
                        }
                        AddClient(room, client);
                        return(true);
                    }
                }
                else
                {
                    outMsg.Write((byte)CommandType.JoinRoom);
                    outMsg.Write((byte)JoinResult.TooMuchPlayers);

                    client.playerConnection.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                    Program.networkBytesOutNow += outMsg.LengthBytes;
                    return(false);
                }
            }
            else
            {
                outMsg.Write((byte)CommandType.JoinRoom);
                outMsg.Write((byte)JoinResult.RoomNotFound);

                client.playerConnection.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered, 0);
                Program.networkBytesOutNow += outMsg.LengthBytes;
                return(false);
            }
        }