puzzle/public/views/Index.vue.js

77 lines
1.8 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
},
// TODO: use vue router
2020-11-25 22:03:35 +01:00
template: `
<div id="app">
<ul class="nav">
<li><span class="btn" @click="view = 'index'">Home</span></li>
<li><span class="btn" @click="view = 'new-game'">New game</span></li>
</ul>
<new-game-dialog v-if="view === 'new-game'"
2021-05-13 14:01:10 +02:00
:images="images"
@newGame="onNewGame"
/>
<div v-if="view === 'index'">
<h1>Running games</h1>
<div class="game-teaser-wrap" v-for="g in gamesRunning">
<game-teaser :game="g" />
</div>
2020-11-25 22:03:35 +01:00
<h1>Finished games</h1>
<div class="game-teaser-wrap" v-for="g in gamesFinished">
<game-teaser :game="g" />
</div>
2020-11-25 22:03:35 +01:00
</div>
</div>`,
data() {
return {
view: 'index',
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
}
}