puzzle/src/server/Game.ts

104 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-05-29 17:58:05 +02:00
import GameCommon from './../common/GameCommon'
2021-06-03 09:07:57 +02:00
import { Change, Game, Input, ScoreMode, ShapeMode, Timestamp } from './../common/Types'
import Util, { logger } from './../common/Util'
import { Rng } from './../common/Rng'
2021-05-17 00:27:47 +02:00
import GameLog from './GameLog'
2021-05-29 15:36:03 +02:00
import { createPuzzle, PuzzleCreationImageInfo } from './Puzzle'
import Protocol from './../common/Protocol'
2021-05-17 00:27:47 +02:00
import GameStorage from './GameStorage'
2020-11-12 19:19:02 +01:00
const log = logger('Game.ts')
2021-05-17 02:32:33 +02:00
async function createGameObject(
gameId: string,
targetTiles: number,
2021-05-29 15:36:03 +02:00
image: PuzzleCreationImageInfo,
2021-05-29 17:58:05 +02:00
ts: Timestamp,
2021-06-03 09:07:57 +02:00
scoreMode: ScoreMode,
shapeMode: ShapeMode
2021-05-29 15:36:03 +02:00
): Promise<Game> {
2020-12-22 22:35:09 +01:00
const seed = Util.hash(gameId + ' ' + ts)
const rng = new Rng(seed)
2021-04-30 00:31:24 +02:00
return {
id: gameId,
rng: { type: 'Rng', obj: rng },
2021-06-03 09:07:57 +02:00
puzzle: await createPuzzle(rng, targetTiles, image, ts, shapeMode),
2021-04-30 00:31:24 +02:00
players: [],
evtInfos: {},
scoreMode,
2021-06-03 09:07:57 +02:00
shapeMode,
2021-04-30 00:31:24 +02:00
}
2020-12-22 22:35:09 +01:00
}
2021-04-30 22:00:29 +02:00
2021-05-17 02:32:33 +02:00
async function createGame(
gameId: string,
targetTiles: number,
2021-05-29 15:36:03 +02:00
image: PuzzleCreationImageInfo,
2021-05-29 17:58:05 +02:00
ts: Timestamp,
2021-06-03 09:07:57 +02:00
scoreMode: ScoreMode,
shapeMode: ShapeMode
2021-05-28 23:01:00 +02:00
): Promise<void> {
2021-05-29 15:36:03 +02:00
const gameObject = await createGameObject(
gameId,
targetTiles,
image,
ts,
2021-06-03 09:07:57 +02:00
scoreMode,
shapeMode
2021-05-29 15:36:03 +02:00
)
2021-04-30 01:03:43 +02:00
2020-12-22 22:54:31 +01:00
GameLog.create(gameId)
2021-06-03 09:07:57 +02:00
GameLog.log(
gameId,
Protocol.LOG_HEADER,
1,
targetTiles,
image,
ts,
scoreMode,
shapeMode
)
2020-12-22 22:35:09 +01:00
2021-04-30 01:03:43 +02:00
GameCommon.setGame(gameObject.id, gameObject)
2021-04-30 21:00:30 +02:00
GameStorage.setDirty(gameId)
2020-12-13 00:11:42 +01:00
}
2021-05-29 15:36:03 +02:00
function addPlayer(gameId: string, playerId: string, ts: Timestamp): void {
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
const diff = ts - GameCommon.getStartTs(gameId)
if (idx === -1) {
GameLog.log(gameId, Protocol.LOG_ADD_PLAYER, playerId, diff)
} else {
GameLog.log(gameId, Protocol.LOG_UPDATE_PLAYER, idx, diff)
}
2020-12-23 01:19:30 +01:00
}
2020-12-22 22:35:09 +01:00
GameCommon.addPlayer(gameId, playerId, ts)
2021-04-30 21:00:30 +02:00
GameStorage.setDirty(gameId)
2020-12-13 00:11:42 +01:00
}
2021-05-28 23:01:00 +02:00
function handleInput(
gameId: string,
playerId: string,
2021-05-29 17:58:05 +02:00
input: Input,
ts: Timestamp
): Array<Change> {
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
const diff = ts - GameCommon.getStartTs(gameId)
GameLog.log(gameId, Protocol.LOG_HANDLE_INPUT, idx, input, diff)
}
2020-12-22 22:35:09 +01:00
const ret = GameCommon.handleInput(gameId, playerId, input, ts)
2021-04-30 21:00:30 +02:00
GameStorage.setDirty(gameId)
2020-12-13 00:11:42 +01:00
return ret
}
2020-11-12 19:19:02 +01:00
export default {
2020-12-22 22:35:09 +01:00
createGameObject,
2020-11-12 19:19:02 +01:00
createGame,
2020-12-13 00:11:42 +01:00
addPlayer,
handleInput,
2020-11-12 19:19:02 +01:00
}