I know there's already one in the Code Gallery, but I feel like mine's much more simple.
Also note that this is my first script in any type of SQL, so it could probably be improved a lot.
First, create the table. To do this, put the following in a new Weapon NPC:
PHP Code:
function onCreated() {
requestsql("CREATE TABLE bank (account TEXT UNIQUE NOT NULL, money INT NOT NULL DEFAULT 0", false);
}
(Thanks to Invernest for the column-constraints)
Once that has been done you should delete the weapon since it doesn't server a purpose anymore.
Now create a new class (for multiple locations) or a level NPC (if you want only one bank) and input the following:
PHP Code:
function onPlayerChats() {
if (player.chat.tokenize()[0] == "/deposit") {
temp.amount = int(player.chat.tokenize()[1]);
if (player.rupees < temp.amount) {
say2("You don't have enough Gralat!");
return;
}
if (temp.amount <= 0) {
say2("You can only deposit an amount
over 0!");
return;
}
temp.req = requestsql("SELECT * FROM bank WHERE account = '" @player.account@ "'", true);
if (temp.req.rows.size() <= 0) {
requestsql("INSERT INTO bank VALUES ('" @player.account@ "', " @temp.amount@ ")", false); //creates a new row for a player that hasn't used the bank
}
else {
requestsql("UPDATE bank SET money = (money + " @temp.amount@ ") WHERE account = '" @player.account@ "'", false); //adds the money
}
player.rupees -= temp.amount;
say2("Deposited " @temp.amount@ " Gralat in the
bank.");
}
if (player.chat.tokenize()[0] == "/withdraw") {
temp.amount = int(player.chat.tokenize()[1]);
if (temp.amount <= 0) {
say2("You can only withdraw an amount
over 0!");
return;
}
temp.req = requestsql("SELECT * FROM bank WHERE account = '" @player.account@ "'", true);
if (temp.req.rows.size() <= 0) {
say2("You have never used the bank yet!");
return;
}
if (temp.req.rows[0].money < temp.amount) {
say2("You don't have enough Gralat
in the bank!");
return;
}
requestsql("UPDATE bank SET money = (money - " @temp.amount@ ") WHERE account = '" @player.account@ "'", false); //takes out the money
player.rupees += temp.amount;
say2("Withdrew " @temp.amount@ " Gralat from the
bank.");
}
if (player.chat == "/balance") {
temp.req = requestsql("SELECT * FROM bank WHERE account = '" @player.account@ "'", true);
if (temp.req.rows.size() <= 0) {
say2("You have never used the bank yet!");
return;
}
say2("You currently have a balance
of " @temp.req.rows[0].money@ " Gralat in the bank.");
}
}
If you need to, change any text that says "Gralat" into whatever you call your money.
Now you should be good to go!
Remember to remind people in a sign or something of the commands "/deposit #" (to put money in the bank), "/withdraw #" (to take money out of the bank), and "/balance" (to check what's in the bank).