Exemplo n.º 1
0
        protected virtual void HandleDestroyRoom(IIncommingMessage message)
        {
            var roomId = message.AsInt();

            RegisteredRoom room;

            _rooms.TryGetValue(roomId, out room);

            if (room == null)
            {
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                // Wrong peer unregistering the room
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            DestroyRoom(room);

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 2
0
        protected virtual void HandleGetCompletionData(IIncommingMessage message)
        {
            var spawnId = message.AsInt();

            SpawnTasks.TryGetValue(spawnId, out var task);

            if (task == null)
            {
                message.Respond("Invalid request", ResponseStatus.Failed);
                return;
            }

            if (task.Requester != message.Peer)
            {
                message.Respond("You're not the requester", ResponseStatus.Unauthorized);
                return;
            }

            if (task.FinalizationPacket == null)
            {
                message.Respond("Task has no completion data", ResponseStatus.Failed);
                return;
            }

            // Respond with data (dictionary of strings)
            message.Respond(task.FinalizationPacket.FinalizationData.ToBytes(), ResponseStatus.Success);
        }
        protected virtual void DestroyRoomRequestHandler(IIncommingMessage message)
        {
            var roomId = message.AsInt();

            logger.Debug($"Client {message.Peer.Id} requested to destroy room server with id {roomId}");

            if (!roomsList.TryGetValue(roomId, out RegisteredRoom room))
            {
                logger.Debug($"But this room does not exist");
                message.Respond("Room does not exist", ResponseStatus.Failed);
                return;
            }

            if (message.Peer != room.Peer)
            {
                logger.Debug($"But it is not the creator of the room");
                message.Respond("You're not the creator of the room", ResponseStatus.Unauthorized);
                return;
            }

            DestroyRoom(room);

            logger.Debug($"Room {roomId} has been successfully destroyed");

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handles a request from user to join a lobby
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandleJoinLobby(IIncommingMessage message)
        {
            var user = GetOrCreateLobbiesExtension(message.Peer);

            if (user.CurrentLobby != null)
            {
                message.Respond("You're already in a lobby", ResponseStatus.Failed);
                return;
            }

            var lobbyId = message.AsInt();

            ILobby lobby;

            lobbies.TryGetValue(lobbyId, out lobby);

            if (lobby == null)
            {
                message.Respond("Lobby was not found", ResponseStatus.Failed);
                return;
            }

            string error;

            if (!lobby.AddPlayer(user, out error))
            {
                message.Respond(error ?? "Failed to add player to lobby", ResponseStatus.Failed);
                return;
            }

            var data = lobby.GenerateLobbyData(user);

            message.Respond(data, ResponseStatus.Success);
        }
Exemplo n.º 5
0
        private void HandleLobbyStateChangeMsg(IIncommingMessage message)
        {
            var newState = (LobbyState)message.AsInt();

            Data.LobbyState = newState;

            Listener?.OnLobbyStateChange(newState);
        }
        protected virtual void SetProcessStartedRequestHandler(IIncommingMessage message)
        {
            var spawnId = message.AsInt();

            if (spawnTasksList.TryGetValue(spawnId, out SpawnTask task))
            {
                task.OnProcessStarted();
                task.Spawner.OnProcessStarted();
            }
        }
Exemplo n.º 7
0
        private void HandleLobbyStateChangeMsg(IIncommingMessage message)
        {
            var newState = (LobbyState)message.AsInt();

            Data.LobbyState = newState;

            if (_listener != null)
            {
                _listener.OnLobbyStateChange(newState);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Handles a request from user to leave a lobby
        /// </summary>
        /// <param name="message"></param>
        private void HandleLeaveLobby(IIncommingMessage message)
        {
            var lobbyId = message.AsInt();

            _lobbiesById.TryGetValue(lobbyId, out var lobby);

            var lobbiesExt = GetOrCreateLobbiesExtension(message.Peer);

            lobby?.RemovePlayer(lobbiesExt);

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 9
0
        protected virtual void HandleProcessStarted(IIncommingMessage message)
        {
            var spawnId = message.AsInt();

            SpawnTasks.TryGetValue(spawnId, out var task);

            if (task == null)
            {
                return;
            }

            task.OnProcessStarted();
            task.Spawner.OnProcessStarted();
        }
Exemplo n.º 10
0
        private void HandleLeftLobbyMsg(IIncommingMessage message)
        {
            var id = message.AsInt();

            // Check the id in case there's something wrong with message order
            if (Id != id)
            {
                return;
            }

            HasLeft = true;

            Listener?.OnLobbyLeft();
        }
Exemplo n.º 11
0
        private void HandleProcessKilled(IIncommingMessage message)
        {
            var spawnId = message.AsInt();

            _spawnTasks.TryGetValue(spawnId, out var task);

            if (task == null)
            {
                return;
            }

            task.OnProcessKilled();
            task.Spawner.OnProcessKilled();
        }
Exemplo n.º 12
0
        protected virtual void HandleGetLobbyInfo(IIncommingMessage message)
        {
            var lobbyId = message.AsInt();

            lobbies.TryGetValue(lobbyId, out ILobby lobby);

            if (lobby == null)
            {
                message.Respond("Lobby not found", ResponseStatus.Failed);
                return;
            }

            message.Respond(lobby.GenerateLobbyData(), ResponseStatus.Success);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Handles a request from user to leave a lobby
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandleLeaveLobby(IIncommingMessage message)
        {
            var lobbyId = message.AsInt();

            lobbies.TryGetValue(lobbyId, out ILobby lobby);

            var lobbiesExt = GetOrCreateLobbyUserPeerExtension(message.Peer);

            if (lobby != null)
            {
                lobby.RemovePlayer(lobbiesExt);
            }

            message.Respond(ResponseStatus.Success);
        }
Exemplo n.º 14
0
    private void HandleGetPeerAccountInfo(IIncommingMessage message)
    {
        int      peerId = message.AsInt();
        UserInfo user   = new UserInfo();

        if (AlbotDBManager.getActiveUsersInfo(peerId, ref user) == false)
        {
            message.Respond("Peer with a given ID is not in the game", ResponseStatus.Error);
            return;
        }

        var packet = new PeerAccountInfoPacket()
        {
            PeerId     = peerId,
            Properties = new Dictionary <string, string>()
            {
                { AlbotDictKeys.icon, user.profilePic }
            },
            Username = user.username
        };

        message.Respond(packet, ResponseStatus.Success);
    }
Exemplo n.º 15
0
        /// <summary>
        /// Handles a request from client to get profile
        /// </summary>
        /// <param name="message"></param>
        protected virtual void ClientProfileRequestHandler(IIncommingMessage message)
        {
            var clientPropCount = message.AsInt();

            var profileExt = message.Peer.GetExtension <ProfilePeerExtension>();

            if (profileExt == null)
            {
                message.Respond("Profile not found", ResponseStatus.Failed);
                return;
            }

            profileExt.Profile.ClientPeer = message.Peer;

            if (!ignoreProfileMissmatchError && clientPropCount != profileExt.Profile.PropertyCount)
            {
                logger.Error(string.Format($"Client requested a profile with {clientPropCount} properties, but server " +
                                           $"constructed a profile with {profileExt.Profile.PropertyCount}. Make sure that you've changed the " +
                                           "profile factory on the ProfilesModule"));
            }

            message.Respond(profileExt.Profile.ToBytes(), ResponseStatus.Success);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Handles a request to retrieve account information
        /// </summary>
        /// <param name="message"></param>
        protected virtual void HandleGetPeerAccountInfo(IIncommingMessage message)
        {
            if (!HasGetPeerInfoPermissions(message.Peer))
            {
                message.Respond("Unauthorized", ResponseStatus.Unauthorized);
                return;
            }

            var peerId = message.AsInt();

            var peer = Server.GetPeer(peerId);

            if (peer == null)
            {
                message.Respond("Peer with a given ID is not in the game", ResponseStatus.Error);
                return;
            }

            var account = peer.GetExtension <IUserExtension>();

            if (account == null)
            {
                message.Respond("Peer has not been authenticated", ResponseStatus.Failed);
                return;
            }

            var data = account.AccountData;

            var packet = new PeerAccountInfoPacket()
            {
                PeerId     = peerId,
                Properties = data.Properties,
                Username   = account.Username
            };

            message.Respond(packet, ResponseStatus.Success);
        }
Exemplo n.º 17
0
        protected virtual void HandleSetReadyStatus(IIncommingMessage message)
        {
            var isReady = message.AsInt() > 0;

            var lobbiesExt = GetOrCreateLobbiesExtension(message.Peer);
            var lobby      = lobbiesExt.CurrentLobby;

            if (lobby == null)
            {
                message.Respond("You're not in a lobby", ResponseStatus.Failed);
                return;
            }

            var member = lobby.GetMemberByExtension(lobbiesExt);

            if (member == null)
            {
                message.Respond("Invalid request", ResponseStatus.Failed);
                return;
            }

            lobby.SetReadyState(member, isReady);
            message.Respond(ResponseStatus.Success);
        }