2021-05-29 17:58:05 +02:00
|
|
|
import GameCommon from './../common/GameCommon'
|
2021-06-04 09:26:37 +02:00
|
|
|
import { Change, Game, Input, ScoreMode, ShapeMode, SnapMode, Timestamp } from './../common/Types'
|
2021-05-31 20:05:41 +02:00
|
|
|
import Util, { logger } from './../common/Util'
|
2021-05-17 01:12:39 +02:00
|
|
|
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'
|
2021-05-17 01:12:39 +02:00
|
|
|
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
|
|
|
|
2021-05-31 20:05:41 +02: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,
|
2021-06-04 09:26:37 +02:00
|
|
|
shapeMode: ShapeMode,
|
|
|
|
|
snapMode: SnapMode
|
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-06-04 09:26:37 +02:00
|
|
|
snapMode,
|
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,
|
2021-06-04 09:26:37 +02:00
|
|
|
shapeMode: ShapeMode,
|
|
|
|
|
snapMode: SnapMode
|
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,
|
2021-06-04 09:26:37 +02:00
|
|
|
shapeMode,
|
|
|
|
|
snapMode
|
2021-05-29 15:36:03 +02:00
|
|
|
)
|
2021-04-30 01:03:43 +02:00
|
|
|
|
2021-06-06 08:57:42 +02:00
|
|
|
GameLog.create(gameId, ts)
|
2021-06-03 09:07:57 +02:00
|
|
|
GameLog.log(
|
|
|
|
|
gameId,
|
|
|
|
|
Protocol.LOG_HEADER,
|
|
|
|
|
1,
|
|
|
|
|
targetTiles,
|
|
|
|
|
image,
|
|
|
|
|
ts,
|
|
|
|
|
scoreMode,
|
2021-06-04 09:26:37 +02:00
|
|
|
shapeMode,
|
|
|
|
|
snapMode
|
2021-06-03 09:07:57 +02:00
|
|
|
)
|
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 {
|
2021-05-31 20:05:41 +02:00
|
|
|
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
|
|
|
|
|
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
|
|
|
|
if (idx === -1) {
|
2021-06-05 23:02:04 +02:00
|
|
|
GameLog.log(gameId, Protocol.LOG_ADD_PLAYER, playerId, ts)
|
2021-05-31 20:05:41 +02:00
|
|
|
} else {
|
2021-06-05 23:02:04 +02:00
|
|
|
GameLog.log(gameId, Protocol.LOG_UPDATE_PLAYER, idx, ts)
|
2021-05-31 20:05:41 +02:00
|
|
|
}
|
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> {
|
2021-05-31 20:05:41 +02:00
|
|
|
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
|
|
|
|
|
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
2021-06-05 23:02:04 +02:00
|
|
|
GameLog.log(gameId, Protocol.LOG_HANDLE_INPUT, idx, input, ts)
|
2021-05-31 20:05:41 +02:00
|
|
|
}
|
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
|
|
|
}
|