Skip to content

Commit

Permalink
add command ausrüsten
Browse files Browse the repository at this point in the history
  • Loading branch information
Feluin committed Oct 23, 2024
1 parent c1433ff commit 7f011fa
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 177 deletions.
93 changes: 88 additions & 5 deletions src/commands/fight.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { ApplicationCommand } from "@/commands/command.js";
import {
APIEmbed,
APIEmbedField,
APIEmbedField, type BooleanCache, type CacheType,
type CommandInteraction,
ContextMenuCommandBuilder,
ContextMenuCommandBuilder, type InteractionResponse,
SlashCommandBuilder,
SlashCommandUserOption,
SlashCommandUserOption
} from "discord.js";
import type { BotContext } from "@/context.js";
import { fight, FightScene } from "@/service/fight.js";
import { JSONEncodable } from "@discordjs/util";
import { bossMap, Entity } from "@/service/fightData.js";
import {BaseEntity, bossMap, Entity} from "@/service/fightData.js";
import {setTimeout} from "node:timers/promises";

export default class FightCommand implements ApplicationCommand {
readonly description = "TBD";
Expand Down Expand Up @@ -49,3 +49,86 @@ export default class FightCommand implements ApplicationCommand {
await fight(playerstats, bossMap[boss], interactionResponse);
}
}


type result = "PLAYER" | "ENEMY" | undefined;

function checkWin(fightscene: FightScene): result {
if (fightscene.player.stats.health < 0) {
return "ENEMY";
}
if (fightscene.enemy.stats.health < 0) {
return "PLAYER";
}
}

export async function fight(
playerstats: BaseEntity,
enemystats: BaseEntity,
interactionResponse: InteractionResponse<BooleanCache<CacheType>>
) {
const enemy = new Entity(enemystats);
const player = new Entity(playerstats);

const scene: FightScene = {
player: player,
enemy: enemy
};
while (checkWin(scene) === undefined) {
player.itemtext = [];
enemy.itemtext = [];
//playerhit first
player.attack(enemy);
// then enemny hit
enemy.attack(player);
//special effects from items

player.stats.items.forEach(value => {
if (!value.afterFight) {
return;
}
value.afterFight(scene);
});
enemy.stats.items.forEach(value => {
if (!value.afterFight) {
return;
}
value.afterFight({player: enemy, enemy: player});
});
await interactionResponse.edit({embeds: [renderFightEmbedded(scene)]});
await setTimeout(200);
}
}

function renderStats(player: Entity) {
while (player.itemtext.length < 5) {
player.itemtext.push("-");
}

return {
name: player.stats.name,
value: `❤️HP${player.stats.health}/${player.maxhealth}
❤️${"=".repeat(Math.max(0, (player.stats.health / player.maxhealth) * 10))}
⚔️Waffe: ${player.stats.weapon?.name ?? "Schwengel"} ${player.lastattack}
🛡️Rüstung: ${player.stats.armor?.name ?? "Nackt"} ${player.lastdefence}
📚Items:
${player.itemtext.join("\n")}
`,
inline: true
};
}

function renderFightEmbedded(fightscene: FightScene): JSONEncodable<APIEmbed> | APIEmbed {
return {
title: `Kampf zwischen ${fightscene.player.stats.name} und ${fightscene.enemy.stats.name}`,
description: fightscene.enemy.stats.description,
fields: [
renderStats(fightscene.player),
renderStats(fightscene.enemy),
{
name: "Verlauf",
value: " "
}
]
};
}
Loading

0 comments on commit 7f011fa

Please sign in to comment.