Skip to content
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 minigame #18

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/commands/general/startCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CommandInteraction, GuildChannel } from "discord.js";
import { startCount } from "../../util";

module.exports = {
name: 'startcounter',
category: 'General',
description: 'Starts a counting channel',
options: [{
type: 'CHANNEL',
name: 'channel',
description: 'If a channel is not provided, one will be created',
required: false
}],
execute(interaction: CommandInteraction) {
let channel = interaction.options[0].channel;
if (channel instanceof GuildChannel) {
startCount(channel).then(channelId => {
interaction.reply(`Counting started in <\\${channelId}>!`);
});
} else {
startCount()
}
}
};
91 changes: 91 additions & 0 deletions src/commands/utils/countingGameUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type { GuildChannel } from 'discord.js';

enum Gamemodes {
NORMAL = 'Normal',
MULTIPLES = 'Multiples',
ODDS = 'Odds',
EVENS = 'Evens',
PRIMES = 'Primes',
PI = 'Pi'
}

enum GamemodeModifiers {
SECOND_CHANCE = 'Second Chance'
}

const piArray = '3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067'.split('').forEach((o, i, a: any) => a[i] = parseInt(a[i]));
const primeArray = '2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997'.split(' ').forEach((o, i, a: any) => a[i] = parseInt(a[i]));

interface countingGameConfig {
gamemode: Gamemodes,
modifiers: GamemodeModifiers,
channel: GuildChannel,
multiple: number,
}

export class countingGame {
public gamemode: Gamemodes;
public modifiers: GamemodeModifiers;
public channel: GuildChannel;
public multiple: number;
public count: number;

public constructor(config: countingGameConfig) {
Object.assign(this, config)
switch (config.gamemode) {
case Gamemodes.ODDS:
this.count = 1;
case Gamemodes.PI:
case Gamemodes.PRIMES:
this.count = 3;
}
}

get channelId() {
return this.channel.id;
}

public setGamemode(gamemode: Gamemodes): Promise<countingGame> {
return new Promise<countingGame>((resolve) => {
this.gamemode = gamemode;
resolve(this);
});
}

public setModifiers(modifiers: GamemodeModifiers): Promise<countingGame> {
return new Promise<countingGame>((resolve) => {
this.modifiers = modifiers;
resolve(this);
});
}

public setCount(count: number): Promise<countingGame> {
return new Promise<countingGame>((resolve) => {
this.count = count;
resolve(this);
});
}

public resetCount(): Promise<countingGame> {
return new Promise<countingGame>((resolve) => {
this.count = 0;
resolve(this);
});
}

public computeNextNumber(): number {
switch (this.gamemode) {
case Gamemodes.NORMAL:
return(this.count + 1)
case Gamemodes.MULTIPLES:
return (this.multiple * (this.count + 1));
case Gamemodes.ODDS:
case Gamemodes.EVENS:
return (this.count + 2);
case Gamemodes.PRIMES:
return (primeArray[this.count + 1]); // actually thought about computing primes but why work hard when you can work smart
case Gamemodes.PI:
return (piArray[this.count + 1]);
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const client = new Discord.Client({
client.commands = new Map();
client.musicQueue = [];
client.audioPlayers = new Map();
client.countingGames = new Map();

client.once('ready', async () => {
if (client.user)
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { AudioPlayer } from '@discordjs/voice';
import type * as Discord from 'discord.js';
import type * as ytsr from "ytsr";
import type { countingGame } from '../src/commands/utils/countingGameUtils';

interface BotClient extends Discord.Client {
commands: Map<string, DiscordCommand>,
audioPlayers: Map<string, AudioPlayer>
countingGames: Map<string, countingGame>
musicQueue: MusicQueue,
cloudProjectId?: string,
channelTimeout: NodeJS.Timeout | null;
Expand Down