From f3d1f62d5f7290bdb586ca2b6ce239358297adb8 Mon Sep 17 00:00:00 2001 From: holzmaster Date: Fri, 6 Sep 2024 15:30:20 +0200 Subject: [PATCH] Implement penalty for radioactive waste --- src/service/loot.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/service/loot.ts b/src/service/loot.ts index 9951d286..4f3d1299 100644 --- a/src/service/loot.ts +++ b/src/service/loot.ts @@ -521,7 +521,7 @@ async function postLootDrop(context: BotContext, channel: GuildBasedChannel & Te const defaultWeights = lootTemplates.map(t => t.weight); - const weights = await getDropWeightAdjustments(interaction.user, defaultWeights); + const { messages, weights } = await getDropWeightAdjustments(interaction.user, defaultWeights); const template = randomEntryWeighted(lootTemplates, weights); const l = await loot.createLoot(template, validUntil, message); @@ -557,7 +557,7 @@ async function postLootDrop(context: BotContext, channel: GuildBasedChannel & Te } : undefined, footer: { - text: `🎉 ${winner.displayName} hat das Geschenk geöffnet`, + text: `🎉 ${winner.displayName} hat das Geschenk geöffnet\n${messages.join("\n")}`.trim(), }, }, ], @@ -632,6 +632,29 @@ export function transferLootToUser(lootId: number, user: User) { return loot.transferLootToUser(lootId, user.id); } -async function getDropWeightAdjustments(user: User, weights: readonly number[]): Promise { - return [...weights]; // TODO: User penalties +type AdjustmentResult = { + messages: string[]; + weights: number[]; +}; + +async function getDropWeightAdjustments( + user: User, + weights: readonly number[], +): Promise { + const waste = await getUserLootsById(user, LootTypeId.RADIOACTIVE_WASTE); + const messages = []; + + let wasteFactor = 1; + if (waste.length > 0) { + const wasteDropPenalty = 1.05; + wasteFactor = Math.min(2, waste.length ** wasteDropPenalty); + messages.push( + "Du hast schon radioaktiven Müll, deshalb ist die Chance auf ein Geschenk geringer.", + ); + } + + return { + messages, + weights: [Math.ceil(weights[0] * wasteFactor) | 0, ...weights.slice(1)], + }; }