import { ApiCall } from "tsrpc";
import { HeroFun } from '../../public/hero';
import { ReqSetDef, ResSetDef } from "../../shared/protocols/dixialeitai/PtlSetDef";

export default async function (call: ApiCall<ReqSetDef, ResSetDef>) {

    let arr = call.req;
    if (arr.isDuplication()) return call.error(globalThis.lng.dixialeitai_5);
    if (arr.length < G.gc.dxlt_com.heroNum[0] || arr.length > G.gc.dxlt_com.heroNum[1]) return call.error(globalThis.lng.dixialeitai_5);

    let heros = await HeroFun.getHeros(call, arr);
    if (heros.filter(hero => hero != null).length < arr.length) return call.error(globalThis.lng.dixialeitai_5);

    await G.mongodb.collection('dxlt').updateOne(
        {
            uid: call.uid
        },
        {
            $set: {
                resetTime: G.time,
                heros: (await call.conn.getDefaultFightData(Object.fromEntries(arr.map((_id, pos) => [pos + 1, _id])))).roles
            }
        },
        {
            upsert: true
        }
    );

    call.succ({});
}