diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index 2a2464f..758f37e 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -19,7 +19,7 @@ import { Schedule } from '../../clock/Schedule' import { calcPvpScore, calcSingleScore, - checkSingleFinish, + checkSingleFinish, checkSubFinish, fetchLevelCfg, fetchSinglePuzzleType, getRank, sendOneQuestion, @@ -30,6 +30,7 @@ import { import { Shop, validShopId } from '../../models/shop/Shop' import { ShopActivity } from '../../models/shop/ShopActivity' import { GameUser } from '../../models/GameUser' +import { BaseConst } from '../../constants/BaseConst' class PuzzleController extends BaseController { @@ -134,9 +135,14 @@ class PuzzleController extends BaseController { await broadcast(history.room, 'answer', {accountid, result, score}) await updateScore(history.room, [{accountid, score }]) - // if (checkSubFinish(history, id)) { - // await sendOneQuestion(history) - // } + if (checkSubFinish(history, id)) { + if (new Schedule().getLeftTime(history.scheduleKey)> 1000) { + new Schedule().beginSchedule(1000, async function () { + await sendOneQuestion(history) + }, history.scheduleKey) + } + + } if (debug) { await sendOneQuestion(history) } @@ -210,7 +216,7 @@ class PuzzleController extends BaseController { async joinMultipleGame(req, res) { let { shop, aid, accountid, debug_begin_sec, debug_qcount } = req.params let data: any = { shop, maxTime: 3600, accountid } - let beginSecond = (debug_begin_sec ? +debug_begin_sec : 20) + let beginSecond = (debug_begin_sec ? +debug_begin_sec : 10) beginSecond = (Math.max(2, beginSecond)) * 1000 /** * 查找店铺设置, 查看一定时间内是否有要开始的活动 @@ -221,7 +227,7 @@ class PuzzleController extends BaseController { let roomId = '' let sessionId = '' let sessionMatch = '' - let beginTime = 0 + if (!aid) { throw new ZError(12, 'params no match') } @@ -236,6 +242,14 @@ class PuzzleController extends BaseController { if (!activity) { throw new ZError(13, 'activity not found') } + let beginTime = activity.getCurrentTime() + const now = Date.now() + beginTime = beginTime < now ? now : beginTime + if (now < activity.prepareBeginTime) { + throw new ZError(14, 'activity not start') + } else if (now> activity.endTime) { + throw new ZError(15, 'activity already end') + } let result = new RoomState().isLock(shop) try { await retry>(async () => { @@ -284,14 +298,19 @@ class PuzzleController extends BaseController { history.members.set(accountid, memberData) history.room = roomId //TODO: 根据配置赋值 - history.total = debug_qcount || 10 - history.begin = Date.now() + beginSecond + history.total = debug_qcount || activity.qcount + if (activity.repeatType === 9) { + history.begin = now + beginSecond + } else { + history.begin = beginTime + } + beginTime = history.begin history.expire = history.begin + (100 || 90) * 1000 history.type = 1 await history.save() sessionMatch = history.id - new Schedule().beginSchedule(beginSecond, async function () { + new Schedule().beginSchedule(beginTime - now, async function () { await startGame(roomId, history.id) }, shop) new RoomState().unlock(shop) diff --git a/src/models/shop/ShopActivity.ts b/src/models/shop/ShopActivity.ts index cd93e5b..f67789d 100644 --- a/src/models/shop/ShopActivity.ts +++ b/src/models/shop/ShopActivity.ts @@ -67,6 +67,7 @@ export class ShopActivityClass extends BaseModule { * 1: 每日 * 2: 每周 * 3: 每月 + * 9: 随到随开 * @type {number} */ @prop() @@ -169,11 +170,197 @@ export class ShopActivityClass extends BaseModule { } let result: any = {id: record.id, name: record.name, desc: record.desc} - result.current = getCurrentTime(record) - result.next = getNextTime(record, result.current) + result.current = record.getCurrentTime() + result.next = record.getNextTime(result.current) return result } + public get prepareBeginTime() { + let begtime = this.getCurrentTime() + return begtime - this.prepareTime * 60 * 1000 + } + + public get endTime() { + const cfg = new GameEnv() + const gameTime = (cfg.pvpInterval + cfg.effectTimeLong) * this.qcount * 1000 + let begtime = this.getCurrentTime() + return begtime + gameTime + } + + public getCurrentTime() { + const cfg = new GameEnv() + const gameTime = (cfg.pvpInterval + cfg.effectTimeLong) * this.qcount * 1000 + const today = moment().startOf('day').toDate().getTime() + switch (this.repeatType) { + case 0: //指定时间和日期 + let day = this.beginDays.find(o => o === today) + if (!day) { + return null + } + return fetchCurrentGameTime(this.beginTime, gameTime, this.prepareTime) + + case 1: //每天固定时间进行的 + // 开始时间大于今天或结束时间小于今天的, 去除 + if (this.beginDay > today || this.endDay < today) { + return null + } + return fetchCurrentGameTime(this.beginTime, gameTime, this.prepareTime) + case 2: //每周固定时间进行的 + // 开始时间大于今天或结束时间小于今天的, 去除 + if (this.beginDay > today || this.endDay < today) { + return null + } + const dayOfWeek = moment().weekday() + if (!this.weekDays.find(o => o === dayOfWeek)) { + return null + } + return fetchCurrentGameTime(this.beginTime, gameTime, this.prepareTime) + case 3: //每月固定时间进行的 + // 开始时间大于今天或结束时间小于今天的, 去除 + if (this.beginDay > today || this.endDay < today) { + return null + } + const dayOfMonth = moment().date() + if (!this.monthDays.find(o => o === dayOfMonth)) { + return null + } + return fetchCurrentGameTime(this.beginTime, gameTime, this.prepareTime) + case 9: + return Date.now() + } + return null + } + + public getNextTime(current: number | null) { + const today = moment().startOf('day').toDate().getTime() + let times: number[] = [] + const now = Date.now() + switch (this.repeatType) { + case 0: //指定时间和日期 + for (let _d of this.beginDays) { + if (_d < today) { + continue + } + for (let _t of this.beginTime) { + times.push(_t * 1000 + _d) + } + } + for (let _t of times) { + if (!current && _t > now) { + return _t + } + if (_t > current && _t > now) { + return _t + } + } + break + case 1: //每天固定时间进行的 + // 结束时间小于今天的, 去除 + if (this.endDay < today) { + return null + } + if (!this.beginTime) { + return null + } + for (let _t of this.beginTime) { + times.push(_t * 1000 + today) + } + let nextDay = moment().startOf('day').add(1, 'day').toDate().getTime() + for (let _t of this.beginTime) { + times.push(_t * 1000 + nextDay) + } + + for (let _t of times) { + if (!current && _t > now) { + return _t + } + if (_t > current && _t > now) { + return _t + } + } + break + case 2: //每周固定时间进行的 + // 结束时间小于今天的, 去除 + if (this.endDay < today) { + return null + } + if (!this.weekDays || this.weekDays.length ===0 || !this.beginTime) { + return null + } + const dayOfWeek = moment().weekday() + this.weekDays.sort() + let days: number[] = [] + for (let _d of this.weekDays) { + if (_d >= dayOfWeek) { + days.push(_d) + } + if (days.length >= 2) { + break + } + } + if (days.length < 2) { + days.push(this.weekDays[0] + 7) + } + const weekStart = moment().startOf('week').toDate().getTime() + for (let _d of days) { + for (let _t of this.beginTime) { + times.push(_t * 1000 + weekStart + _d * 24 * 3600 * 1000) + } + } + + for (let _t of times) { + if (!current && _t > now) { + return _t + } + if (_t > current && _t > now) { + return _t + } + } + break + case 3: //每月固定时间进行的 + // 结束时间小于今天的, 去除 + if (this.endDay < today) { + return null + } + if (this.monthDays.length == 0 || !this.beginTime) { + return null + } + const dayOfMonth = moment().date() + this.monthDays.sort() + let mdays: number[] = [] + for (let _d of this.monthDays) { + if (_d < dayOfMonth) { + continue + } + mdays.push(_d) + if (mdays.length >= 2) { + break + } + } + const monthStart: number = moment().startOf('month').toDate().getTime() + for (let _d of mdays) { + for (let _t of this.beginTime) { + times.push(_t * 1000 + monthStart + _d * 24 * 3600 * 1000) + } + } + if (mdays.length < 2) { + const nextMonth = moment().startOf('month').add(1, 'month').toDate().getTime() + for (let _t of this.beginTime) { + times.push(_t * 1000 + nextMonth + this.monthDays[0] * 24 * 3600 * 1000) + } + } + for (let _t of times) { + if (!current && _t > now) { + return _t + } + if (_t > current && _t > now) { + return _t + } + } + } + return null + } + /** * 根据排名获取奖励 * @param {number} rank @@ -208,177 +395,5 @@ function fetchCurrentGameTime(times: number[], gameTime: number, prepareTime: nu } } -function getCurrentTime(record: any) { - const cfg = new GameEnv() - const gameTime = (cfg.pvpInterval + cfg.effectTimeLong) * record.qcount * 1000 - const today = moment().startOf('day').toDate().getTime() - switch (record.repeatType) { - case 0: //指定时间和日期 - let day = record.beginDays.find(o => o === today) - if (!day) { - return null - } - return fetchCurrentGameTime(record.beginTime, gameTime, record.prepareTime) - - case 1: //每天固定时间进行的 - // 开始时间大于今天或结束时间小于今天的, 去除 - if (record.beginDay > today || record.endDay < today) { - return null - } - return fetchCurrentGameTime(record.beginTime, gameTime, record.prepareTime) - case 2: //每周固定时间进行的 - // 开始时间大于今天或结束时间小于今天的, 去除 - if (record.beginDay > today || record.endDay < today) { - return null - } - const dayOfWeek = moment().weekday() - if (!record.weekDays.find(o => o === dayOfWeek)) { - return null - } - return fetchCurrentGameTime(record.beginTime, gameTime, record.prepareTime) - case 3: //每月固定时间进行的 - // 开始时间大于今天或结束时间小于今天的, 去除 - if (record.beginDay > today || record.endDay < today) { - return null - } - const dayOfMonth = moment().date() - if (!record.monthDays.find(o => o === dayOfMonth)) { - return null - } - return fetchCurrentGameTime(record.beginTime, gameTime, record.prepareTime) - case 9: - return Date.now() - } - return null -} -function getNextTime(record: any, current: number | null) { - const today = moment().startOf('day').toDate().getTime() - let times: number[] = [] - const now = Date.now() - switch (record.repeatType) { - case 0: //指定时间和日期 - for (let _d of record.beginDays) { - if (_d < today) { - continue - } - for (let _t of record.beginTime) { - times.push(_t * 1000 + _d) - } - } - for (let _t of times) { - if (!current && _t > now) { - return _t - } - if (_t > current && _t > now) { - return _t - } - } - break - case 1: //每天固定时间进行的 - // 结束时间小于今天的, 去除 - if (record.endDay < today) { - return null - } - if (!record.beginTime) { - return null - } - for (let _t of record.beginTime) { - times.push(_t * 1000 + today) - } - let nextDay = moment().startOf('day').add(1, 'day').toDate().getTime() - for (let _t of record.beginTime) { - times.push(_t * 1000 + nextDay) - } - - for (let _t of times) { - if (!current && _t > now) { - return _t - } - if (_t > current && _t > now) { - return _t - } - } - break - case 2: //每周固定时间进行的 - // 结束时间小于今天的, 去除 - if (record.endDay < today) { - return null - } - if (!record.weekDays || record.weekDays.length ===0 || !record.beginTime) { - return null - } - const dayOfWeek = moment().weekday() - record.weekDays.sort() - let days: number[] = [] - for (let _d of record.weekDays) { - if (_d >= dayOfWeek) { - days.push(_d) - } - if (days.length >= 2) { - break - } - } - if (days.length < 2) { - days.push(record.weekDays[0] + 7) - } - const weekStart = moment().startOf('week').toDate().getTime() - for (let _d of days) { - for (let _t of record.beginTime) { - times.push(_t * 1000 + weekStart + _d * 24 * 3600 * 1000) - } - } - - for (let _t of times) { - if (!current && _t > now) { - return _t - } - if (_t > current && _t > now) { - return _t - } - } - break - case 3: //每月固定时间进行的 - // 结束时间小于今天的, 去除 - if (record.endDay < today) { - return null - } - if (record.monthDays.length == 0 || !record.beginTime) { - return null - } - const dayOfMonth = moment().date() - record.monthDays.sort() - let mdays: number[] = [] - for (let _d of record.monthDays) { - if (_d < dayOfMonth) { - continue - } - mdays.push(_d) - if (mdays.length >= 2) { - break - } - } - const monthStart: number = moment().startOf('month').toDate().getTime() - for (let _d of mdays) { - for (let _t of record.beginTime) { - times.push(_t * 1000 + monthStart + _d * 24 * 3600 * 1000) - } - } - if (mdays.length < 2) { - const nextMonth = moment().startOf('month').add(1, 'month').toDate().getTime() - for (let _t of record.beginTime) { - times.push(_t * 1000 + nextMonth + record.monthDays[0] * 24 * 3600 * 1000) - } - } - for (let _t of times) { - if (!current && _t > now) { - return _t - } - if (_t > current && _t > now) { - return _t - } - } - } - return null -}