diff --git a/src/api/controllers/puzzle.controller.ts b/src/api/controllers/puzzle.controller.ts index e487b81..4ca5bc5 100644 --- a/src/api/controllers/puzzle.controller.ts +++ b/src/api/controllers/puzzle.controller.ts @@ -111,6 +111,7 @@ class PuzzleController extends BaseController { } history.status = 1 history.markModified('members') + let gameResult = 0 await history.save() if (mode == 1) { let score = result ? calcPvpScore(history.scheduleKey, statMap.comboCount) : 0 @@ -121,24 +122,26 @@ class PuzzleController extends BaseController { // await sendOneQuestion(history) // } } else if (mode == 0) { - let result = checkSingleFinish(history, accountid) - if (result) { + gameResult = checkSingleFinish(history, accountid) + if (gameResult) { history.status = 9 - //TODO:: 计算积分, 并更新排行榜 - let score = calcSingleScore() - let rankObj = { - shop: history.shop, - level: history.level, - accountId: accountid, - score, - mode: 0, - session: history.id + if (gameResult === 1) { + let score = calcSingleScore(history, accountid) + let rankObj = { + shop: history.shop, + level: history.level, + accountId: accountid, + score, + mode: 0, + session: history.id + } + await updateSingleRank(rankObj) } - await updateSingleRank(rankObj) + history.markModified('members') await history.save() } } - return { result, answer: record.a1, stats: history.members } + return { result, answer: record.a1, stats: history.members, gameResult } } @role('anon') diff --git a/src/config/GameEnv.ts b/src/config/GameEnv.ts index 40bb3d5..e2068ca 100644 --- a/src/config/GameEnv.ts +++ b/src/config/GameEnv.ts @@ -15,12 +15,25 @@ export class GameEnv { // 特效播放时间 public effectTimeLong: number + // 单人血量分 + public singleHpRate: number + // 单人最大连击数的分 + public singleComboRate: number + // 单人剩余时间分 + public singleTimeRate: number + // 单人降低难度后的得分折扣 + public singleLowLvlRate: number + public init(data: Map) { this.pvpInterval = (+data.get(99018).value) this.pvpComboRate = +data.get(99017).value this.pvpTimeRate = +data.get(99016).value this.pvpBaseScore = +data.get(99015).value this.effectTimeLong = +data.get(99025).value + this.singleHpRate = +data.get(99002).value + this.singleComboRate = +data.get(99004).value + this.singleTimeRate = +data.get(99003).value + this.singleLowLvlRate = +data.get(99005).value } /** diff --git a/src/models/match/PuzzleSession.ts b/src/models/match/PuzzleSession.ts index c62fab4..1cc5d7d 100644 --- a/src/models/match/PuzzleSession.ts +++ b/src/models/match/PuzzleSession.ts @@ -37,9 +37,25 @@ export class PuzzleStatusClass { */ @prop({default: 0}) maxCombo: number - + /** + * 多人模式中, 该玩家在房间中的sessionId, 用于重连 + * @type {string} + */ @prop() sessionId: string + /** + * 游戏胜利后的得分 + * @type {number} + */ + @prop({default: 0}) + score: number + + /** + * 游戏结果 + * @type {number} + */ + @prop({default: 0}) + public gameResult: number } // @ts-ignore diff --git a/src/services/GameLogic.ts b/src/services/GameLogic.ts index c10d015..e72138b 100644 --- a/src/services/GameLogic.ts +++ b/src/services/GameLogic.ts @@ -150,28 +150,43 @@ export async function updateSingleRank({shop, level, accountId, score, session, * @return {number} 0: 未结束, -1: 失败, 1: 胜利 */ export function checkSingleFinish(history: any, accountId: string): number { - if (history.hasExpired()) { - return -1 - } - let cfg = fetchLevelCfg(history.level) + let result = 0 let stat = history.members.get(accountId) - if (stat.rightCount >= cfg.enemy) { - return 1 + if (history.hasExpired()) { + result = -1 + } else { + let cfg = fetchLevelCfg(history.level) + if (stat.rightCount >= cfg.enemy) { + result = 1 + } else if (stat.errorCount >= cfg.hp) { + result = -1 + } } - if (stat.errorCount >= cfg.hp) { - return -1 + if (result !== 0) { + stat.gameResult = result } - - return 0 + return result } /** - * TODO:: * 计算单人模式的得分 * @return {number} */ -export function calcSingleScore() { - return 0 +export function calcSingleScore(history: any, accountId: string) { + let cfgLevel = fetchLevelCfg(history.level) + let time = (Date.now() - history.createdAt.getTime()) / 1000 + let stat = history.members.get(accountId) + let combo = stat.maxCombo + let hp = cfgLevel.hp - stat.errorCount + hp = hp < 0 ? 0 : hp + time = time < 0 ? 0 : time + let cfg = new GameEnv() + let score = time * cfg.singleTimeRate + hp * cfg.singleHpRate + combo * cfg.singleComboRate + if (history.difficultyMode) { + score *= cfg.singleLowLvlRate + } + stat.score = score + return score } /**