Esempio n. 1
0
        public async UniTaskVoid HandleRequestCreateGuild(RequestHandlerData requestHandler, RequestCreateGuildMessage request, RequestProceedResultDelegate <ResponseCreateGuildMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            await UniTask.Yield();

            IPlayerCharacterData playerCharacter;
            if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter))
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }
            ValidateGuildRequestResult validateResult = playerCharacter.CanCreateGuild(request.guildName);
            if (!validateResult.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = validateResult.GameMessage,
                });
                return;
            }
            string guildName = request.guildName.Trim();
            if (!NameValidating.ValidateGuildName(guildName))
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_GUILD_NAME
                });
                return;
            }
            AsyncResponseData <FindGuildNameResp> findGuildNameResp = await DbServiceClient.FindGuildNameAsync(new FindGuildNameReq()
            {
                GuildName = guildName,
            });

            if (!findGuildNameResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            if (findGuildNameResp.Response.FoundAmount > 0)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_GUILD_NAME_EXISTED,
                });
                return;
            }
            AsyncResponseData <GuildResp> createGuildResp = await DbServiceClient.CreateGuildAsync(new CreateGuildReq()
            {
                LeaderCharacterId = playerCharacter.Id,
                GuildName         = guildName,
            });

            if (!createGuildResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            // Update cache
            GuildData guild = createGuildResp.Response.GuildData;
            GameInstance.Singleton.SocialSystemSetting.DecreaseCreateGuildResource(playerCharacter);
            GameInstance.ServerGuildHandlers.SetGuild(guild.id, guild);
            playerCharacter.GuildId        = guild.id;
            playerCharacter.GuildRole      = guild.GetMemberRole(playerCharacter.Id);
            playerCharacter.SharedGuildExp = 0;
            // Broadcast via chat server
            if (ClusterClient.IsNetworkActive)
            {
                ClusterClient.SendCreateGuild(MMOMessageTypes.UpdateGuild, guild.id, guildName, playerCharacter.Id);
                ClusterClient.SendAddSocialMember(MMOMessageTypes.UpdateGuildMember, guild.id, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level);
            }
            GameInstance.ServerGameMessageHandlers.SendSetGuildData(requestHandler.ConnectionId, guild);
            GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToOne(requestHandler.ConnectionId, guild);
            result.InvokeSuccess(new ResponseCreateGuildMessage());
#endif
        }
        public async UniTaskVoid HandleRequestCreateGuild(RequestHandlerData requestHandler, RequestCreateGuildMessage request, RequestProceedResultDelegate <ResponseCreateGuildMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            await UniTask.Yield();

            IPlayerCharacterData playerCharacter;
            if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter))
            {
                result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }
            ValidateGuildRequestResult validateResult = playerCharacter.CanCreateGuild(request.guildName);
            if (!validateResult.IsSuccess)
            {
                result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage()
                {
                    message = validateResult.GameMessage,
                });
                return;
            }
            FindGuildNameResp findGuildNameResp = await DbServiceClient.FindGuildNameAsync(new FindGuildNameReq()
            {
                GuildName = request.guildName,
            });

            if (findGuildNameResp.FoundAmount > 0)
            {
                result.Invoke(AckResponseCode.Error, new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_GUILD_NAME_EXISTED,
                });
                return;
            }
            GuildResp createGuildResp = await DbServiceClient.CreateGuildAsync(new CreateGuildReq()
            {
                LeaderCharacterId = playerCharacter.Id,
                GuildName         = request.guildName,
            });

            GuildData guild = createGuildResp.GuildData;
            GameInstance.Singleton.SocialSystemSetting.DecreaseCreateGuildResource(playerCharacter);
            GameInstance.ServerGuildHandlers.SetGuild(guild.id, guild);
            playerCharacter.GuildId        = guild.id;
            playerCharacter.GuildRole      = guild.GetMemberRole(playerCharacter.Id);
            playerCharacter.SharedGuildExp = 0;
            if (playerCharacter is BasePlayerCharacterEntity)
            {
                // Sync guild name to client
                (playerCharacter as BasePlayerCharacterEntity).GuildName = request.guildName;
            }
            // Broadcast via chat server
            if (ChatNetworkManager.IsClientConnected)
            {
                ChatNetworkManager.SendCreateGuild(null, MMOMessageTypes.UpdateGuild, guild.id, request.guildName, playerCharacter.Id);
                ChatNetworkManager.SendAddSocialMember(null, MMOMessageTypes.UpdateGuildMember, guild.id, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level);
            }
            GameInstance.ServerGameMessageHandlers.SendSetGuildData(requestHandler.ConnectionId, guild);
            GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToOne(requestHandler.ConnectionId, guild);
            result.Invoke(AckResponseCode.Success, new ResponseCreateGuildMessage());
#endif
        }
Esempio n. 3
0
        private IEnumerator SetPlayerReadyRoutine(long connectionId, string userId, string accessToken, string selectCharacterId)
        {
            // Validate access token
            ValidateAccessTokenJob 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
                        LiteNetLibIdentity        identity = Assets.NetworkSpawn(entityPrefab.Identity.HashAssetId, playerCharacterData.CurrentPosition, Quaternion.identity, 0, connectionId);
                        BasePlayerCharacterEntity playerCharacterEntity = identity.GetComponent <BasePlayerCharacterEntity>();
                        playerCharacterData.CloneTo(playerCharacterEntity);

                        // Summon saved summons
                        for (int i = 0; i < playerCharacterEntity.Summons.Count; ++i)
                        {
                            CharacterSummon summon = playerCharacterEntity.Summons[i];
                            summon.Summon(playerCharacterEntity, summon.Level, summon.summonRemainsDuration, summon.Exp, summon.CurrentHp, summon.CurrentMp);
                            playerCharacterEntity.Summons[i] = summon;
                        }

                        // Load user level
                        GetUserLevelJob loadUserLevelJob = new GetUserLevelJob(Database, userId);
                        loadUserLevelJob.Start();
                        yield return(StartCoroutine(loadUserLevelJob.WaitFor()));

                        playerCharacterEntity.UserLevel = loadUserLevelJob.result;

                        // 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))
                            {
                                PartyData 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))
                            {
                                GuildData 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
                        UserCharacterData 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);
                        }

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

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