import { ApiCall } from "tsrpc";
import { ReqUpStar, ResUpStar } from "../../shared/protocols/weixiuchang/PtlUpStar";
import { weixiuchangType } from "../../shared/protocols/weixiuchang/type";
import { FunWeiXiuChang } from "../../public/weixiuchang";
import { HongDianChange } from "../hongdian/fun";

// 升星
export default async function (call: ApiCall<ReqUpStar, ResUpStar>) {
    // 获取参数
    let conf = JSON.parse(JSON.stringify(G.gc.weixiuchang_zz)); // 避免深拷贝造成变量污染
    // 获取数据
    let data:weixiuchangType = await FunWeiXiuChang.getFindOne(call);
    // 验证数据
    let needConf = conf[data.star].need;
    let bool = true;
    for(let i = 0; i < needConf.length; i++) {
        if(data.schedule[i] != needConf[i].n) {
            bool = false;
            break;
        }
    }
    if(bool) {
        // 升星&升阶
        data.star += 1;
        data.stage = conf[data.star].stage
        data.schedule = FunWeiXiuChang.defaultSchedule;
        await FunWeiXiuChang.updateOne(call, data);
        HongDianChange.sendChangeKey(call.uid, ['weixiuchang']);
        call.succ({star: data.star, schedule: data.schedule});
    } else {
        call.error(lng.weixiuchang_4);
    }
}