"use strict"
import GameCommon from './../../common/GameCommon.js'
import Time from './../../common/Time.js'
import Upload from './../components/Upload.vue.js'
import GameTeaser from './../components/GameTeaser.vue.js'
import ImageTeaser from './../components/ImageTeaser.vue.js'
export default {
components: {
Upload,
GameTeaser,
ImageTeaser,
},
template: `
Running games
New game
Image lib
Finished games
`,
data() {
return {
tiles: 1000,
image: '',
scoreMode: GameCommon.SCORE_MODE_ANY,
gamesRunning: [],
gamesFinished: [],
images: [],
}
},
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
},
computed: {
scoreModeInt () {
return parseInt(this.scoreMode, 10)
},
tilesInt () {
return parseInt(this.tiles, 10)
},
canStartNewGame () {
if (!this.tilesInt || !this.image || ![0, 1].includes(this.scoreModeInt)) {
return false
}
return true
},
},
methods: {
time(start, end) {
const icon = end ? '🏁' : '⏳'
const from = start;
const to = end || Time.timestamp()
const timeDiffStr = Time.timeDiffStr(from, to)
return `${icon} ${timeDiffStr}`
},
mediaImgUploaded(j) {
this.image = j.image
},
async onNewGameClick() {
const res = await fetch('/newgame', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
tiles: this.tilesInt,
image: this.image,
scoreMode: this.scoreModeInt,
}),
})
if (res.status === 200) {
const game = await res.json()
location.assign(game.url)
}
}
}
}