add different score modes

This commit is contained in:
Zutatensuppe 2021-04-27 21:43:53 +02:00
parent 90590d334e
commit abf6e6aa02
6 changed files with 101 additions and 30 deletions

View file

@ -3,6 +3,9 @@ import Protocol from './Protocol.js'
import Time from './Time.js'
import Util from './Util.js'
const SCORE_MODE_FINAL = 0
const SCORE_MODE_ANY = 1
// Map<gameId, GameObject>
const GAMES = {}
@ -10,13 +13,14 @@ function exists(gameId) {
return (!!GAMES[gameId]) || false
}
function __createGameObject(id, rng, puzzle, players, evtInfos) {
function __createGameObject(id, rng, puzzle, players, evtInfos, scoreMode) {
return {
id: id,
rng: rng,
puzzle: puzzle,
players: players,
evtInfos: evtInfos,
scoreMode: scoreMode,
}
}
@ -34,8 +38,8 @@ function __createPlayerObject(id, ts) {
}
}
function newGame({id, rng, puzzle, players, evtInfos}) {
const game = __createGameObject(id, rng, puzzle, players, evtInfos)
function newGame({id, rng, puzzle, players, evtInfos, scoreMode}) {
const game = __createGameObject(id, rng, puzzle, players, evtInfos, scoreMode)
setGame(id, game)
return game
}
@ -158,6 +162,10 @@ function setImageUrl(gameId, imageUrl) {
GAMES[gameId].puzzle.info.imageUrl = imageUrl
}
function getScoreMode(gameId) {
return GAMES[gameId].scoreMode || SCORE_MODE_FINAL
}
function isFinished(gameId) {
return getFinishedTileCount(gameId) === getTileCount(gameId)
}
@ -654,8 +662,23 @@ function handleInput(gameId, playerId, input, ts) {
// Snap the tile to the final destination
moveTilesDiff(gameId, tileIdxs, diff)
finishTiles(gameId, tileIdxs)
changePlayer(gameId, playerId, { points: getPlayerPoints(gameId, playerId) + tileIdxs.length })
_tileChanges(tileIdxs)
if (getScoreMode(gameId) === SCORE_MODE_FINAL) {
changePlayer(gameId, playerId, {
points: getPlayerPoints(gameId, playerId) + tileIdxs.length,
})
_playerChange()
} else if (getScoreMode(gameId) === SCORE_MODE_ANY) {
changePlayer(gameId, playerId, {
points: getPlayerPoints(gameId, playerId) + 1,
})
_playerChange()
} else {
// no score mode... should never occur, because there is a
// fallback to SCORE_MODE_FINAL in getScoreMode function
}
// check if the puzzle is finished
if (getFinishedTileCount(gameId) === getTileCount(gameId)) {
changeData(gameId, { finished: ts })
@ -685,6 +708,12 @@ function handleInput(gameId, playerId, input, ts) {
const zIndex = getMaxZIndexByTileIdxs(gameId, tileIdxs)
setTilesZIndex(gameId, tileIdxs, zIndex)
_tileChanges(tileIdxs)
if (getScoreMode(gameId) === SCORE_MODE_ANY) {
changePlayer(gameId, playerId, {
points: getPlayerPoints(gameId, playerId) + 1,
})
_playerChange()
}
return true
}
return false
@ -751,4 +780,6 @@ export default {
getStartTs,
getFinishTs,
handleInput,
SCORE_MODE_FINAL,
SCORE_MODE_ANY,
}

View file

@ -166,6 +166,7 @@ function encodeGame(data) {
data.puzzle,
data.players,
data.evtInfos,
data.scoreMode,
]
}
@ -182,6 +183,7 @@ function decodeGame(data) {
puzzle: data[3],
players: data[4],
evtInfos: data[5],
scoreMode: data[6],
}
}