import { ApiCall } from "tsrpc";
import { HuoDongFun } from "../../../public/huodongfun";
import { ReqOpen, ResOpen } from "../../../shared/protocols/event/diaoluoduihuan/PtlOpen";
import { prizeType } from '../../../shared/protocols/type';
import { PublicShared } from '../../../shared/public/public';

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let _hdinfo = await HuoDongFun.getHdidInfo(call, call.req.hdid)
    if (!_hdinfo || Object.keys(_hdinfo).length <= 0) {
        // 无此活动
        return call.error('', { code: -1, message: globalThis.lng.huodong_open_1 })
    }

    let db = await G.mongodb.cEvent(`diaoluoduihuan${call.req.hdid}`).findOne({ uid: call.uid, type: `diaoluoduihuan${call.req.hdid}` });


    call.succ({
        record: db?.record || {},
        hdinfo: _hdinfo
    });
}

/**
 * 兑换掉落活动添加掉落物品
 * 掉落活动 后端唯一识别标识 htype 2
 */
export async function event_dldh_addPrize(prize: prizeType[], call: ApiCall, time: number) {
    if (time > G.gc.tanxian_com.maxGuaJiTime) time = G.gc.tanxian_com.maxGuaJiTime;
    let _hdList = await HuoDongFun.gethdList(call, 2)
    for (let index = 0; index < _hdList.length; index++) {
        const element = _hdList[index];
        // 这里能有活动，就已经过了时间的筛选，无需这个时间的判断，取消了
        // if (G.time < PublicShared.getToDayZeroTime(call.conn.gud.cTime) + element.data.event.time) {

        let rand = PublicShared.randomDropAny(element.data.diaoluo);
        // @ts-ignore
        let [n, s, a] = [rand.n, rand.s, rand.a]
        if (n > 0) {
            let multiply = Math.floor(time / s);
            prize.push({ a: a, t: element.data.event.itemId, n: multiply * n });
        }
    }

}