import {ctor} from "../global";
import {initMongoDB} from "../setMongodb";
import {yangchengmubiao} from "../shared/protocols/event/yangchengmubiao/PtlOpen";
import {number} from "mathjs";
import {PublicShared} from "../shared/public/public";

async function start() {
    await initMongoDB()

    const task = {
        '2001': {
            'prize': [{'a': 'item', 't': '4', 'n': 5}, {'a': 'item', 't': '626', 'n': 10}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 50,
            'cond': [],
            'stype': 118
        },
        '2002': {
            'prize': [{'a': 'item', 't': '4', 'n': 5}, {'a': 'item', 't': '626', 'n': 10}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 100,
            'cond': [],
            'stype': 118
        },
        '2003': {
            'prize': [{'a': 'item', 't': '4', 'n': 10}, {'a': 'item', 't': '626', 'n': 20}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 200,
            'cond': [],
            'stype': 118
        },
        '2004': {
            'prize': [{'a': 'item', 't': '4', 'n': 10}, {'a': 'item', 't': '600', 'n': 20}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 250,
            'cond': [],
            'stype': 118
        },
        '2005': {
            'prize': [{'a': 'item', 't': '4', 'n': 10}, {'a': 'item', 't': '600', 'n': 20}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 300,
            'cond': [],
            'stype': 118
        },
        '2006': {
            'prize': [{'a': 'item', 't': '4', 'n': 10}, {'a': 'item', 't': '619', 'n': 1}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 400,
            'cond': [],
            'stype': 118
        },
        '2007': {
            'prize': [{'a': 'item', 't': '4', 'n': 10}, {'a': 'item', 't': '619', 'n': 1}],
            'tujing': '',
            'title': 'intr_yczm_day_des_2',
            'type': 2,
            'pval': 500,
            'cond': [],
            'stype': 118
        }
    };
    let hdid = [100, 101];
    let openday = PublicShared.getOpenServerDay();
    let openhdlist = await G.mongodb.collection("hdinfo").find(
        {hdid: {$in: hdid}, stime: {$lte: openday}, etime: {$gte: openday}}
    ).toArray();

    // 没有开启的活动 返回
    if (openhdlist.length <= 0) {
        return;
    } else {
        hdid = openhdlist.map(hd => hd.hdid);
    }

    let datas: yangchengmubiao[];
    datas = await G.mongodb.cEvent(`yangchengmubiao${hdid[0]}`).find(
        {type: `yangchengmubiao${hdid[0]}`}
    ).toArray();

    for (let i = 0; i < datas.length; i++) {
        let data = datas[i];
        let finishid = new Set(data.finishid["2"]);
        // 查找任务未完成 但设置了领奖标识
        for (let taskid in task) {
            let con = task[taskid];
            let taskval = data.taskval || {};
            if (finishid.has(number(taskid)) && (taskval[taskid] || 0) < con.pval) {
                finishid.delete(number(taskid));
            }
        }
        // 没有删除的
        if (finishid.size == data.finishid["2"].length) {
            continue
        }
        await G.mongodb.collection("event").updateOne(
            {"uid": data.uid, "type": data.type}, {$set: {"finishid.2": [...finishid]}}
        )
        console.log(`修复玩家${data.uid}人才计划${data.type}数据完成...`);
    }
}

ctor();
start().then(() => {
    let s = 0;
    setInterval(() => {
        s += 1;
        console.log(new Date().format("MM-dd hh:mm:ss"));
        if (s >= 3) process.exit(1);
    }, 1000)
    console.log("逻辑执行完成...等待退出！！！");
});


