2020-11-17 22:34:15 +01:00
|
|
|
import GameCommon from './../common/GameCommon.js'
|
2021-04-30 21:00:30 +02:00
|
|
|
import Util from './../common/Util.js'
|
2020-12-21 18:34:57 +01:00
|
|
|
import { Rng } from '../common/Rng.js'
|
2020-12-22 22:35:09 +01:00
|
|
|
import GameLog from './GameLog.js'
|
|
|
|
|
import { createPuzzle } from './Puzzle.js'
|
2020-12-23 01:19:30 +01:00
|
|
|
import Protocol from '../common/Protocol.js'
|
2021-04-30 21:00:30 +02:00
|
|
|
import GameStorage from './GameStorage.js'
|
2020-11-12 19:19:02 +01:00
|
|
|
|
2021-04-27 21:43:53 +02:00
|
|
|
async function createGameObject(gameId, targetTiles, image, ts, scoreMode) {
|
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 },
|
|
|
|
|
puzzle: await createPuzzle(rng, targetTiles, image, ts),
|
|
|
|
|
players: [],
|
|
|
|
|
evtInfos: {},
|
|
|
|
|
scoreMode,
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
2021-04-30 22:00:29 +02:00
|
|
|
|
2021-04-27 21:43:53 +02:00
|
|
|
async function createGame(gameId, targetTiles, image, ts, scoreMode) {
|
2021-04-30 01:03:43 +02:00
|
|
|
const gameObject = await createGameObject(gameId, targetTiles, image, ts, scoreMode)
|
|
|
|
|
|
2020-12-22 22:54:31 +01:00
|
|
|
GameLog.create(gameId)
|
2021-04-27 21:43:53 +02:00
|
|
|
GameLog.log(gameId, Protocol.LOG_HEADER, 1, targetTiles, image, ts, scoreMode)
|
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
|
|
|
}
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
function addPlayer(gameId, playerId, ts) {
|
2020-12-23 01:19:30 +01:00
|
|
|
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
|
2021-04-30 00:58:39 +02:00
|
|
|
const diff = ts - GameCommon.getStartTs(gameId)
|
2020-12-23 01:19:30 +01:00
|
|
|
if (idx === -1) {
|
|
|
|
|
GameLog.log(gameId, Protocol.LOG_ADD_PLAYER, playerId, diff)
|
|
|
|
|
} else {
|
|
|
|
|
GameLog.log(gameId, Protocol.LOG_UPDATE_PLAYER, idx, diff)
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-12-22 22:35:09 +01:00
|
|
|
function handleInput(gameId, playerId, input, ts) {
|
2020-12-23 01:19:30 +01:00
|
|
|
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-12-05 19:45:34 +01:00
|
|
|
getAllGames: GameCommon.getAllGames,
|
2020-12-21 02:29:14 +01:00
|
|
|
getRelevantPlayers: GameCommon.getRelevantPlayers,
|
2020-12-05 19:45:34 +01:00
|
|
|
getActivePlayers: GameCommon.getActivePlayers,
|
|
|
|
|
getFinishedTileCount: GameCommon.getFinishedTileCount,
|
|
|
|
|
getImageUrl: GameCommon.getImageUrl,
|
|
|
|
|
getTileCount: GameCommon.getTileCount,
|
2020-11-17 22:34:15 +01:00
|
|
|
exists: GameCommon.exists,
|
2020-12-03 21:11:52 +01:00
|
|
|
playerExists: GameCommon.playerExists,
|
2020-11-17 22:34:15 +01:00
|
|
|
get: GameCommon.get,
|
2020-12-07 02:38:07 +01:00
|
|
|
getStartTs: GameCommon.getStartTs,
|
|
|
|
|
getFinishTs: GameCommon.getFinishTs,
|
2020-11-12 19:19:02 +01:00
|
|
|
}
|