Skip to content

Commit

Permalink
Refactor rockpaper commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbowen committed Nov 22, 2023
1 parent a01e330 commit a243eba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 104 deletions.
64 changes: 9 additions & 55 deletions packages/mrwhale-discord/src/commands/game/rockpaper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { ChatInputCommandInteraction, InteractionResponse, Message } from "discord.js";
import {
ChatInputCommandInteraction,
InteractionResponse,
Message,
} from "discord.js";

import { rockpaper } from "@mrwhale-io/commands";
import { DiscordCommand } from "../../client/command/discord-command";

export default class extends DiscordCommand {
constructor() {
super({
name: "rockpaper",
description: "Rock. Paper. Scissors.",
type: "game",
usage: "<prefix>rockpaper <rock|paper|scissors>",
examples: ["<prefix>rockpaper scissors"],
aliases: ["rps"],
});
super(rockpaper.data);
this.slashCommandData.addStringOption((option) =>
option
.setName("choice")
Expand All @@ -25,59 +23,15 @@ export default class extends DiscordCommand {
);
}

private compare(first: string, second: string) {
if (first === second) {
return "It's a tie!";
} else if (first === "scissors") {
if (second === "paper") return "Scissors wins! :v:";
else return "Rock wins! :fist:";
} else if (first === "rock") {
if (second === "scissors") return "Rock wins! :fist:";
else return "Paper wins! :hand_splayed:";
} else if (first === "paper") {
if (second === "rock") return "Paper wins! :hand_splayed:";
else return "Scissors wins! :v:";
}
}

async action(message: Message, [choice]: [string]): Promise<Message> {
if (!choice || choice === "") {
return message.reply("Please pass a choice.");
}

const userChoice = choice.trim().toLowerCase();

return message.reply(this.rockPaperScissors(userChoice));
return message.reply(rockpaper.action(choice));
}

slashCommandAction(
interaction: ChatInputCommandInteraction
): Promise<InteractionResponse<boolean>> {
const choice = interaction.options.getString("choice");

return interaction.reply(this.rockPaperScissors(choice));
}

private rockPaperScissors(choice: string): string {
const compChoice = Math.random();
let compChoiceStr = "";

const validChoices = /\b(rock|paper|scissors)\b/;

if (!choice.match(validChoices)) {
return "Please pass rock, paper, scissors.";
}

if (compChoice < 0.34) {
compChoiceStr = "Rock";
} else if (compChoice <= 0.67) {
compChoiceStr = "Paper";
} else {
compChoiceStr = "Scissors";
}

const result = this.compare(choice, compChoiceStr.toLowerCase());

return `${compChoiceStr}. ${result}`;
return interaction.reply(rockpaper.action(choice));
}
}
52 changes: 3 additions & 49 deletions packages/mrwhale-gamejolt/src/commands/game/rockpaper.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,13 @@
import { rockpaper } from "@mrwhale-io/commands";
import { Message } from "@mrwhale-io/gamejolt-client";

import { GameJoltCommand } from "../../client/command/gamejolt-command";

export default class extends GameJoltCommand {
constructor() {
super({
name: "rockpaper",
description: "Rock. Paper. Scissors.",
type: "game",
usage: "<prefix>rockpaper <rock|paper|scissors>",
examples: ["<prefix>rockpaper scissors"],
aliases: ["rps"],
});
}

private compare(first: string, second: string) {
if (first === second) {
return "It's a tie!";
} else if (first === "scissors") {
if (second === "paper") return "Scissors wins! ✌";
else return "Rock wins! 👊";
} else if (first === "rock") {
if (second === "scissors") return "Rock wins! 👊";
else return "Paper wins! ✋";
} else if (first === "paper") {
if (second === "rock") return "Paper wins! ✋";
else return "Scissors wins! ✌";
}
super(rockpaper.data);
}

async action(message: Message, [choice]: [string]): Promise<Message> {
if (!choice || choice === "") {
return message.reply("Please pass a choice.");
}

const userChoice = choice.trim().toLowerCase();
const compChoice = Math.random();
let compChoiceStr = "";

const validChoices = /\b(rock|paper|scissors)\b/;

if (!message.textContent.match(validChoices)) {
return message.reply("Please pass rock, paper, scissors.");
}

if (compChoice < 0.34) {
compChoiceStr = "rock";
} else if (compChoice <= 0.67) {
compChoiceStr = "paper";
} else {
compChoiceStr = "scissors";
}

const result = this.compare(userChoice, compChoiceStr);

return message.reply(`${compChoiceStr}. ${result}`);
return message.reply(rockpaper.action(choice));
}
}

0 comments on commit a243eba

Please sign in to comment.