import { log } from "console";
import { patchFun, patchInit } from "../patch";
import { TanXianFun } from "../public/tanxian";
import { PublicShared } from "../shared/public/public";

class Path {

    async fun1(a: any) {
        let hdid = 1703860546;  // 正式服活动id
        // let hdid = 1703917224;  // 测试服活动id
        let today_zero_time = PublicShared.getToDayZeroTime();

        // 查询今天登录的用户
        let today_login_users = await G.mongodb.collection("user").find(
            { loginTime: { $gte: today_zero_time } },
            { projection: { uid: 1 } }
        ).toArray();

        for (let i = 0; i < today_login_users.length; i++) {
            let uid = today_login_users[i].uid;

            let init: boolean = false;

            let hdata: any = await G.mongodb.cEvent(`yuandan${hdid}`).findOne({
                uid: uid, type: `yuandan${hdid}`
            });
            if (!hdata) {
                init = true;
                hdata = {
                    uid: uid,
                    gift: {},
                    qiandao: {},
                    gameNum: 0,
                    exchange: {},
                    taskfinish: [],
                    taskval: {
                        "1": 1,
                        "2": 0,
                        "3": 0,
                        "4": 0,
                        "5": 0,
                        "6": 0,
                        "7": 0,
                        "8": 0,
                        "9": 0
                    },
                    qiandaoTime: 1,
                    refreshTime: G.time,
                    type: `yuandan${hdid}`,
                }
            }


            let taskval = {};
            // taskid=1, stype=123  今日登录
            taskval["1"] = 1;

            // taskid=2, stype=158  今日参与抓娃娃次数
            taskval["2"] = hdata.gameNum;

            // taskid=3, stype=127  挑战 n 次清缴真主党
            let myData = await G.mongodb.cPlayerInfo('qjzzd').findOne({ uid: uid, type: 'qjzzd' });
            if (myData?.refreFightTime > PublicShared.getToDayZeroTime()) {
                taskval["3"] = myData?.useFightNum || 0;
            } else {
                taskval["3"] = 0;
            }

            // taskid=4, stype=155  领取每日任务最终宝箱
            let task = await G.mongodb.collection('task').findOne({ uid: uid, taskid: 25 });
            if (task && task.lasttime >= today_zero_time && task.finish == 1) {
                taskval["4"] = 1;
            } else {
                taskval["4"] = 0;
            }

            // taskid=5, stype=142  势力 配置xx 捐献进行 n 次捐献
            let jxdata = await G.mongodb.collection('gonghuiUser').findOne({ uid: uid });
            if (jxdata && jxdata.jx?.refreshTime > today_zero_time) {
                if (jxdata.jx?.record) {
                    taskval["5"] = jxdata.jx.record["2"] || 0
                }else{
                    taskval["5"] = 0
                }
            } else {
                taskval["5"] = 0;
            }

            // taskid=6, stype=156  每日钻石消耗
            taskval["6"] = 0;
            let uselogs = (await G.mongodb.collection('rmbuse').find(
                { uid: uid, cTime: { $gte: today_zero_time }, isAdd: false },
                { projection: { change: 1 } }
            ).toArray()).forEach(e => { taskval["6"] += -e.change; });

            // taskid=7, stype=122  进行 n 次快速探险
            taskval["7"] = 0;
            const data = await G.mongodb.collection('tanxian').findOne({ uid: uid });
            if (data?.resetTime > PublicShared.getToDayZeroTime()) {
                taskval["7"] = data.useFastGuaJiNum || 0
            }

            // taskid=8, stype=157  获得vip经验
            let orde_logs = await G.mongodb.collection('payOrderLog' as any).find(
                { uid: uid, ctime: { $gte: today_zero_time * 1000 } },
            ).toArray();

            taskval["8"] = 0;
            orde_logs.forEach(e => {
                let pay = G.gc.pay[e.payId];
                if (pay) taskval["8"] += pay.payExp[0].n;
            })

            // taskid=9, stype=157  获得vip经验
            taskval["9"] = 0;
            orde_logs.forEach(e => {
                let pay = G.gc.pay[e.payId];
                if (pay) taskval["9"] += pay.payExp[0].n;
            })

            if (!init) {
                await G.mongodb.cEvent(`yuandan${hdid}`).updateOne(
                    { uid: uid, type: `yuandan${hdid}` },
                    { $set: { taskval: taskval } }
                )
            } else {
                hdata.taskval = taskval;
                await G.mongodb.cEvent(`yuandan${hdid}`).updateOne({ uid: uid, type: `yuandan${hdid}` }, { $set: hdata }, { upsert: true })
            }

            console.log("玩家执行完成：", uid, taskval);
        }


        return "sucess!!!"
    }



    async run() {
        await this.fun1(1);
    }
}

async function main() {
    await patchInit()
    let patch = new Path();
    await patch.run();
    console.log("逻辑执行完成，等待退出");
    setTimeout(function () {
        console.log('结束程序');
        process.exit();
    }, 3000);
}
main();
