Ejemplo n.º 1
0
        protected virtual void ApplyItemPetSummon(Item item, short level, int exp)
        {
            if (IsDead() || !IsServer || item == null || level <= 0)
            {
                return;
            }
            // Clear all summoned pets
            CharacterSummon tempSummon;

            for (int i = 0; i < Summons.Count; ++i)
            {
                tempSummon = summons[i];
                if (tempSummon.type != SummonType.Pet)
                {
                    continue;
                }
                summons.RemoveAt(i);
                tempSummon.UnSummon(this);
            }
            // Summon new pet
            CharacterSummon newSummon = CharacterSummon.Create(SummonType.Pet, item.DataId);

            newSummon.Summon(this, level, 0f, exp);
            summons.Add(newSummon);
        }
Ejemplo n.º 2
0
        protected virtual void ApplySkillSummon(Skill skill, short level)
        {
            if (IsDead() || !IsServer || skill == null || level <= 0)
            {
                return;
            }
            int i = 0;
            int amountEachTime = skill.summon.amountEachTime.GetAmount(level);

            for (i = 0; i < amountEachTime; ++i)
            {
                CharacterSummon newSummon = CharacterSummon.Create(SummonType.Skill, skill.DataId);
                newSummon.Summon(this, skill.summon.level.GetAmount(level), skill.summon.duration.GetAmount(level));
                summons.Add(newSummon);
            }
            int count = 0;

            for (i = 0; i < summons.Count; ++i)
            {
                if (summons[i].dataId == skill.DataId)
                {
                    ++count;
                }
            }
            int             maxStack       = skill.summon.maxStack.GetAmount(level);
            int             unSummonAmount = count > maxStack ? count - maxStack : 0;
            CharacterSummon tempSummon;

            for (i = unSummonAmount; i > 0; --i)
            {
                int summonIndex = this.IndexOfSummon(skill.DataId, SummonType.Skill);
                tempSummon = summons[summonIndex];
                if (summonIndex >= 0)
                {
                    summons.RemoveAt(summonIndex);
                    tempSummon.UnSummon(this);
                }
            }
        }
        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);
                }
            }
        }
        public override void DeserializeClientReadyExtra(LiteNetLibIdentity playerIdentity, long connectionId, NetDataReader reader)
        {
            if (LogDev)
            {
                Debug.Log("[LanRpgNetworkManager] Deserializing client ready extra");
            }
            PlayerCharacterData       playerCharacterData = new PlayerCharacterData().DeserializeCharacterData(reader);
            BasePlayerCharacterEntity entityPrefab        = playerCharacterData.GetEntityPrefab() as BasePlayerCharacterEntity;

            // If it is not allow this character data, disconnect user
            if (entityPrefab == null)
            {
                Debug.LogError("[LanRpgNetworkManager] Cannot find player character with entity Id: " + playerCharacterData.EntityId);
                return;
            }
            if (!CurrentMapInfo.Id.Equals(playerCharacterData.CurrentMapName))
            {
                playerCharacterData.CurrentPosition = teleportPosition.HasValue ? teleportPosition.Value : CurrentMapInfo.startPosition;
            }
            GameObject spawnObj = Instantiate(entityPrefab.gameObject, playerCharacterData.CurrentPosition, Quaternion.identity);
            BasePlayerCharacterEntity playerCharacterEntity = spawnObj.GetComponent <BasePlayerCharacterEntity>();

            playerCharacterData.CloneTo(playerCharacterEntity);
            Assets.NetworkSpawn(spawnObj, 0, connectionId);

            // Set user Id
            playerCharacterEntity.UserId = playerCharacterEntity.Id;

            // Enable GM commands in Singleplayer / LAN mode
            // TODO: Don't use fixed user level
            if (enableGmCommands == EnableGmCommandType.Everyone)
            {
                playerCharacterEntity.UserLevel = 1;
            }

            // Load world / storage for first character (host)
            if (playerCharacters.Count == 0)
            {
                if (enableGmCommands == EnableGmCommandType.HostOnly)
                {
                    playerCharacterEntity.UserLevel = 1;
                }

                // Load and Spawn buildings
                worldSaveData.LoadPersistentData(playerCharacterEntity.Id, playerCharacterEntity.CurrentMapName);
                foreach (BuildingSaveData building in worldSaveData.buildings)
                {
                    CreateBuildingEntity(building, true);
                }
                // Load storage data
                storageSaveData.LoadPersistentData(playerCharacterEntity.Id);
                foreach (StorageCharacterItem storageItem in storageSaveData.storageItems)
                {
                    StorageId storageId = new StorageId(storageItem.storageType, storageItem.storageOwnerId);
                    if (!storageItems.ContainsKey(storageId))
                    {
                        storageItems[storageId] = new List <CharacterItem>();
                    }
                    storageItems[storageId].Add(storageItem.characterItem);
                }
                // Spawn harvestables
                HarvestableSpawnArea[] harvestableSpawnAreas = FindObjectsOfType <HarvestableSpawnArea>();
                foreach (HarvestableSpawnArea harvestableSpawnArea in harvestableSpawnAreas)
                {
                    harvestableSpawnArea.SpawnAll();
                }
            }

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

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

            // Register player, will use registered player to send chat / player messages
            RegisterPlayerCharacter(connectionId, playerCharacterEntity);
        }
Ejemplo n.º 5
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);
                            }
                        }
                    }
                }
            }
        }