game-village-politics-frontend/app/pages/ranking/[worldId].vue

86 lines
4.5 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { RankingKind } from '~/types/game'
definePageMeta({ layout: 'game' })
const route = useRoute()
const worldId = Number(route.params.worldId)
const { page, error, busy, load } = useRanking(worldId)
useCurrentWorld().enter(worldId)
const { t } = useI18n()
// The kind comes from the URL, so the menu's dropdown and the side menu link here.
const KINDS = ['players', 'tribes', 'villages', 'attack', 'defense'] as const
const kind = computed<RankingKind>(() => (KINDS.includes(route.query.kind as RankingKind) ? (route.query.kind as RankingKind) : 'players'))
watch(kind, (k) => load(k), { immediate: true })
const kinds = computed(() =>
KINDS.map((k) => ({
key: k,
label: t(`ranking.kinds.${k}`),
to: k === 'players' ? `/ranking/${worldId}` : { path: `/ranking/${worldId}`, query: { kind: k } },
active: kind.value === k,
})),
)
const mapLink = (v: { x: number; y: number }) => ({ path: `/map/${worldId}`, query: { x: v.x, y: v.y } })
</script>
<!-- The ranking, like Die Stämme's: kinds on the left, one table, the own row marked. -->
<template>
<div class="space-y-2 p-2.5">
<div class="grid gap-2 @2xl:grid-cols-[10rem_minmax(0,1fr)]">
<UiSideMenu :items="kinds" class="self-start" />
<section class="panel min-w-0">
<div class="panel-head">
<h1 class="panel-title">{{ $t(`ranking.titles.${kind}`) }}</h1>
</div>
<p v-if="error" class="alert m-2" role="alert">{{ error }}</p>
<p v-if="!page" class="py-6 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
<p v-else-if="!page.rows.length" class="py-6 text-center text-sm text-ink-500">{{ $t(`ranking.none.${kind}`) }}</p>
<template v-else>
<table class="grid-table">
<thead>
<tr>
<th class="w-12 text-right">{{ $t('ranking.rank') }}</th>
<th v-if="kind === 'villages'">{{ $t('ranking.village') }}</th>
<th>{{ $t(kind === 'tribes' ? 'ranking.tribe' : 'ranking.player') }}</th>
<th class="text-right">{{ $t(`ranking.value.${kind}`) }}</th>
<th v-if="kind === 'players'" class="text-right">{{ $t('ranking.villages') }}</th>
<th v-if="kind === 'tribes'" class="text-right">{{ $t('ranking.members') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="r in page.rows" :key="r.village?.id ?? r.tribe?.id ?? r.player?.id" :class="r.own ? 'bg-gold-100 font-bold' : 'hover:bg-parchment-100'">
<td class="text-right tabular-nums">{{ r.rank }}</td>
<td v-if="kind === 'villages' && r.village">
<NuxtLink :to="mapLink(r.village)" class="hover:text-blood-700 hover:underline">{{ villageLabel({ ...r.village, player: null }) }}</NuxtLink>
</td>
<td v-if="r.tribe">{{ r.tribe.name }} [{{ r.tribe.tag }}]</td>
<td v-else-if="r.player">
{{ r.player.name }}<span v-if="r.player.tribe" class="ml-1 font-normal text-ink-500">[{{ r.player.tribe.tag }}]</span>
</td>
<td class="text-right tabular-nums">{{ r.value.toLocaleString() }}</td>
<td v-if="kind === 'players'" class="text-right tabular-nums">{{ r.villages }}</td>
<td v-if="kind === 'tribes'" class="text-right tabular-nums">{{ r.members }}</td>
</tr>
</tbody>
</table>
<div class="flex flex-wrap items-center gap-2 bg-parchment-200 px-1.5 py-1">
<button v-if="page.own && page.own.page !== page.page" type="button" class="btn btn-outline" :disabled="busy" @click="load(kind, page.own.page)">
{{ $t('ranking.own', { rank: page.own.rank }) }}
</button>
<span v-else-if="page.own" class="text-sm text-ink-700">{{ $t('ranking.own_rank', { rank: page.own.rank }) }}</span>
<div v-if="page.pages > 1" class="ml-auto flex items-center gap-1 text-sm">
<button type="button" class="btn btn-ghost" :disabled="busy || page.page <= 1" @click="load(kind, page.page - 1)">‹ {{ $t('reports.prev') }}</button>
<span class="tabular-nums text-ink-700">{{ $t('reports.page', { page: page.page, pages: page.pages }) }}</span>
<button type="button" class="btn btn-ghost" :disabled="busy || page.page >= page.pages" @click="load(kind, page.page + 1)">{{ $t('reports.next') }} ›</button>
</div>
</div>
</template>
</section>
</div>
</div>
</template>