63 lines
2.5 KiB
TypeScript
63 lines
2.5 KiB
TypeScript
import { levelingService } from "@/modules/leveling/leveling.service";
|
|
import { economyService } from "@/modules/economy/economy.service";
|
|
import { userTimers } from "@/db/schema";
|
|
import type { EffectHandler } from "./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 handleAddXp: EffectHandler = async (userId, effect, txFn) => {
|
|
await levelingService.addXp(userId, BigInt(effect.amount), txFn);
|
|
return `Gained ${effect.amount} XP`;
|
|
};
|
|
|
|
export const handleAddBalance: EffectHandler = async (userId, effect, txFn) => {
|
|
await economyService.modifyUserBalance(userId, BigInt(effect.amount), 'ITEM_USE', `Used Item`, null, txFn);
|
|
return `Gained ${effect.amount} 🪙`;
|
|
};
|
|
|
|
export const handleReplyMessage: EffectHandler = async (_userId, effect, _txFn) => {
|
|
return effect.message;
|
|
};
|
|
|
|
export const handleXpBoost: EffectHandler = async (userId, effect, txFn) => {
|
|
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 } }
|
|
});
|
|
return `XP Boost (${effect.multiplier}x) active for ${Math.floor(boostDuration / 60)}m`;
|
|
};
|
|
|
|
export const handleTempRole: EffectHandler = async (userId, effect, txFn) => {
|
|
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
|
|
return `Temporary Role granted for ${Math.floor(roleDuration / 60)}m`;
|
|
};
|
|
|
|
export const handleColorRole: EffectHandler = async (_userId, _effect, _txFn) => {
|
|
return "Color Role Equipped";
|
|
};
|