feat(svc): EntryAsync (ticket debit + run insert + candidate classes)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-31 10:59:05 -04:00
parent ba49852c42
commit d550f66481
2 changed files with 226 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Globals;
@@ -56,7 +57,91 @@ public class ArenaTwoPickService : IArenaTwoPickService
return dto;
}
public Task<EntryResponseDto> EntryAsync(long viewerId, int consumeItemType) => throw new NotImplementedException();
public async Task<EntryResponseDto> EntryAsync(long viewerId, int consumeItemType)
{
if (await _runs.GetByViewerIdAsync(viewerId) is not null)
throw new ArenaTwoPickException("arena_two_pick_already_in_progress");
const long ticketItemId = 80001;
var viewer = await LoadViewerForGrantsAsync(viewerId);
var ticket = viewer.Items.FirstOrDefault(i => i.Item.Id == (int)ticketItemId);
int postStateTickets;
if (_entitlements.IsFreeplay)
{
postStateTickets = ticket?.Count ?? 0;
}
else
{
if (ticket is null || ticket.Count < 1)
throw new ArenaTwoPickException("insufficient_ticket");
ticket.Count -= 1;
postStateTickets = ticket.Count;
}
var aCfg = _config.Get<SVSim.Database.Models.Config.ArenaTwoPickConfig>();
var maxWins = Math.Max(1, await _rewards.GetMaxWinCountAsync());
var candidates = SampleCandidateClasses(aCfg.AllowedClassIds, _rng);
var run = new ViewerArenaTwoPickRun
{
ViewerId = viewerId,
EntryId = 0,
RewardScheduleId = aCfg.RewardScheduleId,
ChallengeId = aCfg.ChallengeId,
MaxBattleCount = maxWins,
ClassId = 0,
LeaderSkinId = 0,
CandidateClassIdsJson = JsonSerializer.Serialize(candidates),
SelectTurn = 0,
IsSelectCompleted = false,
SelectedCardIdsJson = "[]",
PendingPickSetsJson = "[]",
NextCandidateId = 1,
ResultListJson = "[]",
WinCount = 0,
LossCount = 0,
IsRetire = false,
};
await _runs.UpsertAsync(run);
run.EntryId = run.Id;
await _runs.UpsertAsync(run);
await _db.SaveChangesAsync();
return new EntryResponseDto
{
EntryInfo = ProjectEntryInfo(run, viewerId),
RewardList = new List<RewardEntryDto>
{
new RewardEntryDto { RewardType = 4, RewardId = ticketItemId, RewardNum = postStateTickets },
},
CandidateClassIds = candidates,
BattleResults = new BattleResultsDto { WinCount = 0, ResultList = new List<int>() },
};
}
private static List<int> SampleCandidateClasses(List<int> allowed, IRandom rng)
{
if (allowed.Count < 3)
throw new InvalidOperationException("ArenaTwoPickConfig.AllowedClassIds needs ≥3 entries");
var shuffled = allowed.OrderBy(_ => rng.Next(int.MaxValue)).ToList();
return shuffled.Take(3).ToList();
}
private async Task<SVSim.Database.Models.Viewer> LoadViewerForGrantsAsync(long viewerId)
{
return await _db.Viewers
.Include(v => v.Currency)
.Include(v => v.Items).ThenInclude(i => i.Item)
.Include(v => v.Cards)
.Include(v => v.Sleeves)
.Include(v => v.Emblems)
.Include(v => v.Degrees)
.Include(v => v.LeaderSkins)
.Include(v => v.MyPageBackgrounds)
.AsSplitQuery()
.FirstAsync(v => v.Id == viewerId);
}
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();