
import { scheduleJob, Job  } from 'node-schedule';
import { SystemConfigModel } from '../db/SystemConfig';
import PvpDefenseType,{ PvpDefenseModel } from '../db/PvpDefense';
import { PVP } from '../pubUtils/dicParam';
import { nowSeconds, getAge, getTimeFun } from '../pubUtils/timeUtil';
import { getPvpGkWarIds, getPvpRankRewards, getPvpHeroRewards, getResultMaxRank, getTodayGuildActivity } from '../pubUtils/data';
import { deepCopy, resResult, getRandSingleEelm } from '../pubUtils/util';
import { getLvByScore } from './pvpService';
import { getAllOnlineRoles, getAllServers, initSingleRank, delGuildActivityRank } from './redisService';
import { MAIL_TYPE, REDIS_KEY, ADULT_AGE, GUEST_MAX_TIME, ADDICTION_PREVENTION_CODE, GUILD_ACTIVITY_STATUS, GUILD_ACTIVITY_TYPE, TASK_TYPE, TIME_OUTPUT_TYPE, REFRESH_TIME, SEND_NAME } from '../consts';
import { RoleModel } from '../db/Role';
import { MailModel, MailType } from '../db/Mail';
import { pinus } from 'pinus';
import { indexOf } from 'underscore';
import { PvpSeasonResultModel } from '../db/PvpSeasonResult';
import { settleGuildWeekly } from './guildService';
import { STATUS } from '../consts/statusCode';
import { sendMailByContent } from './mailService';
import { reportOnline } from '../pubUtils/httpUtil';
import { UserModel } from '../db/User';
import { getGuildActivityByDic, setMedianCe, sendEndMsgToAll, autoDeclare, sendGuildActivityStatus } from './guildActivityService';
import { sendUngotDividendJob, startGuildAuction, startWorldAuction, stopAuction } from './auctionService';
import { DicGuildActivity } from '../pubUtils/dictionary/DicGuildActivity';
import { GuildModel } from '../db/Guild';
import { dispatch } from '../util/dispatcher';
import { Rank } from './rankService';
import { checkTask } from './taskService';
import { everydayRefresh } from './connectorService';

const PER_SECOND = 1 * 1000;
const PER_DAY = 24 * 60 * 60;
const SETTLE_DIFF = 29 * 60;
const pageNum = 500;
const PER_MINUTE = 1 * 60;
var seasonJobId: Job;
var warJobId: Job;
var seasonEndTimeJobId: Job;
let guildWeeklyJobId: Job;
let guildActSecondsJobId: Job; // 军团活动开启后每10(or 1)秒循环的定时任务，到结束活动清除
let guildActEndJobId: Job; // 军团活动开启后每10(or 1)秒循环的定时任务，到结束活动清除
/**
 * 服务器启动即开启定时任务，结算时常是23-24点，实际结算的时间点是23：31分钟
 */
export async function init() {
    let seasonEndTime = 0;
    let systemConfig = await SystemConfigModel.findSystemConfig();//系统全局参数表
    if (!systemConfig) {
        console.log('create season seasonNum = '+ systemConfig?.seasonNum);
        let warIds = getPvpGkWarIds();
        let warId = warIds[0];
        seasonEndTime = <number>getTimeFun().getAfterDay(PVP.PVP_SEASON_DAYS);
        systemConfig = await SystemConfigModel.createSystemConfig( seasonEndTime, warId );
    } else {
        seasonEndTime = systemConfig.seasonEndTime;
        if (systemConfig.seasonEndTime - SETTLE_DIFF <= nowSeconds()) {//服务器启动时，检查当前时间是否大于实际结算的时间，若大于则重新开启定时任务，若小于则按照结束时间开启定时任务
            seasonEndTime = <number>getTimeFun().getAfterDay(PVP.PVP_SEASON_DAYS);
            console.log('update season seasonNum = '+ systemConfig.seasonNum);
            systemConfig.seasonNum++;//赛季值增加1
            await SystemConfigModel.updateSystemConfig({ seasonEndTime, seasonNum: systemConfig.seasonNum, oldSeasonEndTime: systemConfig.seasonEndTime});
            await setPvpSeasonResult({name: 'simpleJobExample' + systemConfig.seasonNum, notSetNext: true, notPush: true });
        } 
    }
    let settleTime = (seasonEndTime - SETTLE_DIFF)* PER_SECOND;
    seasonJobId = scheduleJob('setPvpSeasonResult', settleTime, setPvpSeasonSchdule);//设置实际赛季结算时间

    seasonEndTimeJobId = scheduleJob('resetRank', seasonEndTime * PER_SECOND, resetPvpRanks);//由于24之后，才展示结算之后的信息，为保排行榜信息一致性，设置实际重置排行榜的时间为12点
    warJobId = scheduleJob("0 0 0 * * 1", resetPvpWarId);//每周1零点重置地图
    await resetPvpRanks();//服务器重启，重置排行榜的信息

    // 周功勋结算任务
    guildWeeklyJobId = scheduleJob('settleGuildWeekly', '0 0 0 * * 1', settleGuildWeekly);

    // 每5分钟汇报在线玩家在线情况
    scheduleJob('reportOnline', '0 0/5 * * * *', reportOnlineSchedule);

    // 军团活动排行榜
    guildActivitySchedule();

    // 拍卖行刷新：拍卖阶段刷新，分红发放
    auctionSchedule();

    // 每天5点推送刷新时间消息
    // 顺便每天0点计算前一天活跃玩家中位数战力
    scheduleJob('everyDayRefresh', `0 0 ${REFRESH_TIME} * * ?`, everydayRefresh);
}

function setPvpSeasonSchdule() {
    setPvpSeasonResult();
}

/**
 * pvp定时任务赛季结算
 * @param obj 
 */
export async function setPvpSeasonResult(obj?:{ name:string, notSetNext?: boolean, notPush?: boolean }) {
    console.log('exce setPvpSeasonResult'+ obj?.name);
    let { seasonNum, seasonEndTime, oldSeasonEndTime } = await setNextPvpTime(obj?.notSetNext);//设置下个结算任务
    let resultMaxRank = getResultMaxRank();//根据排行榜的奖励表，获得最大排名挡位的最小值，其余不在结算中结算的玩家按照最大排名挡位在登录或进入pvp时结算
    let maxPage = (resultMaxRank.min + 1000) / pageNum; //保底结算玩家数量
    let lastPageNum = resultMaxRank.min % pageNum;
    for (let page = 0; page < maxPage + 1; page++) {
        let pvpDefenses = await PvpDefenseModel.getPvpDef(pageNum, page == maxPage?lastPageNum:page);
        for (let pvpDefense of pvpDefenses) {
            if (pvpDefense.seasonNum !== seasonNum) {
                await setPvpDefResultOnTime(pvpDefense, seasonNum, oldSeasonEndTime, obj?.notPush);
            }
        }
    }
    return { seasonNum, seasonEndTime, oldSeasonEndTime};
}
/**
 * pvp定时任务结算获得添加邮件信息
 * @param pvpDefense 
 * @param seasonNum 
 * @param oldSeasonEndTime 
 */
export async function setPvpDefResultOnTime(pvpDefense: PvpDefenseType, seasonNum: number, oldSeasonEndTime: number, notPush?: boolean) {
    //检查并返回排名结算以及武将功勋结算
    let { score, pLv, heroScores, challengeCnt, challengeRefTime, rankGoods, heroGoods, rankLv } = await checkResult(pvpDefense, seasonNum, oldSeasonEndTime);
    pvpDefense = await PvpDefenseModel.updateInfo(pvpDefense.roleId, {score, pLv, heroScores, seasonNum, challengeCnt, challengeRefTime});
    //下发邮件
    if (!!rankGoods.length) //排名奖励
        await sendMailByContent(MAIL_TYPE.PVP_RANK_REWARD, pvpDefense.roleId, {
            params: [JSON.stringify(seasonNum), (rankLv>1000?'999+':JSON.stringify(rankLv))],
            goods: rankGoods, endTime: oldSeasonEndTime, notPush
        });
    if (!!heroGoods.length) //武将功勋奖励
        await sendMailByContent(MAIL_TYPE.PVP_RESULT, pvpDefense.roleId, {
            params: [JSON.stringify(seasonNum)],
            goods: heroGoods, endTime: oldSeasonEndTime, notPush
        });
    return pvpDefense;
}
/**
 * 检查并返回排名结算以及武将功勋结算
 * @param pvpDefense 
 * @param seasonNum 
 * @param oldSeasonEndTime 
 * @param rankLv 
 */
export async function checkResult(pvpDefense: PvpDefenseType, seasonNum: number, oldSeasonEndTime:number, rankLv?:number) {
    let pvpRankRewards = getPvpRankRewards();
    let pvpHeroRewards = getPvpHeroRewards();
    if (!rankLv) {
        let r = new Rank(REDIS_KEY.PVP_RANK, {});
        rankLv = await r.getMyRank({ roleId: pvpDefense.roleId });// 获得排行榜排名
    }
    let oldPLv = getLvByScore(pvpDefense.heroScores);//结算前玩家的pvp等级
    let { challengeCnt, challengeRefTime } = pvpDefense;
    let pvpRankReward;
    if (!!rankLv) {
        pvpRankReward = getScore(pvpRankRewards, rankLv);//获得排名挡位信息
    } else {
        pvpRankReward = getResultMaxRank();//最大排名等级挡位信息
    }
    let rankGoods = [];
    if(pvpRankReward) {
        rankGoods = pvpRankReward.reward;//排名奖励
    }
    let heroGoods = [];
    let score = 0;
    let oldHeroScores = deepCopy(pvpDefense.heroScores)
    for (let i = 0; i < pvpDefense.heroScores.length; i++) {
        let heroScore = pvpDefense.heroScores[i];
        let pvpHeroReward = getScore(pvpHeroRewards, heroScore.score);//获得武将功勋奖励
        if (pvpHeroReward) {
            heroScore.score = pvpHeroReward.heroscore;
            if (!!pvpHeroReward.reward[0]) {
                heroGoods.push({
                    hid: heroScore.hid,
                    id: pvpHeroReward.reward[0].id,
                    count: pvpHeroReward.reward[0].count,
                });
            }
        }
        score += heroScore.score; 
    }
    let pLv = getLvByScore(pvpDefense.heroScores);
    //pvp锁定的信息存入赛季结算表中
    await PvpSeasonResultModel.updatePvpSeasonResult(pvpDefense.roleId, { 
        oldSeasonData:{refOppCnt: pvpDefense.refOppCnt, rankLv, score: pvpDefense.score, pLv: oldPLv, heroScores: oldHeroScores, 
            seasonNum: pvpDefense.seasonNum, challengeCnt, challengeRefTime, seasonEndTime: oldSeasonEndTime
        }, heroGoods, rankGoods, show: true });//结算修改玩家pvp信息
    // 更新任务
    await checkTask(pvpDefense.roleId, null, null, TASK_TYPE.PVP_HERO_SCORE, 0, false, { heroScores: pvpDefense.heroScores });
    await checkTask(pvpDefense.roleId, null, null, TASK_TYPE.PVP_RANK, 1, false, { rankLv });
    return { rankLv, score, pLv, heroScores: pvpDefense.heroScores, seasonNum, challengeCnt:PVP.PVP_CHALLENGE_COUNTS, challengeRefTime:0, oldSeasonEndTime, heroGoods:heroGoods.map(({id, count})=>{
        return {id, count};
    }), rankGoods};
}
/**
 * 个人pvp结算，结算中未结算的，都按照最大排名挡位结算
 * @param pvpDefense 
 * @param seasonNum 
 * @param oldSeasonEndTime 
 */
export async function setPvpDefResult(pvpDefense: PvpDefenseType, seasonNum: number, oldSeasonEndTime:number) {
    let role = await RoleModel.findByRoleId(pvpDefense.roleId);
    if (!role) {
        return;
    }
    let resultMaxRank = getResultMaxRank();
    let rankLv = resultMaxRank.min;   //最大排名挡位结算
    let {score, pLv, heroScores, challengeCnt, challengeRefTime, rankGoods, heroGoods } = await checkResult(pvpDefense, seasonNum, oldSeasonEndTime, rankLv);
    pvpDefense = await PvpDefenseModel.updateInfoAndInclude(pvpDefense.roleId, {score, pLv, heroScores, seasonNum, challengeCnt, challengeRefTime});
    let { roleId } = role;
    let r = new Rank(REDIS_KEY.PVP_RANK, {});
    r.setRankWithRoleInfo(pvpDefense.roleId, pvpDefense.score, pvpDefense.updatedAt.getTime(), role, true);
    //下发邮件
    if (!!rankGoods.length) 
        await sendMailByContent(MAIL_TYPE.PVP_RANK_REWARD, roleId, { params: [JSON.stringify(seasonNum), '999+'], goods: rankGoods, endTime: oldSeasonEndTime });
    if (!!heroGoods.length) 
        await sendMailByContent(MAIL_TYPE.PVP_RESULT, roleId, { params: [JSON.stringify(seasonNum)], goods: heroGoods, endTime: oldSeasonEndTime });
    return pvpDefense;
}
/**
 * 每周重置地图
 */
export async function resetPvpWarId() {
    // console.log('resetPvpWarId');
    let systemConfig = await SystemConfigModel.findSystemConfig();
    let warIds: number[] = deepCopy(getPvpGkWarIds());
    let index = indexOf(warIds, systemConfig.warId);
    if (index != -1) {
        warIds.splice(index, 1);
    }
    let warId = getRandSingleEelm(warIds);
    return await SystemConfigModel.updateSystemConfig({ warId });
}
/**
 * 重置下个定时结算
 * @param notSetNext 
 */
async function setNextPvpTime(notSetNext: boolean) {
    let { seasonEndTime, seasonNum } = await SystemConfigModel.findSystemConfig();
    if (!!notSetNext) {
        return { seasonEndTime, seasonNum , oldSeasonEndTime: 0};
    }
    let oldSeasonEndTime = seasonEndTime;
    seasonEndTime = <number>getTimeFun().getAfterDay(PVP.PVP_SEASON_DAYS + 1);
    await SystemConfigModel.updateSeason(seasonEndTime, oldSeasonEndTime);
    let settleTime = (seasonEndTime - SETTLE_DIFF)* PER_SECOND;
    seasonJobId = scheduleJob('setPvpSeasonResult', settleTime, setPvpSeasonSchdule);
    seasonEndTimeJobId = scheduleJob('resetRank', seasonEndTime* PER_SECOND, resetPvpRanks);
    return { seasonEndTime, seasonNum: seasonNum + 1, oldSeasonEndTime };
}

function getScore(arr, score) {
    for (let item of arr) {
        if ((item.max >= score||item.max == -1) && score >= item.min) {
            return item;
        }
    }
}
/**
 * debug接口
 * @param hour 
 */
export async function resetPvpSeasonTime(hour: number) {
    if (!!seasonJobId) {
        seasonJobId.cancel();
    }
    let seasonEndTime = 0;
    let seasonNum = 1;
    if (!!seasonEndTimeJobId) {
        seasonEndTimeJobId.cancel();
    }
    let systemConfig = await SystemConfigModel.findSystemConfig();
    if (!systemConfig) {
        let warIds = getPvpGkWarIds();
        let warId = warIds[0];
        seasonEndTime = <number>getTimeFun().getTimeWithHour(REFRESH_TIME - hour);
        systemConfig = await SystemConfigModel.createSystemConfig( seasonEndTime, warId );
    } else {
        seasonEndTime = <number>getTimeFun().getTimeWithHour(REFRESH_TIME - hour);
        seasonNum = systemConfig.seasonNum + 1;
        await SystemConfigModel.updateSystemConfig({ seasonEndTime, seasonNum, oldSeasonEndTime: 0});
        await setPvpSeasonResult({name: 'simpleJobExample' + systemConfig.seasonNum, notSetNext: true });
        await resetPvpRanks();
    }
    let settleTime = (seasonEndTime - SETTLE_DIFF)* PER_SECOND;
    console.log('settleTime = ' + settleTime)
    seasonJobId = scheduleJob('setPvpSeasonResult', settleTime, setPvpSeasonSchdule);
    return { seasonEndTime, seasonNum };
}


async function resetPvpRanks() {
    await initSingleRank(REDIS_KEY.PVP_RANK);
}

export async function reportOnlineSchedule() {
    let allRoles = await getAllOnlineRoles();
    console.log('reportOnlineSchedule all roles count: ', allRoles.length)
    for(let { roleId, userCode, sid, pkgName } of allRoles) {
        let result = reportOneOnline(roleId, userCode, sid, pkgName);
        if(!result) continue;
    }
}

export async function reportOneOnline(roleId: string, userCode: string, sid: string, pkgName: string) {

    let result = await reportOnline(userCode, pkgName); // 连接sdk
    if(!result || result.code == -1) return false;

    let user = await UserModel.findUserByUserCode(userCode);
    if(!user) return false;
    let { reportTime = new Date(), lastLoginTime = new Date(), guestTime, isGuest, hasAuthenticated, birthday } = user;
    
    let age = getAge(birthday);
    let isAdult = age >= ADULT_AGE;

    if(isGuest || !hasAuthenticated) {
        let lastTime = lastLoginTime > reportTime? lastLoginTime.getTime(): reportTime.getTime();
        let guestTimeInc = Math.floor((Date.now() - lastTime)/1000);
        user = await UserModel.updatePlayTime(userCode, guestTimeInc, result.total); // 记录时间

        guestTime = user.guestTime;

        if ( guestTime > GUEST_MAX_TIME ) {
            pinus.app.channelService.pushMessageByUids('onPlayTime', resResult(STATUS.SUCCESS, {
                isGuest,
                guestTime,                   // 游客已体验时间
                hasAuthenticated,     // 是否进行过实名认证
                isAdult,                                // 是否已成年
                todayPlayTime: result.total,            // 今天已游戏时长
                type: ADDICTION_PREVENTION_CODE.GUEST,
            }  ), [{uid: roleId, sid: sid}]);
        }
    } else {
        if(result.code != ADDICTION_PREVENTION_CODE.SUCCESS && result.age != -1) { // 未成年人防沉迷
            user = await UserModel.updatePlayTime(userCode, 0, result.total, result.code); // 记录时间

            pinus.app.channelService.pushMessageByUids('onPlayTime', resResult(STATUS.SUCCESS, {
                isGuest,
                guestTime,                   // 游客已体验时间
                hasAuthenticated,            // 是否进行过实名认证
                isAdult,                                // 是否已成年
                todayPlayTime: result.total,            // 今天已游戏时长
                type: result.code
            }  ), [{uid: roleId, sid: sid}]);
        }
    }
}

/**
 * 军团活动，每晚8点开启
 */
async function guildActivitySchedule() {
    /***********guildActivitySchedule***********/

    let dicGuildActivity = getTodayGuildActivity(); 
    await delGuildActivityRank(dicGuildActivity.id);

    scheduleJob('guildActivityStart', `${dicGuildActivity.startSeconds} ${dicGuildActivity.startMinute} ${dicGuildActivity.startTime} * * ?`, guildActivityStartSchedule);

    let statusResult = getGuildActivityByDic(dicGuildActivity);
    if(!statusResult) return;

    if(statusResult.status == GUILD_ACTIVITY_STATUS.START) {
        // 断掉活动续期
        if(!guildActSecondsJobId) {
            if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.GATE_ACTIVITY) {
                guildActSecondsJobId = scheduleJob('guildActivitySeconds', `*/10 * * * * *`, gateActivitySeconds);
            } else if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.CITY_ACTIVITY) {
                guildActSecondsJobId = scheduleJob('guildActivitySeconds', `*/10 * * * * *`, cityActivitySeconds);
            } else if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.RACE_ACTIVITY) {
                guildActSecondsJobId = scheduleJob('guildActivitySeconds', `*/1 * * * * *`, raceActivitySeconds);
            }
        }

        if(!guildActEndJobId) {
            if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.GATE_ACTIVITY) {
                guildActEndJobId = scheduleJob('guildActivityEnd', statusResult.time, gateActivityEnd);
            } else if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.CITY_ACTIVITY) {
                guildActEndJobId = scheduleJob('guildActivityEnd', statusResult.time, cityActivityEnd);
            } else if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.RACE_ACTIVITY) {
                guildActEndJobId = scheduleJob('guildActivityEnd', statusResult.time, raceActivityEnd);
            }
        }
    }
}

// 军团晚间活动，每天8点开始
async function guildActivityStartSchedule() {
    await guildActivityStart();
}

export async function guildActivityStart(dicGuildActivity?: DicGuildActivity) {
    console.log('***********开始军团晚间活动****')
    if(!dicGuildActivity) dicGuildActivity = getTodayGuildActivity();
    if(!dicGuildActivity) return;

    if(guildActSecondsJobId||guildActEndJobId) {
        return false
    }

    let servers = await getAllServers(); // 玩家serverId列表
    if(dicGuildActivity.id == GUILD_ACTIVITY_TYPE.GATE_ACTIVITY) {
        guildActSecondsJobId = scheduleJob('guildActivitySeconds', '*/10 * * * * *', gateActivitySeconds);
        // 结束时间
        guildActEndJobId = scheduleJob('guildActivityEnd', Date.now() + dicGuildActivity.duringTime * 1000, gateActivityEnd);
    } else if (dicGuildActivity.id == GUILD_ACTIVITY_TYPE.CITY_ACTIVITY) {
        guildActSecondsJobId = scheduleJob('guildActivitySeconds', '*/10 * * * * *', cityActivitySeconds);
        // 结束时间
        guildActEndJobId = scheduleJob('guildActivityEnd', Date.now() + dicGuildActivity.duringTime * 1000, cityActivityEnd);
    } else if (dicGuildActivity.id == GUILD_ACTIVITY_TYPE.RACE_ACTIVITY) {

        // 开始活动
        let guildServers = pinus.app.getServersByType('guild');
        for(let serverId of servers) {
            let sid = dispatch(serverId.toString(), guildServers);
            await pinus.app.rpc.guild.guildActivityRemote.raceActivityStart.toServer(sid.id, serverId);
        }

        guildActSecondsJobId = scheduleJob('guildActivitySeconds', '*/1 * * * * *', raceActivitySeconds);
        // 结束时间
        guildActEndJobId = scheduleJob('guildActivityEnd', Date.now() + dicGuildActivity.duringTime * 1000, raceActivityEnd);
    }

    for(let serverId of servers) {
        await sendGuildActivityStatus(serverId);
    }
    return true;
}

// 蛮夷入侵
// 结束军团活动
export async function gateActivityEnd() {

    console.log('*****gateActivityEnd');
    await sendEndMsgToAll();
    let servers = pinus.app.getServersByType('guild');
    for(let { id } of servers) {
        await pinus.app.rpc.guild.guildActivityRemote.guildActivityEnd.toServer(id, GUILD_ACTIVITY_TYPE.GATE_ACTIVITY);
    }

    if(guildActSecondsJobId) {
        guildActSecondsJobId.cancel();
        guildActSecondsJobId = undefined;
    }
    if(guildActEndJobId) {
        guildActEndJobId.cancel();
        guildActEndJobId = undefined;
    }
}

// 每10秒下发一次的任务
export async function gateActivitySeconds() {
    console.log('*****gateActivitySeconds')
    let servers = pinus.app.getServersByType('guild');
    for(let { id } of servers) {
        await pinus.app.rpc.guild.guildActivityRemote.sendRankToGuilds.toServer(id, GUILD_ACTIVITY_TYPE.GATE_ACTIVITY);
    }
}

// 诸侯混战
// 结束军团活动
export async function cityActivityEnd() {
    console.log('*****cityActivityEnd');
    await sendEndMsgToAll();
    let servers = pinus.app.getServersByType('guild');
    for(let { id } of servers) {
        await pinus.app.rpc.guild.guildActivityRemote.guildActivityEnd.toServer(id, GUILD_ACTIVITY_TYPE.CITY_ACTIVITY);
    }

    // 发完之后再做下周自动宣战
    let serverlists = await getAllServers();
    for(let serverId of serverlists) {
        await autoDeclare(serverId);
    }
    if(guildActSecondsJobId) {
        guildActSecondsJobId.cancel();
        guildActSecondsJobId = undefined;
    }
    if(guildActEndJobId) {
        guildActEndJobId.cancel();
        guildActEndJobId = undefined;
    }
}

// 每10秒下发一次的任务
export async function cityActivitySeconds() {
    console.log('*****cityActivitySeconds')
    let servers = pinus.app.getServersByType('guild');
    for(let { id } of servers) {
        await pinus.app.rpc.guild.guildActivityRemote.sendRankToGuilds.toServer(id, GUILD_ACTIVITY_TYPE.CITY_ACTIVITY);
    }
}

// 粮草先行
// 结束军团活动
export async function raceActivityEnd() {
    console.log('*****raceActivityEnd');
    await sendEndMsgToAll();
    let servers = pinus.app.getServersByType('guild');
    for(let { id } of servers) {
        await pinus.app.rpc.guild.guildActivityRemote.guildActivityEnd.toServer(id, GUILD_ACTIVITY_TYPE.RACE_ACTIVITY);
    }

    if(guildActSecondsJobId) {
        guildActSecondsJobId.cancel();
        guildActSecondsJobId = undefined;
    }
    if(guildActEndJobId) {
        guildActEndJobId.cancel();
        guildActEndJobId = undefined;
    }
}

// 每10秒下发一次的任务
export async function raceActivitySeconds() {
    console.log('*****raveActivitySeconds')
    let servers = await getAllServers(); // 玩家serverId列表
    let guildServers = pinus.app.getServersByType('guild');
    for(let serverId of servers) {
        let sid = dispatch(serverId.toString(), guildServers);
        await pinus.app.rpc.guild.guildActivityRemote.calWoodenHorseAndSend.toServer(sid.id, serverId);
    }
}

function auctionSchedule() {
    scheduleJob('startGuildAuction', '0 0 20 20 * ?', startGuildAuction);
    scheduleJob('startWorldAuction', '0 0 20 30 * ?', startWorldAuction);
    scheduleJob('stopAuction', '0 0 22 00 * ?', stopAuction);
    scheduleJob('sendUngotDividendJob', '0 0 5 00 * ?', sendUngotDividendJob);
}
