ingame timer

This commit is contained in:
Zutatensuppe 2020-12-07 02:38:07 +01:00
parent 0dcebdd718
commit 3fdd1ae240
6 changed files with 73 additions and 7 deletions

View file

@ -223,6 +223,14 @@ const getTileDrawSize = (gameId) => {
return GAMES[gameId].puzzle.info.tileDrawSize return GAMES[gameId].puzzle.info.tileDrawSize
} }
const getStartTs = (gameId) => {
return GAMES[gameId].puzzle.data.started
}
const getFinishTs = (gameId) => {
return GAMES[gameId].puzzle.data.finished
}
const getMaxGroup = (gameId) => { const getMaxGroup = (gameId) => {
return GAMES[gameId].puzzle.data.maxGroup return GAMES[gameId].puzzle.data.maxGroup
} }
@ -624,5 +632,7 @@ export default {
getFirstOwnedTile, getFirstOwnedTile,
getTileDrawOffset, getTileDrawOffset,
getTileDrawSize, getTileDrawSize,
getStartTs,
getFinishTs,
handleInput, handleInput,
} }

View file

@ -20,4 +20,6 @@ export default {
getFirstOwnedTile: GameCommon.getFirstOwnedTile, getFirstOwnedTile: GameCommon.getFirstOwnedTile,
getTileDrawOffset: GameCommon.getTileDrawOffset, getTileDrawOffset: GameCommon.getTileDrawOffset,
getTileDrawSize: GameCommon.getTileDrawSize, getTileDrawSize: GameCommon.getTileDrawSize,
getStartTs: GameCommon.getStartTs,
getFinishTs: GameCommon.getFinishTs,
} }

View file

@ -27,7 +27,8 @@ function addCanvasToDom(canvas) {
return canvas return canvas
} }
function addMenuToDom(previewImageUrl) { function addMenuToDom(gameId) {
const previewImageUrl = Game.getImageUrl(gameId)
function row (...elements) { function row (...elements) {
const row = document.createElement('tr') const row = document.createElement('tr')
for (let el of elements) { for (let el of elements) {
@ -139,7 +140,7 @@ function addMenuToDom(previewImageUrl) {
scoresTitleEl.appendChild(document.createTextNode('Scores')) scoresTitleEl.appendChild(document.createTextNode('Scores'))
const scoresListEl = document.createElement('table') const scoresListEl = document.createElement('table')
const updateScores = (gameId) => { const updateScores = () => {
const activePlayers = Game.getActivePlayers(gameId) const activePlayers = Game.getActivePlayers(gameId)
const scores = activePlayers.map(p => ({ const scores = activePlayers.map(p => ({
name: p.name, name: p.name,
@ -158,6 +159,46 @@ function addMenuToDom(previewImageUrl) {
} }
} }
const timerStr = () => {
const started = Game.getStartTs(gameId)
const ended = Game.getFinishTs(gameId)
const icon = ended ? '🏁' : '⏳'
const from = started;
const to = ended || Util.timestamp()
const MS = 1
const SEC = MS * 1000
const MIN = SEC * 60
const HOUR = MIN * 60
const DAY = HOUR * 24
let diff = to - from
const d = Math.floor(diff / DAY)
diff = diff % DAY
const h = Math.floor(diff / HOUR)
diff = diff % HOUR
const m = Math.floor(diff / MIN)
diff = diff % MIN
const s = Math.floor(diff / SEC)
return `${icon} ${d}d ${h}h ${m}m ${s}s`
}
const timerCountdownEl = document.createElement('div')
timerCountdownEl.innerText = timerStr()
setInterval(() => {
timerCountdownEl.innerText = timerStr()
}, 1000)
const timerEl = document.createElement('div')
timerEl.classList.add('timer')
timerEl.appendChild(timerCountdownEl)
const scoresEl = document.createElement('div') const scoresEl = document.createElement('div')
scoresEl.classList.add('scores') scoresEl.classList.add('scores')
scoresEl.appendChild(scoresTitleEl) scoresEl.appendChild(scoresTitleEl)
@ -165,6 +206,7 @@ function addMenuToDom(previewImageUrl) {
document.body.appendChild(settingsOverlay) document.body.appendChild(settingsOverlay)
document.body.appendChild(previewOverlay) document.body.appendChild(previewOverlay)
document.body.appendChild(timerEl)
document.body.appendChild(menuEl) document.body.appendChild(menuEl)
document.body.appendChild(scoresEl) document.body.appendChild(scoresEl)
@ -272,8 +314,8 @@ async function main() {
const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(game.puzzle) const bitmaps = await PuzzleGraphics.loadPuzzleBitmaps(game.puzzle)
const {bgColorPickerEl, playerColorPickerEl, nameChangeEl, updateScores} = addMenuToDom(Game.getImageUrl(gameId)) const {bgColorPickerEl, playerColorPickerEl, nameChangeEl, updateScores} = addMenuToDom(gameId)
updateScores(gameId) updateScores()
// Create a dom and attach adapters to it so we can work with it // Create a dom and attach adapters to it so we can work with it
const canvas = addCanvasToDom(Graphics.createCanvas()) const canvas = addCanvasToDom(Graphics.createCanvas())
@ -455,7 +497,7 @@ async function main() {
// DRAW PLAYERS // DRAW PLAYERS
// --------------------------------------------------------------- // ---------------------------------------------------------------
updateScores(gameId) updateScores()
if (DEBUG) Debug.checkpoint('scores done') if (DEBUG) Debug.checkpoint('scores done')
// --------------------------------------------------------------- // ---------------------------------------------------------------

View file

@ -29,6 +29,16 @@ a:hover { color: var(--link-hover-color); }
box-shadow: 0 0 10px 0 rgba(0,0,0,.7); box-shadow: 0 0 10px 0 rgba(0,0,0,.7);
} }
.timer {
position: absolute;
left: 0;
background: var(--bg-color);
padding: 5px;
border: solid 1px black;
box-shadow: 0 0 10px 0 rgba(0,0,0,.7);
}
.menu { .menu {
position: absolute; position: absolute;
left: 50%; left: 50%;

View file

@ -69,4 +69,6 @@ export default {
get: GameCommon.get, get: GameCommon.get,
getSockets: GameCommon.getSockets, getSockets: GameCommon.getSockets,
handleInput: GameCommon.handleInput, handleInput: GameCommon.handleInput,
getStartTs: GameCommon.getStartTs,
getFinishTs: GameCommon.getFinishTs,
} }

View file

@ -78,8 +78,8 @@ app.use('/', async (req, res, next) => {
const games = [ const games = [
...Game.getAllGames().map(game => ({ ...Game.getAllGames().map(game => ({
id: game.id, id: game.id,
started: game.puzzle.data.started, started: Game.getStartTs(game.id),
finished: game.puzzle.data.finished, finished: Game.getFinishTs(game.id),
tilesFinished: Game.getFinishedTileCount(game.id), tilesFinished: Game.getFinishedTileCount(game.id),
tilesTotal: Game.getTileCount(game.id), tilesTotal: Game.getTileCount(game.id),
players: Game.getActivePlayers(game.id).length, players: Game.getActivePlayers(game.id).length,