From c724ae75cd3c1bb73d82c93f5b9c71d8c71e7fc7 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 3 Nov 2018 00:12:18 +1100 Subject: [PATCH] Finished Mori --- BOT_Mori/dialog.json | 8 +++--- BOT_Mori/mori.js | 16 +++++++++-- SERVER_City/city.js | 63 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/BOT_Mori/dialog.json b/BOT_Mori/dialog.json index e01533b..1a58bbe 100644 --- a/BOT_Mori/dialog.json +++ b/BOT_Mori/dialog.json @@ -12,14 +12,14 @@ "Good day. All the travelers are back up. Time for some sleep. Goodnight everybody." ], - "heal": "Here's what I've got at the moment. My prices are based on availability, it's hard to find stuff these days.", + "healHeading": "Here's what I've got at the moment. My prices are based on availability, it's hard to find stuff these days.", "healSuccess": "I've applied a {1} via nanotech .\n**-{2}**:crystals: | **{3}**HP.", "healFailure": "Sorry, not sure I understand what procedure you'd like to purchase.", - "healLocked": "Sorry, you don't meet the requirements for this procedure.", "healNotEnoughInWallet": "Sorry, looks like you don't have the funds for that.", - "healKnockedOut": "You're currently knoecked out (0HP). You require a revive procdure to heal immediately, or you can wait until 7 AM when I revive everyone.", - "healFullHealth": "Looks like you're already full health. Why would you want to heal?", + "healNotKnockedOut": "Sorry, you don't meet the requirements for this procedure.", + "healKnockedOut": "You're currently knocked out (0HP). You require a revive procdure to heal immediately, or you can wait until 7 AM when I revive everyone.", + "healFullHealth": "Looks like you're already at full health. Why would you want to heal?", "noResult": [ "Try again, genius.", diff --git a/BOT_Mori/mori.js b/BOT_Mori/mori.js index 868ee15..905c38c 100644 --- a/BOT_Mori/mori.js +++ b/BOT_Mori/mori.js @@ -72,7 +72,7 @@ client.on('ready', async () => { console.log("Trying to revive..."); shared.OnServerData("reviveAll", () => { //TODO: server-side reviveAll command console.log("Revive successful"); - shared.SendPublicMessage(client, process.env.TAVERN_CHANNEL_ID, dialog("reviveAll")); + shared.SendPublicMessage(client, process.env.TAVERN_CHANNEL_ID, dialog("reviveAll")); //TODO: add a reference to Alexis in the dialog here }); resetInventory(itemCount); }); @@ -125,6 +125,10 @@ function processBasicCommands(client, message) { } return true; + case "help": + printTreatments(message.author, message.channel); + return true; + case "heal": if (!args[0]) { printTreatments(message.author, message.channel); @@ -185,7 +189,7 @@ function printTreatments(user, channel) { .setDescription(treatmentMessage) .setFooter(`${user.username}, you have ${stats.wallet} crystals. Use !heal [OPTION] to buy.`); - shared.SendPublicMessage(client, user, channel, dialog("heal")); + shared.SendPublicMessage(client, user, channel, dialog("healHeading")); channel.send({ embed }); } @@ -196,6 +200,14 @@ function processHealCommand(user, channel, args) { //get the selected treatment let selectedTreatment = availableTreatments.filter((treatment) => treatment[0].toLowerCase() === args[0].toLowerCase())[0]; + if (!selectedTreatment) { + shared.SendPublicMessage(client, user, channel, dialog("healFailure")); + } + + let handleResponse = function(response) { + shared.SendPublicMessage(client, user, channel, dialog(response, selectedTreatment[0], selectedTreatment[1], selectedTreatment[2])); + } + if (selectedTreatment[3]) { //should it be a revive command? shared.OnServerData("revive", handleResponse, user.id, selectedTreatment[1], selectedTreatment[2]); } else { diff --git a/SERVER_City/city.js b/SERVER_City/city.js index 8666163..a0afe9f 100644 --- a/SERVER_City/city.js +++ b/SERVER_City/city.js @@ -315,7 +315,18 @@ async function handleRevive({ data }, fn) { //data[1] = cost //data[2] = amount (potentially percentage) - //TODO + //WARNING: copy/paste + let query = `SELECT health, maxHealth, wallet FROM users WHERE userID=${data[0]} LIMIT 1;`; + return dbConnection.query(query, (err, result) => { + if (err) throw err; + + //not knocked out + if (result[0].health != 0) { + return fn("healNotKnockedOut"); + } + + return innerHeal(data, fn, result, "revive"); + }); } //handle healing a specific player @@ -325,7 +336,53 @@ async function handleHeal({ data }, fn) { //data[1] = cost //data[2] = amount (potentially percentage) - //TODO + let query = `SELECT health, maxHealth, wallet FROM users WHERE userID=${data[0]} LIMIT 1;`; + return dbConnection.query(query, (err, result) => { + if (err) throw err; + + //not knocked out + if (result[0].health == 0) { + return fn("healKnockedOut"); + } + + return innerHeal(data, fn, result, "heal"); + }); +} + +//avoid copy/paste in the healing functions +function innerHeal(data, fn, result, logType = 'unknown') { + //not enough money + if (result[0].wallet < data[1]) { + return fn("healNotEnoughInWallet"); + } + + if (result[0].health == result[0].maxHealth) { + return fn("healFullHealth"); + } + + //parse out the amount that needs regening + let regenAmount = data[2]; + + if (regenAmount[regenAmount.length-1] == "%") { + regenAmount = regenAmount.slice(0, -1); + regenAmount = Math.floor(parseFloat(regenAmount) / 100 * result[0].maxHealth); + } else { + regenAmount = Math.floor(parseFloat(regenAmount)); + } + + //actually do the regen + let newHealth = Math.min(result[0].health + regenAmount, result[0].maxHealth); //I tried making this an SQL function, didn't work + let query = `UPDATE users SET health = ${newHealth}, wallet = wallet - ${data[1]} WHERE userID='${data[0]}' LIMIT 1;`; + return dbConnection.query(query, (err, result) => { + if (err) throw err; + + //logging touch + dbConnection.query(`SELECT health, maxHealth FROM users WHERE userID='${data[0]}' LIMIT 1;`, (err, result) => { + dbLog(data[0], `health ${logType}`, `healed ${regenAmount} - ${result[0].health}/${result[0].maxHealth}`); + }); + + return fn("healSuccess"); + }); } //utility functions @@ -365,7 +422,7 @@ function calculateTimeAgo(seconds) { } if (seconds < 60 * 60) { - return "this hour"; + return "just this hour"; } if (seconds < 60 * 60 * 24) {