Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/service/lootData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.DOENER]: {
id: LootKind.DOENER,
weight: 5,
timeBasedWeight: {
evening: 10,
},
displayName: "Döner",
titleText: "Einen Döner",
dropDescription: "Bewahre ihn gut als Geldanlage auf!",
Expand All @@ -156,6 +159,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.KRANKSCHREIBUNG]: {
id: LootKind.KRANKSCHREIBUNG,
weight: 0.5,
timeBasedWeight: {
morning: 1,
},
displayName: "Arbeitsunfähigkeitsbescheinigung",
titleText: "Einen gelben Urlaubsschein",
dropDescription: "Benutze ihn weise!",
Expand Down Expand Up @@ -222,6 +228,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.AYRAN]: {
id: LootKind.AYRAN,
weight: 1,
timeBasedWeight: {
evening: 2,
},
displayName: "Ayran",
titleText: "Einen Ayran",
dropDescription: "Der gute von Müller",
Expand All @@ -242,6 +251,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.TRICHTER]: {
id: LootKind.TRICHTER,
weight: 1,
timeBasedWeight: {
evening: 2,
},
displayName: "Trichter",
titleText: "Einen Trichter",
dropDescription: "Für die ganz großen Schlücke",
Expand Down Expand Up @@ -313,6 +325,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.OETTINGER]: {
id: LootKind.OETTINGER,
weight: 1,
timeBasedWeight: {
morning: 2,
},
displayName: "Oettinger",
titleText: "Ein warmes Oettinger",
dropDescription: "Ja dann Prost ne!",
Expand Down Expand Up @@ -370,6 +385,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.SAHNE]: {
id: LootKind.SAHNE,
weight: 1,
timeBasedWeight: {
evening: 2,
},
displayName: "Sprühsahne",
titleText: "Sprühsahne",
dropDescription: "Fürs Frühstück oder so",
Expand Down Expand Up @@ -414,6 +432,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.GAULOISES_BLAU]: {
id: LootKind.GAULOISES_BLAU,
weight: 1,
timeBasedWeight: {
evening: 2,
},
displayName: "Gauloises Blau",
titleText: "Eine Schachtel Gauloises Blau",
dropDescription:
Expand Down Expand Up @@ -496,6 +517,9 @@ export const lootTemplateMap: Record<LootKindId, LootTemplate> = {
[LootKind.KAFFEEMUEHLE]: {
id: LootKind.KAFFEEMUEHLE,
weight: 1,
timeBasedWeight: {
morning: 2,
},
displayName: "Kaffeemühle",
titleText: "Eine Kaffeemühle für 400€",
dropDescription: "Kann Kaffee mühlen. Und das gut. Mit Gold.",
Expand Down
26 changes: 24 additions & 2 deletions src/service/lootDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import * as sentry from "@sentry/node";

import type { BotContext } from "#context.ts";
import type { Loot, LootId } from "#storage/db/model.ts";
import type { LootTemplate } from "#storage/loot.ts";
import type { LootTemplate, TimeBasedWeightConfig } from "#storage/loot.ts";
import { randomBoolean, randomEntry, randomEntryWeighted } from "#service/random.ts";
import * as timeUtils from "#utils/time.ts";
import { zonedNow } from "#utils/dateUtils.ts";

import * as lootService from "#service/loot.ts";
import {
Expand Down Expand Up @@ -154,7 +155,12 @@ export async function postLootDrop(
return;
}

const defaultWeights = lootTemplates.map(t => t.weight);
const timeBasedWeightKey = getCurrentTimeBasedKey();

const defaultWeights = timeBasedWeightKey
? lootTemplates.map(t => t.timeBasedWeight?.[timeBasedWeightKey] ?? t.weight)
: lootTemplates.map(t => t.weight);

const { messages, weights } = await getDropWeightAdjustments(interaction.user, defaultWeights);

const template = randomEntryWeighted(lootTemplates, weights);
Expand Down Expand Up @@ -358,6 +364,22 @@ export async function createDropTakenContent(
};
}

function getCurrentTimeBasedKey(): keyof TimeBasedWeightConfig | undefined {
// Caution: using a ZonedDateTime and comparing the hour to the raw values breaks when daylight-saving-time happens (an hour can happen multiple times)
// We disregard these edge-cases, but keep it in mind
const hour = zonedNow().hour;

// :shibakek:
switch (true) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wie unnötig

case 6 <= hour && hour <= 12:
return "morning";
case 18 <= hour && hour <= 24:
return "evening";
default:
return undefined;
}
Comment on lines +372 to +380
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bevor du es anstreichst:
Ich wollte es erst als ternary untereinander schreiben, aber das wurde vom formatter immer in eine einzige Zeile formatiert, weils zu kurz war. Das wollte ich so nicht lassen
Und 2 if-statements waren zu uncool, daher der hack :harold:

}

type AdjustmentResult = {
messages: string[];
weights: number[];
Expand Down
6 changes: 6 additions & 0 deletions src/storage/loot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ export type LootUseCommandInteraction = ChatInputCommandInteraction & {
channel: GuildTextBasedChannel;
};

export interface TimeBasedWeightConfig {
morning?: number;
evening?: number;
}

export interface LootTemplate {
id: LootKindId;
weight: number;
timeBasedWeight?: TimeBasedWeightConfig;
displayName: string;
titleText: string;
dropDescription: string;
Expand Down