web/DuneMenus/src/main/java/com/dunerealms/menus/AtmMenu.java

127 lines
7.5 KiB
Java

package com.dunerealms.menus;
import com.dunerealms.jarvis.DunemarkDenomination;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.entity.Player;
/**
* ONE ATM, three tabs, 5 rows, no PIN, no confirmations.
* Deposit : tap bills and coins, one click -> a new RIA ID is created and shown in the ID box.
* Redeem : type an RIA ID, then one click pays it out as Cash or To bank.
* Withdraw : tap bills and coins, one click takes the cash out of your own account.
*/
final class AtmMenu extends Menu {
enum Tab { DEPOSIT, REDEEM, WITHDRAW }
private static final int[] BILLS = {10, 11, 12, 19, 20};
private static final int[] COINS = {14, 15, 16};
private static final List<DunemarkDenomination> NOTES = List.of(DunemarkDenomination.NOTE_10, DunemarkDenomination.NOTE_20,
DunemarkDenomination.NOTE_50, DunemarkDenomination.NOTE_100, DunemarkDenomination.NOTE_500);
private static final List<DunemarkDenomination> COIN_VALUES = List.of(DunemarkDenomination.COIN_050,
DunemarkDenomination.COIN_1DM, DunemarkDenomination.COIN_2DM);
private Tab tab;
private final List<DunemarkDenomination> selected = new ArrayList<>();
private String issued; // RIA ID created by the last deposit
private String entered; // RIA ID typed on the Redeem tab
AtmMenu(DuneMenusPlugin plugin, Player p, Menu parent, Tab tab) { super(plugin, p, parent); this.tab = tab; }
String id() { return tab == Tab.WITHDRAW ? "withdraw" : "atm"; }
Skin skin() { return tab == Tab.REDEEM ? Skin.GOLD : Skin.TRAVEL; }
String title() { return "Rihla ATM"; }
String subtitle() { return switch (tab) {
case DEPOSIT -> "Put in cash. You get an RIA ID to keep or give away.";
case REDEEM -> "Got an RIA ID? Type it in and take the money as cash or to your bank.";
case WITHDRAW -> "Take cash out of your own bank account."; }; }
int rows() { return 5; }
private void tabButton(int slot, Material m, String label, Tab t) {
var item = Ui.item(m, (tab == t ? "<green><b>" : "<gray>") + label);
put(slot, tab == t ? Ui.glow(item) : item, c -> { tab = t; selected.clear(); redraw(); });
}
private void add(int slot, DunemarkDenomination denomination) {
put(slot, denomination.displayItem(), c -> {
if (selected.size() < 36) selected.add(denomination);
redraw();
});
}
void draw() {
backdrop(Material.BLACK_STAINED_GLASS_PANE);
for (int s : new int[]{3, 5, 6, 7, 8}) put(s, Ui.pane(Material.CYAN_STAINED_GLASS_PANE));
tabButton(0, Material.EMERALD, "Deposit cash", Tab.DEPOSIT);
tabButton(1, Material.NAME_TAG, "Redeem RIA ID", Tab.REDEEM);
tabButton(2, Material.GOLD_INGOT, "Withdraw cash", Tab.WITHDRAW);
back(36);
if (tab == Tab.REDEEM) drawRedeem(); else drawCash();
}
private void drawCash() {
long minor = selected.stream().mapToLong(DunemarkDenomination::faceMinor).sum();
boolean dep = tab == Tab.DEPOSIT;
String verb = dep ? "Deposit" : "Withdraw";
put(4, Ui.glow(Ui.item(Material.LIGHTNING_ROD, "<aqua><b>" + (minor == 0 ? "Choose bills and coins" : verb + " " + Ui.dm(minor)),
minor == 0 ? "<gray>Tap the bills and coins below" : "<gray>Ready")));
put(9, Ui.item(Material.GREEN_STAINED_GLASS_PANE, "<green><b>Bills"));
put(13, Ui.item(Material.YELLOW_STAINED_GLASS_PANE, "<gold><b>Coins"));
for (int i = 0; i < BILLS.length; i++) add(BILLS[i], NOTES.get(i));
for (int i = 0; i < COINS.length; i++) add(COINS[i], COIN_VALUES.get(i));
put(21, Ui.item(Material.RED_STAINED_GLASS_PANE, "<red><b>Clear"), c -> { selected.clear(); redraw(); });
put(24, minor == 0 ? Ui.item(Material.GRAY_DYE, "<gray>" + verb, "<dark_gray>Choose bills and coins first")
: Ui.glow(Ui.item(dep ? Material.EMERALD : Material.GOLD_INGOT, "<green><b>" + verb + " " + Ui.dm(minor),
dep ? "<gray>Creates a new RIA ID" : "<gray>From your bank account")),
c -> {
if (minor <= 0) return;
var pieces = List.copyOf(selected);
if (dep) load(backend().atmCreateRia(player, pieces), r -> {
say(new Backend.Result(r.ok(), r.message()));
if (r.ok()) { issued = r.riaId(); selected.clear(); }
redraw();
});
else load(backend().bankWithdraw(player, pieces), r -> {
say(r);
if (r.ok()) selected.clear();
redraw();
});
});
// 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>")); });
} else {
put(29, Ui.item(Material.NAME_TAG, "<gold><b>Your account", "<gray>Loading…"));
load(backend().bankAccount(player), a -> put(29, Ui.item(Material.NAME_TAG, "<gold><b>Your account",
"<white>" + a.accountNo(), "<gray>Balance: <white>" + Ui.dm(a.balanceMinor()))));
}
}
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",
"<gray>Click, then type it in chat"), c -> {
end();
ChatPrompt.ask(player, "<gold>Type the RIA ID in chat:", text -> { if (text != null && text.matches("RIA-[A-F0-9]{32}")) entered = text; open(); });
});
for (int s : new int[]{28, 30}) put(s, Ui.pane(Material.YELLOW_STAINED_GLASS_PANE));
put(29, Ui.item(Material.NAME_TAG, "<gold><b>RIA ID", entered == null ? "<dark_gray>Nothing entered yet" : "<white>" + entered));
put(24, Ui.item(Material.GRAY_DYE, "<gray>Take as cash", "<dark_gray>Enter an RIA ID first"));
put(26, Ui.item(Material.GRAY_DYE, "<gray>Send to my bank", "<dark_gray>Enter an RIA ID first"));
if (entered == null) return;
final String id = entered;
load(backend().atmLookup(player, id), v -> {
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; 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; redraw(); }));
});
}
}