public void OnUpdateGuildMember(UpdateSocialMemberMessage message)
        {
            GuildData guild;
            BasePlayerCharacterEntity playerCharacterEntity;

            if (ServerGuildHandlers.TryGetGuild(message.id, out guild) && guild.UpdateSocialGroupMember(message))
            {
                switch (message.type)
                {
                case UpdateSocialMemberMessage.UpdateType.Add:
                    if (ServerUserHandlers.TryGetPlayerCharacterById(message.data.id, out playerCharacterEntity))
                    {
                        playerCharacterEntity.GuildId   = message.id;
                        playerCharacterEntity.GuildName = guild.guildName;
                        playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                        ServerGameMessageHandlers.SendSetGuildData(playerCharacterEntity.ConnectionId, guild);
                        ServerGameMessageHandlers.SendAddGuildMembersToOne(playerCharacterEntity.ConnectionId, guild);
                    }
                    ServerGameMessageHandlers.SendAddGuildMemberToMembers(guild, message.data.id, message.data.characterName, message.data.dataId, message.data.level);
                    break;

                case UpdateSocialMemberMessage.UpdateType.Remove:
                    if (ServerUserHandlers.TryGetPlayerCharacterById(message.data.id, out playerCharacterEntity))
                    {
                        playerCharacterEntity.ClearGuild();
                        ServerGameMessageHandlers.SendClearGuildData(playerCharacterEntity.ConnectionId, message.id);
                    }
                    ServerGameMessageHandlers.SendRemoveGuildMemberToMembers(guild, message.data.id);
                    break;
                }
            }
        }
        public void CastleOccupied(int attackerGuildId)
        {
            GuildWarMapInfo mapInfo = CurrentMapInfo as GuildWarMapInfo;

            LastOccupyTime = System.DateTime.Now;
            GuildData guild;

            if (attackerGuildId > 0 && ServerGuildHandlers.TryGetGuild(attackerGuildId, out guild))
            {
                DefenderGuildId   = attackerGuildId;
                DefenderGuildName = guild.guildName;
                ServerSendSystemAnnounce(string.Format(mapInfo.attackerWinMessage, DefenderGuildName));
                GiveGuildBattleRewardTo(DefenderGuildId);
                ExpelLoserGuilds(DefenderGuildId);
                RegenerateMonsters();
            }
            SendGuildWarStatus();
        }
        public void OnUpdateMapUser(UpdateUserCharacterMessage message)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            int       socialId;
            PartyData party;
            GuildData guild;
            switch (message.type)
            {
            case UpdateUserCharacterMessage.UpdateType.Add:
                if (!usersById.ContainsKey(message.data.id))
                {
                    usersById.TryAdd(message.data.id, message.data);
                }
                break;

            case UpdateUserCharacterMessage.UpdateType.Remove:
                usersById.TryRemove(message.data.id, out _);
                break;

            case UpdateUserCharacterMessage.UpdateType.Online:
                if (usersById.ContainsKey(message.data.id))
                {
                    ServerCharacterHandlers.MarkOnlineCharacter(message.data.id);
                    socialId = message.data.partyId;
                    if (socialId > 0 && ServerPartyHandlers.TryGetParty(socialId, out party))
                    {
                        party.UpdateMember(message.data);
                        ServerPartyHandlers.SetParty(socialId, party);
                    }
                    socialId = message.data.guildId;
                    if (socialId > 0 && ServerGuildHandlers.TryGetGuild(socialId, out guild))
                    {
                        guild.UpdateMember(message.data);
                        ServerGuildHandlers.SetGuild(socialId, guild);
                    }
                    usersById[message.data.id] = message.data;
                }
                break;
            }
#endif
        }
        public void OnUpdateGuild(UpdateGuildMessage message)
        {
            BasePlayerCharacterEntity playerCharacterEntity;
            GuildData guild;

            if (ServerGuildHandlers.TryGetGuild(message.id, out guild))
            {
                switch (message.type)
                {
                case UpdateGuildMessage.UpdateType.ChangeLeader:
                    guild.SetLeader(message.characterId);
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    if (ServerUserHandlers.TryGetPlayerCharacterById(message.characterId, out playerCharacterEntity))
                    {
                        playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                    }
                    ServerGameMessageHandlers.SendSetGuildLeaderToMembers(guild);
                    break;

                case UpdateGuildMessage.UpdateType.SetGuildMessage:
                    guild.guildMessage = message.guildMessage;
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    ServerGameMessageHandlers.SendSetGuildMessageToMembers(guild);
                    break;

                case UpdateGuildMessage.UpdateType.SetGuildRole:
                    guild.SetRole(message.guildRole, message.roleName, message.canInvite, message.canKick, message.shareExpPercentage);
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    foreach (string memberId in guild.GetMemberIds())
                    {
                        if (ServerUserHandlers.TryGetPlayerCharacterById(memberId, out playerCharacterEntity))
                        {
                            playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                        }
                    }
                    ServerGameMessageHandlers.SendSetGuildRoleToMembers(guild, message.guildRole, message.roleName, message.canInvite, message.canKick, message.shareExpPercentage);
                    break;

                case UpdateGuildMessage.UpdateType.SetGuildMemberRole:
                    guild.SetMemberRole(message.characterId, message.guildRole);
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    if (ServerUserHandlers.TryGetPlayerCharacterById(message.characterId, out playerCharacterEntity))
                    {
                        playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                    }
                    ServerGameMessageHandlers.SendSetGuildMemberRoleToMembers(guild, message.characterId, message.guildRole);
                    break;

                case UpdateGuildMessage.UpdateType.SetSkillLevel:
                    guild.SetSkillLevel(message.dataId, message.level);
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    ServerGameMessageHandlers.SendSetGuildSkillLevelToMembers(guild, message.dataId);
                    break;

                case UpdateGuildMessage.UpdateType.SetGold:
                    guild.gold = message.gold;
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    ServerGameMessageHandlers.SendSetGuildGoldToMembers(guild);
                    break;

                case UpdateGuildMessage.UpdateType.LevelExpSkillPoint:
                    guild.level      = message.level;
                    guild.exp        = message.exp;
                    guild.skillPoint = message.skillPoint;
                    ServerGuildHandlers.SetGuild(message.id, guild);
                    ServerGameMessageHandlers.SendSetGuildLevelExpSkillPointToMembers(guild);
                    break;

                case UpdateGuildMessage.UpdateType.Terminate:
                    foreach (string memberId in guild.GetMemberIds())
                    {
                        if (ServerUserHandlers.TryGetPlayerCharacterById(memberId, out playerCharacterEntity))
                        {
                            playerCharacterEntity.ClearGuild();
                            ServerGameMessageHandlers.SendClearGuildData(playerCharacterEntity.ConnectionId, message.id);
                        }
                    }
                    ServerGuildHandlers.RemoveGuild(message.id);
                    break;
                }
            }
        }
        private async UniTaskVoid SetPlayerReadyRoutine(long connectionId, string userId, string selectCharacterId)
        {
            CharacterResp characterResp = await DbServiceClient.ReadCharacterAsync(new ReadCharacterReq()
            {
                UserId      = userId,
                CharacterId = selectCharacterId
            });

            PlayerCharacterData playerCharacterData = characterResp.CharacterData;

            // If data is empty / cannot find character, disconnect user
            if (playerCharacterData == null)
            {
                if (LogError)
                {
                    Logging.LogError(LogTag, "Cannot find select character: " + selectCharacterId + " for user: "******"Cannot find player character with entity Id: " + playerCharacterData.EntityId);
                    }
                    Transport.ServerDisconnect(connectionId);
                }
                else
                {
                    // Prepare saving location for this character
                    string  savingCurrentMapName  = playerCharacterData.CurrentMapName;
                    Vector3 savingCurrentPosition = playerCharacterData.CurrentPosition;

                    if (IsInstanceMap())
                    {
                        playerCharacterData.CurrentPosition = MapInstanceWarpToPosition;
                        if (MapInstanceWarpOverrideRotation)
                        {
                            playerCharacterData.CurrentRotation = MapInstanceWarpToRotation;
                        }
                    }

                    // Spawn character entity and set its data
                    Quaternion characterRotation = Quaternion.identity;
                    if (CurrentGameInstance.DimensionType == DimensionType.Dimension3D)
                    {
                        characterRotation = Quaternion.Euler(playerCharacterData.CurrentRotation);
                    }
                    GameObject spawnObj = Instantiate(entityPrefab.gameObject, playerCharacterData.CurrentPosition, characterRotation);
                    BasePlayerCharacterEntity playerCharacterEntity = spawnObj.GetComponent <BasePlayerCharacterEntity>();
                    playerCharacterData.CloneTo(playerCharacterEntity);
                    Assets.NetworkSpawn(spawnObj, 0, connectionId);

                    // Set currencies
                    // Gold
                    GoldResp getGoldResp = await DbServiceClient.GetGoldAsync(new GetGoldReq()
                    {
                        UserId = userId
                    });

                    playerCharacterEntity.UserGold = getGoldResp.Gold;
                    // Cash
                    CashResp getCashResp = await DbServiceClient.GetCashAsync(new GetCashReq()
                    {
                        UserId = userId
                    });

                    playerCharacterEntity.UserCash = getCashResp.Cash;

                    // Prepare saving location for this character
                    if (IsInstanceMap())
                    {
                        instanceMapCurrentLocations.TryAdd(playerCharacterEntity.ObjectId, new KeyValuePair <string, Vector3>(savingCurrentMapName, savingCurrentPosition));
                    }

                    // Set user Id
                    playerCharacterEntity.UserId = userId;

                    // Load user level
                    GetUserLevelResp getUserLevelResp = await DbServiceClient.GetUserLevelAsync(new GetUserLevelReq()
                    {
                        UserId = userId
                    });

                    playerCharacterEntity.UserLevel = getUserLevelResp.UserLevel;

                    // Load party data, if this map-server does not have party data
                    if (playerCharacterEntity.PartyId > 0)
                    {
                        if (!ServerPartyHandlers.ContainsParty(playerCharacterEntity.PartyId))
                        {
                            await LoadPartyRoutine(playerCharacterEntity.PartyId);
                        }
                        PartyData party;
                        if (ServerPartyHandlers.TryGetParty(playerCharacterEntity.PartyId, out party))
                        {
                            ServerGameMessageHandlers.SendSetPartyData(playerCharacterEntity.ConnectionId, party);
                            ServerGameMessageHandlers.SendAddPartyMembersToOne(playerCharacterEntity.ConnectionId, party);
                        }
                        else
                        {
                            playerCharacterEntity.ClearParty();
                        }
                    }

                    // Load guild data, if this map-server does not have guild data
                    if (playerCharacterEntity.GuildId > 0)
                    {
                        if (!ServerGuildHandlers.ContainsGuild(playerCharacterEntity.GuildId))
                        {
                            await LoadGuildRoutine(playerCharacterEntity.GuildId);
                        }
                        GuildData guild;
                        if (ServerGuildHandlers.TryGetGuild(playerCharacterEntity.GuildId, out guild))
                        {
                            playerCharacterEntity.GuildName = guild.guildName;
                            playerCharacterEntity.GuildRole = guild.GetMemberRole(playerCharacterEntity.Id);
                            ServerGameMessageHandlers.SendSetGuildData(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendAddGuildMembersToOne(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildMessage(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildRolesToOne(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildMemberRolesToOne(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildSkillLevelsToOne(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildGold(playerCharacterEntity.ConnectionId, guild);
                            ServerGameMessageHandlers.SendSetGuildLevelExpSkillPoint(playerCharacterEntity.ConnectionId, guild);
                        }
                        else
                        {
                            playerCharacterEntity.ClearGuild();
                        }
                    }

                    // 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;
                    }

                    // Summon saved mount entity
                    if (GameInstance.VehicleEntities.ContainsKey(playerCharacterData.MountDataId))
                    {
                        playerCharacterEntity.Mount(GameInstance.VehicleEntities[playerCharacterData.MountDataId]);
                    }

                    // Force make caches, to calculate current stats to fill empty slots items
                    playerCharacterEntity.ForceMakeCaches();
                    playerCharacterEntity.FillEmptySlots();

                    // Notify clients that this character is spawn or dead
                    if (!playerCharacterEntity.IsDead())
                    {
                        playerCharacterEntity.CallAllOnRespawn();
                    }
                    else
                    {
                        playerCharacterEntity.CallAllOnDead();
                    }

                    // Register player character entity to the server
                    RegisterPlayerCharacter(connectionId, playerCharacterEntity);
                }
            }
        }