コード例 #1
0
        private IEnumerator HandleRequestValidateAccessTokenRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestValidateAccessTokenMessage message = messageHandler.ReadMessage <RequestValidateAccessTokenMessage>();

            ResponseValidateAccessTokenMessage.Error error = ResponseValidateAccessTokenMessage.Error.None;
            string userId      = message.userId;
            string accessToken = message.accessToken;
            ValidateAccessTokenJob validateAccessTokenJob = new ValidateAccessTokenJob(Database, userId, accessToken);

            validateAccessTokenJob.Start();
            yield return(StartCoroutine(validateAccessTokenJob.WaitFor()));

            if (!validateAccessTokenJob.result)
            {
                error       = ResponseValidateAccessTokenMessage.Error.InvalidAccessToken;
                userId      = string.Empty;
                accessToken = string.Empty;
            }
            else
            {
                CentralUserPeerInfo userPeerInfo;
                if (userPeersByUserId.TryGetValue(userId, out userPeerInfo))
                {
                    userPeersByUserId.Remove(userPeerInfo.userId);
                    userPeers.Remove(userPeerInfo.connectionId);
                }
                userPeerInfo = new CentralUserPeerInfo();
                userPeerInfo.connectionId = connectionId;
                userPeerInfo.userId       = userId;
                userPeerInfo.accessToken  = accessToken = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", "");
                userPeersByUserId[userId] = userPeerInfo;
                userPeers[connectionId]   = userPeerInfo;
                UpdateAccessTokenJob updateAccessTokenJob = new UpdateAccessTokenJob(Database, userId, accessToken);
                updateAccessTokenJob.Start();
                yield return(StartCoroutine(updateAccessTokenJob.WaitFor()));
            }
            ResponseValidateAccessTokenMessage responseMessage = new ResponseValidateAccessTokenMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseValidateAccessTokenMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            responseMessage.userId       = userId;
            responseMessage.accessToken  = accessToken;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseValidateAccessToken, responseMessage);
        }
コード例 #2
0
        private IEnumerator SetPlayerReadyRoutine(long connectionId, string userId, string accessToken, string selectCharacterId)
        {
            // Validate access token
            var validateAccessTokenJob = new ValidateAccessTokenJob(Database, userId, accessToken);

            validateAccessTokenJob.Start();
            yield return(StartCoroutine(validateAccessTokenJob.WaitFor()));

            if (!validateAccessTokenJob.result)
            {
                if (LogError)
                {
                    Debug.LogError("[Map Server] Invalid access token for user: "******"[Map Server] Cannot find select character: " + selectCharacterId + " for user: "******"[Map Server] Cannot find player character with entity Id: " + playerCharacterData.EntityId);
                        }
                        transport.ServerDisconnect(connectionId);
                    }
                    else
                    {
                        // Spawn character entity and set its data
                        var identity = Assets.NetworkSpawn(entityPrefab.Identity.HashAssetId, playerCharacterData.CurrentPosition, Quaternion.identity, 0, connectionId);
                        var playerCharacterEntity = identity.GetComponent <BasePlayerCharacterEntity>();
                        playerCharacterData.CloneTo(playerCharacterEntity);

                        // Notify clients that this character is spawn or dead
                        if (!playerCharacterEntity.IsDead())
                        {
                            playerCharacterEntity.RequestOnRespawn();
                        }
                        else
                        {
                            playerCharacterEntity.RequestOnDead();
                        }
                        RegisterPlayerCharacter(connectionId, playerCharacterEntity);

                        // Load party data, if this map-server does not have party data
                        if (playerCharacterEntity.PartyId > 0)
                        {
                            if (!parties.ContainsKey(playerCharacterEntity.PartyId))
                            {
                                yield return(StartCoroutine(LoadPartyRoutine(playerCharacterEntity.PartyId)));
                            }
                            if (parties.ContainsKey(playerCharacterEntity.PartyId))
                            {
                                var party = parties[playerCharacterEntity.PartyId];
                                SendCreatePartyToClient(playerCharacterEntity.ConnectionId, party);
                                SendAddPartyMembersToClient(playerCharacterEntity.ConnectionId, party);
                            }
                            else
                            {
                                playerCharacterEntity.ClearParty();
                            }
                        }

                        // Load guild data, if this map-server does not have guild data
                        if (playerCharacterEntity.GuildId > 0)
                        {
                            if (!guilds.ContainsKey(playerCharacterEntity.GuildId))
                            {
                                yield return(StartCoroutine(LoadGuildRoutine(playerCharacterEntity.GuildId)));
                            }
                            if (guilds.ContainsKey(playerCharacterEntity.GuildId))
                            {
                                var guild = guilds[playerCharacterEntity.GuildId];
                                playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                                SendCreateGuildToClient(playerCharacterEntity.ConnectionId, guild);
                                SendAddGuildMembersToClient(playerCharacterEntity.ConnectionId, guild);
                                SendSetGuildMessageToClient(playerCharacterEntity.ConnectionId, guild);
                                SendSetGuildRolesToClient(playerCharacterEntity.ConnectionId, guild);
                                SendSetGuildMemberRolesToClient(playerCharacterEntity.ConnectionId, guild);
                                SendSetGuildSkillLevelsToClient(playerCharacterEntity.ConnectionId, guild);
                                SendGuildLevelExpSkillPointToClient(playerCharacterEntity.ConnectionId, guild);
                            }
                            else
                            {
                                playerCharacterEntity.ClearGuild();
                            }
                        }

                        // Set user data to map server
                        var userData = new UserCharacterData();
                        userData.userId        = userId;
                        userData.id            = playerCharacterEntity.Id;
                        userData.characterName = playerCharacterEntity.CharacterName;
                        userData.dataId        = playerCharacterEntity.DataId;
                        userData.level         = playerCharacterEntity.Level;
                        userData.currentHp     = playerCharacterEntity.CurrentHp;
                        userData.maxHp         = playerCharacterEntity.CacheMaxHp;
                        userData.currentMp     = playerCharacterEntity.CurrentMp;
                        userData.maxMp         = playerCharacterEntity.CacheMaxMp;
                        usersById[userData.id] = userData;

                        // Add map user to central server and chat server
                        UpdateMapUser(CentralAppServerRegister, UpdateUserCharacterMessage.UpdateType.Add, userData);
                        if (ChatNetworkManager.IsClientConnected)
                        {
                            UpdateMapUser(ChatNetworkManager.Client, UpdateUserCharacterMessage.UpdateType.Add, userData);
                        }

                        var player = Players[connectionId];
                        foreach (var spawnedObject in Assets.SpawnedObjects)
                        {
                            if (spawnedObject.Value.ConnectionId == player.ConnectionId)
                            {
                                continue;
                            }

                            if (spawnedObject.Value.ShouldAddSubscriber(player))
                            {
                                spawnedObject.Value.AddSubscriber(player);
                            }
                        }
                    }
                }
            }
        }