From c6220b09e40742a73c642fa55351bff7adafcfb5 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Thu, 29 Oct 2026 16:27:21 +0100 Subject: [PATCH] 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) --- app/components/game/MenuBar.vue | 12 ++++- app/components/game/VillageFrame.vue | 3 +- app/components/icon/Ui.vue | 2 + app/composables/useRanking.ts | 24 +++++++++ app/composables/useShortcuts.ts | 2 +- app/mock/server.ts | 17 ++++++ app/pages/ranking/[worldId].vue | 80 ++++++++++++++++++++++++++++ app/types/game.ts | 23 ++++++++ i18n/locales/en.ts | 1 + i18n/messageloader.ts | 3 +- i18n/pages/ranking.ts | 23 ++++++++ 11 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 app/composables/useRanking.ts create mode 100644 app/pages/ranking/[worldId].vue create mode 100644 i18n/pages/ranking.ts diff --git a/app/components/game/MenuBar.vue b/app/components/game/MenuBar.vue index d883891..685ec50 100644 --- a/app/components/game/MenuBar.vue +++ b/app/components/game/MenuBar.vue @@ -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(() => { 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 }) diff --git a/app/components/game/VillageFrame.vue b/app/components/game/VillageFrame.vue index 1cae9f2..8c88909 100644 --- a/app/components/game/VillageFrame.vue +++ b/app/components/game/VillageFrame.vue @@ -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}` } : {}), } }) diff --git a/app/components/icon/Ui.vue b/app/components/icon/Ui.vue index 47dabaf..15f330c 100644 --- a/app/components/icon/Ui.vue +++ b/app/components/icon/Ui.vue @@ -18,6 +18,7 @@ export type UiIconName = | 'edit' | 'home' | 'book' + | 'rank' defineProps<{ name: UiIconName }>() @@ -46,6 +47,7 @@ defineProps<{ name: UiIconName }>() + diff --git a/app/composables/useRanking.ts b/app/composables/useRanking.ts new file mode 100644 index 0000000..de6e576 --- /dev/null +++ b/app/composables/useRanking.ts @@ -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(null) + const error = ref(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('ranking.index', { world_id: worldId, kind, page: at ?? null }) + } catch (e) { + error.value = errorText(e) + } finally { + busy.value = false + } + } + + return { page, error, busy, load } +} diff --git a/app/composables/useShortcuts.ts b/app/composables/useShortcuts.ts index 3c5b2b8..f70afb1 100644 --- a/app/composables/useShortcuts.ts +++ b/app/composables/useShortcuts.ts @@ -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, diff --git a/app/mock/server.ts b/app/mock/server.ts index 5018229..8a284a8 100644 --- a/app/mock/server.ts +++ b/app/mock/server.ts @@ -5,6 +5,8 @@ import { onUnmounted, ref } from 'vue' import type { + RankingPage, + RankingRow, Specialisation, AuthResult, QuestBookState, @@ -1375,6 +1377,21 @@ const handlers: Record = { 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. diff --git a/app/pages/ranking/[worldId].vue b/app/pages/ranking/[worldId].vue new file mode 100644 index 0000000..365e19f --- /dev/null +++ b/app/pages/ranking/[worldId].vue @@ -0,0 +1,80 @@ + + + + diff --git a/app/types/game.ts b/app/types/game.ts index a65a5c6..9aa5f14 100644 --- a/app/types/game.ts +++ b/app/types/game.ts @@ -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[] diff --git a/i18n/locales/en.ts b/i18n/locales/en.ts index f098571..5a7cc76 100644 --- a/i18n/locales/en.ts +++ b/i18n/locales/en.ts @@ -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', diff --git a/i18n/messageloader.ts b/i18n/messageloader.ts index d02bdf4..f463dde 100644 --- a/i18n/messageloader.ts +++ b/i18n/messageloader.ts @@ -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 } diff --git a/i18n/pages/ranking.ts b/i18n/pages/ranking.ts new file mode 100644 index 0000000..509e91c --- /dev/null +++ b/i18n/pages/ranking.ts @@ -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