Files
systemrevivalserver/Services/Characters/CharacterService.cs
T
2025-12-06 11:20:32 -05:00

83 lines
2.5 KiB
C#

// Server.Services.Characters/CharacterService.cs
using Microsoft.Extensions.Logging;
using Server.Data;
namespace Server.Services.Characters;
public sealed class CharacterService : ICharacterService
{
private readonly ILogger<CharacterService> _log;
private readonly ICharactersRepository _repo;
public CharacterService(ILogger<CharacterService> log, ICharactersRepository repo)
{ _log = log; _repo = repo; }
public async Task<(bool ok, string? error, Old_Character[] list)> ListAsync(ulong accountId, CancellationToken ct)
{
var one = await _repo.FindByAccountAsync(accountId, ct);
return (true, null, one is null ? Array.Empty<Old_Character>() : new[] { one });
}
public async Task<(bool ok, string? error, Old_Character created)> CreateAsync(ulong accountId, string name, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(name) || name.Length is < 3 or > 32)
return (false, "invalid_name", null!);
name = name.Trim();
// 1 char / account
if (await _repo.FindByAccountAsync(accountId, ct) is not null)
return (false, "char_limit", null!);
// unique names
if (await _repo.FindByNameAsync(name, ct) is not null)
return (false, "name_taken", null!);
var now = DateTime.UtcNow;
var c = new Old_Character
{
AccountId = accountId,
Name = name,
Level = 1,
Xp = 0,
StatStrength = 1,
StatDexterity = 1,
StatWisdom = 1,
StatIntelligence = 1,
StatEndurance = 1,
HpCurrent = 10,
HpMax = 10,
MpCurrent = 0,
MpMax = 0,
StaminaCurrent = 0,
StaminaMax = 0,
ZoneId = 1,
PosX = 0,
PosY = 0,
PosZ = 0,
FacingDeg = 0f,
AppearanceJson = null,
CreatedAt = now,
UpdatedAt = now
};
var created = await _repo.AddAsync(c, ct);
_log.LogInformation("Character {Id} '{Name}' created for account {Acct}", created.Id, created.Name, accountId);
return (true, null, created);
}
public async Task<(bool ok, string? error, Old_Character selected)> SelectAsync(ulong accountId, ulong characterId, CancellationToken ct)
{
var c = await _repo.FindByIdAsync(characterId, ct);
if (c is null || c.AccountId != accountId)
return (false, "char_not_found", null!);
return (true, null, c);
}
}