import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../../public/player';
import { ReqActive, ResActive } from "../../../shared/protocols/gongyu/mingdao/PtlActive";
import { getFinished } from './fun';

export default async function (call: ApiCall<ReqActive, ResActive>) {
    let id = Number(call.req.id);
    if (!G.gc.mingdao[id]) return call.errorCode(-1);
    if (call.conn.gud.shoucangping[id]) return call.errorCode(-2);

    let frontId = id - 1;
    if (G.gc.mingdao[frontId] && call.conn.gud.shoucangping[frontId] < G.gc.mingdao[frontId].level) return call.errorCode(-3);

    let finish = await getFinished(call.conn, G.gc.mingdao[id].condition.map(c => c[0]));

    for (let cond of G.gc.mingdao[id].condition) {
        if (finish[cond[0]] < cond[1]) return call.errorCode(-4);
    }

    let send = Object.assign({}, call.conn.gud.shoucangping);
    send[id] = 1;
    await PlayerFun.addAttr(call, { shoucangping: send });

    call.succ({});
}