0
0
Fork 0

Compare commits

...

7 Commits

6 changed files with 87 additions and 41 deletions

View File

@ -20,9 +20,13 @@ export interface Command {
* Also used to determine whether it should be listed in help output */ * Also used to determine whether it should be listed in help output */
readonly description?: string; readonly description?: string;
/** Called when Command is loaded */ /** Called when Command is loaded
open?(): Promise<void>; * @param commander The Commander instance this Command is managed by
/** Called before Command is unloaded */ */
open?(commander: Commander): Promise<void>;
/** Called before Command is unloaded
* The Commander received during open should not be used after this function returns
*/
close?(): Promise<void>; close?(): Promise<void>;
/** Called when client receives message with command prefix */ /** Called when client receives message with command prefix */
@ -59,7 +63,7 @@ export class Commander {
} }
await this.unregister(name); await this.unregister(name);
await handler.open?.(); await handler.open?.(this);
this.#commands.set(name, handler); this.#commands.set(name, handler);
} }

View File

@ -38,31 +38,6 @@ for (const mod of config.load_mods) {
await commander.registerMod(path.toString()); await commander.registerMod(path.toString());
} }
await commander.register("load", {
admin: true,
async handle(message, parameter) {
if (parameter == undefined) {
return;
}
try {
await commander.registerMod(parameter);
} catch (e) {
message.replyNotice(e.toString(), true);
return;
}
},
});
await commander.register("loaded", {
admin: true,
async handle(message, _) {
message.replyNotice(commander.available().join(","));
},
});
await commander.register("version", { await commander.register("version", {
description: "Get the bots version", description: "Get the bots version",
async handle(message, _) { async handle(message, _) {
@ -108,7 +83,7 @@ try {
} }
} catch (e) { } catch (e) {
if (e instanceof irc.IrcError) { if (e instanceof irc.IrcError) {
console.log(e.toString()); console.error(e.toString());
} else { } else {
throw e; throw e;
} }

27
gryphon/mods/loaded.ts Normal file
View File

@ -0,0 +1,27 @@
/**
* @file
* @copyright 2020, Valentin Anger
* @license ISC
*/
import * as irc from "../../net/irc/mod.ts";
import { Command, Commander } from "../command.ts";
class Loaded implements Command {
readonly admin = true;
readonly command = "loaded";
#commander: Commander | undefined;
async open(commander: Commander) {
this.#commander = commander;
}
async handle(message: irc.Message, argument: string | undefined) {
message.replyNotice(this.#commander!.available().join(","));
}
}
export function create() {
return new Loaded();
}

36
gryphon/mods/modload.ts Normal file
View File

@ -0,0 +1,36 @@
/**
* @file
* @copyright 2020, Valentin Anger
* @license ISC
*/
import * as irc from "../../net/irc/mod.ts";
import { Command, Commander } from "../command.ts";
class Modload implements Command {
readonly admin = true;
readonly command = "load";
#commander: Commander | undefined;
async open(commander: Commander) {
this.#commander = commander;
}
async handle(message: irc.Message, argument: string | undefined) {
if (argument == undefined) {
return;
}
try {
await this.#commander!.registerMod(argument);
} catch (e) {
message.replyNotice(e.toString(), true);
return;
}
}
}
export function create() {
return new Modload();
}

View File

@ -8,7 +8,12 @@ import * as irc from "../../net/irc/mod.ts";
import { Command } from "../command.ts"; import { Command } from "../command.ts";
type Color = "Blue" | "Green" | "Red" | "Yellow"; type Color = "Blue" | "Green" | "Red" | "Yellow";
const colors: Color[] = ["Blue", "Green", "Red", "Yellow"]; const colors: ReadonlyArray<Color> = [
"Blue",
"Green",
"Red",
"Yellow",
] as const;
type Value = type Value =
| "0" | "0"
| "1" | "1"
@ -25,7 +30,7 @@ type Value =
| "D2" | "D2"
| "W" | "W"
| "WD4"; | "WD4";
const values: Value[] = [ const values: ReadonlyArray<Value> = [
"0", "0",
"1", "1",
"2", "2",
@ -41,7 +46,8 @@ const values: Value[] = [
"D2", "D2",
"W", "W",
"WD4", "WD4",
]; ] as const;
const wild_cards: ReadonlyArray<Value> = ["W", "WD4"] as const;
interface Card { interface Card {
color: Color; color: Color;
@ -51,9 +57,9 @@ interface Card {
type Hand = Card[]; type Hand = Card[];
function cardString(card: Card, colored_wild: boolean = false): string { function cardString(card: Card, colored_wild: boolean = false): string {
return `${ return `${irc.bold()}${
irc.color( irc.color(
!colored_wild && ["W", "WD4"].includes(card.value) !colored_wild && wild_cards.includes(card.value)
? irc.Color.Default ? irc.Color.Default
: card.color === "Blue" : card.color === "Blue"
? irc.Color.Blue ? irc.Color.Blue
@ -62,6 +68,7 @@ function cardString(card: Card, colored_wild: boolean = false): string {
: card.color === "Red" : card.color === "Red"
? irc.Color.Red ? irc.Color.Red
: irc.Color.Yellow, : irc.Color.Yellow,
irc.Color.Black,
) )
}${card.value}${irc.reset()}`; }${card.value}${irc.reset()}`;
} }
@ -154,7 +161,6 @@ class Game {
return false; return false;
} }
private async sendCurrentTurn(msg: irc.Message) { private async sendCurrentTurn(msg: irc.Message) {
this.#order[this.#current_player];
const player = this.#order[this.#current_player]; const player = this.#order[this.#current_player];
await msg.replyNotice( await msg.replyNotice(
`It's ${player}'s (${ `It's ${player}'s (${
@ -207,7 +213,7 @@ class Game {
// The color does not matter for wilds // The color does not matter for wilds
return ( return (
c.value == selected_card.value && c.value == selected_card.value &&
(["W", "WD4"].includes(c.value) ? true : c.color == selected_card.color) (wild_cards.includes(c.value) ? true : c.color == selected_card.color)
); );
}); });
if (card_index === undefined || card_index === -1) { if (card_index === undefined || card_index === -1) {
@ -223,7 +229,7 @@ class Game {
selected_card.value === "WD4") selected_card.value === "WD4")
: this.#current_card.color === selected_card.color || : this.#current_card.color === selected_card.color ||
this.#current_card.value === selected_card.value || this.#current_card.value === selected_card.value ||
["WD4", "W"].includes(selected_card.value); wild_cards.includes(selected_card.value);
if (!valid) { if (!valid) {
await msg.replyNotice("This card cannot be played.", true); await msg.replyNotice("This card cannot be played.", true);
@ -283,8 +289,6 @@ class Game {
this.#current_player = 0; this.#current_player = 0;
} }
const previous_length = this.#order.length;
if (this.#order.includes(player)) { if (this.#order.includes(player)) {
this.#hands.delete(player); this.#hands.delete(player);
this.#order = this.#order.filter((v) => v !== player); this.#order = this.#order.filter((v) => v !== player);

View File

@ -4,7 +4,7 @@
* @license ISC * @license ISC
*/ */
import { BufReader } from "https://deno.land/std@v1.0.0-rc1/io/bufio.ts"; import { BufReader } from "https://deno.land/std@v0.65.0/io/bufio.ts";
import IrcError from "./error.ts"; import IrcError from "./error.ts";
import Message from "./message.ts"; import Message from "./message.ts";