import {ApiCall} from "tsrpc";
import {PlayerFun} from '../../public/player';
import {ReqUp, ResUp} from "../../shared/protocols/zhanqianbushu/PtlUp";
import {PublicShared} from "../../shared/public/public";

export default async function (call: ApiCall<ReqUp, ResUp>) {
    let config = G.gc.fightPlan[call.req.type];
    if (!config) return call.error('', {code: -1});

    let conf = config[call.conn.gud.matrix[call.req.type]];

    let need
    if (conf.need.length > 0) {
        need = conf.need.map(_need => {
            return {
                ..._need,
                n: PublicShared.eval(_need.n, {zflv: call.conn.gud.matrix[call.req.type]})
            };
        });
        await PlayerFun.checkNeedIsMeet(call, need);
        await PlayerFun.cutNeed(call, need);
    }

    let obj = Object.assign({}, call.conn.gud.matrix);
    obj[call.req.type]++;
    await PlayerFun.addAttr(call, {matrix: obj});

    call.succ({});
}