159 lines
5.2 KiB
C#
159 lines
5.2 KiB
C#
using AutoMapper;
|
|
using RecNet.Application.Common.Interfaces;
|
|
using RecNet.Application.Common.Security;
|
|
using RecNet.Domain.Common;
|
|
using RecNet.Domain.GameVersions;
|
|
using RecNet.Domain.Profiles;
|
|
using ProfileEntity = RecNet.Domain.Profiles.Profile;
|
|
|
|
namespace RecNet.Application.Profiles;
|
|
|
|
public class ProfileService(
|
|
IEnumerable<IPlatformAuthValidator> validators,
|
|
IGameVersionRepository gameVersionRepository,
|
|
IProfileRepository profileRepository,
|
|
IConfigService configService,
|
|
NameGenerator nameGenerator,
|
|
ITokenService tokenService,
|
|
IMapper mapper) : IProfileService
|
|
{
|
|
public async Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default)
|
|
{
|
|
var profile = await profileRepository.GetByIdAsync(profileId, ct);
|
|
|
|
return profile is null
|
|
? null
|
|
: mapper.Map<ProfileDTO>(profile);
|
|
}
|
|
|
|
public async Task<AvatarDTO?> GetAvatarAsync(Guid profileId, CancellationToken ct = default)
|
|
{
|
|
var avatar = await profileRepository.GetAvatarByProfileIdAsync(profileId, ct);
|
|
|
|
return avatar is null
|
|
? null
|
|
: mapper.Map<AvatarDTO>(avatar);
|
|
}
|
|
|
|
public async Task<AvatarDTO?> UpdateAvatarAsync(
|
|
Guid profileId,
|
|
UpdateAvatarCommand command,
|
|
CancellationToken ct = default)
|
|
{
|
|
var profile = await profileRepository.GetByIdAsync(profileId, ct);
|
|
if (profile is null)
|
|
return null;
|
|
|
|
profile.SetAvatar(
|
|
Avatar.Create(
|
|
command.OutfitSelections,
|
|
command.SkinColor,
|
|
command.HairColor
|
|
)
|
|
);
|
|
|
|
await profileRepository.SaveChangesAsync(ct);
|
|
|
|
return mapper.Map<AvatarDTO>(profile.Avatar);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<PlayerSettingDTO>> GetPlayerSettingsAsync(
|
|
Guid profileId,
|
|
CancellationToken ct = default)
|
|
{
|
|
var settings = await profileRepository.GetSettingsByProfileIdAsync(profileId, ct);
|
|
|
|
return mapper.Map<List<PlayerSettingDTO>>(settings);
|
|
}
|
|
|
|
public async Task<bool> UpdatePlayerSettingAsync(
|
|
Guid profileId,
|
|
UpdatePlayerSettingCommand command,
|
|
CancellationToken ct = default)
|
|
{
|
|
var profile = await profileRepository.GetByIdWithSettingsAsync(profileId, ct);
|
|
if (profile is null)
|
|
return false;
|
|
|
|
profile.SetSetting(command.Key, command.Value);
|
|
|
|
await profileRepository.SaveChangesAsync(ct);
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(
|
|
List<Guid> profileIds,
|
|
CancellationToken ct = default)
|
|
{
|
|
var profiles = await profileRepository.GetByIdsAsync(profileIds, ct);
|
|
|
|
return mapper.Map<List<ProfileDTO>>(profiles);
|
|
}
|
|
|
|
public async Task<LoginProfileResult> LoginAsync(
|
|
LoginProfileCommand command,
|
|
CancellationToken ct = default)
|
|
{
|
|
// 1. Validate Game Version
|
|
if (string.IsNullOrWhiteSpace(command.AppVersion))
|
|
return LoginProfileResult.Fail("App version is required.");
|
|
|
|
var isValidGameVersion = await gameVersionRepository.IsValidAsync(command.AppVersion, ct);
|
|
if (!isValidGameVersion)
|
|
return LoginProfileResult.Fail("Invalid app version.");
|
|
|
|
// 2. Validate Platform Authentication
|
|
var validator = validators.FirstOrDefault(x => x.PlatformType == command.PlatformType);
|
|
if (validator is null)
|
|
return LoginProfileResult.Fail("Invalid platform type");
|
|
|
|
var ignoreAuthValidation = await configService.GetAsync("Profiles:IgnoreAuthValidation", true, ct);
|
|
|
|
var auth = await validator.ValidateAsync(
|
|
command.PlatformAuthentication,
|
|
command.PlatformId,
|
|
ct);
|
|
if (!auth.Succeeded && !ignoreAuthValidation)
|
|
return LoginProfileResult.Fail($"Platform Auth Failed: {auth.Message}");
|
|
|
|
// 3. Get or Create Profile
|
|
var profile = await profileRepository.GetByPlatform(
|
|
command.PlatformType,
|
|
command.PlatformId,
|
|
ct);
|
|
|
|
if (profile is null)
|
|
{
|
|
var name = await nameGenerator.GenerateAsync(ct);
|
|
|
|
profile = ProfileEntity.Create(
|
|
name,
|
|
command.PlatformType,
|
|
command.PlatformId);
|
|
|
|
if (command.PlatformType == PlatformType.Meta)
|
|
profile.SetMetaAuthenticationSecret(Hashing.ComputeSha256(command.PlatformAuthentication));
|
|
|
|
await profileRepository.AddAsync(profile, ct);
|
|
}
|
|
|
|
if (command.PlatformType == PlatformType.Meta && !Hashing.VerifySha256(command.PlatformAuthentication, profile.MetaAuthenticationSecret))
|
|
return LoginProfileResult.Fail("Platform Auth Failed: Invalid authentication");
|
|
|
|
if (profile.IsBanned)
|
|
return LoginProfileResult.Fail("Profile is banned");
|
|
|
|
profile.RecordSuccessfulLogin(command.DeviceId, auth.Name);
|
|
|
|
await profileRepository.SaveChangesAsync(ct);
|
|
|
|
var token = tokenService.GenerateProfileToken(profile);
|
|
|
|
return LoginProfileResult.Success(
|
|
profile: mapper.Map<ProfileDTO>(profile),
|
|
accessToken: token.AccessToken,
|
|
expiresIn: token.ExpiresIn
|
|
);
|
|
}
|
|
} |