Skip to content

Zerfall von radioaktivem müll #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
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
3 changes: 2 additions & 1 deletion src/service/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { checkBirthdays } from "@/service/birthday.js";
import { handleFadingMessages } from "@/service/fadingMessage.js";
import { checkExpiredShifts } from "@/service/lootRoles.js";
import { getTrichterUnserEmbed } from "@/service/trichterUnser.js";
import { degradeItems, exposeWithRadiation } from "@/service/lootDegradation.js";
import { degradeItems, exposeWithRadiation, runHalfLife } from "@/service/lootDegradation.js";

import * as poll from "@/commands/poll.js";
import * as ehre from "@/storage/ehre.js";
Expand Down Expand Up @@ -47,6 +47,7 @@ export async function schedule(context: BotContext) {
cron("0 20 * * FRI", () => getTrichterUnserEmbed(context));
cron("0 * * * *", () => degradeItems(context));
cron("26 18,19 * * *", () => exposeWithRadiation(context));
cron("15 18,19 * * *", () => runHalfLife(context));

const loot = context.commandConfig.loot;
if (loot.enabled) {
Expand Down
72 changes: 70 additions & 2 deletions src/service/lootDegradation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type Snowflake, userMention } from "discord.js";
import type { BotContext } from "@/context.js";

import * as time from "@/utils/time.js";
import * as lootService from "@/service/loot.js";
import { LootAttributeKindId, LootKindId } from "@/service/lootData.js";
import { LootAttributeKindId, LootKindId, resolveLootTemplate } from "@/service/lootData.js";
import log from "@log";
import { randomEntry } from "@/service/random.js";

Expand Down Expand Up @@ -73,11 +74,78 @@ export async function exposeWithRadiation(context: BotContext) {
await context.textChannels.hauptchat.send({
embeds: [
{
description: `:radioactive: ${targetLoot.displayName} von <@${targetLoot.winnerId}> wurde verstrahlt. :radioactive:`,
description: `:radioactive: ${targetLoot.displayName} von ${userMention(targetLoot.winnerId)} wurde verstrahlt. :radioactive:`,
footer: {
text: "Du solltest deinen Müll besser entsorgen",
},
},
],
});
}

export async function runHalfLife(context: BotContext) {
log.info("Running half life");

const allWaste = await lootService.getLootsByKindId(LootKindId.RADIOACTIVE_WASTE);

// See: https://github.com/NullDev/CSZ-Bot/issues/470
// We don't do /2 straigt away, so we can roll this out more slowly initially. We can increase this to 2 once we got this number down in general
// Also, consider aligning this with the drop rate of radioactive waste, so we have that balanced
const targetWasteCount = Math.ceil(allWaste.length / 1.1);

if (targetWasteCount >= allWaste.length) {
return;
}

const wasteToRemove = allWaste.sort((a, b) => Math.random()).slice(targetWasteCount);
if (wasteToRemove.length === 0) {
return;
}

const leadTemplate = resolveLootTemplate(LootKindId.BLEI);
if (!leadTemplate) {
log.error("Could not resolve loot template for lead.");
return;
}

const replacedStats = new Map<Snowflake, number>();

for (const l of wasteToRemove) {
const replaced = await lootService.replaceLoot(
l.id,
{
displayName: leadTemplate.displayName,
description: leadTemplate.infoDescription ?? leadTemplate.dropDescription,
lootKindId: leadTemplate.id,
usedImage: leadTemplate.asset,
winnerId: l.winnerId,
claimedAt: l.claimedAt,
guildId: l.guildId,
channelId: l.channelId,
messageId: l.messageId,
origin: "replacement",
},
true,
);

replacedStats.set(replaced.winnerId, replacedStats.getOrInsert(replaced.winnerId, 0) + 1);
}

const listFormatter = new Intl.ListFormat("de", {
style: "short",
type: "conjunction",
});

const decayStats = replacedStats
.entries()
.toArray()
.map(([user, count]) => `${count}x von ${userMention(user)}`);

await context.textChannels.hauptchat.send({
embeds: [
{
description: `:radioactive: Der Müll ${decayStats.length === 1 ? "eines Users" : "einiger User"} ist zu einem Stück Blei zerfallen: ${listFormatter.format(decayStats)}. :radioactive:`,
},
],
});
}