import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ShiwuFun } from '../../public/shiwu';
import { ReqConcise, ResConcise } from "../../shared/protocols/shiwu/PtlConcise";
import { ShiwuShared } from '../../shared/public/shiwu';

export default async function (call: ApiCall<ReqConcise, ResConcise>) {

    let shiwu = await ShiwuFun.getShiwu(call, call.req._id);
    if (!shiwu) return call.error(globalThis.lng.shiwu_1);

    let fujiaNum = shiwu.colour - 1;
    let allOrange = shiwu.fujia.filter(f => f.colour >= 5).length == fujiaNum;
    if (allOrange && call.req.lockArr.length == fujiaNum) return call.error(globalThis.lng.shiwu_2);

    let need = allOrange && (fujiaNum - call.req.lockArr.length >= 0) ? ShiwuShared.getConciseNeed(fujiaNum - call.req.lockArr.length) : ShiwuShared.getConciseNeed(shiwu);
    await PlayerFun.checkNeedIsMeet(call, need);

    ShiwuFun.randomFujiaAll(shiwu, call.req.lockArr);
    await PlayerFun.cutNeed(call, need);
    await ShiwuFun.changeShiwuAttr(call, shiwu, { fujia: shiwu.fujia });

    call.succ({});
}