import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqRecPrize, ResRecPrize } from "../../shared/protocols/kbzz/PtlRecPrize";
import { HongDianChange } from "../hongdian/fun";
import { kbzzUserDb } from './ApiOpen';

export default async function (call: ApiCall<ReqRecPrize, ResRecPrize>) {
    if (!G.gc.kbzz.scorePrize[call.req.index]) return call.error(globalThis.lng.kbzz_1);

    let dbData = await kbzzUserDb().findOne({ uid: call.uid, type: 'kbzzUser' });
    if (dbData.recScorePrize.includes(call.req.index)) return call.error(globalThis.lng.jjc_5);

    let conf = G.gc.kbzz.scorePrize[call.req.index];
    if (dbData.toDayScore < conf.total) return call.error(globalThis.lng.kbzz_5);

    await PlayerFun.sendPrize(call, conf.prize);

    kbzzUserDb().updateOne({ uid: call.uid, type: 'kbzzUser' }, { $set: { recScorePrize: dbData.recScorePrize.concat(call.req.index) } });

    HongDianChange.sendChangeKey(call.uid, ['kbzzhd'])
    call.succ({
        prize: conf.prize
    });
}