2021-05-09 13:49:40 +02:00
|
|
|
"use strict"
|
|
|
|
|
|
2021-05-01 08:42:39 +02:00
|
|
|
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'
|
2021-04-21 08:39:43 +02:00
|
|
|
|
2020-11-25 22:03:35 +01:00
|
|
|
export default {
|
|
|
|
|
components: {
|
|
|
|
|
Upload,
|
2021-04-20 20:52:31 +02:00
|
|
|
GameTeaser,
|
2021-04-21 08:39:43 +02:00
|
|
|
ImageTeaser,
|
2020-11-25 22:03:35 +01:00
|
|
|
},
|
|
|
|
|
template: `
|
|
|
|
|
<div id="app">
|
|
|
|
|
<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>
|
|
|
|
|
|
|
|
|
|
<h1>New game</h1>
|
2021-04-27 21:43:53 +02:00
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><label>Pieces: </label></td>
|
|
|
|
|
<td><input type="text" v-model="tiles" /></td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><label>Scoring: </label></td>
|
|
|
|
|
<td>
|
|
|
|
|
<label><input type="radio" v-model="scoreMode" value="1" /> Any (Score when pieces are connected to each other or on final location)</label>
|
|
|
|
|
<br />
|
|
|
|
|
<label><input type="radio" v-model="scoreMode" value="0" /> Final (Score when pieces are put to their final location)</label>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><label>Image: </label></td>
|
|
|
|
|
<td>
|
|
|
|
|
<span v-if="image">
|
|
|
|
|
<img :src="image.url" style="width: 150px;" />
|
|
|
|
|
or
|
|
|
|
|
<upload @uploaded="mediaImgUploaded($event)" accept="image/*" label="Upload an image" />
|
|
|
|
|
</span>
|
|
|
|
|
<span v-else>
|
|
|
|
|
<upload @uploaded="mediaImgUploaded($event)" accept="image/*" label="Upload an image" />
|
|
|
|
|
(or select from below)
|
|
|
|
|
</span>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td colspan="2">
|
2021-05-05 22:22:34 +02:00
|
|
|
<button class="btn" :disabled="!canStartNewGame" :class="" @click="onNewGameClick">Start new game</button>
|
2021-04-27 21:43:53 +02:00
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
2020-11-25 22:03:35 +01:00
|
|
|
|
|
|
|
|
<h1>Image lib</h1>
|
|
|
|
|
<div>
|
2021-04-21 08:39:43 +02:00
|
|
|
<image-teaser v-for="i in images" :image="i" @click="image = i" />
|
2021-04-20 20:52:31 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<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 {
|
|
|
|
|
tiles: 1000,
|
|
|
|
|
image: '',
|
2021-04-27 21:43:53 +02:00
|
|
|
scoreMode: GameCommon.SCORE_MODE_ANY,
|
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
|
|
|
|
|
},
|
2021-05-05 22:22:34 +02:00
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-11-25 22:03:35 +01:00
|
|
|
methods: {
|
2020-12-06 21:55:23 +01:00
|
|
|
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}`
|
2020-12-06 21:55:23 +01:00
|
|
|
},
|
2020-11-25 22:03:35 +01:00
|
|
|
mediaImgUploaded(j) {
|
|
|
|
|
this.image = j.image
|
|
|
|
|
},
|
2020-12-24 15:29:32 +01:00
|
|
|
async onNewGameClick() {
|
2020-11-25 22:03:35 +01:00
|
|
|
const res = await fetch('/newgame', {
|
|
|
|
|
method: 'post',
|
|
|
|
|
headers: {
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
2021-04-27 21:43:53 +02:00
|
|
|
body: JSON.stringify({
|
2021-05-05 22:22:34 +02:00
|
|
|
tiles: this.tilesInt,
|
2021-04-27 21:43:53 +02:00
|
|
|
image: this.image,
|
2021-05-05 22:22:34 +02:00
|
|
|
scoreMode: this.scoreModeInt,
|
2021-04-27 21:43:53 +02:00
|
|
|
}),
|
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
|
|
|
}
|
|
|
|
|
}
|