import { executeSoapCommand } from './soap' import { db, DB } from './db' import { getRenamePrice, getCustomizePrice, getChangeRacePrice, getChangeFactionPrice, getLevelUpPrice, getTransferPrice, goldPriceFor, } from './prices' type Meta = Record export interface PaidServiceConfig { price: (meta: Meta) => Promise productName: (character: string, meta: Meta) => string fulfill: (character: string, meta: Meta) => Promise extraFields: string[] // campos (además de character) que el form envía y se guardan en metadata } async function soapFulfill(command: string): Promise { return (await executeSoapCommand(command)) !== null } async function addGold(character: string, goldAmount: number): Promise { try { await db(DB.characters).query('UPDATE characters SET money = money + ? WHERE name = ?', [ goldAmount * 10000, character, ]) return true } catch { return false } } export const PAID_SERVICES: Record = { rename: { price: () => getRenamePrice(), productName: (c) => `Renombrar personaje: ${c}`, fulfill: (c) => soapFulfill(`.char rename ${c}`), extraFields: [], }, customize: { price: () => getCustomizePrice(), productName: (c) => `Personalizar personaje: ${c}`, fulfill: (c) => soapFulfill(`.char customi ${c}`), extraFields: [], }, 'change-race': { price: () => getChangeRacePrice(), productName: (c) => `Cambiar raza: ${c}`, fulfill: (c) => soapFulfill(`.char changerace ${c}`), extraFields: [], }, 'change-faction': { price: () => getChangeFactionPrice(), productName: (c) => `Cambiar facción: ${c}`, fulfill: (c) => soapFulfill(`.char changef ${c}`), extraFields: [], }, 'level-up': { price: () => getLevelUpPrice(), productName: (c) => `Subir a nivel 80: ${c}`, fulfill: (c) => soapFulfill(`.char level ${c} 80`), extraFields: [], }, gold: { price: (m) => goldPriceFor(Number(m.gold_amount)), productName: (c, m) => `Aumentar el Oro: ${m.gold_amount} al Personaje: ${c}`, fulfill: (c, m) => addGold(c, Number(m.gold_amount)), extraFields: ['gold_amount'], }, transfer: { price: () => getTransferPrice(), productName: (c, m) => `Transferir ${c} a la cuenta ${m.destination_account}`, fulfill: (c, m) => soapFulfill(`.char changeaccount ${m.destination_account} ${c}`), extraFields: ['destination_account'], }, } export function getPaidService(slug: string): PaidServiceConfig | null { return PAID_SERVICES[slug] ?? null }