import { ApiCall } from "tsrpc";
import { JJCFun } from '../../public/jjc';
import { PlayerFun } from '../../public/player';
import { jjcChange } from '../../shared/protocols/jjc/PtlOpen';
import { ReqReceive, ResReceive } from "../../shared/protocols/jjc/PtlReceive";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqReceive, ResReceive>) {

    let data = await JJCFun.getData(call);
    let change: jjcChange = {};

    if (data.receivedArr.find(r => r == call.req.id)) return call.error(globalThis.lng.jjc_5);
    if (data.useFightNum < G.gc.jjc_tz[call.req.id].num) return call.error(globalThis.lng.jjc_6);

    await PlayerFun.sendPrize(call, G.gc.jjc_tz[call.req.id].prize);

    change.receivedArr = [].concat(data.receivedArr, call.req.id);

    JJCFun.changeData(call, change);
    HongDianChange.sendChangeKey(call.uid, ['jjchd'])
    call.succ({
        change: change,
        prize: G.gc.jjc_tz[call.req.id].prize
    });
}