mirror of
https://github.com/Luzifer/stadt-land-fluss.git
synced 2024-11-09 11:40:04 +00:00
Compare commits
4 commits
a6b046fe5e
...
675543b18e
Author | SHA1 | Date | |
---|---|---|---|
675543b18e | |||
df0ff292e0 | |||
4deb1e1502 | |||
d1fb9d4ced |
2 changed files with 88 additions and 9 deletions
94
src/app.vue
94
src/app.vue
|
@ -1,8 +1,25 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="container my-3">
|
<div class="container my-3">
|
||||||
<div class="row mb-4">
|
<div class="row mb-4">
|
||||||
<div class="col text-center">
|
<div class="col text-center position-relative">
|
||||||
<h1>Stadt-Land-Fluss</h1>
|
<h1>Stadt-Land-Fluss</h1>
|
||||||
|
|
||||||
|
<span class="position-absolute top-0 end-0">
|
||||||
|
<span
|
||||||
|
v-if="!gameSocketConnected"
|
||||||
|
class="badge text-danger"
|
||||||
|
title="Spiel ist vom Server getrennt"
|
||||||
|
>
|
||||||
|
<i class="fas fa-cloud fa-fw" />
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
class="badge ms-1"
|
||||||
|
title="Anzahl aktiver Spieler"
|
||||||
|
>
|
||||||
|
<i class="fas fa-user fa-fw me-1" />
|
||||||
|
{{ numberActivePlayers }}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -19,7 +36,7 @@
|
||||||
@keypress.enter="broadcastName"
|
@keypress.enter="broadcastName"
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
class="btn btn-success"
|
class="btn btn-secondary"
|
||||||
@click="broadcastName"
|
@click="broadcastName"
|
||||||
>
|
>
|
||||||
<i class="fas fa-pencil fa-fw me-1" />
|
<i class="fas fa-pencil fa-fw me-1" />
|
||||||
|
@ -93,6 +110,20 @@
|
||||||
<i class="fas fa-play fa-fw" />
|
<i class="fas fa-play fa-fw" />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="text-center text-muted small mt-1 text-nowrap">
|
||||||
|
<span class="badge bg-success me-2">
|
||||||
|
<i class="fas fa-check fa-fw me-1" />
|
||||||
|
{{ foreignInstanceAnswerStatus[generateKey(cat, letter)].answered }}
|
||||||
|
</span>
|
||||||
|
<span class="badge bg-warning text-dark me-2">
|
||||||
|
<i class="fas fa-forward fa-fw me-1" />
|
||||||
|
{{ foreignInstanceAnswerStatus[generateKey(cat, letter)].skipped }}
|
||||||
|
</span>
|
||||||
|
<span class="badge bg-secondary">
|
||||||
|
<i class="fas fa-question fa-fw me-1" />
|
||||||
|
{{ foreignInstanceAnswerStatus[generateKey(cat, letter)].thinking }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<div
|
<div
|
||||||
|
@ -133,7 +164,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { categories, gameSocketTemplate } from './config'
|
import { categories, gameSocketTemplate, instanceTimeout } from './config'
|
||||||
import { defineComponent } from 'vue'
|
import { defineComponent } from 'vue'
|
||||||
|
|
||||||
const baseBackoff = 100 // ms
|
const baseBackoff = 100 // ms
|
||||||
|
@ -141,7 +172,12 @@ const maxBackoff = 10000 // ms
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
computed: {
|
computed: {
|
||||||
answersGiven(): Object {
|
activeInstances(): any {
|
||||||
|
return Object.fromEntries(Object.entries(this.knownInstances)
|
||||||
|
.filter((e: any[]) => this.now.getTime() - e[1].lastActive < instanceTimeout))
|
||||||
|
},
|
||||||
|
|
||||||
|
answerKeys(): string[] {
|
||||||
const keys: string[] = []
|
const keys: string[] = []
|
||||||
for (const cat of this.gameState.categories) {
|
for (const cat of this.gameState.categories) {
|
||||||
for (const letter of this.gameState.letters) {
|
for (const letter of this.gameState.letters) {
|
||||||
|
@ -149,13 +185,42 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.fromEntries(keys.map(key => [
|
return keys
|
||||||
key, Object.keys(this.knownInstances).map(instId => ({
|
},
|
||||||
answer: this.knownInstances[instId].answers[key],
|
|
||||||
name: this.knownInstances[instId].name,
|
answersGiven(): Object {
|
||||||
|
return Object.fromEntries(this.answerKeys.map(key => [
|
||||||
|
key, Object.keys(this.activeInstances).map(instId => ({
|
||||||
|
answer: this.activeInstances[instId].answers[key],
|
||||||
|
name: this.activeInstances[instId].name,
|
||||||
})),
|
})),
|
||||||
]))
|
]))
|
||||||
},
|
},
|
||||||
|
|
||||||
|
foreignInstanceAnswerStatus(): any {
|
||||||
|
const foreignInstances = Object.entries(this.activeInstances)
|
||||||
|
.filter(e => e[0] !== this.instance)
|
||||||
|
.map(e => e[1])
|
||||||
|
|
||||||
|
return Object.fromEntries(this.answerKeys
|
||||||
|
.map((key: string) => [
|
||||||
|
key, {
|
||||||
|
answered: foreignInstances
|
||||||
|
.filter((instance: any) => instance.answers[key])
|
||||||
|
.length,
|
||||||
|
skipped: foreignInstances
|
||||||
|
.filter((instance: any) => instance.answers[key] === '')
|
||||||
|
.length,
|
||||||
|
thinking: foreignInstances
|
||||||
|
.filter((instance: any) => instance.answers[key] === undefined)
|
||||||
|
.length,
|
||||||
|
},
|
||||||
|
]))
|
||||||
|
},
|
||||||
|
|
||||||
|
numberActivePlayers(): number {
|
||||||
|
return Object.keys(this.activeInstances).length
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
created(): void {
|
created(): void {
|
||||||
|
@ -169,6 +234,7 @@ export default defineComponent({
|
||||||
|
|
||||||
this.knownInstances[this.instance] = {
|
this.knownInstances[this.instance] = {
|
||||||
answers: {},
|
answers: {},
|
||||||
|
lastActive: new Date().getTime(),
|
||||||
name: this.name,
|
name: this.name,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -178,6 +244,7 @@ export default defineComponent({
|
||||||
backoffCurrent: baseBackoff,
|
backoffCurrent: baseBackoff,
|
||||||
gameId: '',
|
gameId: '',
|
||||||
gameSocket: null as WebSocket | null,
|
gameSocket: null as WebSocket | null,
|
||||||
|
gameSocketConnected: false,
|
||||||
gameState: {
|
gameState: {
|
||||||
categories: [],
|
categories: [],
|
||||||
gameId: '',
|
gameId: '',
|
||||||
|
@ -189,6 +256,7 @@ export default defineComponent({
|
||||||
knownInstances: {} as any,
|
knownInstances: {} as any,
|
||||||
localAnswers: {} as any,
|
localAnswers: {} as any,
|
||||||
name: '',
|
name: '',
|
||||||
|
now: new Date(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -225,11 +293,13 @@ export default defineComponent({
|
||||||
this.gameSocket = new WebSocket(gameSocketTemplate.replace('{gameId}', this.gameId))
|
this.gameSocket = new WebSocket(gameSocketTemplate.replace('{gameId}', this.gameId))
|
||||||
|
|
||||||
this.gameSocket.addEventListener('close', () => {
|
this.gameSocket.addEventListener('close', () => {
|
||||||
|
this.gameSocketConnected = false
|
||||||
this.backoffCurrent = Math.min(maxBackoff, this.backoffCurrent * 1.5)
|
this.backoffCurrent = Math.min(maxBackoff, this.backoffCurrent * 1.5)
|
||||||
window.setTimeout(() => this.connectToGame(), this.backoffCurrent)
|
window.setTimeout(() => this.connectToGame(), this.backoffCurrent)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.gameSocket.addEventListener('open', () => {
|
this.gameSocket.addEventListener('open', () => {
|
||||||
|
this.gameSocketConnected = true
|
||||||
this.sendMessage({ type: 'ohai' })
|
this.sendMessage({ type: 'ohai' })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -312,7 +382,10 @@ export default defineComponent({
|
||||||
},
|
},
|
||||||
|
|
||||||
sendPing(): void {
|
sendPing(): void {
|
||||||
this.sendMessage({ instanceState: this.knownInstances[this.instance], type: 'ping' })
|
this.sendMessage({ instanceState: {
|
||||||
|
...this.knownInstances[this.instance],
|
||||||
|
lastActive: new Date().getTime(),
|
||||||
|
}, type: 'ping' })
|
||||||
},
|
},
|
||||||
|
|
||||||
shuffle(list: Array<any>): Array<any> {
|
shuffle(list: Array<any>): Array<any> {
|
||||||
|
@ -364,6 +437,9 @@ export default defineComponent({
|
||||||
this.connectToGame()
|
this.connectToGame()
|
||||||
|
|
||||||
window.setInterval(() => this.sendPing(), 10000)
|
window.setInterval(() => this.sendPing(), 10000)
|
||||||
|
window.setInterval(() => {
|
||||||
|
this.now = new Date()
|
||||||
|
}, 1000)
|
||||||
},
|
},
|
||||||
|
|
||||||
name: 'StadtLandFlussApp',
|
name: 'StadtLandFlussApp',
|
||||||
|
|
|
@ -34,7 +34,10 @@ const categories: string[] = [
|
||||||
|
|
||||||
const gameSocketTemplate: string = 'wss://tools.hub.luzifer.io/ws/slf-{gameId}'
|
const gameSocketTemplate: string = 'wss://tools.hub.luzifer.io/ws/slf-{gameId}'
|
||||||
|
|
||||||
|
const instanceTimeout: number = 120000 // ms
|
||||||
|
|
||||||
export {
|
export {
|
||||||
categories,
|
categories,
|
||||||
gameSocketTemplate,
|
gameSocketTemplate,
|
||||||
|
instanceTimeout,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue