feat(svc): IArenaTwoPickService + response DTOs + GetTopAsync

6 response DTOs, IArenaTwoPickService interface + ArenaTwoPickException,
ArenaTwoPickService skeleton with GetTopAsync implemented and stubs for
Tasks 13-15. 3 NUnit tests for GetTopAsync all pass. DI: AddScoped.
This commit is contained in:
gamer147
2026-05-31 10:51:41 -04:00
parent a98b60dd36
commit ba49852c42
10 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
/// <summary>
/// What the battle controller needs from the service to build the standard battle-finish
/// envelope (class XP delta + post-state, spot points before/add/after, battle_result echo).
/// Achieved-info is built by the controller from IMissionAssembler.
/// </summary>
public class BattleFinishResultDto
{
public int BattleResult { get; set; }
public int GetClassExperience { get; set; }
public int ClassExperience { get; set; }
public int ClassLevel { get; set; }
public int BeforeSpotPoint { get; set; }
public int AddSpotPoint { get; set; }
public int AfterSpotPoint { get; set; }
}

View File

@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
[MessagePackObject]
public class CardChooseResponseDto
{
[JsonPropertyName("deck_info")] [Key("deck_info")]
public DeckInfoDto DeckInfo { get; set; } = new();
[JsonPropertyName("candidate_card_list")] [Key("candidate_card_list")]
public List<CandidatePairDto>? CandidateCardList { get; set; }
}

View File

@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
[MessagePackObject]
public class ClassChooseResponseDto
{
[JsonPropertyName("class_info")] [Key("class_info")]
public ClassInfoDto ClassInfo { get; set; } = new();
[JsonPropertyName("deck_info")] [Key("deck_info")]
public DeckInfoDto DeckInfo { get; set; } = new();
[JsonPropertyName("candidate_card_list")] [Key("candidate_card_list")]
public List<CandidatePairDto> CandidateCardList { get; set; } = new();
}

View File

@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
[MessagePackObject]
public class EntryResponseDto
{
[JsonPropertyName("entry_info")] [Key("entry_info")]
public EntryInfoDto EntryInfo { get; set; } = new();
[JsonPropertyName("reward_list")] [Key("reward_list")]
public List<RewardEntryDto> RewardList { get; set; } = new();
[JsonPropertyName("candidate_class_ids")] [Key("candidate_class_ids")]
public List<int> CandidateClassIds { get; set; } = new();
[JsonPropertyName("battle_results")] [Key("battle_results")]
public BattleResultsDto BattleResults { get; set; } = new();
}

View File

@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
[MessagePackObject]
public class FinishResponseDto
{
/// <summary>Per-grant deltas — drives "+N received" popup.</summary>
[JsonPropertyName("rewards")] [Key("rewards")]
public List<RewardEntryDto> Rewards { get; set; } = new();
/// <summary>Post-state totals — drives PlayerStaticData.UpdateHaveUserGoodsNumByJsonData.</summary>
[JsonPropertyName("reward_list")] [Key("reward_list")]
public List<RewardEntryDto> RewardList { get; set; } = new();
}

View File

@@ -0,0 +1,25 @@
using System.Text.Json.Serialization;
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
[MessagePackObject]
public class TopResponseDto
{
[JsonPropertyName("entry_info")] [JsonIgnore(Condition = JsonIgnoreCondition.Never)] [Key("entry_info")]
public EntryInfoDto? EntryInfo { get; set; }
[JsonPropertyName("battle_results")] [Key("battle_results")]
public BattleResultsDto? BattleResults { get; set; }
[JsonPropertyName("class_info")] [Key("class_info")]
public ClassInfoDto? ClassInfo { get; set; }
[JsonPropertyName("deck_info")] [Key("deck_info")]
public DeckInfoDto? DeckInfo { get; set; }
[JsonPropertyName("leader_skin_id")] [JsonConverter(typeof(StringifiedLongConverter))] [Key("leader_skin_id")]
public long? LeaderSkinId { get; set; }
}

View File

@@ -105,6 +105,7 @@ public class Program
builder.Services.AddScoped<IViewerStoryProgressRepository, ViewerStoryProgressRepository>();
builder.Services.AddScoped<IArenaTwoPickRunRepository, ArenaTwoPickRunRepository>();
builder.Services.AddScoped<IArenaTwoPickCardPoolService, ArenaTwoPickCardPoolService>();
builder.Services.AddScoped<IArenaTwoPickService, ArenaTwoPickService>();
builder.Services.AddScoped<IStoryService, StoryService>();
builder.Services.AddScoped<IDeckListBuilder, DeckListBuilder>();
builder.Services.AddSingleton<IRandom, SystemRandom>();

View File

@@ -0,0 +1,113 @@
using System.Text.Json;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Globals;
using SVSim.Database.Repositories.Viewer;
using SVSim.Database.Services;
using SVSim.EmulatedEntrypoint.Models.Dtos.Common.ArenaTwoPick;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Services;
public class ArenaTwoPickService : IArenaTwoPickService
{
private readonly IArenaTwoPickRunRepository _runs;
private readonly IArenaTwoPickRewardRepository _rewards;
private readonly IArenaTwoPickCardPoolService _pool;
private readonly IGameConfigService _config;
private readonly IViewerRepository _viewers;
private readonly RewardGrantService _grants;
private readonly IViewerEntitlements _entitlements;
private readonly IRandom _rng;
private readonly SVSimDbContext _db;
public ArenaTwoPickService(
IArenaTwoPickRunRepository runs,
IArenaTwoPickRewardRepository rewards,
IArenaTwoPickCardPoolService pool,
IGameConfigService config,
IViewerRepository viewers,
RewardGrantService grants,
IViewerEntitlements entitlements,
IRandom rng,
SVSimDbContext db)
{
_runs = runs; _rewards = rewards; _pool = pool; _config = config;
_viewers = viewers; _grants = grants; _entitlements = entitlements; _rng = rng; _db = db;
}
public async Task<TopResponseDto> GetTopAsync(long viewerId)
{
var run = await _runs.GetByViewerIdAsync(viewerId);
if (run is null) return new TopResponseDto { EntryInfo = null };
var dto = new TopResponseDto
{
EntryInfo = ProjectEntryInfo(run, viewerId),
BattleResults = ProjectBattleResults(run),
};
if (run.ClassId != 0)
{
dto.ClassInfo = ProjectClassInfo(run);
dto.DeckInfo = ProjectDeckInfo(run);
if (run.WinCount > 0 || run.LossCount > 0)
dto.LeaderSkinId = run.LeaderSkinId;
}
return dto;
}
public Task<EntryResponseDto> EntryAsync(long viewerId, int consumeItemType) => throw new NotImplementedException();
public Task<ClassChooseResponseDto> ChooseClassAsync(long viewerId, int classId) => throw new NotImplementedException();
public Task<CardChooseResponseDto> ChooseCardAsync(long viewerId, long selectedId) => throw new NotImplementedException();
public Task<FinishResponseDto> RetireAsync(long viewerId) => throw new NotImplementedException();
public Task<FinishResponseDto> FinishAsync(long viewerId) => throw new NotImplementedException();
public Task<BattleFinishResultDto> RecordBattleResultAsync(long viewerId, bool isWin) => throw new NotImplementedException();
// --- projection helpers (kept internal so test subclasses could exercise if needed) ---
internal static EntryInfoDto ProjectEntryInfo(ViewerArenaTwoPickRun run, long viewerId) => new()
{
Id = run.EntryId,
ViewerId = viewerId,
RewardScheduleId = run.RewardScheduleId,
ChallengeId = run.ChallengeId,
MaxBattleCount = run.MaxBattleCount,
LeaderSkinId = run.LeaderSkinId,
IsRetire = run.IsRetire ? 1 : 0,
};
internal static BattleResultsDto ProjectBattleResults(ViewerArenaTwoPickRun run)
{
var bools = JsonSerializer.Deserialize<List<bool>>(run.ResultListJson) ?? new();
return new()
{
ResultList = bools.Select(b => b ? 1 : 0).ToList(),
WinCount = run.WinCount,
};
}
internal static ClassInfoDto ProjectClassInfo(ViewerArenaTwoPickRun run)
{
var ids = JsonSerializer.Deserialize<List<int>>(run.CandidateClassIdsJson) ?? new();
return new()
{
ClassId1 = ids.ElementAtOrDefault(0),
ClassId2 = ids.ElementAtOrDefault(1),
ClassId3 = ids.ElementAtOrDefault(2),
SelectedClassId = run.ClassId,
};
}
internal static DeckInfoDto ProjectDeckInfo(ViewerArenaTwoPickRun run)
{
var cards = JsonSerializer.Deserialize<List<long>>(run.SelectedCardIdsJson) ?? new();
return new()
{
TwoPickEntryId = run.EntryId,
ClassId = run.ClassId,
IsSelectCompleted = run.IsSelectCompleted,
SelectedCardIds = cards,
SelectTurn = run.SelectTurn == 0 ? 1 : run.SelectTurn,
};
}
}

View File

@@ -0,0 +1,20 @@
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick;
namespace SVSim.EmulatedEntrypoint.Services;
public interface IArenaTwoPickService
{
Task<TopResponseDto> GetTopAsync(long viewerId);
Task<EntryResponseDto> EntryAsync(long viewerId, int consumeItemType);
Task<ClassChooseResponseDto> ChooseClassAsync(long viewerId, int classId);
Task<CardChooseResponseDto> ChooseCardAsync(long viewerId, long selectedId);
Task<FinishResponseDto> RetireAsync(long viewerId);
Task<FinishResponseDto> FinishAsync(long viewerId);
Task<BattleFinishResultDto> RecordBattleResultAsync(long viewerId, bool isWin);
}
public class ArenaTwoPickException : Exception
{
public string ErrorCode { get; }
public ArenaTwoPickException(string errorCode) : base(errorCode) { ErrorCode = errorCode; }
}

View File

@@ -0,0 +1,87 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Viewer;
using SVSim.EmulatedEntrypoint.Services;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Services;
public class ArenaTwoPickServiceTopTests
{
private static async Task<(SVSimDbContext, IArenaTwoPickRunRepository)> SetupAsync()
{
var factory = new SVSimTestFactory();
var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await db.Database.EnsureCreatedAsync();
return (db, new ArenaTwoPickRunRepository(db));
}
[Test]
public async Task GetTopAsync_returns_null_entry_info_when_no_run()
{
var (db, runRepo) = await SetupAsync();
await using var _ = db;
var svc = BuildService(db, runRepo);
var dto = await svc.GetTopAsync(viewerId: 99);
Assert.That(dto.EntryInfo, Is.Null);
Assert.That(dto.ClassInfo, Is.Null);
Assert.That(dto.DeckInfo, Is.Null);
}
[Test]
public async Task GetTopAsync_after_entry_omits_class_info_and_deck_info()
{
var (db, runRepo) = await SetupAsync();
await using var _ = db;
await runRepo.UpsertAsync(new ViewerArenaTwoPickRun
{
ViewerId = 99, EntryId = 1234, RewardScheduleId = 1, ChallengeId = 1,
MaxBattleCount = 7, ClassId = 0,
CandidateClassIdsJson = "[1,7,8]",
CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow,
});
var svc = BuildService(db, runRepo);
var dto = await svc.GetTopAsync(viewerId: 99);
Assert.That(dto.EntryInfo, Is.Not.Null);
Assert.That(dto.EntryInfo!.Id, Is.EqualTo(1234));
Assert.That(dto.ClassInfo, Is.Null);
Assert.That(dto.DeckInfo, Is.Null);
Assert.That(dto.BattleResults!.WinCount, Is.EqualTo(0));
}
[Test]
public async Task GetTopAsync_during_card_select_emits_class_and_deck_info()
{
var (db, runRepo) = await SetupAsync();
await using var _ = db;
await runRepo.UpsertAsync(new ViewerArenaTwoPickRun
{
ViewerId = 99, EntryId = 1234, MaxBattleCount = 7, ClassId = 1, LeaderSkinId = 1,
CandidateClassIdsJson = "[1,7,8]",
SelectTurn = 5, IsSelectCompleted = false,
SelectedCardIdsJson = "[100111010,100121010,100131010,100141010,100114010,100124010,100134010,100144010]",
CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow,
});
var svc = BuildService(db, runRepo);
var dto = await svc.GetTopAsync(viewerId: 99);
Assert.That(dto.ClassInfo, Is.Not.Null);
Assert.That(dto.ClassInfo!.ClassId1, Is.EqualTo(1));
Assert.That(dto.DeckInfo, Is.Not.Null);
Assert.That(dto.DeckInfo!.SelectTurn, Is.EqualTo(5));
Assert.That(dto.DeckInfo.SelectedCardIds.Count, Is.EqualTo(8));
}
private static IArenaTwoPickService BuildService(SVSimDbContext db, IArenaTwoPickRunRepository runRepo)
{
// GetTopAsync only uses _runs — every other dep can be null! because the test path
// never touches them. The 9th positional arg (db) is required from Task 13 onward.
return new ArenaTwoPickService(runRepo, null!, null!, null!, null!, null!, null!, null!, db);
}
}