mirror of
https://gitea.tendokyu.moe/Kayori/MedusaPluginChargeMachine.git
synced 2026-10-11 16:58:20 +03:00
- chargeoptions is the real field gating 2000/5000/10000 yen display - chargeid (not chargenum) carries the actual denomination on charge - getrecentcharge/getlastlog: fixed num mismatch, added missing result section, and added placeholder items for empty result sets
68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System.Text.RegularExpressions;
|
|
using Abstractions.Entities;
|
|
using Abstractions.Handlers;
|
|
using Abstractions.SerializationTypes;
|
|
using Abstractions.Services;
|
|
using MedusaPluginChargeMachine.Entities;
|
|
using MedusaPluginChargeMachine.Models.Request;
|
|
using MedusaPluginChargeMachine.Models.Response;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using ISessionService = MedusaPluginChargeMachine.Services.ISessionService;
|
|
|
|
namespace MedusaPluginChargeMachine.Handlers.LA9;
|
|
|
|
public partial class ChargeEaChargeHandler(
|
|
ICardService cardService,
|
|
ISessionService sessionService,
|
|
UserManager<User> userManager,
|
|
ChargeMachineDbContext db) : Handler<ChargeRequest, ChargeResponse>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Module("eacharge");
|
|
Method("charge");
|
|
}
|
|
|
|
// chargeid carries the actual denomination as e.g. "N10000P"
|
|
// (Note/10000/Paseli) - chargenum is just the quantity of that
|
|
// denomination being charged (normally "1" per bill inserted).
|
|
[GeneratedRegex(@"^N(\d+)P$")]
|
|
private static partial Regex ChargeIdPattern();
|
|
|
|
public override async Task<ChargeResponse> HandleAsync(ChargeRequest req, GameModel model)
|
|
{
|
|
var match = ChargeIdPattern().Match(req.ChargeId);
|
|
if (!match.Success)
|
|
throw new InvalidOperationException($"Unrecognized chargeid format: '{req.ChargeId}'");
|
|
|
|
var denomination = int.Parse(match.Groups[1].Value);
|
|
var amount = denomination * int.Parse(req.ChargeNumber);
|
|
|
|
var foundSession = sessionService.GetSession(req.SessionId);
|
|
|
|
var card = await cardService.FindByCardId(foundSession.refId)!;
|
|
|
|
card!.User.PaseliAmount += amount;
|
|
|
|
await userManager.UpdateAsync(card.User);
|
|
|
|
db.ChargeHistories.Add(new ChargeHistory
|
|
{
|
|
CardId = foundSession.refId,
|
|
ChargeNum = int.Parse(req.ChargeNumber),
|
|
ChargeAmount = amount,
|
|
OrderDate = DateTime.UtcNow
|
|
});
|
|
await db.SaveChangesAsync();
|
|
|
|
return new ChargeResponse
|
|
{
|
|
Status = 0,
|
|
Balance = new XrpcInt(card.User.PaseliAmount),
|
|
ChargeDailyLimit = new XrpcInt(100000),
|
|
ChargeDaily = new XrpcInt(0),
|
|
ChargePoint = new XrpcInt(amount)
|
|
};
|
|
}
|
|
}
|