Factions can be joined when all necessary bots and the server are running

This commit is contained in:
2018-10-29 06:04:14 +11:00
parent 09901ca10f
commit b4e42e4d76
9 changed files with 109 additions and 10 deletions

View File

@@ -83,3 +83,9 @@ RANK_3_THRESHOLD=30
SERVER_ADDRESS="http://127.0.0.1" SERVER_ADDRESS="http://127.0.0.1"
SERVER_PASS_KEY="" SERVER_PASS_KEY=""
SERVER_PORT="4500" SERVER_PORT="4500"
# Database
DATABASE_HOST="localhost"
DATABASE_USER=""
DATABASE_PASSWORD=""

View File

@@ -57,6 +57,8 @@ client.on('ready', async () => {
} }
console.log("Logged in as: " + client.user.username + " - " + client.user.id); 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 // Create an event listener for messages

View File

@@ -57,6 +57,8 @@ client.on('ready', async () => {
} }
console.log("Logged in as: " + client.user.username + " - " + client.user.id); 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 // Create an event listener for messages

View File

@@ -57,6 +57,8 @@ client.on('ready', async () => {
} }
console.log("Logged in as: " + client.user.username + " - " + client.user.id); 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 // Create an event listener for messages

View File

@@ -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 //shared code
let calcRandom = require('../Shared/calc_random'); let calcRandom = require('../Shared/calc_random');
@@ -110,12 +125,54 @@ io.on("connection", async (socket) => {
}); });
socket.on("conversion", async ({ data }, fn) => { socket.on("conversion", async ({ data }, fn) => {
console.log("received a conversion request..."); console.log("received a conversion request... " + data);
//data[0] = user ID //data[0] = user ID
//data[1] = factionRole
if (fn) { //possible arguments to fn: ["joined", "alreadyJoined", "conversionLocked", "newUser"]
fn("newUser"); //["joined", "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");
});
}
})
});
}); });
}); });

View File

@@ -14,6 +14,7 @@
"dependencies": { "dependencies": {
"dotenv": "^6.1.0", "dotenv": "^6.1.0",
"forever": "^0.15.3", "forever": "^0.15.3",
"mysql": "^2.16.0",
"socket.io": "^2.1.1", "socket.io": "^2.1.1",
"socketio-auth": "^0.1.1" "socketio-auth": "^0.1.1"
} }

View File

@@ -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
);

View File

View File

@@ -77,11 +77,6 @@ exports.ChangeFaction = function(client, factionRole, channel, member, fn) {
member = guild.members.get(user.id); member = guild.members.get(user.id);
} }
if (member.roles.has(factionRole)) {
//can't change to this faction
fn("alreadyJoined");
}
let handleResponse = async function(response) { let handleResponse = async function(response) {
if (response === "conversionLocked") { //can't change too fast if (response === "conversionLocked") { //can't change too fast
fn(response); fn(response);
@@ -97,5 +92,5 @@ exports.ChangeFaction = function(client, factionRole, channel, member, fn) {
fn(response); fn(response);
}; };
dataRequest.OnServerData("conversion", handleResponse, member.user.id); dataRequest.OnServerData("conversion", handleResponse, member.user.id, factionRole);
} }