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

export default async function (call: ApiCall<ReqRec, ResRec>) {
    const req = call.req;
    const conf = G.gc.shoot;
    const data = await G.mongodb.collection('playerInfo', 'shootGame').findOne({ uid: call.uid, type: 'shootGame' });
    const passList = data.passList || {};

    if (!conf[req.id] || !conf[req.id].prize[req.level]) return call.error(globalThis.lng.shootgame_1);

    if (conf[Number(req.id) - 1] && !!!data.passList[Number(req.id) - 1]) return call.error(globalThis.lng.shootgame_2);

    if (data?.receiveList.includes(req.id)) return call.error(globalThis.lng.shootgame_3);

    await PlayerFun.sendPrize(call, conf[req.id].prize[req.level].content);

    let obj = {};
    obj[`passList.${req.id}`] = 1;

    G.mongodb.collection('playerInfo', 'shootGame').updateOne(
        { uid: call.uid, type: 'shootGame' },
        { $inc: obj, $push: { receiveList: req.id } }
    );

    call.succ({
        prize: conf[req.id].prize[req.level].content
    });
}