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

export default async function (call: ApiCall<ReqRec, ResRec>) {
    let dbData = await G.mongodb.collection('slzdUser').findOne({ uid: call.uid });

    if (dbData.data.winRec.includes(call.req.index)) return call.error(globalThis.lng.slzd_5);
    if (dbData.data.winNum < G.gc.slzd.fightWinNumPrize[call.req.index].total) return call.error(globalThis.lng.slzd_6);

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

    await G.mongodb.collection('slzdUser').updateOne(
        { uid: call.uid },
        { $set: { 'data.winRec': [...dbData.data.winRec, call.req.index] } }
    );

    HongDianChange.sendChangeKey(call.uid, ['slzdhd'])

    call.succ({
        prize: G.gc.slzd.fightWinNumPrize[call.req.index].prize
    });
}