import { ApiCall } from "tsrpc";
import { PlayerFun } from '../../public/player';
import { ReqRec, ResRec } from "../../shared/protocols/hero/PtlRec";

export default async function (call: ApiCall<ReqRec, ResRec>) {
    if (!call.conn.lshd.hero[call.req.heroId]) return call.errorCode(-1);

    let rec = (await G.mongodb.cPlayerInfo('gbzj').findOne({ uid: call.uid, type: 'gbzj' }))?.rec || {};
    if (rec[call.req.heroId]) return call.errorCode(-2);

    G.mongodb.cPlayerInfo('gbzj').updateOne(
        { uid: call.uid, type: 'gbzj' },
        { $set: G.mongodb.createTreeObj({ key: `rec.${call.req.heroId}`, val: true }) },
        { upsert: true }
    );

    await PlayerFun.sendPrize(call, G.gc.hero[call.req.heroId].prize);

    call.succ({
        prize: G.gc.hero[call.req.heroId].prize
    });
}