You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
2.5 KiB

import {
ApplicationCommandType,
AutocompleteContext, type CommandContext,
CommandOptionType, Message,
SlashCommand,
type SlashCreator
} from "slash-create";
import {type GameCharacter, listCharacters, loadCharacter, saveCharacter} from "../character.js";
import {renderStatus} from "../renderStatus.js";
import {readFile} from "fs/promises";
import {join} from "path";
import {
AbstractCharacterStatusCommand,
CharacterOptionTemplate,
type GameCharacterData,
type LoadedCharacterData
} from "./base.js";
export class LuckCharacterCommand extends AbstractCharacterStatusCommand {
constructor(creator: SlashCreator) {
super(creator, {
name: "luck",
description: "Modifies the luck of the given character(s).",
type: ApplicationCommandType.CHAT_INPUT,
guildIDs: process.env.DEVELOPMENT_GUILD_ID,
options: [
{
...CharacterOptionTemplate,
name: "character",
description: "The name of the character(s) to grant luck to or remove luck from.",
required: true,
},
{
type: CommandOptionType.INTEGER,
name: "delta",
description: "The amount of luck to apply to the character(s) (default -1).",
max_value: 7,
min_value: -7,
required: false,
},
]
});
}
async process(ctx: CommandContext, characters: Map<string, readonly GameCharacterData[]>): Promise<readonly [string, LoadedCharacterData[]] | readonly [string, LoadedCharacterData] | readonly LoadedCharacterData[] | LoadedCharacterData | string> {
const delta: number = ctx.options["delta"]
const description: string[] = []
const result: LoadedCharacterData[] = []
for (const character of characters.get("character")!) {
if (!character.success) {
description.push(`**${character.name}** ${delta > 0 ? "recovered" : "spent"} ${Math.abs(delta)} Luck${delta !== 0 ? "!" : "."}`)
continue
}
character.newData = {
...(character.newData ?? character.originalData)
}
const wasDoomed = (character.newData.luck ?? 7) === 0
character.newData.luck = Math.max(0, Math.min((character.newData.luck ?? 7) + delta, 7))
const isDoomed = character.newData.luck === 0
description.push(`**${character.name}** ${delta > 0 ? "recovered" : "spent"} ${Math.abs(delta)} Luck${delta !== 0 ? "!" : "."}${
!wasDoomed && isDoomed ? " ***Doomed...***" : wasDoomed && !isDoomed ? " ***Fate averted!***" : ""}`)
result.push(character)
}
return [description.join("\n"), result]
}
}