Created an empty shell of a server

This commit is contained in:
2018-10-28 23:15:40 +11:00
parent ba4316ef3d
commit 729209288d
17 changed files with 299 additions and 185 deletions

View File

@@ -82,3 +82,4 @@ RANK_3_THRESHOLD=30
# Server
SERVER_ADDRESS="http://127.0.0.1"
SERVER_PASS_KEY=""
SERVER_PORT="4500"

View File

@@ -60,7 +60,7 @@ client.on('ready', async () => {
console.log("Logged in as: " + client.user.username + " - " + client.user.id);
//ADAM updates stamina (1) and health by 1% every 2 min.
cron.schedule('*/2 * * * *', () => {
cron.schedule('*/2 * * * *', () => { //TODO: move this to Mori
console.log('Updating STAMINA every 2 min.');
shared.SendServerData("updateStamina");
});

View File

@@ -3,7 +3,6 @@ exports = module.exports = {};
let dataRequest = require("../Shared/data_request");
let discord = require('discord.js');
let shared = require("../Shared/shared");
let calcRandom = require('../Shared/calc_random');
//ProcessGameplayCommands
//client - discord.js client
@@ -17,66 +16,15 @@ exports.ProcessGameplayCommands = function(client, message, dialog) {
switch (command) {
case "checkin":
let checkinAmount = calcRandom.Random(4, 9);
let checkInResponse = String(dataRequest.SendServerData("checkin", message.author.id, checkinAmount));
if (checkInResponse === "available") {
shared.SendPublicMessage(client, message.author, message.channel, dialog("checkin", checkinAmount));
shared.AddXP(client, message.author, 1); //1XP
exports.HandleLevelUp(client, message.member, message.channel, dialog);
} else {
shared.SendPublicMessage(client, message.channel, dialog("checkinLocked", message.author.id, checkInResponse));
}
exports.ProcessCheckinCommand(client, message.member, message.channel, dialog);
return true;
case "give": //TODO: fold this code into a function
let amount = Math.floor(parseFloat(args[0]));
if (isNaN(amount)) {
shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id));
return true;
}
//not enough
if (amount <= 0) {
shared.SendPublicMessage(client, message.channel, dialog("giveNotAboveZero", message.author.id));
return true;
}
//didn't mention anyone
if (message.mentions.members.size == 0) {
shared.SendPublicMessage(client, message.channel, dialog("giveInvalidUser", message.author.id));
return true;
}
let targetMember = message.mentions.members.first();
//can't give to yourself
if (targetMember.id === message.author.id) {
shared.SendPublicMessage(client, message.channel, dialog("giveInvalidUserSelf", message.author.id));
return true;
}
let accountBalance = dataRequest.LoadServerData("account", message.author.id);
//not enough money in account
if (accountBalance < amount) {
shared.SendPublicMessage(client, message.channel, dialog("giveNotEnoughInAccount", message.author.id));
return true;
}
//try to send the money
if (dataRequest.SendServerData("transfer", message.author.id, targetMember.id, amount) != "success") {
shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id));
return true;
}
//print the success message
shared.SendPublicMessage(client, message.author, message.channel, dialog("giveSuccessful", targetMember.id, amount));
case "give":
exports.ProcessGiveCommand(client, message, args, dialog);
return true;
case "stats":
exports.ProcessStatsCommand(client, message.member, message.channel, dialog);
return true;
}
@@ -92,16 +40,15 @@ exports.ProcessGameplayCommands = function(client, message, dialog) {
//factionShorthand - the shorthand name of the new faction (TEMPORARY)
exports.ProcessFactionChangeAttempt = function(client, message, factionRole, dialog, factionShorthand) {
//tailor this for each faction leader?
shared.ChangeFaction(client, factionRole, message.channel, message.member)
.then(result => {
switch (result) {
let handleResponse = async function(response) {
switch (response) {
case "alreadyJoined":
shared.SendPublicMessage(client, message.channel, dialog("alreadyJoined" + factionShorthand, message.author.id));
break;
case "hasConvertedToday":
case "conversionLocked":
shared.SendPublicMessage(client, message.channel, dialog("conversionLocked", message.author.id));
break;
case "createdUser":
case "newUser":
shared.SendPublicMessage(client, message.author, shared.GetFactionChannel(factionRole), dialog("newUserPublicMessage", shared.GetFactionName(factionRole), shared.GetFactionChannel(factionRole)));
shared.SendPrivateMessage(client, message.author, dialog("newUserPrivateMessage", dialog("newUserPrivateMessageRemark" + factionShorthand)));
break;
@@ -112,9 +59,83 @@ exports.ProcessFactionChangeAttempt = function(client, message, factionRole, dia
//DEBUGGING
console.log("processFactionChangeAttempt failed:" + result);
}
})
.catch(console.error);
return true;
}
shared.ChangeFaction(client, factionRole, message.channel, message.member, handleResponse);
}
//ProcessStatsCommand
//client - discord.js client
//member - discord.js member
//channel - discord.js channel
//dialog - dialog function
exports.ProcessCheckinCommand = function(client, member, channel, dialog) {
let handleResponse = function(checkinResponse, checkinAmount) {
if (checkinResponse === "available") {
shared.SendPublicMessage(client, member.user, channel, dialog("checkin", checkinAmount));
exports.HandleLevelUp(client, member, channel, dialog);
} else {
shared.SendPublicMessage(client, channel, dialog("checkinLocked", member.user.id, checkinResponse));
}
}
dataRequest.OnServerData("checkin", handleResponse, member.user.id); //ID of the person who checked in TODO: username too
}
//ProcessStatsCommand
//client - discord.js client
//message - discord.js message
//args - arguments to the give command
//dialog - dialog function
exports.ProcessGiveCommand = function(client, message, args, dialog) {
let amount = Math.floor(parseFloat(args[0]));
if (isNaN(amount)) {
shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id));
return;
}
//not enough
if (amount <= 0) {
shared.SendPublicMessage(client, message.channel, dialog("giveNotAboveZero", message.author.id));
return;
}
//didn't mention anyone
if (message.mentions.members.size == 0) {
shared.SendPublicMessage(client, message.channel, dialog("giveInvalidUser", message.author.id));
return;
}
let targetMember = message.mentions.members.first();
//can't give to yourself
if (targetMember.id === message.author.id) {
shared.SendPublicMessage(client, message.channel, dialog("giveInvalidUserSelf", message.author.id));
return;
}
let handleResponse = function(accountBalance) {
//not enough money in account
if (accountBalance < amount) {
shared.SendPublicMessage(client, message.channel, dialog("giveNotEnoughInAccount", message.author.id));
return;
}
//try to send the money
let handleResponse = function(response) {
if (response !== "success") {
shared.SendPublicMessage(client, message.channel, dialog("giveFailed", message.author.id));
} else {
//print the success message
shared.SendPublicMessage(client, message.author, message.channel, dialog("giveSuccessful", targetMember.id, amount));
}
}
dataRequest.OnServerData("transfer", handleResponse, message.author.id, targetMember.id, amount);
}
dataRequest.OnServerData("account", handleResponse, message.author.id);
}
//ProcessStatsCommand
@@ -124,49 +145,21 @@ exports.ProcessFactionChangeAttempt = function(client, message, factionRole, dia
//dialog - dialog function
exports.ProcessStatsCommand = function(client, member, channel, dialog) {
exports.HandleLevelUp(client, member, channel, dialog);
let stats = exports.GetStats(member.user);
exports.GetStats(member.user, (stats) => {
exports.PrintStats(client, member, channel, stats);
});
}
//GetStats
//user - discord.js user OR username
exports.GetStats = function(user) { //Grabs all parameters from server
//fn - function to pass the stats to
exports.GetStats = function(user, fn) {
//handle user strings
if (typeof(user) === "string") {
user = client.users.find(item => item.username === user || item.id === user);
}
let userStatsResponse = String(dataRequest.LoadServerData("userStats", user.id)).split(",");
if (userStatsResponse[0] == "failure") {
throw "server returned an error to userStats request";
}
let strength = parseFloat(userStatsResponse[1]); //TODO: constants representing the player structure instead of [0]
let speed = parseFloat(userStatsResponse[2]);
let stamina = parseFloat(userStatsResponse[3]);
let health = parseFloat(userStatsResponse[4]);
let maxStamina = parseFloat(userStatsResponse[5]);
let maxHealth = parseFloat(userStatsResponse[6]);
let wallet = parseFloat(userStatsResponse[7]);
let experience = parseFloat(userStatsResponse[8]);
let level = Math.floor(parseFloat(userStatsResponse[9]));
let levelPercent = parseFloat(userStatsResponse[10]);
let statPoints = parseFloat(userStatsResponse[11]);
return {
strength: strength,
speed: speed,
stamina: stamina,
health: health,
maxStamina: maxStamina,
maxHealth: maxHealth,
wallet: wallet,
experience: experience,
level: level,
levelPercent: levelPercent,
statPoints: statPoints
};
dataRequest.OnServerData("userStats", fn, user.id);
}
//PrintStats
@@ -229,19 +222,16 @@ exports.HandleLevelUp = function(client, member, channel, dialog) {
}
// Sees if the user is supposed to level up
let [levelUpResponse, level, statPoints] = shared.LevelUp(client, member);
//handle channel strings
if (typeof(channel) === "string") {
channel = client.channels.find(item => item.name === channel || item.id === channel);
}
let handleResponse = function(response, level, statPoints) {
//handle levelling up
if (levelUpResponse === "levelUp" || levelUpResponse === "RankUp") {
if (response === "levelUp" || response === "RankUp") {
if (level >= process.env.RANK_3_THRESHOLD) {
shared.SendPublicMessage(client, member.user, channel, dialog("levelUpCap", dialog("levelUpCapRemark"), level));
} else {
shared.SendPublicMessage(client, member.user, channel, dialog("LevelUp", dialog("levelUpRemark"), level, statPoints));
}
}
}
shared.LevelUp(client, member, handleResponse);
}

View File

@@ -1,5 +1,5 @@
{
"name": "nodejs-elasticbeanstalk",
"name": "ADAM",
"version": "1.0.0",
"description": "",
"main": "server.js",

View File

@@ -108,7 +108,8 @@ function processBasicCommands(client, message) {
return true;
case "genesis":
return core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_B_ROLE, dialog, "Genesis");
core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_B_ROLE, dialog, "Genesis");
return true;
//ADAM and the faction leaders print the intros in the gate
case "introgenesis":

View File

@@ -1,5 +1,5 @@
{
"name": "nodejs-elasticbeanstalk",
"name": "ADAM_CptMon",
"version": "1.0.0",
"description": "",
"main": "server.js",

View File

@@ -108,7 +108,8 @@ function processBasicCommands(client, message) {
return true;
case "hand":
return core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_C_ROLE, dialog, "Hand");
core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_C_ROLE, dialog, "Hand");
return true;
//ADAM and the faction leaders print the intros in the gate
case "introhand":

View File

@@ -1,5 +1,5 @@
{
"name": "nodejs-elasticbeanstalk",
"name": "ADAM_Dairo",
"version": "1.0.0",
"description": "",
"main": "server.js",

View File

@@ -108,7 +108,8 @@ function processBasicCommands(client, message) {
return true;
case "obsidian":
return core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_A_ROLE, dialog, "Obsidian");
core.ProcessFactionChangeAttempt(client, message, process.env.GROUP_A_ROLE, dialog, "Obsidian");
return true;
//ADAM and the faction leaders print the intros in the gate
case "introobsidian":

View File

@@ -1,5 +1,5 @@
{
"name": "nodejs-elasticbeanstalk",
"name": "ADAM_Kamala",
"version": "1.0.0",
"description": "",
"main": "server.js",

View File

@@ -1,5 +1,5 @@
{
"name": "nodejs-elasticbeanstalk",
"name": "Librarian",
"version": "1.0.0",
"description": "",
"main": "server.js",

View File

@@ -1,3 +1,114 @@
// .env Variables
require('dotenv').config({path: '../.env'});
require("dotenv").config({path: "../.env"});
//server tools
let express = require("express");
let socket = require("socket.io");
//express setup
let app = express();
let server = app.listen(process.env.SERVER_PORT, () => {
console.log("Listening to requests on port " + process.env.SERVER_PORT);
});
//shared code
let calcRandom = require('../Shared/calc_random');
//socket.io setup
let io = socket(server);
//TODO: isolate these responses to specific bots
io.on("connection", async (socket) => {
console.log("made socket connection");
//update the playerbase's stamina on command
socket.on("updateStamina", async ({ userID, data }) => {
console.log("updating stamina for all users...");
//TODO: update the stamina
});
//handle checkin
socket.on("checkin", async ({ 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[0] = user ID
if (fn) {
fn("newUser"); //["joined", "conversionLocked", "newUser"]
}
});
});

20
SERVER_City/package.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "server_city",
"version": "1.0.0",
"description": "",
"main": "city.js",
"scripts": {
"start": "forever -o forever.log -e error.log start city.js",
"restart": "forever -o forever.log -e error.log restart city.js",
"stop": "forever stop city.js",
"node": "node city.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^6.1.0",
"express": "^4.16.4",
"forever": "^0.15.3",
"socket.io": "^2.1.1"
}
}

View File

@@ -2,16 +2,22 @@
exports = module.exports = {};
require("dotenv").config({path: "../.env"});
let request = require("sync-request");
exports.LoadServerData = function(dataType, usersID = "") {
let response = request("GET", `${process.env.SERVER_ADDRESS}/getData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=${dataType}&userid=${usersID}`);
// console.log(response.getBody());
return response.getBody();
//socket tools
let io = require("socket.io-client")(`${process.env.SERVER_ADDRESS}:${process.env.SERVER_PORT}`);
//SendServerData
//dataType - the type of data being sent
//userID (optional) - the id of the user to be bundled with the data
//...data (optional) - any data you wish to send
exports.SendServerData = function(dataType, userID = "", ...data){
io.emit(dataType, { userID: userID, data: data });
}
exports.SendServerData = function(dataType, usersID = "", dataToSend="", dataToSend2 = ""){
let response = request("GET", `${process.env.SERVER_ADDRESS}/sendData.php?pk=${process.env.SERVER_PASS_KEY}&dataType=${dataType}&userid=${usersID}&dataToSend=${dataToSend}&dataToSend2=${dataToSend2}`);
// console.log(response.getBody());
return response.getBody();
//OnServerData
//dataType - the type of data being sent and received
//fn (optional) - the aknowledgement function that is called on the other end (takes the result as an argument)
//...data (optional) - any data you wish to send
exports.OnServerData = function(dataType, fn, ...data) {
io.emit(dataType, { data: data }, fn);
}

View File

@@ -58,7 +58,7 @@ exports.GetFactionChannel = function(factionRole) {
//factionRole - a faction role
//channel - discord.js channel OR channel name
//member - discord.js member
exports.ChangeFaction = async function(client, factionRole, channel, member) {
exports.ChangeFaction = function(client, factionRole, channel, member, fn) {
//factionRole must be a faction role
if (!exports.CheckFaction(factionRole)) {
throw "factionRole is not a faction!";
@@ -79,31 +79,23 @@ exports.ChangeFaction = async function(client, factionRole, channel, member) {
if (member.roles.has(factionRole)) {
//can't change to this faction
return "alreadyJoined";
fn("alreadyJoined");
}
if (dataRequest.LoadServerData("hasConvertedToday", member.user.id) == 1) {
//can't change too fast
return "hasConvertedToday";
let handleResponse = async function(response) {
if (response === "conversionLocked") { //can't change too fast
fn(response);
return;
}
//Creates a new user
var newUserResponse = String(dataRequest.SendServerData("newUser", member.user.id, "New user."));
//joins the new faction
await member.removeRole(process.env.GROUP_A_ROLE);
await member.removeRole(process.env.GROUP_B_ROLE);
await member.removeRole(process.env.GROUP_C_ROLE);
await member.addRole(factionRole);
//send the server the info (for logging)
dataRequest.SendServerData("conversion", member.user.id, "Converted to " + exports.GetFactionName(factionRole));
fn(response);
};
if (newUserResponse === "createdUser") {
//send the private welcoming message
return newUserResponse;
} else {
//send the public welcoming message
return "joined";
}
dataRequest.OnServerData("conversion", handleResponse, member.user.id);
}

View File

@@ -1,8 +1,8 @@
{
"name": "shared",
"name": "Shared",
"version": "1.0.0",
"description": "",
"main": "calc_random.js",
"main": "shared.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -10,6 +10,6 @@
"license": "ISC",
"dependencies": {
"dotenv": "^6.1.0",
"sync-request": "^6.0.0"
"socket.io-client": "^2.1.1"
}
}

View File

@@ -18,7 +18,8 @@ exports.AddXP = function(client, user, amount) {
//LevelUp
//client - discord.js client
//member - member to get the level up
exports.LevelUp = function(client, member) { //NOTE: why is this called separately?
//fn - function to pass the result to
exports.LevelUp = function(client, member, fn) {
//handle member strings
if (typeof(member) === "string") {
//get the member
@@ -32,22 +33,12 @@ exports.LevelUp = function(client, member) { //NOTE: why is this called separate
if (client.user.username == process.env.GROUP_B_LEADER_NAME && !member.roles.has(process.env.GROUP_B_ROLE)) return;
if (client.user.username == process.env.GROUP_C_LEADER_NAME && !member.roles.has(process.env.GROUP_C_ROLE)) return;
let response = String(dataRequest.SendServerData("getLevelUp", member.user.id));
let responseArray = response.split(",");
let responseMessage = responseArray[0];
let level = Math.floor(parseFloat(responseArray[1]));
let statPoints = parseFloat(responseArray[2]);
let handleResponse = function(response, level, statPoints) {
let rankUp = exports.RankUp(client, member, level);
if (rankUp == "rankUp") {
return [rankUp, level, statPoints];
} else if (responseMessage === "levelup") {
return ["levelUp", level, statPoints];
} else {
return ["", level, statPoints];
fn(rankUp === "rankUp" ? rankUp : response, level, statPoints);
}
dataRequest.OnServerData("levelUp", handleResponse, member.user.id);
}
//GetLevelUp