import { ApiCall } from "tsrpc";
import { FightFun } from '../public/fight';
import { joinFightData } from '../shared/fightControl/fightType';
import { ReqFightTest, ResFightTest } from "../shared/protocols/PtlFightTest";
import { HeroShared } from '../shared/public/hero';
import { PublicShared } from '../shared/public/public';

export default async function (call: ApiCall<ReqFightTest, ResFightTest>) {

    let joins: joinFightData[] = [];

    let pd = await FightFun.getPlayerFightData(call.uid);

    call.req.roles.forEach((d, i) => {
        let player = call.req.player?.[i] || { ...pd, fightHeros: d.map(d => d.hero.heroId.toString()) };

        let a: any[] = d.map((hd, index) => {

            let buff = HeroShared.getHeroBasicAttr({ ...hd.hero, lv: hd.hero.lv || 1, jieji: hd.hero.jieji || 0 }, player, index + 1);
            let hero = {
                attr: buff,
                ...hd.hero
            };

            PublicShared.mergeProperty(hero.attr, hd.attr);
            hero.attr.maxHp = hero.attr.hp;
            return hero;
        });

        joins.push({
            player: JSON.parse(JSON.stringify(player)),
            roles: Object.fromEntries(a.map((h, p) => [p + 1, h]))
        });
    });

    let res = FightFun.fight(joins);

    call.succ({
        result: res
    });
}