game-village-politics-frontend/app/pages/worlds.vue

90 lines
3.8 KiB
Vue

<script setup lang="ts">
import type { StartDirection } from '~/types/game'
const { worlds, error, busy, refresh, join } = useWorld()
const mock = isMockServer
const { enter: enterWorld } = useCurrentWorld()
refresh()
async function enter(worldId: number, direction: StartDirection = 'random') {
const villageId = await join(worldId, direction)
if (villageId) await navigateTo(`/village/${villageId}`)
}
// A new player picks where to start, like the direction choice in Die Stämme.
const choosing = ref<number | null>(null)
const DIRECTIONS: StartDirection[] = ['nw', 'ne', 'random', 'sw', 'se']
async function resetMock() {
await ws.send('mock.reset')
await refresh()
}
</script>
<template>
<div class="mx-auto max-w-2xl px-4 py-10">
<header class="mb-6">
<h1 class="font-display text-3xl font-semibold text-wood-900" style="font-variant: small-caps">{{ $t('worlds.title') }}</h1>
<p class="mt-1 text-ink-500">{{ $t('worlds.subtitle') }}</p>
</header>
<p v-if="error" class="alert mb-4" role="alert">{{ error }}</p>
<div class="panel overflow-hidden">
<p v-if="!worlds" class="px-4 py-8 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
<p v-else-if="!worlds.length" class="px-4 py-8 text-center text-sm text-ink-500">{{ $t('worlds.none') }}</p>
<ul v-else class="divide-y-2 divide-parchment-200">
<li v-for="w in worlds" :key="w.id" class="flex flex-wrap items-center gap-x-4 gap-y-3 px-4 py-4">
<span class="grid size-11 shrink-0 place-items-center rounded-sm border-2 border-wood-700 bg-moss-600 text-parchment-50">
<IconUi name="globe" class="size-6" />
</span>
<div class="min-w-0 flex-1">
<p class="truncate font-display text-lg font-semibold text-wood-900">{{ w.name }}</p>
<p class="mt-0.5 flex flex-wrap gap-1.5">
<span class="badge badge-plain">{{ $t('worlds.speed_value', { speed: w.speed }) }}</span>
<span v-if="w.village_id" class="badge badge-moss">{{ $t('worlds.joined') }}</span>
</p>
</div>
<div class="flex w-full gap-2 sm:w-auto">
<button
type="button"
class="btn flex-1 sm:flex-none"
:class="w.village_id ? 'btn-primary' : 'btn-outline'"
:disabled="busy"
@click="w.village_id ? enter(w.id) : (choosing = choosing === w.id ? null : w.id)"
>
{{ w.village_id ? $t('worlds.open') : $t('worlds.join') }}
</button>
<NuxtLink v-if="w.village_id" :to="`/map/${w.id}`" class="btn btn-outline" @click="enterWorld(w.id, w.village_id)">{{ $t('worlds.map') }}</NuxtLink>
</div>
<!-- The start direction: a small compass of five buttons. -->
<div v-if="choosing === w.id && !w.village_id" class="w-full border border-wood-700 bg-parchment-200 p-2">
<p class="text-[13px] font-semibold">{{ $t('worlds.start') }}</p>
<p class="mb-2 text-xs text-ink-500">{{ $t('worlds.start_hint') }}</p>
<div class="grid w-64 grid-cols-2 gap-1">
<button
v-for="d in DIRECTIONS"
:key="d"
type="button"
class="btn"
:class="d === 'random' ? 'btn-primary col-span-2' : 'btn-outline'"
:disabled="busy"
@click="enter(w.id, d)"
>
{{ $t(`worlds.directions.${d}`) }}
</button>
</div>
</div>
</li>
</ul>
</div>
<p v-if="mock" class="mt-6 text-center">
<button type="button" class="btn btn-ghost text-xs text-ink-500" :disabled="busy" @click="resetMock">
{{ $t('worlds.reset') }}
</button>
</p>
</div>
</template>