diff --git a/game-server/app/services/auctionService.ts b/game-server/app/services/auctionService.ts index 52d5691fe..25ffd707a 100644 --- a/game-server/app/services/auctionService.ts +++ b/game-server/app/services/auctionService.ts @@ -1,7 +1,8 @@ -import { LOT_CODE_LEN, AUCTION_STAGE, AUCTION_SOURCE } from './../consts'; +import { LOT_CODE_LEN, AUCTION_STAGE, AUCTION_SOURCE, AUCTION_TIME } from './../consts'; import { ItemReward } from "../domain/dbGeneral"; import { genCode } from '../pubUtils/util'; import { LotModel, LotParam } from '../db/Lot'; +import { getNextTime } from '../pubUtils/timeUtil'; /** * @description 生成拍卖数据 @@ -13,21 +14,21 @@ import { LotModel, LotParam } from '../db/Lot'; * @param {ItemReward[]} rewards */ export async function genAuction(guildCode: string, sourceType: number, sourceCode: string, serverId: number, rewards: ItemReward[]) { - const curTime = new Date(); + const begin = getNextTime(new Date(), AUCTION_TIME.GUILD_BEGIN_HOUR, AUCTION_TIME.GUILD_BEGIN_MIN); + const end = getNextTime(new Date(), AUCTION_TIME.WORLD_END_HOUR, AUCTION_TIME.WORLD_END_MIN); const lots: LotParam[] = rewards.map(reward => { const { id, count } = reward; const code = genCode(LOT_CODE_LEN); return { auctionStage: AUCTION_STAGE.DEFAULT, sourceType, - sourceCode, serverId, guildCode, code, gid: id, count, - maxPrice: 100, begin: curTime, end: curTime, curPrice: 10, // ! 此行为临时数据 + sourceCode, serverId, guildCode, code, gid: id, count, begin, end, + maxPrice: 100, curPrice: 10, // ! 此行为临时数据 } }); const result = await LotModel.createRecs(lots); return result; } - /** * @description 获取活动参加者 * @export diff --git a/game-server/test/timeUtils.test.ts b/game-server/test/timeUtils.test.ts new file mode 100644 index 000000000..28953e28f --- /dev/null +++ b/game-server/test/timeUtils.test.ts @@ -0,0 +1,31 @@ +import 'mocha'; +import { expect } from 'chai'; +import { getNextTime } from '../app/pubUtils/timeUtil'; + +const millisecondsPerDay = 24 * 60 * 60 * 1000; + +function checkTime(date: Date, h: number, m: number, s: number) { + expect(date.getHours()).to.be.equal(h); + expect(date.getMinutes()).to.be.equal(m); + expect(date.getSeconds()).to.be.equal(s); +} + +describe('测试时间相关工具方法', function () { + it('获取某个时间后的某点,返回当天', function () { + const baseDate = new Date(); + const newHour = 9, newMin = 9, newSec = 9; + baseDate.setHours(newHour - 1, 0, 0); + const newDate = getNextTime(baseDate, newHour, newMin, newSec); + checkTime(newDate, newHour, newMin, newSec); + expect(newDate.getDate()).to.be.equal(baseDate.getDate()); + }); + + it('获取某个时间后的某点,返回后一天', function () { + const baseDate = new Date(); + const newHour = 9, newMin = 9, newSec = 9; + baseDate.setHours(newHour + 1, 0, 0); + const newDate = getNextTime(baseDate, newHour, newMin, newSec); + checkTime(newDate, newHour, newMin, newSec); + expect(newDate.getDate()).to.be.equal(new Date(baseDate.getTime() + millisecondsPerDay).getDate()); + }); +}); \ No newline at end of file diff --git a/shared/consts/constModules/auctionConst.ts b/shared/consts/constModules/auctionConst.ts index e0ff9ee49..5ad8d97b4 100644 --- a/shared/consts/constModules/auctionConst.ts +++ b/shared/consts/constModules/auctionConst.ts @@ -13,4 +13,16 @@ export const AUCTION_SOURCE = { RACE: 4, // 粮草先行 }; -export const LOT_CODE_LEN = 8; \ No newline at end of file +export const LOT_CODE_LEN = 8; + +// 世界拍卖开始时间需晚于军团拍卖结束时间 +export const AUCTION_TIME = { + GUILD_BEGIN_HOUR: 8, // 军团拍卖开始 + GUILD_BEGIN_MIN: 20, // 军团拍卖开始 + WORLD_BEGIN_HOUR: 8, // 世界拍卖开始 + WORLD_BEGIN_MIN: 30, // 世界拍卖开始 + GUILD_END_HOUR: 8, // 军团拍卖结束 + GUILD_END_MIN: 30, // 军团拍卖结束 + WORLD_END_HOUR: 10, // 世界拍卖结束 + WORLD_END_MIN: 0, // 世界拍卖结束 +} \ No newline at end of file diff --git a/shared/pubUtils/timeUtil.ts b/shared/pubUtils/timeUtil.ts index 0ae1a8a6e..ccc92fbab 100644 --- a/shared/pubUtils/timeUtil.ts +++ b/shared/pubUtils/timeUtil.ts @@ -129,4 +129,19 @@ export function formatTime(d: Date, type: number) { if(month < 10) monthStr = '0' + monthStr; return `${yearStr}/${monthStr}/${dayStr}`; } -} \ No newline at end of file +} + +/** + * @description 获取指定时间后的某个时间点,如给定 date 后的晚八点整 + * @export + * @param {Date} date 给定时间 + * @param {number} h 给定小时 + * @param {number} [m=0] 给定分钟 + * @param {number} [s=0] 给定秒 + * @returns {Date} + */ +export function getNextTime(date: Date, h: number, m = 0, s = 0): Date { + const milliseconds = date.getTime(); + const timeToday = new Date(milliseconds).setHours(h, m, s); + return timeToday > milliseconds ? new Date(timeToday) : new Date(timeToday + PER_DAY * PER_SECOND); +}