feat: world list with join
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
86eb2f6a8a
commit
4b82bc9d45
|
|
@ -0,0 +1,28 @@
|
|||
import type { WorldSummary } from '~/types/game'
|
||||
|
||||
export function useWorld() {
|
||||
const worlds = useState<WorldSummary[] | null>('worlds', () => null)
|
||||
const error = ref<string | null>(null)
|
||||
const busy = ref(false)
|
||||
const errorText = useErrorText()
|
||||
|
||||
async function run<T>(fn: () => Promise<T>) {
|
||||
busy.value = true
|
||||
error.value = null
|
||||
try {
|
||||
return await fn()
|
||||
} catch (e) {
|
||||
error.value = errorText(e)
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const refresh = () => run(async () => (worlds.value = await ws.send<WorldSummary[]>('world.index')))
|
||||
|
||||
/** Joins (or re-enters) a world and returns the player's village there. */
|
||||
const join = (worldId: number) =>
|
||||
run(async () => (await ws.send<{ village_id: number }>('world.join', { world_id: worldId })).village_id)
|
||||
|
||||
return { worlds, error, busy, refresh, join }
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
<script setup lang="ts">
|
||||
const { worlds, error, busy, refresh, join } = useWorld()
|
||||
const mock = isMockServer
|
||||
|
||||
refresh()
|
||||
|
||||
async function enter(worldId: number) {
|
||||
const villageId = await join(worldId)
|
||||
if (villageId) await navigateTo(`/village/${villageId}`)
|
||||
}
|
||||
|
||||
async function resetMock() {
|
||||
await ws.send('mock.reset')
|
||||
await refresh()
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h1>{{ $t('worlds.title') }}</h1>
|
||||
<p v-if="error" role="alert">{{ error }}</p>
|
||||
<p v-if="!worlds">{{ $t('common.loading') }}</p>
|
||||
<p v-else-if="!worlds.length">{{ $t('worlds.none') }}</p>
|
||||
<table v-else>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ $t('worlds.name') }}</th>
|
||||
<th>{{ $t('worlds.speed') }}</th>
|
||||
<th>{{ $t('worlds.village') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="w in worlds" :key="w.id">
|
||||
<td>{{ w.name }}</td>
|
||||
<td>{{ w.speed }}×</td>
|
||||
<td>
|
||||
<button type="button" :disabled="busy" @click="enter(w.id)">
|
||||
{{ w.village_id ? $t('worlds.open') : $t('worlds.join') }}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p v-if="mock">
|
||||
<button type="button" :disabled="busy" @click="resetMock">{{ $t('worlds.reset') }}</button>
|
||||
</p>
|
||||
</section>
|
||||
</template>
|
||||
Loading…
Reference in New Issue