Bind Rihla receipts to settled facts when recovering pending cash
This commit is contained in:
parent
abfa82bca8
commit
146a5c46ae
3 changed files with 37 additions and 15 deletions
|
|
@ -24,7 +24,7 @@ final class AtmMenu extends Menu {
|
|||
private final List<DunemarkDenomination> selected = new ArrayList<>();
|
||||
private String entered; // RIA ID typed on the Redeem tab
|
||||
private String receiptOperation,receiptReference,receiptMessage;
|
||||
private long receiptAmount;
|
||||
private Long receiptAmount;
|
||||
|
||||
AtmMenu(DuneMenusPlugin plugin, Player p, Menu parent, Tab tab) { super(plugin, p, parent); this.tab = tab; }
|
||||
String id() { return switch(tab){case WITHDRAW->"withdraw";case DEPOSIT->"deposit";case REDEEM->"redeem";case RECEIPT->"receipt";default->"atm";}; }
|
||||
|
|
@ -66,7 +66,7 @@ final class AtmMenu extends Menu {
|
|||
put(36,Ui.item(Material.ARROW,"<gray>Back"),c->{tab=Tab.HOME;selected.clear();redraw();});
|
||||
if(tab==Tab.RECEIPT){
|
||||
put(13,Ui.item(Material.PAPER,"<gold>"+receiptOperation,
|
||||
"<gray>Amount: <white>"+Ui.dm(receiptAmount),"<gray>"+receiptMessage));
|
||||
receiptAmount==null?"<gray>"+receiptMessage:"<gray>Amount: <white>"+Ui.dm(receiptAmount),"<gray>"+receiptMessage));
|
||||
if(receiptReference!=null&&!receiptReference.isBlank())put(29,Ui.item(Material.NAME_TAG,"<gold>RIA ID","<white>"+receiptReference));
|
||||
return;
|
||||
}
|
||||
|
|
@ -91,20 +91,21 @@ final class AtmMenu extends Menu {
|
|||
|
||||
if (dep) load(backend().atmCreateRia(player, pieces), r -> {
|
||||
say(new Backend.Result(r.ok(), r.message()));
|
||||
if (r.ok()) { receipt("Cash deposit",minor,r.riaId(),r.message()); }
|
||||
if (r.ok()) { receipt("Cash deposit",r.receipt(),r.message()); }
|
||||
else redraw();
|
||||
});
|
||||
else load(backend().bankWithdraw(player, pieces), r -> {
|
||||
say(r);
|
||||
if (r.ok()) receipt("Cash withdrawal",minor,null,r.message());
|
||||
if (r.ok()) receipt("Cash withdrawal",r.receipt(),r.message());
|
||||
else redraw();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void receipt(String operation,long amount,String reference,String message){
|
||||
receiptOperation=operation;receiptAmount=amount;receiptReference=reference;receiptMessage=message;
|
||||
private void receipt(String operation,Backend.CashReceipt facts,String message){
|
||||
receiptOperation=facts!=null&&facts.recovered()?"Recovered Rihla transaction":operation;
|
||||
receiptAmount=facts==null?null:facts.amountMinor();receiptReference=facts==null?null:facts.riaId();receiptMessage=message;
|
||||
selected.clear();tab=Tab.RECEIPT;redraw();
|
||||
}
|
||||
|
||||
|
|
@ -123,9 +124,9 @@ final class AtmMenu extends Menu {
|
|||
if (v == null) { put(22, Ui.item(Material.BARRIER, "<red>RIA ID not found")); return; }
|
||||
put(22, Ui.glow(Ui.item(Material.GOLD_BLOCK, "<gold><b>" + Ui.dm(v.amountMinor()), "<gray>From: <white>" + v.fromName())));
|
||||
put(24, Ui.glow(Ui.item(Material.GOLD_INGOT, "<green><b>Take as cash", "<gray>Bills and coins in your inventory")),
|
||||
c -> load(backend().atmRedeem(player, v, false), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Cash",v.amountMinor(),id,r.message());} else redraw(); }));
|
||||
c -> load(backend().atmRedeem(player, v, false), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Cash",r.receipt(),r.message());} else redraw(); }));
|
||||
put(26, Ui.glow(Ui.item(Material.ENDER_CHEST, "<aqua><b>Send to my bank", "<gray>Credited to your account")),
|
||||
c -> load(backend().atmRedeem(player, v, true), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Bank",v.amountMinor(),id,r.message());} else redraw(); }));
|
||||
c -> load(backend().atmRedeem(player, v, true), r -> { say(r); if (r.ok()) {entered = null;receipt("RIA payout · Bank",r.receipt(),r.message());} else redraw(); }));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,9 @@ public interface Backend {
|
|||
/** Complete a future exceptionally with this to mean "this feature is not wired yet". */
|
||||
final class Unavailable extends RuntimeException { public Unavailable() { super("not wired", null, false, false); } }
|
||||
|
||||
record Result(boolean ok, String message) {
|
||||
record CashReceipt(long amountMinor,String riaId,boolean recovered) {}
|
||||
record Result(boolean ok, String message, CashReceipt receipt) {
|
||||
public Result(boolean ok,String message){this(ok,message,null);}
|
||||
public static Result yes(String m) { return new Result(true, m); }
|
||||
public static Result no(String m) { return new Result(false, m); }
|
||||
}
|
||||
|
|
@ -96,7 +98,9 @@ public interface Backend {
|
|||
// receives the full amount and collects it in-game; cancelling releases the reservation.
|
||||
default CompletableFuture<Account> bankAccount(Player p) { return na(); }
|
||||
/** ATM, tab 1. The player taps bills and coins; the menu sends the total. Backend takes the cash and CREATES a new RIA ID worth that amount. */
|
||||
record Issued(boolean ok, String message, String riaId) {}
|
||||
record Issued(boolean ok, String message, String riaId, CashReceipt receipt) {
|
||||
public Issued(boolean ok,String message,String riaId){this(ok,message,riaId,null);}
|
||||
}
|
||||
default CompletableFuture<Issued> atmCreateRia(Player p, List<DunemarkDenomination> pieces) { return na(); }
|
||||
/** ATM, tab 2. Look an RIA ID up so the menu can show the amount before payout. Return null/exceptional if unknown. */
|
||||
record Voucher(String riaId, long amountMinor, String fromName) {}
|
||||
|
|
|
|||
|
|
@ -97,7 +97,8 @@ final class JarvisBackend implements Backend, Listener {
|
|||
}
|
||||
return cashAction(player,request).thenApply(row->{
|
||||
String voucher=number(row,"voucher");
|
||||
return new Issued(!voucher.isEmpty(),voucher.isEmpty()?cashResult(row).message():"Rihla voucher created.",voucher);
|
||||
var result=cashResult(row);
|
||||
return new Issued(result.ok()&&!voucher.isEmpty(),result.message(),voucher,result.receipt());
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +134,8 @@ final class JarvisBackend implements Backend, Listener {
|
|||
request.addProperty("voucher",voucher.riaId());
|
||||
request.addProperty("requestId",UUID.randomUUID().toString());
|
||||
var row=accepted(jarvis.requestRihla(actor,request));
|
||||
return Result.yes("Credited "+Ui.dm(money(row,"amountMinor"))+" to your Postbank account.");
|
||||
long amount=money(row,"amountMinor");
|
||||
return new Result(true,"Credited "+Ui.dm(amount)+" to your Postbank account.",new CashReceipt(amount,voucher.riaId(),false));
|
||||
}));
|
||||
}
|
||||
var pieces=change(voucher.amountMinor());
|
||||
|
|
@ -192,13 +194,14 @@ final class JarvisBackend implements Backend, Listener {
|
|||
return List.copyOf(result);
|
||||
}
|
||||
|
||||
private static Result cashResult(JsonObject result) {
|
||||
static Result cashResult(JsonObject result) {
|
||||
if(result.has("localError"))return Result.no(text(result,"localError"));
|
||||
if(text(result,"state").equals("aborted"))return Result.no("The previous unfinished Rihla cash action was released. Select your cash again.");
|
||||
if(!text(result,"state").equals("committed"))return Result.no("Rihla cash was not settled.");
|
||||
String voucher=number(result,"voucher");
|
||||
String prefix=yes(result,"recovered")?"Previous Rihla cash action recovered · ":"Rihla cash settled · ";
|
||||
return Result.yes(prefix+Ui.dm(money(result,"amountMinor"))+(voucher.isEmpty()?".":" · "+voucher+"."));
|
||||
long amount=money(result,"amountMinor");
|
||||
return new Result(true,prefix+Ui.dm(amount)+(voucher.isEmpty()?".":" · "+voucher+"."),new CashReceipt(amount,voucher,yes(result,"recovered")));
|
||||
}
|
||||
|
||||
private static String number(JsonObject row,String key){
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff