mirror of
https://github.com/Ratstail91/SANCTUM.git
synced 2025-11-29 02:24:27 +11:00
Wallet balance is working
This commit is contained in:
@@ -139,6 +139,12 @@ function processBasicCommands(client, message) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case "wallet": //DEBUGGING
|
||||||
|
shared.OnServerData("account", (amount) => {
|
||||||
|
shared.SendPublicMessage(client, message.channel, "Wallet: " + amount);
|
||||||
|
}, message.author.id);
|
||||||
|
return true;
|
||||||
|
|
||||||
//ADAM and the faction leaders print the intros in the gate
|
//ADAM and the faction leaders print the intros in the gate
|
||||||
//TODO: prune the unneeded intros from each bot
|
//TODO: prune the unneeded intros from each bot
|
||||||
case "intro":
|
case "intro":
|
||||||
|
|||||||
@@ -42,90 +42,31 @@ io.on("connection", async (socket) => {
|
|||||||
console.log(socket.client.username + " disconnected");
|
console.log(socket.client.username + " disconnected");
|
||||||
});
|
});
|
||||||
|
|
||||||
//update the playerbase's stamina on command
|
socket.on("updateStamina", handleUpdateStamina);
|
||||||
socket.on("updateStamina", async ({ userID, data }) => {
|
socket.on("conversion", handleConversion);
|
||||||
console.log("updating stamina for all users...");
|
socket.on("checkin", handleCheckin);
|
||||||
//TODO: update the stamina
|
socket.on("account", handleAccount);
|
||||||
|
socket.on("transfer", handleTransfer);
|
||||||
|
socket.on("userStats", handleUserStats);
|
||||||
|
socket.on("addXP", handleAddXP);
|
||||||
|
socket.on("levelUp", handleLevelUp);
|
||||||
|
});
|
||||||
|
|
||||||
|
//listen
|
||||||
|
server.listen(process.env.SERVER_PORT);
|
||||||
|
console.log("listening on port " + process.env.SERVER_PORT);
|
||||||
|
|
||||||
|
//update the playerbase's stamina on command
|
||||||
|
async function handleUpdateStamina({ userID, data }) {
|
||||||
|
let query = "UPDATE users SET stamina = stamina + 1 WHERE stamina < maxStamina;";
|
||||||
|
dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log("updated stamina for all users");
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
//handle checkin
|
//handle initial faction join and faction conversions
|
||||||
socket.on("checkin", async ({ data }, fn) => {
|
async function handleConversion({ data }, fn) {
|
||||||
console.log("received a checkin request...");
|
|
||||||
//TODO: handle checkins (grant crystal bonus)
|
|
||||||
//TODO: handle XP (grant 1 XP)
|
|
||||||
|
|
||||||
if (fn) {
|
|
||||||
fn("available", calcRandom.Random(4, 9)); //TODO: ["available", time since last checkin], randomAmount
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//handle account requests
|
|
||||||
socket.on("account", async ({ data }, fn) => {
|
|
||||||
console.log("received an account request...");
|
|
||||||
//data[0] = ID of the person to check
|
|
||||||
|
|
||||||
if (fn) {
|
|
||||||
fn(0); //TODO: accountBalance
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//handle transfering data between accounts
|
|
||||||
socket.on("transfer", async ({ data }, fn) => {
|
|
||||||
console.log("received a transfer request...");
|
|
||||||
//data[0] = ID of the source account
|
|
||||||
//data[1] = ID of the destination account
|
|
||||||
//data[2] = amount to send
|
|
||||||
|
|
||||||
if (fn) {
|
|
||||||
fn("failure"); //TODO: ["success", "failure"]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//handle the user stats
|
|
||||||
socket.on("userStats", async ({ data }, fn) => {
|
|
||||||
console.log("received a userStats request...");
|
|
||||||
//data[0] = user ID
|
|
||||||
|
|
||||||
//NOTE: build a temporary structure to pass back
|
|
||||||
let stats = {
|
|
||||||
strength: 0,
|
|
||||||
speed: 0,
|
|
||||||
stamina: 0,
|
|
||||||
health: 0,
|
|
||||||
maxStamina: 0,
|
|
||||||
maxHealth: 0,
|
|
||||||
wallet: 0,
|
|
||||||
experience: 0,
|
|
||||||
level: 0,
|
|
||||||
levelPercent: 0,
|
|
||||||
statPoints: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
if (fn) {
|
|
||||||
fn(stats);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//DEBUGGING?
|
|
||||||
socket.on("addXP", async ({ userID, data }) => {
|
|
||||||
console.log("received an addXP request...");
|
|
||||||
//data[0] = amount
|
|
||||||
|
|
||||||
//TODO: add an amount of XP to a user account
|
|
||||||
});
|
|
||||||
|
|
||||||
//handle levelling up
|
|
||||||
socket.on("levelUp", async ({ data }, fn) => {
|
|
||||||
console.log("received a levelUp request...");
|
|
||||||
//data[0] = user ID
|
|
||||||
|
|
||||||
if (fn) {
|
|
||||||
fn("none", 0, 0); //["none", "levelUp"], level, statPoints
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on("conversion", async ({ data }, fn) => {
|
|
||||||
console.log("received a conversion request... " + data);
|
|
||||||
//data[0] = user ID
|
//data[0] = user ID
|
||||||
//data[1] = factionRole
|
//data[1] = factionRole
|
||||||
|
|
||||||
@@ -154,12 +95,12 @@ io.on("connection", async (socket) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//check if enough time has passed to join a new faction
|
//check if enough time has passed to join a new faction
|
||||||
let query = `SELECT NOW() - factionChanged FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
let query = `SELECT TIME_TO_SEC(TIMEDIFF(NOW(), factionChanged)) FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
|
||||||
return dbConnection.query(query, (err, result) => {
|
return dbConnection.query(query, (err, result) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log(result[0]['NOW() - factionChanged ']);
|
console.log(result[0]['TIME_TO_SEC(TIMEDIFF(NOW(), factionChanged))']);
|
||||||
if(result[0]['NOW() - factionChanged '] < 60) { //faction time change in seconds TODO: 7 days
|
if(result[0]['TIME_TO_SEC(TIMEDIFF(NOW(), factionChanged))'] < 60 * 60 * 24 * 7) { //7 days
|
||||||
console.log("conversionLocked");
|
console.log("conversionLocked");
|
||||||
return fn("conversionLocked"); //too soon
|
return fn("conversionLocked"); //too soon
|
||||||
} else {
|
} else {
|
||||||
@@ -167,15 +108,138 @@ io.on("connection", async (socket) => {
|
|||||||
query = `UPDATE users SET faction = ${data[1]}, factionChanged = NOW() WHERE userID='${data[0]}';`;
|
query = `UPDATE users SET faction = ${data[1]}, factionChanged = NOW() WHERE userID='${data[0]}';`;
|
||||||
return dbConnection.query(query, (err, result) => {
|
return dbConnection.query(query, (err, result) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
console.log("joined");
|
console.log("joined"); //TODO: convert these to database logs
|
||||||
return fn("joined");
|
return fn("joined");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
});
|
|
||||||
|
|
||||||
//listen
|
//handle checkin, and add 1 XP
|
||||||
server.listen(process.env.SERVER_PORT);
|
async function handleCheckin({ data }, fn) {
|
||||||
console.log("listening on port " + process.env.SERVER_PORT);
|
//handle checkins (grant crystal bonus)
|
||||||
|
//TODO: handle XP (grant 1 XP)
|
||||||
|
|
||||||
|
//arguments to fn: ["available", time since last checkin], randomAmount
|
||||||
|
|
||||||
|
let randomAmount = calcRandom.Random(4, 9);
|
||||||
|
|
||||||
|
let query = `SELECT TIME_TO_SEC(TIMEDIFF(NOW(), lastCheckin)) FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log(result);
|
||||||
|
|
||||||
|
if (result[0]['TIME_TO_SEC(TIMEDIFF(NOW(), lastCheckin))'] == null || result[0]['TIME_TO_SEC(TIMEDIFF(NOW(), lastCheckin))'] > 60 * 60 * 22) { //22 hours
|
||||||
|
let query = `UPDATE users SET lastCheckin = NOW(), wallet = wallet + ${randomAmount} WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
return fn("available", randomAmount);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return fn(result[0]['TIME_TO_SEC(TIMEDIFF(NOW(), lastCheckin))']); //TODO: Time ago function
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//handle account requests
|
||||||
|
async function handleAccount({ data }, fn) {
|
||||||
|
//data[0] = ID of the person to check
|
||||||
|
|
||||||
|
let query = `SELECT wallet FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
fn(result[0].wallet);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//handle transfering wallet balance between accounts
|
||||||
|
async function handleTransfer({ data }, fn) {
|
||||||
|
console.log("received a transfer request...");
|
||||||
|
//data[0] = ID of the source account
|
||||||
|
//data[1] = ID of the destination account
|
||||||
|
//data[2] = amount to send
|
||||||
|
|
||||||
|
//parameters to fn: ["success", "failure"]
|
||||||
|
|
||||||
|
//check there's enough in the sender's wallet
|
||||||
|
let query = `SELECT wallet - ${data[2]} FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
//too little in there
|
||||||
|
if (result[0][`wallet - ${data[2]}`] < 0) {
|
||||||
|
return fn("failure");
|
||||||
|
}
|
||||||
|
|
||||||
|
//check the recipient is real
|
||||||
|
let query = `SELECT * FROM users WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (result.length == 0) {
|
||||||
|
return fn("failure");
|
||||||
|
}
|
||||||
|
|
||||||
|
//subtract from the sender
|
||||||
|
let query = `UPDATE users SET wallet = wallet - ${data[2]} WHERE userID='${data[0]}' LIMIT 1;`;
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
//add to the recipient
|
||||||
|
let query = `UPDATE users SET wallet = wallet + ${data[2]} WHERE userID='${data[1]}' LIMIT 1;`;
|
||||||
|
return dbConnection.query(query, (err, result) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
//TODO: log here
|
||||||
|
//finally
|
||||||
|
return fn("success");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//handle the user stats
|
||||||
|
async function handleUserStats({ data }, fn) {
|
||||||
|
console.log("received a userStats request...");
|
||||||
|
//data[0] = user ID
|
||||||
|
|
||||||
|
//NOTE: build a temporary structure to pass back
|
||||||
|
let stats = {
|
||||||
|
strength: 0,
|
||||||
|
speed: 0,
|
||||||
|
stamina: 0,
|
||||||
|
health: 0,
|
||||||
|
maxStamina: 0,
|
||||||
|
maxHealth: 0,
|
||||||
|
wallet: 0,
|
||||||
|
experience: 0,
|
||||||
|
level: 0,
|
||||||
|
levelPercent: 0,
|
||||||
|
statPoints: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
if (fn) {
|
||||||
|
fn(stats);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//DEBUGGING?
|
||||||
|
async function handleAddXP({ userID, data }) {
|
||||||
|
console.log("received an addXP request...");
|
||||||
|
//data[0] = amount
|
||||||
|
|
||||||
|
//TODO: add an amount of XP to a user account
|
||||||
|
}
|
||||||
|
|
||||||
|
//handle levelling up
|
||||||
|
async function handleLevelUp({ data }, fn) {
|
||||||
|
console.log("received a levelUp request...");
|
||||||
|
//data[0] = user ID
|
||||||
|
|
||||||
|
if (fn) {
|
||||||
|
fn("none", 0, 0); //["none", "levelUp"], level, statPoints
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ CREATE TABLE IF NOT EXISTS users (
|
|||||||
strength int NOT NULL DEFAULT 5,
|
strength int NOT NULL DEFAULT 5,
|
||||||
speed int NOT NULL DEFAULT 5,
|
speed int NOT NULL DEFAULT 5,
|
||||||
|
|
||||||
|
upgradePoints int NOT NULL DEFAULT 0,
|
||||||
|
|
||||||
wallet int NOT NULL DEFAULT 0,
|
wallet int NOT NULL DEFAULT 0,
|
||||||
upgradePoints int NOT NULL DEFAULT 0
|
lastCheckin DATETIME NULL
|
||||||
);
|
);
|
||||||
Reference in New Issue
Block a user