Skip to content

Commit aa8822a

Browse files
committed
added adjusting coin balance for game and minimum threshold
a certain number must be reached in order for coins to be awarded coins will now be awarded to all participants KNOWN ERRORS: floating point issue for numbers when coins are being awarded
1 parent 93aa164 commit aa8822a

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

src/commandDetails/coin/leaderboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const getCoinLeaderboardEmbed = async (
2525
// Initialize user's coin balance if they have not already
2626
const userBalance = await getCoinBalanceByUserId(userId);
2727
let previousBalance = -1;
28-
let position = 0;
28+
let position = 0;
2929
let rank = 0;
3030
let offset = 0;
3131
let i = 0;

src/components/coin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export enum UserCoinEvent {
2828
BonusActivity,
2929
BonusInterviewerList,
3030
Blackjack,
31+
Counting,
3132
RpsLoss,
3233
RpsDrawAgainstCodey,
3334
RpsWin,

src/events/messageCreate.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { openDB } from '../components/db';
2020
import { spawnSync } from 'child_process';
2121
import { User } from 'discord.js';
2222
import { getCoinEmoji } from '../components/emojis';
23+
import { adjustCoinBalanceByUserId, UserCoinEvent } from '../components/coin';
2324

2425
const ANNOUNCEMENTS_CHANNEL_ID: string = vars.ANNOUNCEMENTS_CHANNEL_ID;
2526
const RESUME_CHANNEL_ID: string = vars.RESUME_CHANNEL_ID;
@@ -32,8 +33,9 @@ const CONVERTED_IMG_PATH = 'tmp/img.jpg';
3233
// Variables and constants associated with the counting game
3334
const coinsPerMessage: number = 0.1; // Number of coins awarded = coinsPerMessage * highest counting number * messages sent by user
3435
const countingAuthorDelay: number = 1; // The minimum number of users that must count for someone to go again
35-
const previousCountingAuthors: Array<User> = [];
36-
const authorMessageCounts: Map<User, number> = new Map();
36+
const previousCountingAuthors: Array<User> = []; // Stores the most recent counters
37+
const authorMessageCounts: Map<User, number> = new Map(); // Stores how many messages each user sent
38+
const coinAwardNumberThreshold: number = 20; // The minimum number that must be reached for coins to be awarded
3739
let currentCountingNumber: number = 1;
3840

3941
/*
@@ -248,26 +250,33 @@ const endCountingGame = async (
248250
message: Message,
249251
reasonForFailure: string
250252
): Promise<Message<boolean> | undefined> => {
251-
const sortedAuthorMessageCounts: Array<[User, number]> = Array.from(authorMessageCounts).sort((a, b) => b[1] - a[1]); // Turns map into descending sorted array
252-
const coinsAwarded: Array<string> = ['**Coins awarded:**'];
253-
sortedAuthorMessageCounts.forEach((pair) => {
254-
pair[1] *= coinsPerMessage * currentCountingNumber; // Changes number of messages sent to number of coins awarded
255-
coinsAwarded.push(`<@${pair[0].id}> - ${pair[1]} ${getCoinEmoji()}`);
256-
});
257-
258-
// ** REMEMBER TO ACTUALLY AWARD COINS
259-
253+
// Builds game over embed
260254
const endGameEmbed = new EmbedBuilder()
261-
.setColor(DEFAULT_EMBED_COLOUR)
262-
.setTitle('Counting Game Over')
263-
.setDescription(coinsAwarded.join('\n'));
264-
endGameEmbed.addFields([
255+
.setColor(DEFAULT_EMBED_COLOUR)
256+
.setTitle('Counting Game Over')
257+
.addFields([
265258
{
266259
name: 'Reason for Game Over',
267260
value: reasonForFailure,
268261
},
269262
]);
270263

264+
if (currentCountingNumber < coinAwardNumberThreshold) {
265+
endGameEmbed.setDescription(`Coins will not be awarded because the threshold, ${coinAwardNumberThreshold}, was not reached.`);
266+
}
267+
else
268+
{
269+
const sortedAuthorMessageCounts: Array<[User, number]> = Array.from(authorMessageCounts).sort((a, b) => b[1] - a[1]); // Turns map into descending sorted array
270+
const coinsAwarded: Array<string> = ['**Coins awarded:**'];
271+
for (let pair of sortedAuthorMessageCounts) {
272+
pair[1] *= coinsPerMessage * currentCountingNumber; // Changes number of messages sent to number of coins awarded
273+
coinsAwarded.push(`<@${pair[0].id}> - ${pair[1]} ${getCoinEmoji()}`);
274+
await adjustCoinBalanceByUserId(message.author.id, pair[1], UserCoinEvent.Counting);
275+
}
276+
277+
endGameEmbed.setDescription(coinsAwarded.join('\n'));
278+
}
279+
271280
currentCountingNumber = 1;
272281
message.react('❌');
273282
previousCountingAuthors.length = 0;

0 commit comments

Comments
 (0)