import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqRec, ResRec } from "../../../shared/protocols/hbzb/jfs/PtlRec";
import { HongDianChange } from "../../hongdian/fun";
import { checkIsOpen } from './ApiOpen';

export default async function (call: ApiCall<ReqRec, ResRec>) {
    if (!checkIsOpen()) return call.error(globalThis.lng.hbzb_7);
    let dbData = await G.mongodb.cPlayerInfo('hbzb').findOne({ uid: call.uid, type: 'hbzb' });

    if (dbData.data.winRecPrize.includes(call.req.index)) return call.error(globalThis.lng.hbzb_9);
    if (dbData.data.winNum < G.gc.hbzb.jfsFightWinNumPrize[call.req.index].total) return call.error(globalThis.lng.hbzb_10);

    await PlayerFun.sendPrize(call, G.gc.hbzb.jfsFightWinNumPrize[call.req.index].prize);

    await G.mongodb.cPlayerInfo('hbzb').updateOne(
        { uid: call.uid, type: 'hbzb' },
        { $set: { 'data.winRecPrize': [...dbData.data.winRecPrize, call.req.index] } }
    );
    HongDianChange.sendChangeKey(call.uid, ['hbzbhd'])
    call.succ({
        prize: G.gc.hbzb.jfsFightWinNumPrize[call.req.index].prize
    });
}