import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqBuy, ResBuy } from "../../shared/protocols/gonglukuangbiao/PtlBuy";

export default async function (call: ApiCall<ReqBuy, ResBuy>) {
    call.req.num = Math.floor(call.req.num);
    if (call.req.num <= 0) return call.error('', { code: 0 });

    let db = await G.mongodb.collection('wanted').findOne({ uid: call.uid });
    if (db.toDayBuyNum + call.req.num > G.gc.gonglukuangbiao.vipBuyFightNum[call.conn.gud.vip]) return call.error('', { code: -1 });

    let _con = JSON.parse(JSON.stringify(G.gc.gonglukuangbiao.buyFightNumNeed))
    let need = []
    for (let index = db.toDayBuyNum; index < db.toDayBuyNum + call.req.num; index++) {
        need.push(_con[index])
    }

    await PlayerFun.checkNeedIsMeet(call, need);
    await PlayerFun.cutNeed(call, need);

    G.mongodb.collection('wanted').updateOne({ uid: call.uid }, { $inc: { toDayBuyNum: call.req.num } });

    call.succ({});
}