import { core, GLog, Timer } from "cgserver" import { CreateMatchInsInfo, EMatchState, GMatchInsSer, MatchInsModel } from "../../../../Common/Service/MatchInstanceService" import { EMatchLoopType, GMatchSer, MatchModel } from "../../../../Common/Service/MatchService" import { TexasServer } from "../TexasServer" import { TexasMatch } from "./TexasMatch" export class TexasMatchManager { protected _server:TexasServer=null protected _matches:{[ins_id:string]:TexasMatch}={} protected _dayTimer=new Timer() //是否已经处理过异常比赛了 protected _is_exception_match_did=false /** * 每天0点应该调用一次这个初始化,初始化到第二天的 * 注意事项,比赛最好不要开到0点,因为0点我们刷新创建当天的比赛 * @param server */ async init(server:TexasServer) { this._server=server let matchMs:MatchModel[] = await GMatchSer.gets() //上次意外关闭的比赛 await this._initExceptionMatchIns() //初始化 for(let i=0;i { this.init(this._server) },dt) } async _initExceptionMatchIns() { if(this._is_exception_match_did) { return } this._is_exception_match_did=true let matchins:MatchInsModel[] = await GMatchInsSer.gets({id:1,start_time:1,match_id:1},{"$or":[{"state":EMatchState.Playing},{"state":EMatchState.Waiting}],"start_time":{"$lt":Date.now()}}) if(!matchins) { return } for(let i=0;i { let cmii = new CreateMatchInsInfo() cmii.id=mm.id+"_"+ core.format(start_time,"YYYY_MM_DD") cmii.start_time=start_time cmii.match_id=mm.id return cmii } let start_time = 0 let now = Date.now() if(mm.looptype==EMatchLoopType.None) { if(mm.match_starttime>now) { start_time=mm.match_starttime let cmii = _createMatchInsInfo(start_time) cmiis.push(cmii) } } else if(mm.looptype==EMatchLoopType.Day) { let now_dt = new Date() let date = new Date(mm.match_starttime) let start = new Date(now_dt.getFullYear(),now_dt.getMonth(),now_dt.getDate(),date.getHours(),date.getMinutes(),date.getSeconds()) let start_t = start.getTime() if(start_t>now) { start_time=start_t let cmii = _createMatchInsInfo(start_time) cmiis.push(cmii) } start_t=start_t+24*60*60*1000 if(start_t>now) { start_time=start_t let cmii = _createMatchInsInfo(start_time) cmiis.push(cmii) } return cmiis } else if(mm.looptype==EMatchLoopType.Week) { let now_dt = new Date() let date = new Date(mm.match_starttime) if(date.getDay()==now_dt.getDay()) { let start = new Date(now_dt.getFullYear(),now_dt.getMonth(),now_dt.getDate(),date.getHours(),date.getMinutes(),date.getSeconds()) let start_t = start.valueOf() if(start_t>now) { start_time=start_t let cmii = _createMatchInsInfo(start_time) cmiis.push(cmii) } } } return cmiis } /** * 已经存在不创建 * @param cmii * @returns */ async createMatch(cmii:CreateMatchInsInfo) { let match:TexasMatch=this.getMatch(cmii.id) if(match) { return match } match=new TexasMatch() await match.init(cmii,this._server) this._matches[cmii.id]=match return match } getMatch(ins_id:string) { return this._matches[ins_id] } onSignup(ins_id:string,user_id:number) { let match:TexasMatch = this.getMatch(ins_id) if(!match) { //比赛还没被服务器给实例化出来 return } match.onSignup(user_id) } }