diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index cbc7deb..6034b30 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -18,7 +18,7 @@ import { RoomState } from '../../services/RoomState' import { retry } from '../../utils/promise.util' import { RoomLockErr } from '../../common/RoomLockErr' import { Schedule } from '../../clock/Schedule' -import { startGame, transformRecord } from '../../services/GameLogic' +import { calcScore, startGame, transformRecord } from '../../services/GameLogic' import { ObjectId } from 'bson' @@ -93,7 +93,7 @@ class PuzzleController extends BaseController { history.markModified('members') await history.save() if (mode == 1) { - let score = result ? 3 : -1 + let score = result ? calcScore(history.scheduleKey, statMap.comboCount) : 0 await broadcast(history.room, 'answer', {accountid, result, score}) await updateScore(history.room, [{accountid, score }]) diff --git a/src/clock/Schedule.ts b/src/clock/Schedule.ts index 832969d..20eaa06 100644 --- a/src/clock/Schedule.ts +++ b/src/clock/Schedule.ts @@ -53,4 +53,21 @@ export class Schedule { scheduleActive(name: string): boolean { return this.gameClock.has(name) && this.gameClock.get(name).active } + + /** + * 获取某个计时器剩余时间 + * @param {string} name + * @return {number} + */ + getLeftTime(name: string) { + if (!this.gameClock.has(name)) { + return 0 + } + let clock = this.gameClock.get(name) + if (!clock.active) { + this.gameClock.delete(name) + return 0 + } + return clock.time - clock.elapsedTime + } } diff --git a/src/common/DataParser.ts b/src/common/DataParser.ts index 2d35f66..dd22aca 100644 --- a/src/common/DataParser.ts +++ b/src/common/DataParser.ts @@ -1,6 +1,7 @@ import * as jetpack from 'fs-jetpack' import { BaseConst } from '../constants/BaseConst' import { error } from './Debug' +import { GameEnv } from '../config/GameEnv' const $cfg = new Map() const jsonPath = 'config' @@ -47,6 +48,7 @@ export var DataParser = (function () { } } } + new GameEnv().init($cfg.get(BaseConst.COMPOUND)) Object.assign(global, { $cfg: $cfg }) diff --git a/src/config/GameEnv.ts b/src/config/GameEnv.ts new file mode 100644 index 0000000..9de95e1 --- /dev/null +++ b/src/config/GameEnv.ts @@ -0,0 +1,21 @@ +import { singleton } from '../decorators/singleton' +import { compound_vo } from './parsers/compound_vo' + +@singleton +export class GameEnv { + // PVP每题可用时间 + public pvpInterval: number + // PVP当前连击数的分 + public pvpComboRate: number + // PVP剩余时间分 + public pvpTimeRate: number + // PVP答对基础分 + public pvpBaseScore: number + + public init(data: Map) { + this.pvpInterval = (+data.get(99018).value) * 1000 + this.pvpComboRate = +data.get(99017).value + this.pvpTimeRate = +data.get(99016).value + this.pvpBaseScore = +data.get(99015).value + } +} diff --git a/src/services/GameLogic.ts b/src/services/GameLogic.ts index dc94d15..4f1205a 100644 --- a/src/services/GameLogic.ts +++ b/src/services/GameLogic.ts @@ -11,6 +11,7 @@ import { Schedule } from '../clock/Schedule' import { BaseConst } from '../constants/BaseConst' import { QCategoryCache } from './QCategoryCache' import { PuzzleSession } from '../models/match/PuzzleSession' +import { GameEnv } from '../config/GameEnv' export function transformRecord(records: any[]) { @@ -88,7 +89,7 @@ export async function sendOneQuestion(history: any) { await sendQuestion(roomId, qdata) history.current++ await history.save() - new Schedule().beginSchedule(BaseConst.MATCH_ANSWER_TIME, async function () { + new Schedule().beginSchedule(new GameEnv().pvpInterval, async function () { let subHistory = await PuzzleSession.findById(history.id) let datas = [] for (let [accountid, data] of subHistory.members) { @@ -96,7 +97,7 @@ export async function sendOneQuestion(history: any) { data.answer.set(qid, 0) data.errorCount++ data.comboCount = 0 - datas.push({ accountid, score: -1 }) + datas.push({ accountid, score: 0 }) } } subHistory.markModified('members') @@ -105,3 +106,10 @@ export async function sendOneQuestion(history: any) { await sendOneQuestion(subHistory) }, history.scheduleKey) } + + +export function calcScore(timeKey: string, combo: number) { + const time = new Schedule().getLeftTime(timeKey) + const cfg = new GameEnv() + return cfg.pvpBaseScore + time / 100 * cfg.pvpTimeRate + combo * cfg.pvpComboRate +}