-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Counting game #535
Open
w1dering
wants to merge
5
commits into
main
Choose a base branch
from
counting-game
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Counting game #535
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
93aa164
initial commit for counting game
w1dering aa8822a
added adjusting coin balance for game and minimum threshold
w1dering a6dc113
ran linter
w1dering 99cad69
completed suggestions, added cap to number of coins earned
w1dering e64c9bb
ran linter
w1dering File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,18 +14,32 @@ import { PDFDocument } from 'pdf-lib'; | |
import { Logger } from 'winston'; | ||
import { applyBonusByUserId } from '../components/coin'; | ||
import { vars } from '../config'; | ||
import { sendKickEmbed } from '../utils/embeds'; | ||
import { sendKickEmbed, DEFAULT_EMBED_COLOUR } from '../utils/embeds'; | ||
import { convertPdfToPic } from '../utils/pdfToPic'; | ||
import { openDB } from '../components/db'; | ||
import { spawnSync } from 'child_process'; | ||
import { User } from 'discord.js'; | ||
import { getCoinEmoji } from '../components/emojis'; | ||
import { adjustCoinBalanceByUserId, UserCoinEvent } from '../components/coin'; | ||
|
||
const ANNOUNCEMENTS_CHANNEL_ID: string = vars.ANNOUNCEMENTS_CHANNEL_ID; | ||
const RESUME_CHANNEL_ID: string = vars.RESUME_CHANNEL_ID; | ||
const COUNTING_CHANNEL_ID: string = vars.COUNTING_CHANNEL_ID; | ||
const IRC_USER_ID: string = vars.IRC_USER_ID; | ||
const PDF_FILE_PATH = 'tmp/resume.pdf'; | ||
const HEIC_FILE_PATH = 'tmp/img.heic'; | ||
const CONVERTED_IMG_PATH = 'tmp/img.jpg'; | ||
|
||
// Variables and constants associated with the counting game | ||
const COINS_PER_MESSAGE = 0.1; // Number of coins awarded = COINS_PER_MESSAGE * highest counting number * number of messages sent by user | ||
const COUNTING_AUTHOR_DELAY = 1; // The minimum number of users that must count for someone to go again | ||
const previousCountingAuthors: Array<User> = []; // Stores the most recent counters | ||
const authorMessageCounts: Map<User, number> = new Map(); // Stores how many messages each user sent | ||
const COIN_AWARD_NUMBER_THRESHOLD = 20; // The minimum number that must be reached for coins to be awarded | ||
const MAX_COINS_PER_NUMBER_COUNTED = 2; // The maximum number of coins a user can receive every 100 numbers counted | ||
const MAX_COINS_PER_MESSAGE_SENT = 20; | ||
let currentCountingNumber = 1; | ||
|
||
/* | ||
* If honeypot is to exist again, then add HONEYPOT_CHANNEL_ID to the config | ||
* and add a check for a message's channel ID being equal to HONEYPOT_CHANNEL_ID | ||
|
@@ -93,13 +107,12 @@ const convertResumePdfsIntoImages = async ( | |
message: Message, | ||
): Promise<Message<boolean> | undefined> => { | ||
const attachment = message.attachments.first(); | ||
const hasAttachment = attachment; | ||
const isPDF = attachment && attachment.contentType === 'application/pdf'; | ||
const isImage = | ||
attachment && attachment.contentType && attachment.contentType.startsWith('image'); | ||
|
||
// If no resume pdf is provided, nuke message and DM user about why their message got nuked | ||
if (!(hasAttachment && (isPDF || isImage))) { | ||
if (!(attachment && (isPDF || isImage))) { | ||
const user = message.author.id; | ||
const channel = message.channelId; | ||
|
||
|
@@ -200,6 +213,91 @@ const convertResumePdfsIntoImages = async ( | |
} | ||
}; | ||
|
||
const countingGameLogic = async ( | ||
client: Client, | ||
message: Message, | ||
): Promise<Message<boolean> | undefined> => { | ||
// Check to see if game should end | ||
let reasonForFailure = ''; | ||
if (isNaN(Number(message.content))) { | ||
// Message was not a number | ||
reasonForFailure = `"${message.content}" is not a number!`; | ||
} else if (previousCountingAuthors.find((author) => author === message.author)) { | ||
// Author is still on cooldown | ||
reasonForFailure = `<@${message.author.id}> counted too recently!`; | ||
} else if (Number(message.content) != currentCountingNumber) { | ||
// Wrong number was sent | ||
reasonForFailure = `${message.content} is not the next number! The next number was ${currentCountingNumber}.`; | ||
} | ||
|
||
if (reasonForFailure) { | ||
return endCountingGame(client, message, reasonForFailure); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Subtract currentCountingNumber by 1 when the game ends because this value of currentCountingNumber was not actually reached. |
||
} | ||
|
||
// If checks passed, continue the game | ||
currentCountingNumber++; | ||
message.react('✅'); | ||
previousCountingAuthors.unshift(message.author); // Add current author to list of authors on cooldown | ||
while (previousCountingAuthors.length > COUNTING_AUTHOR_DELAY) { | ||
previousCountingAuthors.pop(); // Remove last author from cooldown | ||
} | ||
const currentAuthorCount: number | undefined = authorMessageCounts.get(message.author); | ||
authorMessageCounts.set(message.author, currentAuthorCount ? currentAuthorCount + 1 : 1); | ||
|
||
return; | ||
}; | ||
|
||
const endCountingGame = async ( | ||
client: Client, | ||
message: Message, | ||
reasonForFailure: string, | ||
): Promise<Message<boolean> | undefined> => { | ||
currentCountingNumber--; // since the current counting number wasn't reached, decrement the value | ||
// Builds game over embed | ||
const endGameEmbed = new EmbedBuilder() | ||
.setColor(DEFAULT_EMBED_COLOUR) | ||
.setTitle('Counting Game Over') | ||
.addFields([ | ||
{ | ||
name: 'Reason for Game Over', | ||
value: reasonForFailure, | ||
}, | ||
]); | ||
|
||
if (currentCountingNumber < COIN_AWARD_NUMBER_THRESHOLD) { | ||
endGameEmbed.setDescription( | ||
`Coins will not be awarded because the threshold, ${COIN_AWARD_NUMBER_THRESHOLD}, was not reached.`, | ||
); | ||
} else { | ||
const sortedAuthorMessageCounts: Array<[User, number]> = Array.from(authorMessageCounts).sort( | ||
(a, b) => b[1] - a[1], | ||
); // Turns map into descending sorted array | ||
const coinsAwarded: Array<string> = ['**Coins awarded:**']; | ||
for (const pair of sortedAuthorMessageCounts) { | ||
// Changes number of messages sent to number of coins awarded | ||
// Multiplication and division of 100 should prevent floating point errors | ||
pair[1] = Math.min( | ||
Math.round((pair[1] * Math.round(1000 * COINS_PER_MESSAGE) * currentCountingNumber) / 100) / | ||
10, | ||
MAX_COINS_PER_NUMBER_COUNTED * currentCountingNumber, | ||
MAX_COINS_PER_MESSAGE_SENT * pair[1], | ||
); | ||
|
||
coinsAwarded.push(`<@${pair[0].id}> - ${pair[1]} ${getCoinEmoji()}`); | ||
await adjustCoinBalanceByUserId(message.author.id, pair[1], UserCoinEvent.Counting); | ||
} | ||
|
||
endGameEmbed.setDescription(coinsAwarded.join('\n')); | ||
} | ||
|
||
currentCountingNumber = 1; | ||
message.react('❌'); | ||
previousCountingAuthors.length = 0; | ||
authorMessageCounts.clear(); | ||
|
||
return await message.channel?.send({ embeds: [endGameEmbed] }); | ||
}; | ||
|
||
export const initMessageCreate = async ( | ||
client: Client, | ||
logger: Logger, | ||
|
@@ -219,6 +317,10 @@ export const initMessageCreate = async ( | |
await convertResumePdfsIntoImages(client, message); | ||
} | ||
|
||
if (message.channelId === COUNTING_CHANNEL_ID) { | ||
await countingGameLogic(client, message); | ||
} | ||
|
||
// Ignore DMs; include announcements, thread, and regular text channels | ||
if (message.channel.type !== ChannelType.DM) { | ||
await applyBonusByUserId(message.author.id); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add this variable into config/vars.template.json for future devs.