mirror of
https://github.com/Ratstail91/SANCTUM.git
synced 2025-11-29 02:24:27 +11:00
Moved some sharable code for the faction leaders into Shared/Core
This commit is contained in:
163
Shared/Core/core.js
Normal file
163
Shared/Core/core.js
Normal file
@@ -0,0 +1,163 @@
|
||||
exports = module.exports = {};
|
||||
|
||||
let shared = require("../shared");
|
||||
let calcRandom = require('../../modules/calcRandom');
|
||||
|
||||
//ProcessGameplayCommands
|
||||
//client - discord.js client
|
||||
//message - discord.js message
|
||||
//dialog - the dialog function
|
||||
exports.ProcessGameplayCommands = function(client, message, dialog) {
|
||||
// "This is the best way to define args. Trust me."
|
||||
// - Some tutorial dude on the internet
|
||||
let args = message.content.slice(process.env.PREFIX.length).trim().split(/ +/g);
|
||||
let command = args.shift().toLowerCase();
|
||||
|
||||
switch (command) {
|
||||
case "checkin":
|
||||
let checkinAmount = calcRandom.random(4, 9);
|
||||
let checkInResponse = String(dataRequest.sendServerData("checkin", checkinAmount, message.author.id));
|
||||
if (checkInResponse === "available") {
|
||||
shared.SendPublicMessage(client, message.author, message.channel, dialog("checkin", checkinAmount));
|
||||
shared.AddXP(client, message.author, 1); //1XP
|
||||
} else {
|
||||
shared.SendPublicMessage(client, message.channel, dialog("checkinLocked", message.author.id, checkInResponse));
|
||||
}
|
||||
return true;
|
||||
|
||||
case "give": //TODO: fold this code into a function
|
||||
let amount = Math.floor(args[0]);
|
||||
|
||||
//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", targetMember.id, message.author.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));
|
||||
return true;
|
||||
|
||||
case "stats": //TODO: fold this code into a function
|
||||
// Sees if the user is supposed to level up
|
||||
let levelUp = shared.LevelUp(client, message.member); //TODO: process automatically
|
||||
|
||||
// Grabs all parameters from server
|
||||
//TODO: improve this once the server-side has been updated
|
||||
let attacker = String(dataRequest.loadServerData("userStats",message.author.id)).split(",");
|
||||
|
||||
if (attacker[0] == "failure") {
|
||||
shared.SendPublicMessage(client, message.author, message.channel, "The server returned an error.");
|
||||
return true;
|
||||
}
|
||||
|
||||
let attackerStrength = parseFloat(attacker[1]); //TODO: constants representing the player structure instead of [0]
|
||||
let attackerSpeed = parseFloat(attacker[2]);
|
||||
let attackerStamina = parseFloat(attacker[3]);
|
||||
let attackerHealth = parseFloat(attacker[4]);
|
||||
let attackerMaxStamina = parseFloat(attacker[5]);
|
||||
let attackerMaxHealth = parseFloat(attacker[6]);
|
||||
let attackerWallet = parseFloat(attacker[7]);
|
||||
let attackerXP = parseFloat(attacker[8]);
|
||||
let attackerLVL = Math.floor(parseFloat(attacker[9]));
|
||||
let attackerLvlPercent = parseFloat(attacker[10]);
|
||||
let attackerStatPoints = parseFloat(attacker[11]);
|
||||
|
||||
// Forms stats into a string
|
||||
var levelText = `:level: **${attackerLVL}**`; //NOTE: I don't like backticks
|
||||
var levelProgress = `(${attackerLvlPercent}%)`;
|
||||
var crystalText = `:crystals: **${attackerWallet}**`;
|
||||
var cannisterText = `:cannister: **${attackerStatPoints}**`;
|
||||
var userStats = "```" + `STR: ${attackerStrength} | SPD: ${attackerSpeed} | STAM: ${attackerStamina}/${attackerMaxStamina} | HP: ${attackerHealth}/${attackerMaxHealth}` + "```";
|
||||
|
||||
// Says level is maxed out if it is LVL 30+
|
||||
if (attackerLVL >= process.env.RANK_3_THRESHOLD) {
|
||||
levelProgress = "(MAX)";
|
||||
}
|
||||
|
||||
// Creates embed & sends it
|
||||
const embed = new Discord.RichEmbed()
|
||||
.setAuthor(`${message.member.displayName}`, message.author.avatarURL)
|
||||
.setColor(message.member.displayColor)
|
||||
.setDescription(`${levelText} ${levelProgress} | ${crystalText} | ${cannisterText}`)
|
||||
.addField("Stats", userStats)
|
||||
.setFooter("Commands: !help | !lore | !checkin | !give");
|
||||
|
||||
message.channel.send(embed);
|
||||
|
||||
//handle levelling up
|
||||
if (levelUp === "levelUp" || levelUp === "RankUp") {
|
||||
if (attackerLVL >= process.env.RANK_3_THRESHOLD) {
|
||||
shared.SendPublicMessage(client, message.author, message.channel, dialog("levelUpCap", dialog("levelUpCapRemark"), attackerLVL));
|
||||
} else {
|
||||
shared.SendPublicMessage(client, message.author, message.channel, dialog("LevelUp", dialog("levelUpRemark"), attackerLVL, attackerStatPoints));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//didn't process it
|
||||
return false;
|
||||
}
|
||||
|
||||
//ProcessFactionChangeAttempt
|
||||
//client - discord.js client
|
||||
//message - discord.js message
|
||||
//factionRole - the new faction's role
|
||||
//dialog - the dialog function
|
||||
//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) {
|
||||
case "alreadyJoined":
|
||||
shared.SendPublicMessage(client, message.channel, dialog("alreadyJoined" + factionShorthand, message.author.id));
|
||||
break;
|
||||
case "hasConvertedToday":
|
||||
shared.SendPublicMessage(client, message.channel, dialog("conversionLocked", message.author.id));
|
||||
break;
|
||||
case "createdUser":
|
||||
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;
|
||||
case "joined":
|
||||
shared.SendPublicMessage(client, message.author, message.channel, dialog("join" + factionShorthand));
|
||||
break;
|
||||
default:
|
||||
//DEBUGGING
|
||||
console.log("processFactionChangeAttempt failed:" + result);
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
return true;
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
// .env Variables
|
||||
require('dotenv').config({path: '../.env'});
|
||||
|
||||
let shared = require("./shared.js");
|
||||
|
||||
let Discord = require("discord.js");
|
||||
let client = new Discord.Client();
|
||||
|
||||
//message handler
|
||||
client.on("ready", function() {
|
||||
console.log("Logged in as " + client.user.tag);
|
||||
});
|
||||
|
||||
client.on("reconnecting", function() {
|
||||
console.log("Reconnecting...");
|
||||
});
|
||||
|
||||
client.on("disconnect", function() {
|
||||
console.log("Disconnected");
|
||||
});
|
||||
|
||||
client.on("message", function(msg) {
|
||||
//only react to commands
|
||||
if (msg.content.slice(0, 1) !== process.env.PREFIX) {
|
||||
return;
|
||||
}
|
||||
|
||||
//get the command
|
||||
let command = msg.content.slice(1).split(" ")[0];
|
||||
let args = msg.content.slice(1 + command.length).trim();
|
||||
|
||||
//handle command
|
||||
switch(command) {
|
||||
//used for debugging
|
||||
case "create":
|
||||
shared.ChangeFaction(client, process.env.GROUP_A_ROLE, "bot-spam", msg.member);
|
||||
break;
|
||||
|
||||
case "xp":
|
||||
shared.AddXP(client, msg.author, parseInt(args));
|
||||
break;
|
||||
|
||||
case "levelup":
|
||||
shared.LevelUp(client, msg.member);
|
||||
break;
|
||||
|
||||
case "rankup":
|
||||
shared.RankUp(client, msg.member, parseInt(args));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
//DEBUGGING: eventually be require("./dialog.json")
|
||||
let dialogJson = {
|
||||
"hello": [
|
||||
"Hi there {1} {2} {3}!",
|
||||
"Howdy {1} {3}, of the {3} clan!"
|
||||
],
|
||||
"goodbye": "See ya!"
|
||||
}
|
||||
|
||||
let dialog = shared.GenerateDialogFunction(dialogJson);
|
||||
console.log(dialog("hello", "Kayne", "Matthew", "Ruse"));
|
||||
|
||||
//actually log in
|
||||
client.login(process.env.DEVELOPER_TOKEN);
|
||||
|
||||
//TODO: change usernames to tags throughout the shared library
|
||||
//FIXME: The server currently queries chest count, which is not in the database.
|
||||
|
||||
Reference in New Issue
Block a user