This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
heroes.js
220 lines (196 loc) · 7.58 KB
/
heroes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
module.exports = {
connect, processRequest, getHeroEffect, getHeroLevel, addXP
}
var mongodb, ucollection;
const fs = require('fs');
const logger = require('./log.js');
const dbManager = require('./dbmanager.js');
const heroDB = require('../heroes/heroes.json');
const quests = require('./quest.js');
const forge = require('./forge.js');
const react = require('./reactions.js');
const utils = require('./localutils.js');
function connect(db) {
mongodb = db;
ucollection = db.collection('users');
}
function processRequest(userID, args, guild, channelID, callback) {
ucollection.findOne({ discord_id: userID }).then((dbUser) => {
if(!dbUser) return;
var req = args.shift();
switch (req) {
case undefined:
getHero(dbUser, callback);
break;
case "list":
getHeroes(dbUser, callback);
break;
case "info":
getInfo(dbUser, args, callback);
break;
case "get":
assign(dbUser, args, channelID, callback);
break;
case "lead":
if(guild)
getRating(guild, args[0], callback);
break;
}
});
}
function getHero(dbUser, callback) {
var h = dbUser.hero;
if(!h) {
let stars = dbManager.countCardLevels(dbUser.cards);
var msg = "**" + dbUser.username + "**, you have no hero yet. \n";
if(stars >= 50) msg += "To choose one, use `->hero list`";
else msg += "You can get one once you have more than 50 \u2B50 stars (you have now " + stars + "\u2B50 stars)";
callback(msg);
return;
}
callback(h.subname + " **" + h.name + "** level **" + getHeroLevel(h.exp) + "** arrives!",
"./heroes/" + h.name.toLowerCase().replace(/ /g, "_") + ".png");
}
function getHeroes(dbUser, callback) {
let stars = dbManager.countCardLevels(dbUser.cards);
if(stars < 50) {
callback("**" + dbUser.username + "**, you should have at least 50 \u2B50 stars to have a hero.\n"
+ "You have now " + stars + " \u2B50 stars.");
return;
}
callback("Use `->hero info [hero name]` or `->hero info all`", "./heroes/list.png");
}
function getInfo(dbUser, args, callback) {
var req = args.join(' ');
if(req == 'all') {
callback("Use `->hero get [hero name]`.\n"
+ "Use `->hero info [hero name]` to get specific hero info",
"./heroes/all.png");
return;
}
if(req == '' || req == ' ') return;
var h = heroDB.filter(h => h.name.toLowerCase().includes(req))[0];
if(h) {
console.log(h.name.toLowerCase().replace(/ /g, "_"));
callback("Use `->hero get [hero name]`.",
"./heroes/" + h.name.toLowerCase().replace(/ /g, "_") + ".png");
}
}
function assign(dbUser, args, channelID, callback) {
var hasHero = dbUser.hero != undefined;
if(hasHero) {
if(dbUser.exp < 2500)
return callback(utils.formatError(dbUser, null, "hero change requires 2500 Tomatoes!\n"));
if(dbUser.lastHeroChange) {
let hours = 168 - utils.getHoursDifference(dbUser.lastHeroChange);
let days = Math.floor(hours/24);
if(hours > 0)
return callback(utils.formatError(dbUser, "Can't change hero",
"you can get new hero in **" + days + " days " + (hours - days*24) + " hours**"));
}
}
var stars = dbManager.countCardLevels(dbUser.cards);
if(stars < 50)
return callback(utils.formatError(dbUser, null,
"you can get one once you have more than 50 \u2B50 stars (you have " + stars + "\u2B50 stars)"));
var req = args.join(' ');
if(req == '' || req == ' ') return;
var h = heroDB.filter(h => h.name.toLowerCase().includes(req))[0];
if(h) {
if(hasHero)
react.addNewConfirmation(dbUser.discord_id,
utils.formatWarning(dbUser, "You want to change your hero?", "hero change will cost you **2500** Tomatoes\nYou can change hero once a week"),
channelID, () => switchHero(h, dbUser, callback));
else switchHero(h, dbUser, callback);
} else
callback(utils.formatError(null, null, "Can't find hero `" + req + "`"));
}
function switchHero(newHero, dbUser, callback) {
var upd = dbUser.hero? {
$set: {"hero.name": newHero.name, "hero.subname": newHero.subname, lastHeroChange: new Date()},
$inc: {exp: -2500}} : {$set: {hero: newHero, lastHeroChange: new Date()}};
ucollection.update(
{ discord_id: dbUser.discord_id }, upd
).then(() => {
callback(utils.formatConfirm(dbUser, "Yaaay!", "you made contract with **"
+ newHero.name + "**! Congratulations! \u{1F389} \nUse `->hero` to summon your new companion and view her level"));
});
}
function getHeroLevel(exp) {
return Math.floor((Math.log(exp) / Math.log(5)) * Math.sqrt(exp));
}
function addXP(user, amount) {
if(user.hero) {
amount = forge.getCardEffect(user, 'heroup', amount)[0];
ucollection.update(
{ discord_id: user.discord_id },
{$inc: {'hero.exp': amount}}
);
}
}
function getHeroEffect(user, action, value, ...params) {
if(user.hero) {
switch(user.hero.name.toLowerCase()) {
case 'akaza akari':
if(action == 'claim_akari') return Math.floor(value *.65);
//;~;\\
break;
case 'toshino kyoko':
if(action == 'auc') return true;
if(action == 'forge') return 0;
break;
case 'funami yui':
if(action == 'daily') return 300 + (value * 100);
if(action == 'cooldown') return Math.floor(value * .5);
break;
case 'yoshikawa chinatsu':
if(action == 'questReward') return Math.floor(value * 1.8);
if(action == 'questComplete') {
quests.addBonusQuest(user, () => {
value("Dark spell from Chinatsu granted you another quest! Use `->quest` to see it");
});
}
break;
}
}
return value;
}
function getRating(guild, arg, callback) {
if (arg === "global") {
ucollection.aggregate([
{ $project: { _id: '$username', hero: '$hero' } },
{ $sort: { 'hero.exp': -1 } },
{ $limit: 10 }
]).toArray((err, users) => {
callback("**Global** hero rating:\n" + nameOwners(users));
});
} else {
var users = Object.keys(guild.members);
ucollection.aggregate([
{ $match: { discord_id: { $in: users }, hero: { $exists: true } } },
{ $project: { _id: '$username', hero: '$hero' } },
{ $sort: { 'hero.exp': -1 } }
]).limit(10).toArray((err, users) => {
if (!users) return callback("**Local** hero rating:\nNo heroes found on this server.")
callback("**Local** hero rating:\n" + nameOwners(users));
});
}
}
function nameOwners(col) {
let res = '';
for (let i = 0; i < col.length; i++) {
if (!col[i].hero || !col[i].hero.name) continue;
res += (i + 1).toString() + ". ";
res += "**" + col[i]._id + "** -- [";
res += col[i].hero.name + "] -- **";
res += getHeroLevel(col[i].hero.exp) + "**\n";
}
return res;
}
function countAnimated(cards) {
var c = 0;
cards.forEach(function(element) {
if(element.animated) c += element.level;
}, this);
return c;
}