
import { query } from '../sql/query'; // 确保路径是正确的
const schedule = require('node-schedule');

async function asyncOpenServer() {
    const sql = 'SELECT * FROM game_server where status in (0,3) and open_time > 0';
    return new Promise((resolve, reject) => {
        query(sql, (error, results, fields) => {
            if (error) throw error;
            // 处理查询结果
            console.log(results);
            (results as any[]).forEach(row => {
                const timestamp = new Date().getTime();
                if (timestamp >= row.open_time) {
                    const updateSql = `update  game_server set status = 1  where id = ${row.id} `;
                    query(updateSql, (error, res, fields) => {
                        resolve(results)
                    })
                }
            })
        });
    });
}

export default class Task {
    openServer() {
        const rule = new schedule.RecurrenceRule();
        rule.second = [0];
        const job = schedule.scheduleJob(rule, async function () {
            try {
                const result = await asyncOpenServer(); // 使用await等待异步任务完成
                console.log(result);
            } catch (error) {
                console.error('区服任务执行出错:', error);
            }
        });
    }
}

