Skip to content

Commit

Permalink
refactor: alphabetize chat commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed Oct 19, 2024
1 parent 45b3a3b commit 4a79a84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions packages/client/src/chatCommands.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVariantFromArgs, getVariantFromPartial } from "./chatCommands";
import { getVariantFromArgs, getVariantNameFromPartial } from "./chatCommands";

jest.mock("./chat", () => ({}));
jest.mock("./Globals", () => ({}));
Expand All @@ -16,49 +16,49 @@ describe("functions", () => {
const partial = getVariantFromArgs(
"Brown-Fives & Prism (6 Suits)".split(" "),
);
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
});
describe("partial input", () => {
test("is valid", () => {
const partial = getVariantFromArgs("Brown-Fives & Pri".split(" "));
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
test("is valid", () => {
const partial = getVariantFromArgs("Brown-Fives".split(" "));
expect(getVariantFromPartial(partial)).toBe(brownFives);
expect(getVariantNameFromPartial(partial)).toBe(brownFives);
});
});
describe("double spaces", () => {
test("is valid", () => {
const partial = getVariantFromArgs(
" Brown-Fives & Prism (6 Suits) ".split(" "),
);
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
});
describe("spaces before or after -, &, (, )", () => {
test("is valid", () => {
const partial = getVariantFromArgs(
" Brown -Fives& Prism( 6 Suits ) ".split(" "),
);
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
});
describe("capitalize", () => {
test("is valid", () => {
const partial = getVariantFromArgs(
"bROWN-FivES & prism (6 SUITS)".split(" "),
);
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
});
describe("all possible cases", () => {
test("is valid", () => {
const partial = getVariantFromArgs(
" bROWN -FivES &prism(6 suits )".split(" "),
);
expect(getVariantFromPartial(partial)).toBe(brownFivesPrism6Suits);
expect(getVariantNameFromPartial(partial)).toBe(brownFivesPrism6Suits);
});
});
});
Expand Down
44 changes: 22 additions & 22 deletions packages/client/src/chatCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { createJSONFromReplay } from "./lobby/createReplayJSON";
type Callback = (...args: any) => void;
export const chatCommands = new Map<string, Callback>();

// /copy
chatCommands.set("copy", (room: string) => {
createJSONFromReplay(room);
});

// /friend [username]
function friend(room: string, args: readonly string[]) {
// Validate that the format of the command is correct.
Expand Down Expand Up @@ -191,7 +196,7 @@ function setVariant(room: string, args: readonly string[]) {
// Sanitize the variant name.
let variantName = getVariantFromArgs(args);
// Get the first match.
variantName = getVariantFromPartial(variantName);
variantName = getVariantNameFromPartial(variantName);
if (variantName === "") {
sendSelfPMFromServer(
`The variant of "${args.join(" ")}" is not valid.`,
Expand Down Expand Up @@ -319,6 +324,21 @@ chatCommands.set("tagsdeleteall", (room: string) => {
});
});

// /terminate terminates the ongoing game. This is the same as right-clicking the VTK button.
chatCommands.set("terminate", (room: string) => {
if (globals.tableID === -1) {
sendSelfPMFromServer(
"You are not currently at a table, so you cannot use the <code>/terminate</code> command.",
room,
SelfChatMessageType.Error,
);
return;
}
globals.conn!.send("tableTerminate", {
tableID: globals.tableID,
});
});

// /playerinfo (username)
function playerinfo(_room: string, args: readonly string[]) {
let usernames: readonly string[];
Expand Down Expand Up @@ -380,26 +400,6 @@ chatCommands.set("version", (room: string) => {
sendSelfPMFromServer(msg, room, SelfChatMessageType.Info);
});

// /copy
chatCommands.set("copy", (room: string) => {
createJSONFromReplay(room);
});

// /terminate terminates the ongoing game. This is the same as right-clicking the VTK button.
chatCommands.set("terminate", (room: string) => {
if (globals.tableID === -1) {
sendSelfPMFromServer(
"You are not currently at a table, so you cannot use the <code>/terminate</code> command.",
room,
SelfChatMessageType.Error,
);
return;
}
globals.conn!.send("tableTerminate", {
tableID: globals.tableID,
});
});

export function getVariantFromArgs(args: readonly string[]): string {
const patterns = {
doubleSpaces: / {2,}/g,
Expand Down Expand Up @@ -434,7 +434,7 @@ function capitalize(input: string) {
return input.toLowerCase().replaceAll(pattern, (x) => x.toUpperCase());
}

export function getVariantFromPartial(search: string): string {
export function getVariantNameFromPartial(search: string): string {
const firstVariant = VARIANT_NAMES.find((variantName) =>
variantName.startsWith(search),
);
Expand Down

0 comments on commit 4a79a84

Please sign in to comment.