@@ -20,6 +20,7 @@ import { openDB } from '../components/db';
2020import { spawnSync } from 'child_process' ;
2121import { User } from 'discord.js' ;
2222import { getCoinEmoji } from '../components/emojis' ;
23+ import { adjustCoinBalanceByUserId , UserCoinEvent } from '../components/coin' ;
2324
2425const ANNOUNCEMENTS_CHANNEL_ID : string = vars . ANNOUNCEMENTS_CHANNEL_ID ;
2526const 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
3334const coinsPerMessage : number = 0.1 ; // Number of coins awarded = coinsPerMessage * highest counting number * messages sent by user
3435const 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
3739let 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