import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqRec, ResRec } from "../../../shared/protocols/event/xianshizhaomu/PtlRec";
import { xszmdb } from './ApiOpen';

export default async function (call: ApiCall<ReqRec, ResRec>) {
    let conf = G.gc.xianshizhaomu.eventPrize[call.req.index];
    if (!conf) return call.error('', { code: -1 });

    let db = await xszmdb().findOne({ uid: call.uid, type: 'xianshizhaomu' });
    if (!db?.recordNum || db.recordNum < conf.total || db?.recordPrize?.includes(call.req.index)) return call.error('', { code: -2 });

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

    xszmdb().updateOne(
        { uid: call.uid, type: 'xianshizhaomu' },
        { $push: { recordPrize: call.req.index } },
        { upsert: true }
    );

    call.succ({
        prize: conf.prize
    });
}