Exemple #1
0
        private void SetMyPropertiesMessageHandler(IIncomingMessage message)
        {
            // Get lobby user peer extension
            var lobbyUser = GetOrCreateLobbyUserPeerExtension(message.Peer);

            // Get current lobby this user joined in
            var lobby = lobbyUser.CurrentLobby;

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

            // Properties to be changed
            var properties = new Dictionary <string, string>().FromBytes(message.AsBytes());

            // Get member of lobby by its lobby user extension
            var member = lobby.GetMemberByExtension(lobbyUser);

            foreach (var dataProperty in properties)
            {
                // We don't change properties directly,
                // because we want to allow an implementation of lobby
                // to do "sanity" checking
                if (!lobby.SetPlayerProperty(member, dataProperty.Key, dataProperty.Value))
                {
                    message.Respond("Failed to set property: " + dataProperty.Key, ResponseStatus.Failed);
                    return;
                }
            }

            message.Respond(ResponseStatus.Success);
        }
Exemple #2
0
        private void UpdateDisplayNameRequestHandler(IIncomingMessage message)
        {
            var userExtension = message.Peer.GetExtension <IUserPeerExtension>();

            if (userExtension == null || userExtension.Account == null)
            {
                message.Respond("Invalid session", ResponseStatus.Unauthorized);
                return;
            }

            var newProfileData = new Dictionary <string, string>().FromBytes(message.AsBytes());

            try
            {
                if (profilesList.TryGetValue(userExtension.Username, out ObservableServerProfile profile))
                {
                    profile.GetProperty <ObservableString>((short)ObservablePropertiyCodes.DisplayName).Set(newProfileData["displayName"]);
                    profile.GetProperty <ObservableString>((short)ObservablePropertiyCodes.Avatar).Set(newProfileData["avatarUrl"]);

                    message.Respond(ResponseStatus.Success);
                }
                else
                {
                    message.Respond("Invalid session", ResponseStatus.Unauthorized);
                }
            }
            catch (Exception e)
            {
                message.Respond($"Internal Server Error: {e}", ResponseStatus.Error);
            }
        }
Exemple #3
0
        private void OnUserLeftChannelHandler(IIncomingMessage message)
        {
            var data = new List <string>().FromBytes(message.AsBytes());

            if (OnUserLeftChannelEvent != null)
            {
                OnUserLeftChannelEvent.Invoke(data[0], data[1]);
            }
        }
Exemple #4
0
        protected virtual void CreateLobbyHandle(IIncomingMessage message)
        {
            // We may need to check permission of requester
            if (!CheckIfHasPermissionToCreate(message.Peer))
            {
                message.Respond("Insufficient permissions", ResponseStatus.Unauthorized);
                return;
            }

            // Let's get or create new lobby user peer extension
            var lobbyUser = GetOrCreateLobbyUserPeerExtension(message.Peer);

            // If peer is already in a lobby and system does not allow to create if user is joined
            if (dontAllowCreatingIfJoined && lobbyUser.CurrentLobby != null)
            {
                message.Respond("You are already in a lobby", ResponseStatus.Failed);
                return;
            }

            // Deserialize properties of the lobby
            var options = MstProperties.FromBytes(message.AsBytes());

            // Get lobby factory Id or empty string
            string lobbyFactoryId = options.AsString(MstDictKeys.LOBBY_FACTORY_ID);

            if (string.IsNullOrEmpty(lobbyFactoryId))
            {
                message.Respond("Invalid request (undefined factory)", ResponseStatus.Failed);
                return;
            }

            // Get the lobby factory
            factories.TryGetValue(lobbyFactoryId, out ILobbyFactory factory);

            if (factory == null)
            {
                message.Respond("Unavailable lobby factory", ResponseStatus.Failed);
                return;
            }

            var newLobby = factory.CreateLobby(options, message.Peer);

            if (!AddLobby(newLobby))
            {
                message.Respond("Lobby registration failed", ResponseStatus.Error);
                return;
            }

            logger.Info("Lobby created: " + newLobby.Id);

            // Respond with success and lobby id
            message.Respond(newLobby.Id, ResponseStatus.Success);
        }
Exemple #5
0
        protected virtual void FindGamesRequestHandler(IIncomingMessage message)
        {
            var list = new List <GameInfoPacket>();

            var filters = MstProperties.FromBytes(message.AsBytes());

            foreach (var provider in GameProviders)
            {
                list.AddRange(provider.GetPublicGames(message.Peer, filters));
            }

            // Convert to generic list and serialize to bytes
            var bytes = list.Select(l => (ISerializablePacket)l).ToBytes();

            message.Respond(bytes, ResponseStatus.Success);
        }
Exemple #6
0
        /// <summary>
        /// Handles a message from game server, which includes player profiles updates
        /// </summary>
        /// <param name="message"></param>
        protected virtual void ProfileUpdateHandler(IIncomingMessage message)
        {
            if (!HasPermissionToEditProfiles(message.Peer))
            {
                Logs.Error("Master server received an update for a profile, but peer who tried to " +
                           "update it did not have sufficient permissions");
                return;
            }

            var data = message.AsBytes();

            using (var ms = new MemoryStream(data))
            {
                using (var reader = new EndianBinaryReader(EndianBitConverter.Big, ms))
                {
                    // Read profiles count
                    var count = reader.ReadInt32();

                    for (var i = 0; i < count; i++)
                    {
                        // Read userId
                        var userId = reader.ReadString();

                        // Read updates length
                        var updatesLength = reader.ReadInt32();

                        // Read updates
                        var updates = reader.ReadBytes(updatesLength);

                        try
                        {
                            if (profilesList.TryGetValue(userId, out ObservableServerProfile profile))
                            {
                                profile.ApplyUpdates(updates);
                            }
                        }
                        catch (Exception e)
                        {
                            Logs.Error("Error while trying to handle profile updates from master server");
                            Logs.Error(e);
                        }
                    }
                }
            }
        }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        public void GameAccessRequestHandler(IIncomingMessage message)
        {
            if (lobbyRoom == null)
            {
                message.Respond("Game is not running", ResponseStatus.Failed);
                return;
            }

            var requestData = new MstProperties(new Dictionary <string, string>().FromBytes(message.AsBytes()));

            lobbyRoom.GetAccess(message.Peer, requestData, (access, error) =>
            {
                if (access == null)
                {
                    message.Respond(error ?? "Failed to get access to game", ResponseStatus.Failed);
                    return;
                }

                // Send back the access
                message.Respond(access, ResponseStatus.Success);
            });
        }
Exemple #8
0
        private void OnUserJoinedChannelHandler(IIncomingMessage message)
        {
            var data = new List <string>().FromBytes(message.AsBytes());

            OnUserJoinedChannelEvent?.Invoke(data[0], data[1]);
        }