Skip to content

Commit

Permalink
Some more bank interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
benbernard committed Oct 9, 2019
1 parent d4c1a02 commit cb5925a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 18 deletions.
37 changes: 32 additions & 5 deletions netrun/bernard/bank-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,52 @@ import {convertStrToMoney} from "./utils.js";
class ThisScript extends TK.Script {
async perform() {
let bank = new BankMessaging(this.ns);
this.bank = bank;

let action = this.pullFirstArg();

if (action === "info") {
let response = await bank.walletInfo(this.args[0]);
this.tlog(
`Wallet: ${response.name}, portion: ${
response.portion
}, amount: ${this.cFormat(response.amount)}`
);
this.tlog(this.formatWallet(response));
} else if (action === "deposit") {
let wallet = this.args[0];
let amount = convertStrToMoney(this.args[1]);

this.tlog(`Depositing Amount: ${this.cFormat(amount)} to: ${wallet}`);
let response = await bank.deposit(wallet, amount);
this.tlog(`Deposit Response: ${JSON.stringify(response)}`);
} else if (action === "set") {
let sets = [];
for (let arg of this.args) {
let [name, amount] = arg.split("=");
sets.push([name, convertStrToMoney(amount)]);
}
let response = await bank.setBalances(sets);
this.tlog(`Set Balance Response: ${JSON.stringify(response)}`);
await this.printAllWallets();
} else if (action === "all" || action === "infos") {
await this.printAllWallets();
} else if (action === "clear" || action === "infos") {
let response = await bank.clear();
this.tlog(`Clear response: ${JSON.stringify(response)}`);
await this.printAllWallets();
} else {
this.tlog(`Unkown bank request: ${action}`);
}
}

async printAllWallets() {
let wallets = (await this.bank.allWallets()).wallets;
wallets.forEach(wallet => {
this.tlog(this.formatWallet(wallet));
});
}

formatWallet(wallet) {
return `Wallet: ${wallet.name}, portion: ${
wallet.portion
}, amount: ${this.cFormat(wallet.amount)}`;
}
}

export let main = ThisScript.runner();
29 changes: 28 additions & 1 deletion netrun/bernard/bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ class ThisScript extends TK.Script {
let wallet = this.wallet(req.data.wallet);
let amount = req.data.amount;
this.deposit(wallet, amount, req);
} else if (type === BankMessaging.SET_BALANCES) {
let amountSum = this.walletTotal();
for (let [name, amount] of req.data.sets) {
let wallet = this.wallet(name);
amountSum = amountSum - wallet.amount + amount;
}

let success = false;
if (amountSum < this.actualMoney()) {
success = true;

for (let [name, amount] of req.data.sets) {
let wallet = this.wallet(name);
wallet.amount = amount;
}
}

this.messaging.sendResponse(req, {success});
} else if (type === BankMessaging.ALL_WALLETS) {
return this.messaging.sendResponse(req, {wallets: this.wallets});
} else if (type === BankMessaging.CLEAR) {
this.initializeState();
this.messaging.sendResponse(req, {success: true});
} else {
this.tlog(`Bank received unknown message type: ${type}`);
}
Expand All @@ -102,8 +125,12 @@ class ThisScript extends TK.Script {
this.messaging.sendResponse(req, {success: true});
}

walletTotal() {
return this.wallets.reduce((sum, e) => e.amount + sum, 0);
}

unallocatedMoney() {
let walletTotal = this.wallets.reduce((sum, e) => e.amount + sum, 0);
let walletTotal = this.walletTotal();
return this.actualMoney() - walletTotal;
}

Expand Down
11 changes: 11 additions & 0 deletions netrun/bernard/baseScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export class NSObject {
}
}

uuid() {
return (
Math.random()
.toString(36)
.substring(2, 15) +
Math.random()
.toString(36)
.substring(2, 15)
);
}

tlog(...msgs) {
this.ns.tprint(msgs.join(" "));
}
Expand Down
2 changes: 1 addition & 1 deletion netrun/bernard/gangs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Gang extends GangNSObject {
}

recruit() {
let name = `Member-${this.memberCount()}`;
let name = `Member-${this.uuid()}`;
let result = this.gang.recruitMember(name);
if (!result) throw new Error(`Could not recruit ${name}`);

Expand Down
31 changes: 20 additions & 11 deletions netrun/bernard/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ export class Messaging extends NSObject {
return this.ns.getPortHandle(this.responsePort);
}

uuid() {
return (
Math.random()
.toString(36)
.substring(2, 15) +
Math.random()
.toString(36)
.substring(2, 15)
);
}

createMessage(data, metadata = {}) {
return {
uuid: this.uuid(),
Expand Down Expand Up @@ -114,9 +103,29 @@ export class BankMessaging extends Messaging {
amount,
});
}

// Pairs of wallet name and amount to set to
setBalances(pairs) {
return this.sendAndWait({
type: BankMessaging.SET_BALANCES,
sets: pairs,
});
}

allWallets() {
return this.sendAndWait({type: BankMessaging.ALL_WALLETS});
}

clear() {
return this.sendAndWait({type: BankMessaging.CLEAR});
}
}

// Message types
BankMessaging.WALLET_INFO = "wallet_info";
BankMessaging.PURCHASE_SERVER = "purchase_server";
BankMessaging.PURCHASE_EQUIPMENT = "purchase_equipment";
BankMessaging.DEPOSIT = "deposit";
BankMessaging.SET_BALANCES = "set_balances";
BankMessaging.ALL_WALLETS = "all_wallets";
BankMessaging.CLEAR = "clear";

0 comments on commit cb5925a

Please sign in to comment.