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

async function start() {
    await initMongoDB()

    const hdid = [100, 101];
    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
        }
    };
    // 中间插入了2004 所以之前的任务id一次往后顺延
    const change = {2004: 2005, 2005: 2006, 2006: 2007};
    // 刷新活动配置
    await G.mongodb.collection("hdinfo").updateMany({hdid: {$in: hdid}}, {
        "$set": {'data.tasklist.2': task}
    })

    let datas: yangchengmubiao[] = [];
    // 刷新玩家领取记录
    datas = datas.concat(await G.mongodb.cEvent(`yangchengmubiao${hdid[0]}`).find(
        {type: `yangchengmubiao${hdid[0]}`}
    ).toArray());

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

    for (let i = 0; i < datas.length; i++) {
        let data = datas[i];
        let finished = data.finishid["2"].map((task) => {
            return change[task] ? change[task] : task
        });
        let maxtaskval = Math.max(...Object.values(data.taskval));

        let taskval = {};
        for (let taskid in task) {
            taskval[taskid] = maxtaskval;
        }

        await G.mongodb.collection("event").updateOne(
            {uid: data.uid, type: data.type}, {$set: {taskval: taskval, "finishid.2": finished}}
        )
        console.log(`修复玩家${data.uid}人才计划数据完成...`);
    }
}

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("逻辑执行完成...等待退出！！！");
});


