import { ApiCall } from "tsrpc";
import { HeroFun } from '../../public/hero';
import { PlayerFun } from '../../public/player';
import { ReqLvUp, ResLvUp } from "../../shared/protocols/hero/PtlLvUp";
import { ServiceType } from '../../shared/protocols/serviceProto';
import { HeroShared } from '../../shared/public/hero';
import { event_cszl_record } from '../event/chuanshuozhilu/ApiOpen';
import { HongDianChange } from "../hongdian/fun";
import {PublicShared} from "../../shared/public/public";
import kfjsFun from "../../public/kaifujingsai";

export default async function (call: ApiCall<ReqLvUp, ResLvUp, ServiceType>) {

    let hero = await HeroFun.getHero(call, call.req.id)
    let up = call.req.up || 1
    if (!hero) return call.errorCode(-1);
    if (hero.lv >= call.conn.gud.lv * 3 || hero.lv + up > call.conn.gud.lv * 3) return call.errorCode(-2);

    let need = []
    new Array(up).fill(1).map((v, i) => {
        need.push(...HeroShared.getHeroLvUpNeed(hero.heroId, hero.lv + i));
    })

    need = PublicShared.mergePrize(need);

    await PlayerFun.checkNeedIsMeet(call, need);
    await PlayerFun.cutNeed(call, need);
    await HeroFun.changeHeroAttr(call, hero, {
        lv: hero.lv + up
    });
    event_cszl_record(call.conn.gud, hero.heroId, hero.lv, 'lv')

    HongDianChange.sendChangeKey(call.uid, ['taskhd', 'huodonghd']);

    call.succ({ lv: hero.lv });

    kfjsFun.setHeroLvCount(call)
}
