forked from syntaxbullet/AuroraBot-discord
feat: Introduce modular inventory item effect handling and centralize Discord interaction routing.
This commit is contained in:
@@ -8,12 +8,7 @@ import { UserError } from "@/lib/errors";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction, ItemUsageData } from "@/lib/types";
|
||||
|
||||
// Helper to extract duration in seconds
|
||||
const getDuration = (effect: any): number => {
|
||||
if (effect.durationHours) return effect.durationHours * 3600;
|
||||
if (effect.durationMinutes) return effect.durationMinutes * 60;
|
||||
return effect.durationSeconds || 60; // Default to 60s if nothing provided
|
||||
};
|
||||
|
||||
|
||||
export const inventoryService = {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
@@ -165,53 +160,17 @@ export const inventoryService = {
|
||||
const results: string[] = [];
|
||||
|
||||
// 2. Apply Effects
|
||||
// 2. Apply Effects
|
||||
const { effectHandlers } = await import("./effects/registry");
|
||||
|
||||
for (const effect of usageData.effects) {
|
||||
switch (effect.type) {
|
||||
case 'ADD_XP':
|
||||
await levelingService.addXp(userId, BigInt(effect.amount), txFn);
|
||||
results.push(`Gained ${effect.amount} XP`);
|
||||
break;
|
||||
case 'ADD_BALANCE':
|
||||
await economyService.modifyUserBalance(userId, BigInt(effect.amount), 'ITEM_USE', `Used ${item.name}`, null, txFn);
|
||||
results.push(`Gained ${effect.amount} 🪙`);
|
||||
break;
|
||||
case 'REPLY_MESSAGE':
|
||||
results.push(effect.message);
|
||||
break;
|
||||
case 'XP_BOOST':
|
||||
const boostDuration = getDuration(effect);
|
||||
const expiresAt = new Date(Date.now() + boostDuration * 1000);
|
||||
await txFn.insert(userTimers).values({
|
||||
userId: BigInt(userId),
|
||||
type: 'EFFECT',
|
||||
key: 'xp_boost',
|
||||
expiresAt: expiresAt,
|
||||
metadata: { multiplier: effect.multiplier }
|
||||
}).onConflictDoUpdate({
|
||||
target: [userTimers.userId, userTimers.type, userTimers.key],
|
||||
set: { expiresAt: expiresAt, metadata: { multiplier: effect.multiplier } }
|
||||
});
|
||||
results.push(`XP Boost (${effect.multiplier}x) active for ${Math.floor(boostDuration / 60)}m`);
|
||||
break;
|
||||
case 'TEMP_ROLE':
|
||||
const roleDuration = getDuration(effect);
|
||||
const roleExpiresAt = new Date(Date.now() + roleDuration * 1000);
|
||||
await txFn.insert(userTimers).values({
|
||||
userId: BigInt(userId),
|
||||
type: 'ACCESS',
|
||||
key: `role_${effect.roleId}`,
|
||||
expiresAt: roleExpiresAt,
|
||||
metadata: { roleId: effect.roleId }
|
||||
}).onConflictDoUpdate({
|
||||
target: [userTimers.userId, userTimers.type, userTimers.key],
|
||||
set: { expiresAt: roleExpiresAt }
|
||||
});
|
||||
// Actual role assignment happens in the Command layer
|
||||
results.push(`Temporary Role granted for ${Math.floor(roleDuration / 60)}m`);
|
||||
break;
|
||||
case 'COLOR_ROLE':
|
||||
results.push("Color Role Equipped");
|
||||
break;
|
||||
const handler = effectHandlers[effect.type];
|
||||
if (handler) {
|
||||
const result = await handler(userId, effect, txFn);
|
||||
results.push(result);
|
||||
} else {
|
||||
console.warn(`No handler found for effect type: ${effect.type}`);
|
||||
results.push(`Effect ${effect.type} applied (no description)`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user