From b4e42e4d76883555a1f9020a4f5055d84761bf61 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Oct 2018 06:04:14 +1100 Subject: [PATCH] Factions can be joined when all necessary bots and the server are running --- .envdev | 6 +++ ADAM_CptMon/adam-cptmon.js | 2 + ADAM_Dairo/adam-dairo.js | 2 + ADAM_Kamala/adam-kamala.js | 2 + SERVER_City/city.js | 65 +++++++++++++++++++++++-- SERVER_City/package.json | 1 + SERVER_City/scripts/create_database.sql | 34 +++++++++++++ SERVER_City/scripts/test_database.sql | 0 Shared/factions.js | 7 +-- 9 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 SERVER_City/scripts/create_database.sql create mode 100644 SERVER_City/scripts/test_database.sql diff --git a/.envdev b/.envdev index 2c9c61e..34523dc 100644 --- a/.envdev +++ b/.envdev @@ -83,3 +83,9 @@ RANK_3_THRESHOLD=30 SERVER_ADDRESS="http://127.0.0.1" SERVER_PASS_KEY="" SERVER_PORT="4500" + +# Database +DATABASE_HOST="localhost" +DATABASE_USER="" +DATABASE_PASSWORD="" + diff --git a/ADAM_CptMon/adam-cptmon.js b/ADAM_CptMon/adam-cptmon.js index bfae93a..6cc040c 100644 --- a/ADAM_CptMon/adam-cptmon.js +++ b/ADAM_CptMon/adam-cptmon.js @@ -57,6 +57,8 @@ client.on('ready', async () => { } console.log("Logged in as: " + client.user.username + " - " + client.user.id); + + shared.ConnectToServer(client.user.username, process.env.SERVER_ADDRESS, process.env.SERVER_PORT, process.env.SERVER_PASS_KEY); }); // Create an event listener for messages diff --git a/ADAM_Dairo/adam-dairo.js b/ADAM_Dairo/adam-dairo.js index 2cc1a75..f0c8b6e 100644 --- a/ADAM_Dairo/adam-dairo.js +++ b/ADAM_Dairo/adam-dairo.js @@ -57,6 +57,8 @@ client.on('ready', async () => { } console.log("Logged in as: " + client.user.username + " - " + client.user.id); + + shared.ConnectToServer(client.user.username, process.env.SERVER_ADDRESS, process.env.SERVER_PORT, process.env.SERVER_PASS_KEY); }); // Create an event listener for messages diff --git a/ADAM_Kamala/adam-kamala.js b/ADAM_Kamala/adam-kamala.js index cc5ecd3..f636d47 100644 --- a/ADAM_Kamala/adam-kamala.js +++ b/ADAM_Kamala/adam-kamala.js @@ -57,6 +57,8 @@ client.on('ready', async () => { } console.log("Logged in as: " + client.user.username + " - " + client.user.id); + + shared.ConnectToServer(client.user.username, process.env.SERVER_ADDRESS, process.env.SERVER_PORT, process.env.SERVER_PASS_KEY); }); // Create an event listener for messages diff --git a/SERVER_City/city.js b/SERVER_City/city.js index 6f7de73..744db98 100644 --- a/SERVER_City/city.js +++ b/SERVER_City/city.js @@ -16,6 +16,21 @@ ioAuth(io, { } }); +//mysql +let mysql = require("mysql"); + +let dbConnection = mysql.createConnection({ + host: process.env.DATABASE_HOST, + user: process.env.DATABASE_USER, + password: process.env.DATABASE_PASSWORD +}); + +dbConnection.connect((err) => { + if (err) throw err; + console.log("Connected to the database"); + dbConnection.query("USE sanctum;"); +}); + //shared code let calcRandom = require('../Shared/calc_random'); @@ -110,12 +125,54 @@ io.on("connection", async (socket) => { }); socket.on("conversion", async ({ data }, fn) => { - console.log("received a conversion request..."); + console.log("received a conversion request... " + data); //data[0] = user ID + //data[1] = factionRole - if (fn) { - fn("newUser"); //["joined", "conversionLocked", "newUser"] - } + //possible arguments to fn: ["joined", "alreadyJoined", "conversionLocked", "newUser"] + + //find the last time this user converted + let query = `SELECT faction FROM users WHERE userID='${data[0]}' LIMIT 1;`; + + return dbConnection.query(query, (err, result) => { + if (err) throw err; + + //check if this is a new user + if (result.length === 0) { + let query = `INSERT INTO users (userID, faction, factionChanged) VALUES (${data[0]}, ${data[1]}, NOW());`; + return dbConnection.query(query, (err, result) => { + if (err) throw err; + console.log("new user"); + return fn("newUser"); + }); + } + + //check if already joined this faction + if (result[0].faction == data[1]) { //faction == factionRole + console.log("alreadyJoined"); + return fn("alreadyJoined"); + } + + //check if enough time has passed to join a new faction + let query = `SELECT NOW() - factionChanged FROM users WHERE userID='${data[0]}' LIMIT 1;`; + + return dbConnection.query(query, (err, result) => { + if (err) throw err; + console.log(result[0]['NOW() - factionChanged ']); + if(result[0]['NOW() - factionChanged '] < 60) { //faction time change in seconds TODO: 7 days + console.log("conversionLocked"); + return fn("conversionLocked"); //too soon + } else { + //update the database with the join + query = `UPDATE users SET faction = ${data[1]}, factionChanged = NOW() WHERE userID='${data[0]}';`; + return dbConnection.query(query, (err, result) => { + if (err) throw err; + console.log("joined"); + return fn("joined"); + }); + } + }) + }); }); }); diff --git a/SERVER_City/package.json b/SERVER_City/package.json index bdaf3f0..9db356b 100644 --- a/SERVER_City/package.json +++ b/SERVER_City/package.json @@ -14,6 +14,7 @@ "dependencies": { "dotenv": "^6.1.0", "forever": "^0.15.3", + "mysql": "^2.16.0", "socket.io": "^2.1.1", "socketio-auth": "^0.1.1" } diff --git a/SERVER_City/scripts/create_database.sql b/SERVER_City/scripts/create_database.sql new file mode 100644 index 0000000..74360e3 --- /dev/null +++ b/SERVER_City/scripts/create_database.sql @@ -0,0 +1,34 @@ +CREATE DATABASE IF NOT EXISTS sanctum; + +USE sanctum; + +CREATE TABLE IF NOT EXISTS log ( + id int NOT NULL AUTO_INCREMENT PRIMARY KEY, + discordID bigint, + time TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, + type varchar(32), + data varchar(255) +); + +CREATE TABLE IF NOT EXISTS users ( + id int NOT NULL AUTO_INCREMENT PRIMARY KEY, + userID bigint NOT NULL, + timeJoined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + + faction bigint, + factionChanged DATETIME NULL, + + level int NOT NULL DEFAULT 1, + experience int NOT NULL DEFAULT 0, + + maxHealth int NOT NULL DEFAULT 100, + health int NOT NULL DEFAULT 100, + maxStamina int NOT NULL DEFAULT 5, + stamina int NOT NULL DEFAULT 5, + + strength int NOT NULL DEFAULT 5, + speed int NOT NULL DEFAULT 5, + + wallet int NOT NULL DEFAULT 0, + upgradePoints int NOT NULL DEFAULT 0 +); \ No newline at end of file diff --git a/SERVER_City/scripts/test_database.sql b/SERVER_City/scripts/test_database.sql new file mode 100644 index 0000000..e69de29 diff --git a/Shared/factions.js b/Shared/factions.js index 4cbd18b..5891606 100644 --- a/Shared/factions.js +++ b/Shared/factions.js @@ -77,11 +77,6 @@ exports.ChangeFaction = function(client, factionRole, channel, member, fn) { member = guild.members.get(user.id); } - if (member.roles.has(factionRole)) { - //can't change to this faction - fn("alreadyJoined"); - } - let handleResponse = async function(response) { if (response === "conversionLocked") { //can't change too fast fn(response); @@ -97,5 +92,5 @@ exports.ChangeFaction = function(client, factionRole, channel, member, fn) { fn(response); }; - dataRequest.OnServerData("conversion", handleResponse, member.user.id); + dataRequest.OnServerData("conversion", handleResponse, member.user.id, factionRole); }