User clarified: the 0..7 win reward tiers came from Shadowverse Worlds Beyond (sequel), not the original game we're emulating. Original SV's Take Two caps at 5 total battles played and has no loss limit (verified on prod: queueing continues with 2+ losses). - arena-two-pick-rewards.json: drop 6w + 7w tiers (12 rows remain). - ArenaTwoPickConfig: remove MaxLosses property. - ArenaTwoPickService: termination is now battlesPlayed >= maxBattles (5 from MAX(reward.WinCount)). RecordBattleResult no longer flips IsSelectCompleted on the 2nd loss. - ResolveMaxBattleCountAsync empty-catalog default 7 → 5. - Tests updated for the new counts (16 → 12 rows, max 7 → 5). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
431 lines
17 KiB
C#
431 lines
17 KiB
C#
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
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;
|
|
private readonly ICurrencySpendService _spend;
|
|
|
|
public ArenaTwoPickService(
|
|
IArenaTwoPickRunRepository runs,
|
|
IArenaTwoPickRewardRepository rewards,
|
|
IArenaTwoPickCardPoolService pool,
|
|
IGameConfigService config,
|
|
IViewerRepository viewers,
|
|
RewardGrantService grants,
|
|
IViewerEntitlements entitlements,
|
|
IRandom rng,
|
|
SVSimDbContext db,
|
|
ICurrencySpendService spend)
|
|
{
|
|
_runs = runs; _rewards = rewards; _pool = pool; _config = config;
|
|
_viewers = viewers; _grants = grants; _entitlements = entitlements; _rng = rng; _db = db;
|
|
_spend = spend;
|
|
}
|
|
|
|
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 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");
|
|
|
|
var aCfg = _config.Get<SVSim.Database.Models.Config.ArenaTwoPickConfig>();
|
|
var viewer = await LoadViewerForGrantsAsync(viewerId);
|
|
|
|
// Dispatch on the client's chosen payment method (ArenaData.eARENA_PAY).
|
|
RewardEntryDto? feeEntry = consumeItemType switch
|
|
{
|
|
1 => await DebitCrystalsAsync(viewer, aCfg.CrystalCost),
|
|
3 => DebitTicket(viewer, aCfg.TicketItemId, aCfg.TicketCost),
|
|
4 => await DebitRupiesAsync(viewer, aCfg.RupyCost),
|
|
5 => null, // Free entry — no fee.
|
|
_ => throw new ArenaTwoPickException("invalid_consume_item_type"),
|
|
};
|
|
|
|
var maxWins = await ResolveMaxBattleCountAsync();
|
|
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();
|
|
|
|
var rewardList = feeEntry is null ? new List<RewardEntryDto>() : new List<RewardEntryDto> { feeEntry };
|
|
|
|
return new EntryResponseDto
|
|
{
|
|
EntryInfo = ProjectEntryInfo(run, viewerId),
|
|
RewardList = rewardList,
|
|
CandidateClassIds = candidates,
|
|
BattleResults = new BattleResultsDto { WinCount = 0, ResultList = new List<int>() },
|
|
};
|
|
}
|
|
|
|
private RewardEntryDto DebitTicket(SVSim.Database.Models.Viewer viewer, int ticketItemId, int ticketCost)
|
|
{
|
|
var ticket = viewer.Items.FirstOrDefault(i => i.Item.Id == ticketItemId);
|
|
int postStateCount;
|
|
if (_entitlements.IsFreeplay)
|
|
{
|
|
postStateCount = ticket?.Count ?? 0;
|
|
}
|
|
else
|
|
{
|
|
if (ticket is null || ticket.Count < ticketCost)
|
|
throw new ArenaTwoPickException("insufficient_ticket");
|
|
ticket.Count -= ticketCost;
|
|
postStateCount = ticket.Count;
|
|
}
|
|
return new RewardEntryDto
|
|
{
|
|
RewardType = (int)SVSim.Database.Enums.UserGoodsType.Item,
|
|
RewardId = ticketItemId,
|
|
RewardNum = postStateCount,
|
|
};
|
|
}
|
|
|
|
private async Task<RewardEntryDto> DebitCrystalsAsync(SVSim.Database.Models.Viewer viewer, int cost)
|
|
{
|
|
var result = await _spend.TrySpendAsync(viewer, SVSim.Database.Services.SpendCurrency.Crystal, cost);
|
|
if (!result.Success)
|
|
throw new ArenaTwoPickException("insufficient_crystal");
|
|
return new RewardEntryDto
|
|
{
|
|
RewardType = (int)SVSim.Database.Enums.UserGoodsType.Crystal,
|
|
RewardId = 0,
|
|
RewardNum = (int)result.PostStateTotal,
|
|
};
|
|
}
|
|
|
|
private async Task<RewardEntryDto> DebitRupiesAsync(SVSim.Database.Models.Viewer viewer, int cost)
|
|
{
|
|
var result = await _spend.TrySpendAsync(viewer, SVSim.Database.Services.SpendCurrency.Rupee, cost);
|
|
if (!result.Success)
|
|
throw new ArenaTwoPickException("insufficient_rupy");
|
|
return new RewardEntryDto
|
|
{
|
|
RewardType = (int)SVSim.Database.Enums.UserGoodsType.Rupy,
|
|
RewardId = 0,
|
|
RewardNum = (int)result.PostStateTotal,
|
|
};
|
|
}
|
|
|
|
private async Task<int> ResolveMaxBattleCountAsync()
|
|
{
|
|
var rawMaxWins = await _rewards.GetMaxWinCountAsync();
|
|
if (rawMaxWins == 0)
|
|
{
|
|
Console.Error.WriteLine("[ArenaTwoPickService] ArenaTwoPickRewards catalog empty; defaulting MaxBattleCount=5. Run SVSim.Bootstrap to seed.");
|
|
return 5;
|
|
}
|
|
return rawMaxWins;
|
|
}
|
|
|
|
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)
|
|
.Include(v => v.Classes).ThenInclude(c => c.Class)
|
|
.AsSplitQuery()
|
|
.FirstAsync(v => v.Id == viewerId);
|
|
}
|
|
public async Task<ClassChooseResponseDto> ChooseClassAsync(long viewerId, int classId)
|
|
{
|
|
var run = await _runs.GetByViewerIdAsync(viewerId)
|
|
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
|
|
if (run.ClassId != 0)
|
|
throw new ArenaTwoPickException("arena_two_pick_invalid_state");
|
|
var candidates = JsonSerializer.Deserialize<List<int>>(run.CandidateClassIdsJson) ?? new();
|
|
if (!candidates.Contains(classId))
|
|
throw new ArenaTwoPickException("arena_two_pick_class_not_offered");
|
|
|
|
run.ClassId = classId;
|
|
run.LeaderSkinId = ResolveClassDefaultLeaderSkin(classId);
|
|
var pairs = _pool.GeneratePickSetsForTurn(classId, turn: 1, startingPairId: run.NextCandidateId, _rng);
|
|
run.NextCandidateId += pairs.Count;
|
|
run.SelectTurn = 1;
|
|
run.PendingPickSetsJson = JsonSerializer.Serialize(pairs);
|
|
await _runs.UpsertAsync(run);
|
|
|
|
return new ClassChooseResponseDto
|
|
{
|
|
ClassInfo = ProjectClassInfo(run),
|
|
DeckInfo = ProjectDeckInfo(run),
|
|
CandidateCardList = pairs.Select(p => new CandidatePairDto
|
|
{
|
|
Id = p.Id, Turn = p.Turn, SetNum = p.SetNum,
|
|
CardId1 = p.CardId1, CardId2 = p.CardId2,
|
|
IsSelected = p.IsSelected ? 1 : 0,
|
|
}).ToList(),
|
|
};
|
|
}
|
|
|
|
// Placeholder: class default skin = class id. Matches the capture's "leader_skin_id":"1" when class_id=1.
|
|
private static long ResolveClassDefaultLeaderSkin(int classId) => classId;
|
|
|
|
public async Task<CardChooseResponseDto> ChooseCardAsync(long viewerId, long selectedId)
|
|
{
|
|
var run = await _runs.GetByViewerIdAsync(viewerId)
|
|
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
|
|
if (run.ClassId == 0 || run.IsSelectCompleted)
|
|
throw new ArenaTwoPickException("arena_two_pick_invalid_state");
|
|
|
|
var pending = JsonSerializer.Deserialize<List<CandidatePair>>(run.PendingPickSetsJson) ?? new();
|
|
var pick = pending.FirstOrDefault(p => p.Id == selectedId)
|
|
?? throw new ArenaTwoPickException("arena_two_pick_invalid_selection");
|
|
|
|
var selectedCards = JsonSerializer.Deserialize<List<long>>(run.SelectedCardIdsJson) ?? new();
|
|
selectedCards.Add(pick.CardId1);
|
|
selectedCards.Add(pick.CardId2);
|
|
run.SelectedCardIdsJson = JsonSerializer.Serialize(selectedCards);
|
|
|
|
List<CandidatePair>? nextPairs = null;
|
|
if (run.SelectTurn < 15)
|
|
{
|
|
run.SelectTurn += 1;
|
|
nextPairs = _pool.GeneratePickSetsForTurn(run.ClassId, run.SelectTurn, run.NextCandidateId, _rng);
|
|
run.NextCandidateId += nextPairs.Count;
|
|
run.PendingPickSetsJson = JsonSerializer.Serialize(nextPairs);
|
|
}
|
|
else
|
|
{
|
|
run.IsSelectCompleted = true;
|
|
run.PendingPickSetsJson = "[]";
|
|
}
|
|
await _runs.UpsertAsync(run);
|
|
|
|
return new CardChooseResponseDto
|
|
{
|
|
DeckInfo = ProjectDeckInfo(run),
|
|
CandidateCardList = nextPairs?.Select(p => new CandidatePairDto
|
|
{
|
|
Id = p.Id, Turn = p.Turn, SetNum = p.SetNum,
|
|
CardId1 = p.CardId1, CardId2 = p.CardId2,
|
|
IsSelected = p.IsSelected ? 1 : 0,
|
|
}).ToList(),
|
|
};
|
|
}
|
|
public Task<FinishResponseDto> RetireAsync(long viewerId) => GrantRunRewardsAndDeleteAsync(viewerId, requireComplete: false);
|
|
|
|
public Task<FinishResponseDto> FinishAsync(long viewerId) => GrantRunRewardsAndDeleteAsync(viewerId, requireComplete: true);
|
|
|
|
private async Task<FinishResponseDto> GrantRunRewardsAndDeleteAsync(long viewerId, bool requireComplete)
|
|
{
|
|
var run = await _runs.GetByViewerIdAsync(viewerId)
|
|
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
|
|
|
|
// Classic SV Take Two: run ends after MaxBattles total games played, regardless of the
|
|
// win/loss split. No separate loss cap (Worlds Beyond's 2-loss rule does not apply here).
|
|
// MaxBattles is derived from MAX(reward.WinCount), which is 5 for the live TK2 catalog.
|
|
var maxBattles = await ResolveMaxBattleCountAsync();
|
|
int battlesPlayed = run.WinCount + run.LossCount;
|
|
bool runOver = battlesPlayed >= maxBattles;
|
|
if (requireComplete && !runOver)
|
|
throw new ArenaTwoPickException("arena_two_pick_run_not_complete");
|
|
|
|
var rewardRows = await _rewards.GetRewardsByWinCountAsync(run.WinCount);
|
|
var viewer = await LoadViewerForGrantsAsync(viewerId);
|
|
|
|
var deltas = new List<RewardEntryDto>();
|
|
foreach (var r in rewardRows)
|
|
{
|
|
var goodsType = (SVSim.Database.Enums.UserGoodsType)r.RewardType;
|
|
await _grants.ApplyAsync(viewer, goodsType, r.RewardId, r.RewardNum);
|
|
// Rewards = deltas (per-grant amounts), not post-state totals.
|
|
deltas.Add(new RewardEntryDto { RewardType = r.RewardType, RewardId = r.RewardId, RewardNum = r.RewardNum });
|
|
}
|
|
await _db.SaveChangesAsync();
|
|
|
|
var postStates = ComputePostStateRewardList(rewardRows, viewer);
|
|
|
|
await _runs.DeleteAsync(viewerId);
|
|
return new FinishResponseDto { Rewards = deltas, RewardList = postStates };
|
|
}
|
|
|
|
private static List<RewardEntryDto> ComputePostStateRewardList(
|
|
IReadOnlyList<SVSim.Database.Models.ArenaTwoPickReward> rows, SVSim.Database.Models.Viewer viewer)
|
|
{
|
|
var entries = new List<RewardEntryDto>();
|
|
foreach (var r in rows)
|
|
{
|
|
int postState = r.RewardType switch
|
|
{
|
|
(int)SVSim.Database.Enums.UserGoodsType.Rupy => (int)viewer.Currency!.Rupees,
|
|
(int)SVSim.Database.Enums.UserGoodsType.Crystal => (int)viewer.Currency!.Crystals,
|
|
(int)SVSim.Database.Enums.UserGoodsType.RedEther => (int)viewer.Currency!.RedEther,
|
|
(int)SVSim.Database.Enums.UserGoodsType.Item => viewer.Items.FirstOrDefault(i => i.Item.Id == (int)r.RewardId)?.Count ?? r.RewardNum,
|
|
_ => r.RewardNum,
|
|
};
|
|
entries.Add(new RewardEntryDto { RewardType = r.RewardType, RewardId = r.RewardId, RewardNum = postState });
|
|
}
|
|
return entries;
|
|
}
|
|
|
|
public async Task<BattleFinishResultDto> RecordBattleResultAsync(long viewerId, bool isWin)
|
|
{
|
|
var run = await _runs.GetByViewerIdAsync(viewerId)
|
|
?? throw new ArenaTwoPickException("arena_two_pick_no_active_run");
|
|
|
|
var aCfg = _config.Get<SVSim.Database.Models.Config.ArenaTwoPickConfig>();
|
|
var results = JsonSerializer.Deserialize<List<bool>>(run.ResultListJson) ?? new();
|
|
results.Add(isWin);
|
|
run.ResultListJson = JsonSerializer.Serialize(results);
|
|
if (isWin) run.WinCount += 1; else run.LossCount += 1;
|
|
await _runs.UpsertAsync(run);
|
|
|
|
var viewer = await LoadViewerForGrantsAsync(viewerId);
|
|
int before = (int)(viewer.Currency?.SpotPoints ?? 0);
|
|
|
|
int newClassXp = GrantClassXp(viewer, run.ClassId, aCfg.ClassXpPerBattle);
|
|
int classLevel = ResolveClassLevel(viewer, run.ClassId);
|
|
|
|
viewer.Currency!.SpotPoints += (ulong)aCfg.SpotPointsPerBattle;
|
|
int after = (int)viewer.Currency.SpotPoints;
|
|
await _db.SaveChangesAsync();
|
|
|
|
return new BattleFinishResultDto
|
|
{
|
|
BattleResult = isWin ? 1 : 0,
|
|
GetClassExperience = aCfg.ClassXpPerBattle,
|
|
ClassExperience = newClassXp,
|
|
ClassLevel = classLevel,
|
|
BeforeSpotPoint = before,
|
|
AddSpotPoint = aCfg.SpotPointsPerBattle,
|
|
AfterSpotPoint = after,
|
|
};
|
|
}
|
|
|
|
private static int GrantClassXp(SVSim.Database.Models.Viewer viewer, int classId, int xp)
|
|
{
|
|
var row = viewer.Classes.FirstOrDefault(c => c.Class.Id == classId);
|
|
if (row is null) return 0;
|
|
row.Exp += xp;
|
|
return row.Exp;
|
|
}
|
|
|
|
private static int ResolveClassLevel(SVSim.Database.Models.Viewer viewer, int classId)
|
|
{
|
|
var row = viewer.Classes.FirstOrDefault(c => c.Class.Id == classId);
|
|
return row is null ? 1 : row.Level;
|
|
}
|
|
|
|
// --- 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,
|
|
};
|
|
}
|
|
}
|