Files
TenWholeYears.Server/src/RecNet.Domain/Profiles/Profile.cs
2026-06-20 22:58:33 -05:00

106 lines
2.8 KiB
C#

using RecNet.Domain.Common;
using RecNet.Domain.Exceptions;
namespace RecNet.Domain.Profiles;
public class Profile
{
public const int MinNameLength = 3;
public const int MaxNameLength = 32;
private Profile(
string name,
PlatformType platform,
string platformId)
{
SetName(name);
Platform = platform;
PlatformId = RequireValue(platformId, nameof(platformId));
CreatedAt = DateTimeOffset.UtcNow;
}
public Guid ProfileId { get; } = Guid.NewGuid();
public string Name { get; private set; } = string.Empty;
public PlatformType Platform { get; private set; }
public string PlatformId { get; private set; }
public List<string> DeviceIds { get; private set; } = [];
// EZ
public bool IsBanned { get; private set; }
public bool IsModerator { get; private set; }
public DateTimeOffset CreatedAt { get; private set; }
public Avatar Avatar { get; private set; } = Avatar.Empty;
public ICollection<PlayerSetting> Settings { get; } = [];
public static Profile Create(
string name,
PlatformType platform,
string platformId)
=> new(name, platform, platformId);
public void SetName(string name)
{
var requiredName = RequireValue(name, nameof(name));
if (requiredName.Length is < MinNameLength or > MaxNameLength)
throw new DomainException($"Username must be between {MinNameLength} and {MaxNameLength} characters long.");
Name = requiredName;
}
public void AddDeviceId(string deviceId)
{
deviceId = RequireValue(deviceId, nameof(deviceId));
if (DeviceIds.Contains(deviceId))
return;
DeviceIds.Add(deviceId);
}
public void RecordSuccessfulLogin(string deviceId, string? platformName)
{
if (!string.IsNullOrWhiteSpace(platformName) && Name != platformName)
SetName(platformName);
AddDeviceId(deviceId);
}
public void Ban()
=> IsBanned = true;
public void Unban()
=> IsBanned = false;
public void GrantModerator()
=> IsModerator = true;
public void RevokeModerator()
=> IsModerator = false;
public void SetAvatar(Avatar avatar)
=> Avatar = avatar ?? throw new DomainException("Avatar is required.");
public void SetSetting(string key, string value)
{
var existing = Settings.FirstOrDefault(x => x.Key == key);
if (existing is null)
{
Settings.Add(new PlayerSetting(ProfileId, key, value));
return;
}
existing.UpdateValue(value);
}
private static string RequireValue(string value, string parameterName)
=> !string.IsNullOrWhiteSpace(value) ? value : throw new DomainException($"{parameterName} is required.");
}