feat: ranking page: players, villages, attackers and defenders, own row marked; menu entry and key K

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-29 16:27:21 +01:00
parent def5553b14
commit c6220b09e4
11 changed files with 186 additions and 4 deletions

View File

@ -19,7 +19,7 @@ const world = computed(() => (props.inWorld && current.value ? (worlds.value?.fi
if (props.inWorld && !worlds.value) refresh()
interface Entry {
key: 'village' | 'map' | 'reports'
key: 'village' | 'map' | 'reports' | 'ranking'
to: string
icon: UiIconName
active: boolean
@ -56,6 +56,16 @@ const entries = computed<Entry[]>(() => {
to: kind === 'all' ? `/reports/${c.worldId}` : { path: `/reports/${c.worldId}`, query: { type: kind } },
})),
})
out.push({
key: 'ranking',
to: `/ranking/${c.worldId}`,
icon: 'rank',
active: route.path.startsWith('/ranking/'),
sub: (['players', 'villages', 'attack', 'defense'] as const).map((kind) => ({
label: t(`ranking.kinds.${kind}`),
to: kind === 'players' ? `/ranking/${c.worldId}` : { path: `/ranking/${c.worldId}`, query: { kind } },
})),
})
return out
})

View File

@ -62,6 +62,7 @@ const screenTitle = computed(() => {
return buildingName(v.village.value, openBuilding.value, t)
}
if (route.path.startsWith('/reports/')) return t('reports.title')
if (route.path.startsWith('/ranking/')) return t('app.ranking')
const s = VILLAGE_SCREENS.find((s) => s.path && route.path.startsWith(`${base.value}${s.path}`))
return s ? t(`village.screens.${s.name}`) : ''
})
@ -114,7 +115,7 @@ useShortcuts(() => {
const worldId = v.village.value?.world_id
return {
...Object.fromEntries(VILLAGE_SCREENS.map((s) => [s.name, `${base}${s.path}`])),
...(worldId ? { map: `/map/${worldId}`, reports: `/reports/${worldId}` } : {}),
...(worldId ? { map: `/map/${worldId}`, reports: `/reports/${worldId}`, ranking: `/ranking/${worldId}` } : {}),
}
})
</script>

View File

@ -18,6 +18,7 @@ export type UiIconName =
| 'edit'
| 'home'
| 'book'
| 'rank'
defineProps<{ name: UiIconName }>()
</script>
@ -46,6 +47,7 @@ defineProps<{ name: UiIconName }>()
<path v-else-if="name === 'up'" d="M12 19V5M6 11l6-6 6 6" />
<path v-else-if="name === 'minus'" d="M6 12h12" />
<path v-else-if="name === 'plus'" d="M12 6v12M6 12h12" />
<path v-else-if="name === 'rank'" d="M4 20V13h5v7M9 20V8h6v12M15 20v-9h5v9M3 20h18" />
<g v-else-if="name === 'pin'">
<path d="M12 21s-6-5.5-6-11a6 6 0 0 1 12 0c0 5.5-6 11-6 11z" />
<circle cx="12" cy="10" r="2" />

View File

@ -0,0 +1,24 @@
import type { RankingKind, RankingPage } from '~/types/game'
/** One world's ranking: a page of one kind. */
export function useRanking(worldId: number) {
const page = ref<RankingPage | null>(null)
const error = ref<string | null>(null)
const busy = ref(false)
const errorText = useErrorText()
/** Without a page, the server opens on the one with the own row. */
async function load(kind: RankingKind, at?: number) {
busy.value = true
error.value = null
try {
page.value = await ws.send<RankingPage>('ranking.index', { world_id: worldId, kind, page: at ?? null })
} catch (e) {
error.value = errorText(e)
} finally {
busy.value = false
}
}
return { page, error, busy, load }
}

View File

@ -1,5 +1,5 @@
/** Keys for the screens of a world, like the shortcuts in Die Stämme: one letter, no modifier. */
export const SHORTCUTS = { overview: 'v', build: 'h', recruit: 't', rally: 'p', workforce: 'w', map: 'm', reports: 'r' } as const
export const SHORTCUTS = { overview: 'v', build: 'h', recruit: 't', rally: 'p', workforce: 'w', map: 'm', reports: 'r', ranking: 'k' } as const
/**
* Jumps between screens by key while the page is mounted. Keys typed into a field,

View File

@ -5,6 +5,8 @@
import { onUnmounted, ref } from 'vue'
import type {
RankingPage,
RankingRow,
Specialisation,
AuthResult,
QuestBookState,
@ -1375,6 +1377,21 @@ const handlers: Record<string, Handler> = {
return { book: questBook(state, v), village: view(state, v, now) }
},
// Only the player's own villages exist in the mock, so the ranking is short.
'ranking.index': (state, user, data): RankingPage => {
const kind = (['players', 'villages', 'attack', 'defense'] as const).find((k) => k === data.kind) ?? 'players'
const mine = state.villages.filter((v) => v.user_id === user.id && v.world_id === Number(data.world_id))
const player = { id: user.id, name: user.name }
const points = (v: MockVillage) => v.buildings.reduce((sum, b) => sum + b.level * 5, 0)
const rows: RankingRow[] =
kind === 'players' && mine.length
? [{ rank: 1, player, value: mine.reduce((sum, v) => sum + points(v), 0), villages: mine.length, own: true }]
: kind === 'villages'
? mine.map((v, i) => ({ rank: i + 1, player, village: { id: v.id, name: v.name, x: v.x, y: v.y }, value: points(v), own: true }))
: []
return { kind, page: 1, pages: 1, rows, own: rows.length ? { rank: 1, page: 1 } : null }
},
'report.index': (state, user, data): ReportPage => reportPage(state, user, Number(data.world_id), Number(data.page) || 1, data.type || null),
// Opening a report marks it read.

View File

@ -0,0 +1,80 @@
<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', '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('ranking.player') }}</th>
<th class="text-right">{{ $t(`ranking.value.${kind}`) }}</th>
<th v-if="kind === 'players'" class="text-right">{{ $t('ranking.villages') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="r in page.rows" :key="r.village?.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>{{ r.player.name }}</td>
<td class="text-right tabular-nums">{{ r.value.toLocaleString() }}</td>
<td v-if="kind === 'players'" class="text-right tabular-nums">{{ r.villages }}</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>

View File

@ -270,6 +270,29 @@ export type Report =
| (ReportBase & { type: 'attack' | 'defense'; payload: BattleReport | ScatteredRaid })
| (ReportBase & { type: 'support'; payload: SupportReport })
export type RankingKind = 'players' | 'villages' | 'attack' | 'defense'
export interface RankingRow {
rank: number
player: { id: number; name: string }
/** Only in the village ranking. */
village?: { id: number; name: string; x: number; y: number }
/** Points, or opponents defeated (by the food the fallen ate). */
value: number
/** Only in the player ranking. */
villages?: number
own: boolean
}
/** `ranking.index`: one page; without a page asked for, the one with the own row. */
export interface RankingPage {
kind: RankingKind
page: number
pages: number
rows: RankingRow[]
own: { rank: number; page: number } | null
}
/** `report.index`, `report.delete` and `report.readAll`. */
export interface ReportPage {
reports: Report[]

View File

@ -7,6 +7,7 @@ export default {
shortcut: 'Key: {key}',
map: 'Map',
reports: 'Reports',
ranking: 'Ranking',
unread_reports: '{count} unread reports',
village: 'Village',
change_world: 'Change world',

View File

@ -4,12 +4,13 @@ import en from './locales/en'
import auth from './pages/auth'
import map from './pages/map'
import quests from './pages/quests'
import ranking from './pages/ranking'
import reports from './pages/reports'
import village from './pages/village'
import worlds from './pages/worlds'
// Explicit imports instead of import.meta.glob: @nuxtjs/i18n also loads this file in the
// Nitro build, where glob imports don't exist. Add new page files here, in path order.
export default mergeMessages({ en }, [auth, map, quests, reports, village, worlds], { packages: [networking] }) as {
export default mergeMessages({ en }, [auth, map, quests, ranking, reports, village, worlds], { packages: [networking] }) as {
en: Record<string, any>
}

23
i18n/pages/ranking.ts Normal file
View File

@ -0,0 +1,23 @@
import type { TranslationFile } from '@blax-software/i18n'
export default {
en: {
ranking: {
kinds: { players: 'Players', villages: 'Villages', attack: 'Attackers', defense: 'Defenders' },
titles: { players: 'Players by points', villages: 'Villages by points', attack: 'Opponents defeated in attack', defense: 'Opponents defeated in defence' },
none: {
players: 'No players yet.',
villages: 'No villages yet.',
attack: 'Nobody has defeated anyone in an attack yet.',
defense: 'Nobody has beaten off an attack yet.',
},
rank: 'Rank',
player: 'Player',
village: 'Village',
villages: 'Villages',
value: { players: 'Points', villages: 'Points', attack: 'Defeated', defense: 'Defeated' },
own: 'Your rank: {rank}',
own_rank: 'You are ranked {rank}.',
},
},
} satisfies TranslationFile