-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfight.ts
More file actions
232 lines (212 loc) · 7.61 KB
/
fight.ts
File metadata and controls
232 lines (212 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { setTimeout } from "node:timers/promises";
import type { JSONEncodable } from "@discordjs/util";
import type { ApplicationCommand } from "#commands/command.ts";
import {
type APIEmbed,
type BooleanCache,
type CacheType,
type CommandInteraction,
type InteractionResponse,
SlashCommandBuilder,
type User,
} from "discord.js";
import type { BotContext } from "#context.ts";
import {
type BaseEntity,
baseStats,
bossMap,
Entity,
type EquipableArmor,
type EquipableItem,
type EquipableWeapon,
type FightScene,
} from "#service/fightData.ts";
import { getFightInventoryEnriched, removeItemsAfterFight } from "#/storage/fightInventory.ts";
import { getLastFight, insertResult } from "#/storage/fightHistory.ts";
async function getFighter(user: User): Promise<BaseEntity> {
const userInventory = await getFightInventoryEnriched(user.id);
return {
...baseStats,
name: user.displayName,
weapon: {
name: userInventory.weapon?.itemInfo?.displayName ?? "Nichts",
...(userInventory.weapon?.gameTemplate as EquipableWeapon),
},
armor: {
name: userInventory.armor?.itemInfo?.displayName ?? "Nichts",
...(userInventory.armor?.gameTemplate as EquipableArmor),
},
items: userInventory.items.map(value => {
return {
name: value.itemInfo?.displayName ?? "Error",
...(value.gameTemplate as EquipableItem),
};
}),
};
}
export default class FightCommand implements ApplicationCommand {
readonly description = "TBD";
readonly name = "fight";
readonly applicationCommand = new SlashCommandBuilder()
.setName(this.name)
.setDescription(this.description)
.addStringOption(builder =>
builder
.setRequired(true)
.setName("boss")
.setDescription("Boss")
//switch to autocomplete when we reach 25
.addChoices(
Object.entries(bossMap)
.filter(boss => boss[1].enabled)
.map(boss => {
return {
name: boss[1].name,
value: boss[0],
};
}),
),
);
async handleInteraction(command: CommandInteraction, _context: BotContext) {
if (!command.isChatInputCommand()) {
throw new Error("Invalid command type");
}
const boss = command.options.get("boss", true).value as string;
const _lastFight = await getLastFight(command.user.id);
// if (lastFight !== undefined && (new Date(lastFight?.createdAt).getTime() > new Date().getTime() - 1000 * 60 * 60 * 24 * 5)) {
// await command.reply({
// embeds:
// [{
// title: "Du bist noch nicht bereit",
// color: 0xe74c3c,
// description: `Dein Letzter Kampf gegen ${bossMap[lastFight.bossName]?.name} ist noch keine 5 Tage her. Gegen ${bossMap[boss].name} hast du sowieso Chance, aber noch weniger, wenn nicht die vorgeschriebenen Pausenzeiten einhältst `
// }
// ]
// ,
// ephemeral: true
// }
// );
// return;
// }
const interactionResponse = await command.deferReply();
const playerstats = await getFighter(command.user);
await fight(command.user, playerstats, boss, { ...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";
}
}
function renderEndScreen(fightscene: FightScene): APIEmbed | JSONEncodable<APIEmbed> {
const result = checkWin(fightscene);
const fields = [
renderStats(fightscene.player),
renderStats(fightscene.enemy),
{
name: "Verlauf",
value: " ",
},
{
name: " Die Items sind dir leider beim Kampf kaputt gegangen: ",
value: fightscene.player.stats.items.map(value => value.name).join(" \n"),
},
];
if (result === "PLAYER") {
return {
title: `Mit viel Glück konnte ${fightscene.player.stats.name} ${fightscene.enemy.stats.name} besiegen `,
color: 0x57f287,
description: fightscene.enemy.stats.description,
fields: fields,
};
}
if (result === "ENEMY") {
return {
title: `${fightscene.enemy.stats.name} hat ${fightscene.player.stats.name} gnadenlos vernichtet`,
color: 0xed4245,
description: fightscene.enemy.stats.description,
fields: fields,
};
}
return {
title: `Kampf zwischen ${fightscene.player.stats.name} und ${fightscene.enemy.stats.name}`,
description: fightscene.enemy.stats.description,
fields: fields,
};
}
export async function fight(
user: User,
playerstats: BaseEntity,
boss: string,
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
for (const value of player.stats.items) {
if (!value.afterFight) {
continue;
}
value.afterFight(scene);
}
for (const value of enemy.stats.items) {
if (!value.afterFight) {
continue;
}
value.afterFight({ player: enemy, enemy: player });
}
await interactionResponse.edit({ embeds: [renderFightEmbedded(scene)] });
await setTimeout(200);
}
await interactionResponse.edit({ embeds: [renderEndScreen(scene)] });
//delete items
await removeItemsAfterFight(user.id);
//
await insertResult(user.id, boss, checkWin(scene) === "PLAYER");
}
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.lastDefense}
📚 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: " ",
},
],
};
}