puzzle/public/views/Index.vue.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-05-09 13:49:40 +02:00
"use strict"
2021-05-01 08:42:39 +02:00
import Time from './../../common/Time.js'
import GameTeaser from './../components/GameTeaser.vue.js'
2021-05-13 14:01:10 +02:00
import NewGameDialog from './../components/NewGameDialog.vue.js'
2020-11-25 22:03:35 +01:00
export default {
components: {
2021-04-20 20:52:31 +02:00
GameTeaser,
2021-05-13 14:01:10 +02:00
NewGameDialog,
2020-11-25 22:03:35 +01:00
},
template: `
<div id="app">
2021-05-13 14:01:10 +02:00
<span class="btn" @click="showNewGameDialog = !showNewGameDialog">New game</span>
<new-game-dialog
v-if="showNewGameDialog"
:images="images"
@newGame="onNewGame"
/>
2020-11-25 22:03:35 +01:00
<h1>Running games</h1>
2021-04-20 20:52:31 +02:00
<div class="game-teaser-wrap" v-for="g in gamesRunning">
<game-teaser :game="g" />
2020-11-25 22:03:35 +01:00
</div>
2021-04-20 20:52:31 +02:00
<h1>Finished games</h1>
<div class="game-teaser-wrap" v-for="g in gamesFinished">
<game-teaser :game="g" />
2020-11-25 22:03:35 +01:00
</div>
</div>`,
data() {
return {
2021-05-13 14:01:10 +02:00
showNewGameDialog: false,
2021-05-01 08:51:52 +02:00
gamesRunning: [],
gamesFinished: [],
images: [],
2020-11-08 12:56:54 +01:00
}
2020-11-25 22:03:35 +01:00
},
2021-05-01 08:51:52 +02:00
async created() {
const res = await fetch('/api/index-data')
const json = await res.json()
this.gamesRunning = json.gamesRunning
this.gamesFinished = json.gamesFinished
this.images = json.images
},
2020-11-25 22:03:35 +01:00
methods: {
time(start, end) {
const icon = end ? '🏁' : '⏳'
const from = start;
2021-04-14 19:30:45 +02:00
const to = end || Time.timestamp()
const timeDiffStr = Time.timeDiffStr(from, to)
return `${icon} ${timeDiffStr}`
},
2021-05-13 14:01:10 +02:00
async onNewGame(gameSettings) {
2020-11-25 22:03:35 +01:00
const res = await fetch('/newgame', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
2021-05-13 14:01:10 +02:00
body: JSON.stringify(gameSettings),
2020-11-10 18:48:16 +01:00
})
2020-11-25 22:03:35 +01:00
if (res.status === 200) {
const game = await res.json()
location.assign(game.url)
2020-11-19 12:38:50 +01:00
}
2020-11-09 01:54:05 +01:00
}
2020-11-07 11:35:29 +01:00
}
}