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.
 
 
 
motw-tracker/src/commands/experience.ts

65 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 ExperienceCharacterCommand extends AbstractCharacterStatusCommand {
constructor(creator: SlashCreator) {
super(creator, {
name: "experience",
description: "Modifies the EXP total 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 EXP to or remove EXP from.",
required: true,
},
{
type: CommandOptionType.INTEGER,
name: "delta",
description: "The amount of EXP to apply to the character(s) (default +1).",
max_value: 25,
min_value: -25,
required: false,
},
]
});
}
async process(ctx: CommandContext, characters: Map<string, readonly GameCharacterData[]>): Promise<readonly [string, LoadedCharacterData[]] | readonly [string, LoadedCharacterData] | readonly LoadedCharacterData[] | LoadedCharacterData | string> {
const delta = ctx.options["delta"] ?? 1
const description: string[] = []
const result: LoadedCharacterData[] = []
for (const character of characters.get("character")!) {
if (!character.success) {
description.push(`**${character.name}** ${delta >= 0 ? "gained" : "lost"} ${Math.abs(delta)} EXP${delta >= 0 ? "!" : "."}`)
continue
}
character.newData = {
...character.newData ?? character.originalData
}
const oldLevels = Math.floor(character.newData.experience / 5)
character.newData.experience = Math.max(0, (character.newData.experience ?? 0) + delta)
const levels = Math.floor(character.newData.experience / 5)
result.push(character)
description.push(`**${character.name}** ${delta >= 0 ? "gained" : "lost"} ${Math.abs(delta)} EXP${delta >= 0 ? "!" : "."}${levels > oldLevels ? " ***Level up!***" : ""}`)
}
return [description.join("\n"), result]
}
}