feat(gift): unified GiftController over ViewerPresent + route aliases
This commit is contained in:
@@ -1,31 +1,29 @@
|
|||||||
|
using System.Globalization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using SVSim.Database;
|
using SVSim.Database;
|
||||||
using SVSim.Database.Enums;
|
using SVSim.Database.Enums;
|
||||||
|
using SVSim.Database.Models;
|
||||||
using SVSim.Database.Services.Inventory;
|
using SVSim.Database.Services.Inventory;
|
||||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Common;
|
using SVSim.EmulatedEntrypoint.Mapping;
|
||||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Gift;
|
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Gift;
|
||||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Gift;
|
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Gift;
|
||||||
|
|
||||||
namespace SVSim.EmulatedEntrypoint.Controllers;
|
namespace SVSim.EmulatedEntrypoint.Controllers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tutorial-scoped gift endpoints. We do NOT implement a generic gift system here —
|
/// Persistent gift inbox. /gift/top + /tutorial/gift_top are pure URL aliases over the
|
||||||
/// only the /tutorial/gift_top and /tutorial/gift_receive aliases needed for the
|
/// same ViewerPresent query; /gift/receive_gift + /tutorial/gift_receive share a single
|
||||||
/// step 31 → 41 reward flow. A full gift inbox is future work; if/when needed,
|
/// ReceiveImpl whose only divergence is the route-gated tutorial-state bump.
|
||||||
/// add /gift/top and /gift/receive_gift aliases to this controller.
|
///
|
||||||
|
/// Tutorial gifts are seeded as real ViewerPresent rows during /tool/signup
|
||||||
|
/// (see ViewerRepository.RegisterAnonymousViewer) — this controller carries no static
|
||||||
|
/// gift catalog.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GiftController : SVSimController
|
public class GiftController : SVSimController
|
||||||
{
|
{
|
||||||
/// <summary>The hardcoded tutorial gift bundle every fresh viewer sees at step 31.</summary>
|
private const int PageSize = 30;
|
||||||
public static readonly IReadOnlyList<PresentDto> TutorialGifts = new[]
|
private const int GiftReceiveTutorialStep = 41;
|
||||||
{
|
|
||||||
new PresentDto { PresentId = "71478626", RewardType = "1", RewardDetailId = "0", RewardCount = "400", Message = "For completing the tutorial" },
|
|
||||||
new PresentDto { PresentId = "71478627", RewardType = "9", RewardDetailId = "0", RewardCount = "100", Message = "For completing the tutorial" },
|
|
||||||
new PresentDto { PresentId = "71478628", RewardType = "4", RewardDetailId = "1", RewardCount = "3", Message = "For completing the tutorial", ItemType = 1 },
|
|
||||||
new PresentDto { PresentId = "71478629", RewardType = "4", RewardDetailId = "80001", RewardCount = "40", Message = "For completing the tutorial", ItemType = 2 },
|
|
||||||
new PresentDto { PresentId = "71478630", RewardType = "4", RewardDetailId = "90001", RewardCount = "1", Message = "For completing the tutorial", ItemType = 2 },
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly SVSimDbContext _db;
|
private readonly SVSimDbContext _db;
|
||||||
private readonly IInventoryService _inv;
|
private readonly IInventoryService _inv;
|
||||||
@@ -36,180 +34,153 @@ public class GiftController : SVSimController
|
|||||||
_inv = inv;
|
_inv = inv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("/gift/top")]
|
||||||
[HttpPost("/tutorial/gift_top")]
|
[HttpPost("/tutorial/gift_top")]
|
||||||
public async Task<ActionResult<GiftTopResponse>> TutorialGiftTop([FromBody] GiftTopRequest request)
|
public async Task<ActionResult<GiftTopResponse>> Top([FromBody] GiftTopRequest request)
|
||||||
{
|
{
|
||||||
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
||||||
|
|
||||||
var claimedList = await _db.ViewerClaimedTutorialGifts
|
var (unclaimed, history) = await ReadTopWindowAsync(viewerId, request.Page);
|
||||||
.Where(g => g.ViewerId == viewerId)
|
|
||||||
.Select(g => g.PresentId)
|
|
||||||
.ToListAsync();
|
|
||||||
var claimed = new HashSet<string>(claimedList);
|
|
||||||
|
|
||||||
var nowString = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
|
|
||||||
var presents = TutorialGifts
|
|
||||||
.Where(p => !claimed.Contains(p.PresentId))
|
|
||||||
.Select(p => Clone(p, nowString))
|
|
||||||
.ToList();
|
|
||||||
var history = TutorialGifts
|
|
||||||
.Where(p => claimed.Contains(p.PresentId))
|
|
||||||
.Select(p => Clone(p, nowString))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
return new GiftTopResponse
|
return new GiftTopResponse
|
||||||
{
|
{
|
||||||
PresentList = presents,
|
PresentList = unclaimed.Select(PresentMapper.ToWire).ToList(),
|
||||||
PresentHistoryList = history,
|
PresentHistoryList = history.Select(PresentMapper.ToWire).ToList(),
|
||||||
LimitOverPresentList = new(),
|
LimitOverPresentList = new(), // expiration sweep deferred — always [] for now
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("/gift/receive_gift")]
|
||||||
|
public Task<ActionResult<GiftReceiveResponse>> Receive([FromBody] GiftReceiveRequest r)
|
||||||
|
=> ReceiveImpl(r, advanceTutorial: false);
|
||||||
|
|
||||||
[HttpPost("/tutorial/gift_receive")]
|
[HttpPost("/tutorial/gift_receive")]
|
||||||
public async Task<ActionResult<GiftReceiveResponse>> TutorialGiftReceive([FromBody] GiftReceiveRequest request)
|
public Task<ActionResult<GiftReceiveResponse>> TutorialReceive([FromBody] GiftReceiveRequest r)
|
||||||
|
=> ReceiveImpl(r, advanceTutorial: true);
|
||||||
|
|
||||||
|
private async Task<ActionResult<GiftReceiveResponse>> ReceiveImpl(
|
||||||
|
GiftReceiveRequest request, bool advanceTutorial)
|
||||||
{
|
{
|
||||||
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
||||||
|
|
||||||
var requestedIds = request.PresentIdArray.ToHashSet();
|
var requested = request.PresentIdArray.ToHashSet();
|
||||||
|
var state = request.State; // 1 = MAIL_READ (claim), 3 = MAIL_DELETE
|
||||||
|
|
||||||
// Resolve which of the requested ids are still claimable for this viewer before opening tx.
|
// Pull only currently-Unclaimed rows matching the request — already-Claimed /
|
||||||
var alreadyClaimedList = await _db.ViewerClaimedTutorialGifts
|
// Deleted / Expired rows are silently ignored (idempotent retry semantics).
|
||||||
.Where(g => g.ViewerId == viewerId && requestedIds.Contains(g.PresentId))
|
var targets = await _db.ViewerPresents
|
||||||
.Select(g => g.PresentId)
|
.Where(p => p.ViewerId == viewerId
|
||||||
|
&& p.Status == PresentStatus.Unclaimed
|
||||||
|
&& requested.Contains(p.PresentId))
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
var alreadyClaimed = new HashSet<string>(alreadyClaimedList);
|
|
||||||
|
|
||||||
var toClaim = TutorialGifts
|
|
||||||
.Where(p => requestedIds.Contains(p.PresentId) && !alreadyClaimed.Contains(p.PresentId))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// Open inventory tx with MissionData loaded for tutorial-step advance.
|
|
||||||
await using var tx = await _inv.BeginAsync(viewerId, configure:
|
await using var tx = await _inv.BeginAsync(viewerId, configure:
|
||||||
cfg => cfg.WithInclude(v => v.MissionData));
|
cfg => cfg.WithInclude(v => v.MissionData));
|
||||||
|
|
||||||
// Apply grants via tx. Collect post-state per (type, detailId) for reward_list.
|
|
||||||
// Each GrantAsync returns a list of GrantedReward with post-state totals; for currencies
|
|
||||||
// only one entry is returned; for cards the cascade may return more entries (card + cosmetics).
|
|
||||||
// reward_list must carry post-state totals (client does direct assignment).
|
|
||||||
var rewardListEntries = new List<GiftRewardListEntry>();
|
var rewardListEntries = new List<GiftRewardListEntry>();
|
||||||
foreach (var p in toClaim)
|
var now = DateTime.UtcNow;
|
||||||
|
|
||||||
|
foreach (var p in targets)
|
||||||
{
|
{
|
||||||
var goodsType = WireRewardTypeToUserGoodsType(int.Parse(p.RewardType));
|
if (state == 1)
|
||||||
var granted = await tx.GrantAsync(goodsType, long.Parse(p.RewardDetailId), int.Parse(p.RewardCount));
|
|
||||||
// Use the first granted entry's post-state for the top-level gift reward_list entry.
|
|
||||||
// Gift rewards are currencies and items only (no cards in TutorialGifts), so granted
|
|
||||||
// always has exactly one element. The post-state total is already correct from tx.
|
|
||||||
if (granted.Count > 0)
|
|
||||||
{
|
{
|
||||||
rewardListEntries.Add(new GiftRewardListEntry
|
var granted = await tx.GrantAsync(
|
||||||
|
(UserGoodsType)p.RewardType,
|
||||||
|
p.RewardDetailId,
|
||||||
|
(int)p.RewardCount);
|
||||||
|
|
||||||
|
// reward_list carries POST-STATE TOTALS (client does direct assignment).
|
||||||
|
// See project_wire_reward_list_post_state. GrantAsync already returns post-state.
|
||||||
|
if (granted.Count > 0)
|
||||||
{
|
{
|
||||||
RewardType = p.RewardType,
|
rewardListEntries.Add(new GiftRewardListEntry
|
||||||
RewardId = p.RewardDetailId,
|
{
|
||||||
RewardNum = granted[0].RewardNum.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
RewardType = p.RewardType.ToString(CultureInfo.InvariantCulture),
|
||||||
});
|
RewardId = p.RewardDetailId.ToString(CultureInfo.InvariantCulture),
|
||||||
|
RewardNum = granted[0].RewardNum.ToString(CultureInfo.InvariantCulture),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Status = PresentStatus.Claimed;
|
||||||
|
p.ClaimedAt = now;
|
||||||
|
}
|
||||||
|
else if (state == 3)
|
||||||
|
{
|
||||||
|
// MAIL_DELETE: no grant, no reward_list entry, no history. Tombstone the
|
||||||
|
// row so re-deletes are idempotent under the same WHERE-Unclaimed filter.
|
||||||
|
p.Status = PresentStatus.Deleted;
|
||||||
|
p.ClaimedAt = now; // overload as "decided-at" — tombstone never reaches wire
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advance tutorial state from 31 → 41 as a side-effect of this claim (no separate
|
// Tutorial step advance — route-gated, no Source/state checks. Preserve-max so
|
||||||
// /tutorial/update → 41 call appears in the prod capture). Preserve max — don't downgrade
|
// replays don't downgrade viewers already past 41.
|
||||||
// viewers who are already past step 41.
|
if (advanceTutorial && tx.Viewer.MissionData.TutorialState < GiftReceiveTutorialStep)
|
||||||
const int GiftReceiveTutorialStep = 41;
|
|
||||||
if (tx.Viewer.MissionData.TutorialState < GiftReceiveTutorialStep)
|
|
||||||
{
|
|
||||||
tx.Viewer.MissionData.TutorialState = GiftReceiveTutorialStep;
|
tx.Viewer.MissionData.TutorialState = GiftReceiveTutorialStep;
|
||||||
}
|
|
||||||
|
|
||||||
// Persist claim receipts inside the same tx.
|
await tx.CommitAsync(); // throws DbUpdateConcurrencyException on RowVersion conflict
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
foreach (var p in toClaim)
|
|
||||||
{
|
|
||||||
_db.ViewerClaimedTutorialGifts.Add(new SVSim.Database.Models.ViewerClaimedTutorialGift
|
|
||||||
{
|
|
||||||
ViewerId = viewerId,
|
|
||||||
PresentId = p.PresentId,
|
|
||||||
ClaimedAt = now,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await tx.CommitAsync();
|
|
||||||
|
|
||||||
var nowString = now.ToString("yyyy-MM-dd HH:mm:ss");
|
// Rebuild the inbox window (page 1) — the client wipes its local lists and rebuilds
|
||||||
var allClaimedList = await _db.ViewerClaimedTutorialGifts
|
// from these.
|
||||||
.Where(g => g.ViewerId == viewerId)
|
var (unclaimed, history) = await ReadTopWindowAsync(viewerId, page: 1);
|
||||||
.Select(g => g.PresentId)
|
|
||||||
.ToListAsync();
|
|
||||||
var allClaimed = new HashSet<string>(allClaimedList);
|
|
||||||
|
|
||||||
// Derive presentList/historyList up front so IsUnreceivedPresent can read the count
|
// is_unreceived_present drives the home-screen inbox badge — must be the DB count
|
||||||
// without re-filtering. unclaimedPresents are the gifts still on offer after this call;
|
// post-commit, NOT hardcoded false (hiding the badge after partial claims).
|
||||||
// claimedPresents are everything the viewer has ever received (this call + prior calls).
|
var stillUnclaimed = await _db.ViewerPresents
|
||||||
var unclaimedPresents = TutorialGifts
|
.AnyAsync(p => p.ViewerId == viewerId && p.Status == PresentStatus.Unclaimed);
|
||||||
.Where(p => !allClaimed.Contains(p.PresentId))
|
|
||||||
.Select(p => Clone(p, nowString))
|
|
||||||
.ToList();
|
|
||||||
var claimedPresents = TutorialGifts
|
|
||||||
.Where(p => allClaimed.Contains(p.PresentId))
|
|
||||||
.Select(p => Clone(p, nowString))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
return new GiftReceiveResponse
|
return new GiftReceiveResponse
|
||||||
{
|
{
|
||||||
CardList = new(),
|
CardList = new(), // capture is []; reward_list carries the grants
|
||||||
// Echo only the ids actually granted by THIS call. Building this from `requestedIds`
|
|
||||||
// would falsely confirm a re-grant on idempotent retries: the client would re-show
|
// Echo only ids actually transitioned by THIS call — NOT requested ids, which
|
||||||
// the "received N gifts" popup and direct-assign the same post-state totals it already
|
// would re-fire the "received N gifts" popup on replay.
|
||||||
// applied, double-toasting the user. Sort ascending to match the prod-capture order.
|
ReceivedIds = targets
|
||||||
ReceivedIds = toClaim
|
.Select(t => t.PresentId)
|
||||||
.Select(p => p.PresentId)
|
.OrderBy(x => x, StringComparer.Ordinal)
|
||||||
.OrderBy(x => x)
|
|
||||||
.ToList(),
|
.ToList(),
|
||||||
// Same idempotency contract: only the gifts granted in THIS call belong in the
|
|
||||||
// per-reward summary list. The client uses this to drive the +N popups.
|
// Per-gift summary for the "+N received" popup. Empty on state=3.
|
||||||
TotalReceiveCountList = toClaim
|
TotalReceiveCountList = (state == 1 ? targets : Enumerable.Empty<ViewerPresent>())
|
||||||
.Select(p => new TotalReceiveCountDto
|
.Select(t => new TotalReceiveCountDto
|
||||||
{
|
{
|
||||||
RewardType = int.Parse(p.RewardType),
|
RewardType = t.RewardType,
|
||||||
RewardDetailId = long.Parse(p.RewardDetailId),
|
RewardDetailId = t.RewardDetailId,
|
||||||
RewardCount = long.Parse(p.RewardCount),
|
RewardCount = t.RewardCount,
|
||||||
ItemType = p.ItemType ?? 0,
|
ItemType = t.ItemType ?? 0,
|
||||||
IsUsable = true,
|
IsUsable = true,
|
||||||
}).ToList(),
|
}).ToList(),
|
||||||
PresentList = unclaimedPresents,
|
|
||||||
PresentHistoryList = claimedPresents,
|
PresentList = unclaimed.Select(PresentMapper.ToWire).ToList(),
|
||||||
// True when there are still unclaimed gifts on offer — drives the inbox badge state.
|
PresentHistoryList = history.Select(PresentMapper.ToWire).ToList(),
|
||||||
// Hardcoding false hid the badge after partial claims even though present_list still
|
IsUnreceivedPresent = stillUnclaimed,
|
||||||
// carried unclaimed entries.
|
|
||||||
IsUnreceivedPresent = unclaimedPresents.Count > 0,
|
|
||||||
// reward_list entries carry POST-STATE TOTALS (from tx.GrantAsync).
|
|
||||||
// See project memory: project_wire_reward_list_post_state.
|
|
||||||
// Iterate toClaim so idempotent re-receive doesn't re-emit post-state entries.
|
|
||||||
RewardList = rewardListEntries,
|
RewardList = rewardListEntries,
|
||||||
// Echo the persisted state, not a hardcoded 41. The state may already be past 41
|
|
||||||
// for replay/edge-case calls (the Math.Max-preserve block above keeps it stable);
|
// Echo persisted state, not a hardcoded 41 — preserve-max above keeps it stable.
|
||||||
// emitting 41 anyway would surface a regressed step to the client and desync the
|
|
||||||
// tutorial-state machine.
|
|
||||||
TutorialStep = tx.Viewer.MissionData.TutorialState,
|
TutorialStep = tx.Viewer.MissionData.TutorialState,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static UserGoodsType WireRewardTypeToUserGoodsType(int wireType) => wireType switch
|
private async Task<(List<ViewerPresent> Unclaimed, List<ViewerPresent> History)> ReadTopWindowAsync(
|
||||||
|
long viewerId, int page)
|
||||||
{
|
{
|
||||||
1 => UserGoodsType.Crystal,
|
int pageOneIndexed = Math.Max(1, page);
|
||||||
4 => UserGoodsType.Item,
|
int skip = (pageOneIndexed - 1) * PageSize;
|
||||||
9 => UserGoodsType.Rupy,
|
|
||||||
_ => throw new InvalidOperationException($"Unmapped gift wire reward_type {wireType}"),
|
|
||||||
};
|
|
||||||
|
|
||||||
private static PresentDto Clone(PresentDto p, string createTime) => new()
|
// Unclaimed: chronological (oldest first — capture order matches this).
|
||||||
{
|
var unclaimed = await _db.ViewerPresents
|
||||||
PresentId = p.PresentId,
|
.Where(p => p.ViewerId == viewerId && p.Status == PresentStatus.Unclaimed)
|
||||||
RewardType = p.RewardType,
|
.OrderBy(p => p.CreatedAt).ThenBy(p => p.Id)
|
||||||
RewardDetailId = p.RewardDetailId,
|
.Skip(skip).Take(PageSize)
|
||||||
RewardCount = p.RewardCount,
|
.ToListAsync();
|
||||||
ConditionNumber = p.ConditionNumber,
|
|
||||||
PresentLimitType = p.PresentLimitType,
|
// History: most-recent-first (standard inbox UX).
|
||||||
RewardLimitTime = p.RewardLimitTime,
|
var history = await _db.ViewerPresents
|
||||||
CreateTime = createTime,
|
.Where(p => p.ViewerId == viewerId && p.Status == PresentStatus.Claimed)
|
||||||
ItemType = p.ItemType,
|
.OrderByDescending(p => p.ClaimedAt).ThenByDescending(p => p.Id)
|
||||||
Message = p.Message,
|
.Skip(skip).Take(PageSize)
|
||||||
};
|
.ToListAsync();
|
||||||
|
|
||||||
|
return (unclaimed, history);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user