-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgameLogic.js
More file actions
221 lines (196 loc) · 6.99 KB
/
gameLogic.js
File metadata and controls
221 lines (196 loc) · 6.99 KB
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
221
const {
factorial,
isPrime,
parseExpression,
simplifyExpression,
evaluateExpression
} = require('./mathOperations');
const {calculateComplexity} = require('./complexityCalculator');
const statsManager = require('./statsManager');
async function processMessage(message, say, client, isEval = false) {
if (!/^[\d+\-*/^√().\s!]+$|^.*sqrt\(.*\).*$/.test(message.text)) {
return;
}
let number;
let expression;
let complexity;
try {
expression = parseExpression(message.text);
number = Math.round(await evaluateExpression(expression)); // Round the result
complexity = calculateComplexity(message.text);
if (isEval) {
// For !eval, just return the result without affecting game state
return `Expression: ${message.text}, Evaluated: ${number}, Complexity: ${complexity}`;
}
const stats = statsManager.getStats();
if (complexity > stats.mostComplicatedOperation.complexity) {
statsManager.updateStats({
mostComplicatedOperation: {
expression: message.text,
user: message.user,
complexity: complexity
}
});
await statsManager.saveStats();
}
} catch (error) {
console.log('Error evaluating expression:', error);
if (isEval) {
return `Error evaluating expression: ${error.message}`;
}
return;
}
const stats = statsManager.getStats();
if (!stats.userStats[message.user]) {
stats.userStats[message.user] = {
successful: 0,
unsuccessful: 0,
totalComplexity: 0,
countWithComplexity: 0
};
statsManager.updateStats({userStats: stats.userStats});
}
const lastUser = statsManager.getLastUser();
let currentCount = statsManager.getCurrentCount();
if (message.user === lastUser) {
await handleIncorrectCount(message, say, client, "You can't count twice in a row.");
} else if (number !== currentCount) {
await handleIncorrectCount(message, say, client, `The next number should have been ${currentCount}.`);
} else {
await handleCorrectCount(message, say, client, number, complexity);
}
// Add a return statement here
return `Expression: ${message.text}, Evaluated: ${number}, Complexity: ${complexity}`;
}
async function handleIncorrectCount(message, say, client, reason) {
await say(`<@${message.user}> messed up! ${reason} The count resets to 1.`);
statsManager.setCurrentCount(1);
statsManager.updateStats({currentCount: 1});
statsManager.setLastUser(null);
const stats = statsManager.getStats();
stats.userStats[message.user].unsuccessful++;
statsManager.updateStats({userStats: stats.userStats});
await client.reactions.add({
channel: message.channel,
timestamp: message.ts,
name: 'x'
});
await statsManager.saveStats();
}
async function handleCorrectCount(message, say, client, number, complexity) {
try {
let reactionEmoji = getReactionEmoji(number);
await client.reactions.add({
channel: message.channel,
timestamp: message.ts,
name: reactionEmoji
});
await checkAndHandleMilestones(message, say, number);
updateGameState(message.user, number, complexity);
await statsManager.saveStats();
} catch (error) {
console.error(error);
}
}
function getReactionEmoji(number) {
switch (number) {
case 42: return 'rocket';
case 69: return 'cancer';
case 123: return '1234';
case 314: return 'pie';
case 420: return 'herb';
case 666: return 'smiling_imp';
case 777: return 'four_leaf_clover';
case 1000: return 'fireworks';
case 1234: return '1234';
case 1337: return 'computer';
case 2048: return 'jigsaw';
case 3141: return 'abacus';
case 5000: return 'raised_hand_with_fingers_splayed';
case 9000: return 'muscle';
case 12345: return '1234';
default:
if (number % 100 === 0) return '💯';
return 'white_check_mark';
}
}
async function checkAndHandleMilestones(message, say, number) {
const specialMilestones = [
42, // The Answer to Life, the Universe, and Everything
69, // Nice
123, // Sequential numbers
314, // Pi
420, // Herb
666, // Devil's number
777, // Lucky number
1000, // Big round number
1234, // Sequential numbers
1337, // LEET
2048, // Popular game
3141, // More digits of Pi
5000, // Another big milestone
9000, // It's over 9000!
12345 // More sequential numbers
];
if (number % 100 === 0 || specialMilestones.includes(number)) {
const stats = statsManager.getStats();
stats.milestones[number] = message.user;
statsManager.updateStats({milestones: stats.milestones});
const emoji = getReactionEmoji(number);
const unicodeEmoji = getUnicodeEmoji(emoji);
await say(`${unicodeEmoji} Congratulations <@${message.user}>! You've reached ${number}! ${unicodeEmoji}`);
}
}
function getUnicodeEmoji(shortcode) {
const emojiMap = {
'rocket': '🚀',
'cancer': '♋',
'pie': '🥧',
'herb': '🌿',
'smiling_imp': '😈',
'four_leaf_clover': '🍀',
'fireworks': '🎆',
'1234': '🔢',
'computer': '💻',
'jigsaw': '🧩',
'abacus': '🧮',
'raised_hand_with_fingers_splayed': '🖐️',
'muscle': '💪',
'white_check_mark': '✅',
'💯': '💯'
};
return emojiMap[shortcode] || shortcode;
}
function updateGameState(user, number, complexity) {
const stats = statsManager.getStats();
let currentCount = statsManager.getCurrentCount();
currentCount++;
statsManager.setCurrentCount(currentCount);
statsManager.updateStats({currentCount: currentCount});
statsManager.setLastUser(user);
stats.totalSuccessfulCounts++;
stats.userStats[user].successful++;
stats.userStats[user].totalComplexity += complexity;
stats.userStats[user].countWithComplexity++;
if (currentCount > stats.highestCount) {
stats.highestCount = currentCount - 1;
stats.highestCountTimestamp = new Date().toISOString();
}
statsManager.updateStats(stats);
// Track prime numbers
if (isPrime(currentCount - 1)) {
stats.userStats[user].primes = (stats.userStats[user].primes || 0) + 1;
}
// Track perfect squares
if (Math.sqrt(currentCount - 1) % 1 === 0) {
stats.userStats[user].perfectSquares = (stats.userStats[user].perfectSquares || 0) + 1;
}
statsManager.updateStats({userStats: stats.userStats});
}
module.exports = {
processMessage,
getReactionEmoji,
checkAndHandleMilestones,
updateGameState,
getUnicodeEmoji
};