import {ctor} from "../global";
import {initMongoDB} from "../setMongodb";

async function start() {
    await initMongoDB()

    let datas = await G.mongodb.collection("event").find(
        {type: {$in: ["yangchengmubiao100", "yangchengmubiao101"]}}
    ).toArray();

    for (let i = 0; i < datas.length; i++) {
        let add = 0;
        let data = datas[i];
        if (!Array.isArray(data.taskval)) continue;
        let taskval: { [taskid: string]: number } = {};
        for (let i = 0; i < data.taskval.length; i++) {
            if (data.taskval[i] === null) continue;

            if (typeof data.taskval[i] == "number") {
                add = data.taskval[i];
            } else {
                taskval = Object.assign(taskval, data.taskval[i]);
            }
        }
        for (let taskid in taskval) {
            taskval[taskid] = (taskval[taskid] || 0) + add;
        }
        await G.mongodb.collection("event").updateOne(
            {uid: data.uid, type: data.type}, {$set: {taskval: taskval}}
        )
        console.log(`修复玩家${data.uid}人才计划数据完成...`);
    }
}

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


