import { ApiCall } from "tsrpc";
import { ReqGame, ResGame } from "../../../shared/protocols/event/christmas/PtlGame";
import { HongDianChange } from "../../hongdian/fun";
import { Christmasfun } from "./fun";
import {PlayerFun} from "../../../public/player";


export default async function (call: ApiCall<ReqGame, ResGame>) {
    let initCon = await Christmasfun.getCon(call)
    let need = initCon[call.req.hdid].data.gameneed;
    let freenum = initCon[call.req.hdid].data.gamefree
    let addval = initCon[call.req.hdid].data.game[call.req.index];

    let _mydata = await Christmasfun.getMyData(call, call.req.hdid)
    // 判断是否有免费配置
    if (_mydata.gamenum >= freenum) {
        // 判断消耗是否满足
        await PlayerFun.checkNeedIsMeet(call, need);
        // 扣除消耗
        await PlayerFun.cutNeed(call, need);
    }

    let _setData = {}
    _mydata["val"] += addval
    _mydata["gamenum"] += 1
    _setData["val"] = _mydata.val
    _setData["gamenum"] = _mydata["gamenum"]
    await Christmasfun.setMyData(call.uid, call.req.hdid, { $set: _setData})
    // 监听任务
    G.emit("Class_task_154", 'Class_task_154', call, 1, 0);
    let changedata = { mydata: _mydata}
    // 推送红点
    HongDianChange.sendChangeKey(call.uid, ['huodonghd']);
    call.succ(changedata);
}