import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ShiwuFun } from '../../public/shiwu';
import { ReqExtract, ResExtract } from "../../shared/protocols/shiwu/PtlExtract";
import { prizeType } from '../../shared/protocols/type';
import { PublicShared } from '../../shared/public/public';

export default async function (call: ApiCall<ReqExtract, ResExtract>) {
    if (call.req._ids.length < 1) return call.error('', { code: -1 });

    let items = await ShiwuFun.getShiwu(call, call.req._ids);
    if (items.length != call.req._ids.length) return call.error('', { code: -2 });
    if (items.filter(i => !!i.wearId).length > 0) return call.error('', { code: -3 });

    let prize: prizeType[] = [];
    items.forEach(i => {
        let val = Math.floor((140 - G.gc.shiwu[i.shiwuId].lv) * G.gc.shiwucom.base.tilianfactor[i.colour] * 100);
        let round = PublicShared.randomNum(1, 100) <= val;

        let _next = G.gc.shiwu[i.shiwuId].next
        if (round && _next != 0) {
            if (i.colour == 5) {
                prize.push({ a: 'shiwu', t: _next, n: 1, colour: i.colour });
            } else {
                prize.push({ a: 'shiwu', t: _next, n: 1, colour: i.colour + 1 });
            }
        } else {
            prize.push(...G.gc.shiwucom.base.tilian[i.colour]);
        }
    });

    await PlayerFun.cutShiwu(call, call.req._ids);
    let send = await PlayerFun.sendPrize(call, prize);

    call.succ({
        prize: send,
        newIds: []
    });
}