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.
88 lines
3.5 KiB
88 lines
3.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 HarmCharacterCommand extends AbstractCharacterStatusCommand {
|
|
constructor(creator: SlashCreator) {
|
|
super(creator, {
|
|
name: "harm",
|
|
description: "Harms the given character(s).",
|
|
type: ApplicationCommandType.CHAT_INPUT,
|
|
guildIDs: process.env.DEVELOPMENT_GUILD_ID,
|
|
options: [
|
|
{
|
|
...CharacterOptionTemplate,
|
|
description: "The name of the character(s) to harm.",
|
|
required: true,
|
|
},
|
|
{
|
|
type: CommandOptionType.INTEGER,
|
|
name: "damage",
|
|
description: "The amount of harm to deal to the character(s).",
|
|
max_value: 99,
|
|
min_value: 0,
|
|
required: true,
|
|
},
|
|
{
|
|
type: CommandOptionType.BOOLEAN,
|
|
name: "piercing",
|
|
description: "If set, ignores any armor the character(s) may have.",
|
|
},
|
|
{
|
|
type: CommandOptionType.BOOLEAN,
|
|
name: "destabilize",
|
|
description: "True to force unstable, False to never set unstable. Default based on remaining health.",
|
|
required: false,
|
|
}
|
|
]
|
|
});
|
|
}
|
|
|
|
async process(ctx: CommandContext, characters: Map<string, readonly GameCharacterData[]>): Promise<readonly [string, LoadedCharacterData[]] | readonly [string, LoadedCharacterData] | readonly LoadedCharacterData[] | LoadedCharacterData | string> {
|
|
const baseDamage: number = ctx.options["damage"]
|
|
const piercing: boolean = ctx.options["piercing"] ?? false
|
|
const makeUnstable: boolean|null = ctx.options["destabilize"] ?? null
|
|
const description: string[] = []
|
|
const result: LoadedCharacterData[] = []
|
|
for (const character of characters.get("character")!) {
|
|
if (!character.success) {
|
|
description.push(`**${character.name}** took ${baseDamage} Harm${piercing ? " ignore-armour" : ""}${baseDamage > 0 ? "!" : "."}${makeUnstable ? " ***Unstable!***" : ""}`)
|
|
continue
|
|
}
|
|
character.newData = {
|
|
...(character.newData ?? character.originalData)
|
|
}
|
|
const effectiveDamage = Math.max(0, baseDamage - (piercing ? 0 : (character.newData.armor ?? 0)))
|
|
const blocked = Math.max(0, baseDamage - effectiveDamage)
|
|
const wasUnstable = character.newData.unstable ?? false
|
|
const wasAlive = (character.newData.health ?? 8) > 0
|
|
character.newData.health = Math.max(0, (character.newData.health ?? 8) - effectiveDamage)
|
|
if ((makeUnstable === null && character.newData.health <= 4) || makeUnstable) {
|
|
character.newData.unstable = true
|
|
}
|
|
const isUnstable = character.newData.unstable ?? false
|
|
const isAlive = character.newData.health > 0
|
|
description.push(`**${character.name}** took ${effectiveDamage} Harm${
|
|
piercing ? " ignore-armour" : blocked > 0 ? ` (${blocked} blocked)` : ""}${effectiveDamage > 0 ? "!" : "."}${
|
|
wasAlive && !isAlive ? " ***Defeated...***" : !wasUnstable && isUnstable ? " ***Unstable!***" : ""}`)
|
|
result.push(character)
|
|
}
|
|
return [description.join("\n"), result]
|
|
}
|
|
}
|
|
|