Recover a committed Rihla voucher after lost client response
This commit is contained in:
parent
021f68c602
commit
e56a20c909
7 changed files with 34 additions and 6 deletions
|
|
@ -22,6 +22,7 @@ final class AtmMenu extends Menu {
|
|||
DunemarkDenomination.COIN_1DM, DunemarkDenomination.COIN_2DM);
|
||||
private Tab tab;
|
||||
private final List<DunemarkDenomination> selected = new ArrayList<>();
|
||||
private boolean recoveredDeposit;
|
||||
private String issued; // RIA ID created by the last deposit
|
||||
private String entered; // RIA ID typed on the Redeem tab
|
||||
|
||||
|
|
@ -91,10 +92,12 @@ final class AtmMenu extends Menu {
|
|||
// framed ID box
|
||||
for (int s : new int[]{28, 30}) put(s, Ui.pane(Material.YELLOW_STAINED_GLASS_PANE));
|
||||
if (dep) {
|
||||
put(29, issued == null ? Ui.item(Material.NAME_TAG, "<gold><b>Your new RIA ID", "<dark_gray>Appears here after you deposit")
|
||||
: Ui.glow(Ui.item(Material.NAME_TAG, "<gold><b>Your new RIA ID", "<white>" + issued, "<gray>Give it to anyone to collect the money",
|
||||
"<yellow>Click to show in chat")),
|
||||
c -> { if (issued != null) player.sendMessage(Ui.text("<gold>RIA ID: <click:copy_to_clipboard:'" + issued + "'><white><u>" + issued + "</u></click>")); });
|
||||
drawIssuedVoucher();
|
||||
if (!recoveredDeposit) load(backend().atmRecentDeposit(player), voucher -> {
|
||||
recoveredDeposit = true;
|
||||
issued = voucher.isEmpty() ? null : voucher;
|
||||
drawIssuedVoucher();
|
||||
});
|
||||
} else {
|
||||
put(29, Ui.item(Material.NAME_TAG, "<gold><b>Rihla reference", "<gray>Loading…"));
|
||||
load(backend().bankAccount(player), a -> put(29, Ui.item(Material.NAME_TAG, "<gold><b>Rihla reference",
|
||||
|
|
@ -102,6 +105,13 @@ final class AtmMenu extends Menu {
|
|||
}
|
||||
}
|
||||
|
||||
private void drawIssuedVoucher() {
|
||||
put(29, issued == null ? Ui.item(Material.NAME_TAG, "<gold><b>Your new RIA ID", "<dark_gray>Appears here after you deposit")
|
||||
: Ui.glow(Ui.item(Material.NAME_TAG, "<gold><b>Your new RIA ID", "<white>" + issued, "<gray>Give it to anyone to collect the money",
|
||||
"<yellow>Click to show in chat")),
|
||||
c -> { if (issued != null) player.sendMessage(Ui.text("<gold>RIA ID: <click:copy_to_clipboard:'" + issued + "'><white><u>" + issued + "</u></click>")); });
|
||||
}
|
||||
|
||||
private void drawRedeem() {
|
||||
put(4, Ui.glow(Ui.item(Material.LIGHTNING_ROD, "<aqua><b>Redeem an RIA ID", "<gray>1. Type the ID", "<gray>2. Pick Cash or To bank")));
|
||||
put(13, Ui.item(Material.WRITABLE_BOOK, entered == null ? "<yellow><b>Type RIA ID" : "<yellow><b>Change RIA ID",
|
||||
|
|
|
|||
|
|
@ -91,6 +91,7 @@ public interface Backend {
|
|||
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) {}
|
||||
default CompletableFuture<String> atmRecentDeposit(Player p) { return na(); }
|
||||
default CompletableFuture<Voucher> atmLookup(Player p, String riaId) { return na(); }
|
||||
/** ATM, tab 2. Pay the RIA ID out: toBank=false gives cash, toBank=true credits the player's bank account. One click, no confirmation. */
|
||||
default CompletableFuture<Result> atmRedeem(Player p, Voucher voucher, boolean toBank) { return na(); }
|
||||
|
|
|
|||
|
|
@ -108,6 +108,11 @@ final class JarvisBackend implements Backend, Listener {
|
|||
return cashAction(player,request).thenApply(JarvisBackend::cashResult);
|
||||
}
|
||||
|
||||
@Override public CompletableFuture<String> atmRecentDeposit(Player player) {
|
||||
UUID actor=player.getUniqueId();
|
||||
return remote(player,()->number(accepted(jarvis.requestRihla(actor,action("cash_recent_deposit"))),"voucher"));
|
||||
}
|
||||
|
||||
@Override public CompletableFuture<Voucher> atmLookup(Player player,String code) {
|
||||
UUID actor=player.getUniqueId();
|
||||
return remote(player,()->{
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ public final class JarvisTranslator extends JavaPlugin implements Listener {
|
|||
var query=input.deepCopy();
|
||||
query.addProperty("uuid",player.toString());
|
||||
String action=query.get("action").getAsString();
|
||||
if(action.startsWith("cash_")&&!action.equals("cash_lookup"))query.addProperty("server",serverKey);
|
||||
if(action.startsWith("cash_")&&!Set.of("cash_lookup","cash_recent_deposit").contains(action))query.addProperty("server",serverKey);
|
||||
return api.rihla(query);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -204,6 +204,13 @@ export function createRihlaCashLedger(bank,{readReceipt,postbank,clock=Date.now}
|
|||
|
||||
return async (actor,input)=>{
|
||||
if(!validUuid(input.uuid))fail('Invalid native cash identity.');
|
||||
if(input.action==='cash_recent_deposit'){
|
||||
const deposit=await bank.first(`SELECT o.voucher FROM ria_cash_operations o
|
||||
LEFT JOIN ria_cash_redemptions r ON r.voucher=o.voucher
|
||||
WHERE o.owner_id=? AND o.player_uuid=? AND o.direction='deposit' AND o.state='committed' AND r.id IS NULL
|
||||
ORDER BY o.created_at DESC,o.id DESC LIMIT 1`,[actor.id,input.uuid]);
|
||||
return {ok:true,voucher:deposit?.voucher??null};
|
||||
}
|
||||
if(input.action==='cash_lookup')return voucher(actor,input);
|
||||
if(!NATIVE_SERVERS.includes(input.server))fail('Invalid native cash server.');
|
||||
if(input.action==='cash_pending')return pending(actor,input);
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ export function createGameRihlaAPI(central,bank,settings={},deps={}){
|
|||
create:['action','uuid','amount','recipientUuid','quoteReference','requestId'],
|
||||
cancel:['action','uuid','number'],collect:['action','uuid','number'],collectAll:['action','uuid'],
|
||||
cash_prepare:['action','uuid','server','requestId','direction','pieces'],cash_pending:['action','uuid','server'],
|
||||
cash_settle:['action','uuid','server','requestId'],cash_prepare_abort:['action','uuid','server','requestId'],cash_lookup:['action','uuid','voucher'],
|
||||
cash_settle:['action','uuid','server','requestId'],cash_prepare_abort:['action','uuid','server','requestId'],cash_lookup:['action','uuid','voucher'],cash_recent_deposit:['action','uuid'],
|
||||
cash_redeem_bank:['action','uuid','server','requestId','voucher'],
|
||||
cash_redeem_cash:['action','uuid','server','requestId','voucher','direction','pieces']
|
||||
};
|
||||
|
|
|
|||
|
|
@ -77,6 +77,10 @@ test('funded notes issue once and only exact committed native surrender creates
|
|||
recordReceipt(deposit,nativeReceipt(surrender));
|
||||
const issued=await settle(deposit);
|
||||
assert.match(issued.voucher,/^RIA-[A-F0-9]{32}$/);
|
||||
const recent={action:'cash_recent_deposit',uuid};
|
||||
assert.deepEqual(await cash(actor,recent),{ok:true,voucher:issued.voucher});
|
||||
assert.deepEqual(await cash(other,recent),{ok:true,voucher:null});
|
||||
assert.deepEqual(await cash(actor,{...recent,uuid:'10000000-0000-0000-0000-000000000009'}),{ok:true,voucher:null});
|
||||
assert.equal((await settle(deposit)).voucher,issued.voucher);
|
||||
const otherUuid='10000000-0000-0000-0000-000000000007';
|
||||
const lookup={action:'cash_lookup',uuid:otherUuid,voucher:issued.voucher};
|
||||
|
|
@ -85,6 +89,7 @@ test('funded notes issue once and only exact committed native surrender creates
|
|||
const redeemed=await Promise.all([cash(other,redeem),cash(other,redeem)]);
|
||||
assert.deepEqual(redeemed.map(result=>result.state),['redeemed','redeemed']);
|
||||
await assert.rejects(cash(other,lookup),/unavailable/);
|
||||
assert.deepEqual(await cash(actor,recent),{ok:true,voucher:null});
|
||||
await assert.rejects(cash(actor,{...redeem,uuid}),/unavailable/);
|
||||
assert.equal((await fixture.account(other)).balance_minor,501);
|
||||
const ids=Array.from({length:3},(_,index)=>'10000000-0000-0000-0000-00000000001'+index);
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff