Skip to content

Commit 0e26b0e

Browse files
committed
Rewrite to use callbacks
1 parent 8e758d3 commit 0e26b0e

3 files changed

Lines changed: 67 additions & 86 deletions

File tree

src/commands/devcommands/loot-drop.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
ChannelType,
33
type CommandInteraction,
4-
type GuildBasedChannel,
54
SlashCommandBuilder,
65
SlashCommandNumberOption,
76
} from "discord.js";
@@ -11,8 +10,9 @@ import type { ApplicationCommand } from "#/commands/command.ts";
1110
import { ensureChatInputCommand } from "#/utils/interactionUtils.ts";
1211

1312
import * as lootDataService from "#/service/lootData.ts";
13+
import * as lootService from "#/service/loot.ts";
1414

15-
import { postLootDrop } from "#/service/lootDrop.ts";
15+
import { postLootDrop, type LootClaimCallback } from "#/service/lootDrop.ts";
1616

1717
export default class LootDropCommand implements ApplicationCommand {
1818
name = "loot-drop";
@@ -54,11 +54,19 @@ export default class LootDropCommand implements ApplicationCommand {
5454
});
5555
return;
5656
}
57-
await postLootDrop(context, command.channel, command.user, undefined, {
58-
kind: "predefined",
59-
rarity: undefined,
60-
template: lootTemplate,
61-
});
57+
const predefinedLootClaim: LootClaimCallback = async (winner, message) => {
58+
const loot = await lootService.createLoot(
59+
lootTemplate,
60+
winner,
61+
message,
62+
"drop",
63+
null,
64+
null,
65+
);
66+
if (!loot) return undefined;
67+
return { loot, template: lootTemplate, rarity: undefined, messages: [] };
68+
};
69+
await postLootDrop(context, command.channel, command.user, predefinedLootClaim);
6270
await command.reply({
6371
content: `Es wurde ${lootTemplate.id} gedroppt!`,
6472
ephemeral: true,

src/service/lootData.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
231231
context,
232232
interaction.channel,
233233
interaction.user,
234-
loot.id,
234+
lootDropService.randomizedLootClaim(loot.id),
235235
);
236236
return false;
237237
},
@@ -915,17 +915,24 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
915915
return false;
916916
}
917917

918+
const transferWrappedItemLoot: lootDropService.LootClaimCallback = async winner => {
919+
const claimedLoot = await lootService.transferLootToUser(
920+
randomItem.id,
921+
winner,
922+
true,
923+
);
924+
return {
925+
loot: claimedLoot,
926+
template: lootTemplate,
927+
rarity: rarityAttributeTemplate,
928+
messages: [],
929+
};
930+
};
918931
const claimed = await lootDropService.postLootDrop(
919932
context,
920933
interaction.channel,
921934
interaction.user,
922-
undefined,
923-
{
924-
kind: "transfer",
925-
rarity: rarityAttributeTemplate,
926-
template: lootTemplate,
927-
sourceLootId: randomItem.id,
928-
},
935+
transferWrappedItemLoot,
929936
);
930937
if (!claimed) {
931938
await lootService.deleteLoot(randomItem.id);

src/service/lootDrop.ts

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ButtonStyle,
66
ChannelType,
77
ComponentType,
8+
type Message,
89
type TextChannel,
910
type User,
1011
type Interaction,
@@ -81,22 +82,38 @@ export async function runDropAttempt(context: BotContext) {
8182
log.info(
8283
`Randomization hit threshold (${lootConfig.dropChance}). Dropping loot to ${targetChannel.name}!`,
8384
);
84-
await postLootDrop(context, targetChannel, undefined, undefined);
85+
await postLootDrop(context, targetChannel, undefined, randomizedLootClaim());
8586
}
8687

87-
type LootDrop = {
88+
export type ClaimedLootDrop = {
89+
loot: Loot;
8890
template: LootTemplate;
8991
rarity: LootAttributeTemplate | undefined;
90-
donor: User | undefined;
91-
predecessorLootId: LootId | undefined;
92-
messages: string[];
92+
messages: readonly string[];
9393
};
9494

95-
async function randomizedLootDrop(
96-
user: User,
97-
donor?: User,
98-
predecessorLootId?: LootId,
99-
): Promise<LootDrop> {
95+
export type LootClaimCallback = (
96+
winner: User,
97+
message: Message<true>,
98+
) => Promise<ClaimedLootDrop | undefined>;
99+
100+
export function randomizedLootClaim(predecessorLootId: LootId | null = null): LootClaimCallback {
101+
return async (winner, message) => {
102+
const drop = await randomizedLootDrop(winner);
103+
const loot = await lootService.createLoot(
104+
drop.template,
105+
winner,
106+
message,
107+
"drop",
108+
predecessorLootId,
109+
drop.rarity ?? null,
110+
);
111+
if (!loot) return undefined;
112+
return { loot, ...drop };
113+
};
114+
}
115+
116+
export async function randomizedLootDrop(user: User): Promise<Omit<ClaimedLootDrop, "loot">> {
100117
const timeBasedWeightKey = getCurrentTimeBasedKey();
101118

102119
const defaultWeights = timeBasedWeightKey
@@ -110,56 +127,19 @@ async function randomizedLootDrop(
110127
const rarities = lootAttributeTemplates.filter(a => a.classId === LootAttributeClass.RARITY);
111128
const rarityWeights = rarities.map(a => a.initialDropWeight ?? 0);
112129

113-
const rarityAttribute =
114-
template.id === LootKind.NICHTS ? null : randomEntryWeighted(rarities, rarityWeights);
115-
116-
return {
117-
template,
118-
rarity: rarityAttribute ?? undefined,
119-
donor,
120-
predecessorLootId,
121-
messages,
122-
};
123-
}
130+
const rarity =
131+
template.id === LootKind.NICHTS
132+
? undefined
133+
: (randomEntryWeighted(rarities, rarityWeights) ?? undefined);
124134

125-
function fixedLootDrop(
126-
template: LootTemplate,
127-
rarity?: LootAttributeTemplate,
128-
donor?: User,
129-
predecessorLootId?: LootId,
130-
): LootDrop {
131-
return {
132-
template,
133-
rarity,
134-
donor,
135-
predecessorLootId,
136-
messages: [],
137-
};
135+
return { template, rarity, messages };
138136
}
139137

140-
type RandomizedLootDropStrategy = { kind: "randomized" };
141-
type PredefinedLootDropStrategy = {
142-
kind: "predefined";
143-
template: LootTemplate;
144-
rarity: LootAttributeTemplate | undefined;
145-
};
146-
type TransferLootDropStrategy = {
147-
kind: "transfer";
148-
template: LootTemplate;
149-
rarity: LootAttributeTemplate | undefined;
150-
sourceLootId: LootId;
151-
};
152-
export type LootDropStrategy =
153-
| RandomizedLootDropStrategy
154-
| PredefinedLootDropStrategy
155-
| TransferLootDropStrategy;
156-
157138
export async function postLootDrop(
158139
context: BotContext,
159140
channel: GuildBasedChannel & TextBasedChannel,
160141
donor: User | undefined,
161-
predecessorLootId: LootId | undefined,
162-
strategy: LootDropStrategy = { kind: "randomized" },
142+
onClaim: LootClaimCallback,
163143
): Promise<Loot | undefined> {
164144
const takeLootButton = new ButtonBuilder()
165145
.setCustomId("take-loot")
@@ -225,27 +205,11 @@ export async function postLootDrop(
225205
return;
226206
}
227207

228-
const lootDrop =
229-
strategy.kind === "randomized"
230-
? await randomizedLootDrop(interaction.user, donor, predecessorLootId)
231-
: fixedLootDrop(strategy.template, strategy.rarity, donor, predecessorLootId);
232-
const { template, rarity: rarityAttribute, messages } = lootDrop;
233-
234-
const claimedLoot =
235-
strategy.kind === "transfer"
236-
? await lootService.transferLootToUser(strategy.sourceLootId, interaction.user, true)
237-
: await lootService.createLoot(
238-
template,
239-
interaction.user,
240-
message,
241-
"drop",
242-
predecessorLootId ?? null,
243-
rarityAttribute ?? null,
244-
);
208+
const claimed = await onClaim(interaction.user, message);
245209

246210
const reply = await interaction.deferReply({ flags: MessageFlags.Ephemeral });
247211

248-
if (!claimedLoot) {
212+
if (!claimed) {
249213
await reply.edit({
250214
content: `Upsi, da ist was schief gelaufi oder jemand anderes war schnelli ${context.emoji.sadHamster}`,
251215
});
@@ -254,6 +218,8 @@ export async function postLootDrop(
254218

255219
await reply.delete();
256220

221+
const { loot: claimedLoot, template, rarity: rarityAttribute, messages } = claimed;
222+
257223
log.info(
258224
`User ${interaction.user.username} claimed loot ${claimedLoot.id} (template: ${template.id})`,
259225
);

0 commit comments

Comments
 (0)