import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqBuyLv, ResBuyLv } from "../../../shared/protocols/event/zhanling/PtlBuyLv";
import { HongDianChange } from "../../hongdian/fun";

export default async function (call: ApiCall<ReqBuyLv, ResBuyLv>) {
    let lv = call.req.lv;

    if (lv < 1) return call.error(globalThis.lng.pata_getprize_1);

    let data = await G.mongodb.cEvent('zhanling').findOne({ uid: call.uid, type: 'zhanling' });

    if (G.gc.zhanling.lv[data.lv + lv] == undefined) return call.error(globalThis.lng.pata_getprize_1);

    let need = G.gc.zhanling.lvNeed.map(need => { return { ...need, n: need.n * lv }; });

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

    let change: ResBuyLv['change'] = {};

    change.lv = data.lv + lv;
    change.exp = data.exp + 1000;

    G.mongodb.cEvent('zhanling').updateOne(
        { uid: call.uid, type: 'zhanling' },
        { $set: change }
    );

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

    call.succ({
        change: change
    });
}