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-
157138export 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