import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqBuyNum, ResBuyNum } from "../../shared/protocols/conglinshoulie/PtlBuyNum";
import { HongDianChange } from "../hongdian/fun";
import { clslDb } from './ApiOpen';

export default async function (call: ApiCall<ReqBuyNum, ResBuyNum>) {

    let db = await clslDb().findOne({ uid: call.uid, type: 'clsl' });
    let buyNum = db?.buyFightNum || 0;
    let hasNum = G.gc.clsl_com.vipBuyFightNum[call.conn.gud.vip];

    if (buyNum >= hasNum) return call.errorCode(-1);

    let _need = [G.gc.clsl_com.buyFightNumNeed[buyNum]]
    await PlayerFun.checkNeedIsMeet(call, _need);
    await PlayerFun.cutNeed(call, _need);

    clslDb().updateOne({ uid: call.uid, type: 'clsl' }, { $inc: { buyFightNum: 1 } });

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

    call.succ({});
}