import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqFbBuyNum, ResFbBuyNum } from "../../shared/protocols/gonghui/PtlFbBuyNum";

export default async function (call: ApiCall<ReqFbBuyNum, ResFbBuyNum>) {
    let _buyNum = (await G.mongodb.collection('gonghuiUser').findOne({ uid: call.uid }))?.fuben?.addNum || 0;
    let shiliConfFightNumBuyNeed = G.gc.shili_com.fightNumBuyNeed
    let atn = shiliConfFightNumBuyNeed[_buyNum] || shiliConfFightNumBuyNeed[shiliConfFightNumBuyNeed.length - 1] 
    await PlayerFun.checkNeedIsMeet(call, [atn]);
    await PlayerFun.cutNeed(call, [atn]);
    // 策划特别强调加上（到时候别又搞取消。。。。。。。。。。。。。。。。。。。。。。。。。）
    if (_buyNum >= shiliConfFightNumBuyNeed.length) {
        return call.error('', { code: -1, message: globalThis.lng.gonghui_10 })
    }

    G.mongodb.collection('gonghuiUser').updateOne({ uid: call.uid }, { $inc: { 'fuben.fightNum': 1, 'fuben.addNum': 1 } });

    call.succ({});
}