diff --git a/src/constants/BaseConst.ts b/src/constants/BaseConst.ts index b3be77d..4c4e49d 100644 --- a/src/constants/BaseConst.ts +++ b/src/constants/BaseConst.ts @@ -4,4 +4,8 @@ export class BaseConst{ public static readonly RANK_SCORE = 'rank_score' // 多人答题时间 public static readonly MATCH_ANSWER_TIME = 10000 + // 游戏结束后房间自动解散时间 + public static readonly ROOM_AUTO_CLOSE_TIME = 100000 + // 第一题延后发送时间 + public static readonly FIST_QUESTION_DELAY = 2000 } diff --git a/src/services/GameLogic.ts b/src/services/GameLogic.ts index 816c9f2..d7a1e18 100644 --- a/src/services/GameLogic.ts +++ b/src/services/GameLogic.ts @@ -1,5 +1,5 @@ import { - beginGame, + beginGame, closeRoom, endGame, sendQuestion, updateRound, @@ -27,7 +27,8 @@ export function transformRecord(records: any[]) { title: o.question, answers, type: 1, - category: type + '-' + subType + category: type + '-' + subType, + quality: o.quality } }) } @@ -52,7 +53,10 @@ export async function startGame(roomId: string, history: any) { let records = await Puzzle.randomQuestions({}, history.total) history.questions = records.map(o => o._id) await history.save() - await sendOneQuestion(history) + new Schedule().beginSchedule(BaseConst.FIST_QUESTION_DELAY, async function () { + await sendOneQuestion(history) + }, history.scheduleKey) + } export async function sendOneQuestion(history: any) { @@ -63,6 +67,9 @@ export async function sendOneQuestion(history: any) { history.status = 9 await history.save() await endGame(roomId, {}) + new Schedule().beginSchedule(BaseConst.ROOM_AUTO_CLOSE_TIME, async function () { + await closeRoom(roomId) + }, history.scheduleKey + '_close') return } diff --git a/src/services/WsSvr.ts b/src/services/WsSvr.ts index 8539b1a..7a5300d 100644 --- a/src/services/WsSvr.ts +++ b/src/services/WsSvr.ts @@ -64,6 +64,26 @@ export async function kickClient(roomId, clientId) { }) } +/** + * 关闭房间 + * @param roomId + * @return {Promise>} + */ +export async function closeRoom(roomId) { + console.log(`close room: ${roomId}`) + const url = `${apiBase}/room/call` + const args = [] + const data = { + roomId, + method: 'disconnect', + args: JSON.stringify(args) + } + return axios.post(url, data) + .then(res => { + return res.data + }) +} + /** * 开始游戏 * @param roomId