import { ApiCall } from "tsrpc";
import { JJCFun } from '../../public/jjc';
import { PlayerFun } from '../../public/player';
import { ReqBuyFightNum, ResBuyFightNum } from "../../shared/protocols/jjc/PtlBuyFightNum";
import { jjcChange } from '../../shared/protocols/jjc/PtlOpen';
import { PublicShared } from '../../shared/public/public';
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqBuyFightNum, ResBuyFightNum>) {

    const data = await JJCFun.getData(call);
    const change: jjcChange = {};
    const buyNum = call.req.num || 1;
    const maxBuyNum = G.gc.jjc_com.buyFightNumVip[call.conn.gud.vip] as number;

    if (data.buyFightNum + buyNum > maxBuyNum) return call.error(globalThis.lng.jjc_1);

    let need = new Array(call.req.num).fill(1).map((v, i) => i + 1).map(n => {
        return {
            a: 'attr',
            t: 'rmbmoney',
            n: Math.floor((data.buyFightNum + n) / 2 * 10) + 10
        };
    });
    need = PublicShared.mergePrize(need);

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

    change.buyFightNum = data.buyFightNum + buyNum;

    JJCFun.changeData(call, change);

    HongDianChange.sendChangeKey(call.uid, ['jjchd']);
    call.succ({
        change: change
    });
}