Пример #1
0
        //[AuthorizeJwt(GuardianApplicationRole.ZoneServer)] //TODO: Eventually we'll need to auth these zoneservers.
        public async Task <IActionResult> WorldTeleportCharacter([FromBody][NotNull] ZoneServerWorldTeleportCharacterRequest requestModel,
                                                                 [FromServices][NotNull] ICharacterLocationRepository characterLocationRepository,
                                                                 [FromServices][NotNull] IGameObjectBehaviourDataServiceClient <WorldTeleporterInstanceModel> worldTelporterDataClient,
                                                                 [FromServices][NotNull] IPlayerSpawnPointDataServiceClient playerSpawnDataClient)
        {
            if (requestModel == null)
            {
                throw new ArgumentNullException(nameof(requestModel));
            }
            if (characterLocationRepository == null)
            {
                throw new ArgumentNullException(nameof(characterLocationRepository));
            }
            if (worldTelporterDataClient == null)
            {
                throw new ArgumentNullException(nameof(worldTelporterDataClient));
            }
            if (playerSpawnDataClient == null)
            {
                throw new ArgumentNullException(nameof(playerSpawnDataClient));
            }

            //TODO: Right now there is no verification of WHO/WHAT is actually teleporting the player.
            //We need an authorization system with player-owned zone servers. So that we can determine
            //who is requesting to transfer the session and then verify that a player is even on
            //that zone server.
            ProjectVersionStage.AssertBeta();

            //We don't await so that we can get rolling on this VERY async multi-part process.
            //TODO: Handle failure
            ResponseModel <WorldTeleporterInstanceModel, SceneContentQueryResponseCode> teleporterInstanceResponse = await worldTelporterDataClient.GetBehaviourInstance(requestModel.WorldTeleporterId);

            //TODO: Handle failure
            ResponseModel <PlayerSpawnPointInstanceModel, SceneContentQueryResponseCode> pointInstanceResponse = await playerSpawnDataClient.GetSpawnPointInstance(teleporterInstanceResponse.Result.RemoteSpawnPointId);

            //Remove current location and update the new location.
            await characterLocationRepository.TryDeleteAsync(requestModel.CharacterGuid.EntityId);

            await characterLocationRepository.TryCreateAsync(new CharacterLocationModel(requestModel.CharacterGuid.EntityId,
                                                                                        pointInstanceResponse.Result.InitialPosition.x,
                                                                                        pointInstanceResponse.Result.InitialPosition.y,
                                                                                        pointInstanceResponse.Result.InitialPosition.z,
                                                                                        pointInstanceResponse.Result.WorldId));

            //TODO: Better indicate reason for failure.
            return(Ok());
        }
Пример #2
0
		public async Task<CharacterSessionEnterResponse> EnterSession([FromRoute(Name = "id")] int characterId,
			[FromServices] ICharacterLocationRepository characterLocationRepository,
			[FromServices] IZoneServerRepository zoneServerRepository)
		{
			if(!await IsCharacterIdValidForUser(characterId, CharacterRepository))
				return new CharacterSessionEnterResponse(CharacterSessionEnterResponseCode.InvalidCharacterIdError);

			int accountId = ClaimsReader.GetAccountIdInt(User);

			//This checks to see if the account, not just the character, has an active session.
			//We do this before we check anything to reject quick even though the query behind this
			//may be abit more expensive
			//As a note, this checks (or should) CLAIMED SESSIONS. So, it won't prevent multiple session entries for an account
			//This is good because we actually use the left over session data to re-enter the instances on disconnect.
			if(await CharacterSessionRepository.AccountHasActiveSession(accountId))
				return new CharacterSessionEnterResponse(CharacterSessionEnterResponseCode.AccountAlreadyHasCharacterSession);

			//They may have a session entry already, which is ok. So long as they don't have an active claimed session
			//which the above query checks for.
			bool hasSession = await CharacterSessionRepository.ContainsAsync(characterId);

			//We need to check active or not
			if (hasSession)
			{
				//It's possible that the session no longer matches the character's
				//persisted location. We should check their location and put them in the correct zone.

				//If it's active we can just retrieve the data and send them off on their way
				CharacterSessionModel sessionModel = await CharacterSessionRepository.RetrieveAsync(characterId, true);

				if (await characterLocationRepository.ContainsAsync(characterId))
				{
					CharacterLocationModel locationModel = await characterLocationRepository.RetrieveAsync(characterId);
					//They have a location, verify it matches the session
					if (locationModel.WorldId != sessionModel.ZoneEntry.WorldId)
					{
						//The location world and the session's world do not match, so remove the session.
						await CharacterSessionRepository.TryDeleteAsync(sessionModel.CharacterId);
					}
					else
						return new CharacterSessionEnterResponse(sessionModel.ZoneId);
				}
				else
					//TODO: Handle case when we have an inactive session that can be claimed
					return new CharacterSessionEnterResponse(sessionModel.ZoneId);
			}

			//If we didn't return above then we should be in a state where the below can handle this now.

			try
			{
				int targetSessionZoneId = 0;

				//TO know what zone we should connect to we need to check potential
				//character location.
				if (await characterLocationRepository.ContainsAsync(characterId))
				{
					CharacterLocationModel locationModel = await characterLocationRepository.RetrieveAsync(characterId);

					//We have no session so we need to find a zone that matches this server.
					ZoneInstanceEntryModel firstWithWorldId = await zoneServerRepository.FindFirstWithWorldId(locationModel.WorldId);

					//TODO: Should we request one be created for the user??
					//There is NO instance available for this world.
					if (firstWithWorldId == null)
					{
						//Location is basically invalid since there is no running world
						await characterLocationRepository.TryDeleteAsync(locationModel.CharacterId);
					}
					else
					{
						targetSessionZoneId = firstWithWorldId.ZoneId;
					}
				}

				//Try to get into any zone
				if (targetSessionZoneId == 0)
				{
					ZoneInstanceEntryModel entryModel = await zoneServerRepository.AnyAsync();

					if (entryModel != null)
						targetSessionZoneId = entryModel.ZoneId;
				}

				//Still zero means literally no zone servers are available.
				if(targetSessionZoneId == 0)
					return new CharacterSessionEnterResponse(CharacterSessionEnterResponseCode.GeneralServerError);

				if(await CharacterSessionRepository.TryCreateAsync(new CharacterSessionModel(characterId, targetSessionZoneId)))
					return new CharacterSessionEnterResponse(targetSessionZoneId);
				else
					return new CharacterSessionEnterResponse(CharacterSessionEnterResponseCode.GeneralServerError);

			}
			catch (Exception e)
			{
				if(Logger.IsEnabled(LogLevel.Error))
					Logger.LogError($"Character with ID: {characterId} failed to create session. Potentially no default world assigned or World with session was deleted and orphaned. Reason: {e.Message}");
				throw;
			}
		}