diff --git a/app/components/game/MenuBar.vue b/app/components/game/MenuBar.vue index 685ec50..b2da1b6 100644 --- a/app/components/game/MenuBar.vue +++ b/app/components/game/MenuBar.vue @@ -12,6 +12,8 @@ const { user, logout } = useAuth() const { current } = useCurrentWorld() const { worlds, refresh } = useWorld() const unread = useUnreadReports() +const villageContext = useVillageContextOrNull() +const villageState = computed(() => villageContext?.village.value ?? null) const { now } = useServerClock() const route = useRoute() @@ -19,7 +21,7 @@ const world = computed(() => (props.inWorld && current.value ? (worlds.value?.fi if (props.inWorld && !worlds.value) refresh() interface Entry { - key: 'village' | 'map' | 'reports' | 'ranking' + key: 'village' | 'map' | 'reports' | 'ranking' | 'tribe' to: string icon: UiIconName active: boolean @@ -56,12 +58,21 @@ const entries = computed(() => { to: kind === 'all' ? `/reports/${c.worldId}` : { path: `/reports/${c.worldId}`, query: { type: kind } }, })), }) + out.push({ + key: 'tribe', + to: `/tribe/${c.worldId}`, + icon: 'users', + active: route.path.startsWith('/tribe/'), + // Invites waiting while the player has no tribe. + count: villageState.value?.tribe.tribe ? undefined : villageState.value?.tribe.invites, + sub: [], + }) out.push({ key: 'ranking', to: `/ranking/${c.worldId}`, icon: 'rank', active: route.path.startsWith('/ranking/'), - sub: (['players', 'villages', 'attack', 'defense'] as const).map((kind) => ({ + sub: (['players', 'tribes', 'villages', 'attack', 'defense'] as const).map((kind) => ({ label: t(`ranking.kinds.${kind}`), to: kind === 'players' ? `/ranking/${c.worldId}` : { path: `/ranking/${c.worldId}`, query: { kind } }, })), diff --git a/app/components/game/VillageFrame.vue b/app/components/game/VillageFrame.vue index e01d565..bd8e3ec 100644 --- a/app/components/game/VillageFrame.vue +++ b/app/components/game/VillageFrame.vue @@ -63,6 +63,7 @@ const screenTitle = computed(() => { } if (route.path.startsWith('/reports/')) return t('reports.title') if (route.path.startsWith('/ranking/')) return t('app.ranking') + if (route.path.startsWith('/tribe/')) return t('app.tribe') const s = VILLAGE_SCREENS.find((s) => s.path && route.path.startsWith(`${base.value}${s.path}`)) return s ? t(`village.screens.${s.name}`) : '' }) @@ -127,7 +128,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}`, ranking: `/ranking/${worldId}` } : {}), + ...(worldId ? { map: `/map/${worldId}`, reports: `/reports/${worldId}`, ranking: `/ranking/${worldId}`, tribe: `/tribe/${worldId}` } : {}), } }) diff --git a/app/composables/useShortcuts.ts b/app/composables/useShortcuts.ts index f70afb1..c57b677 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', ranking: 'k' } as const +export const SHORTCUTS = { overview: 'v', build: 'h', recruit: 't', rally: 'p', workforce: 'w', map: 'm', reports: 'r', ranking: 'k', tribe: 's' } as const /** * Jumps between screens by key while the page is mounted. Keys typed into a field, diff --git a/app/composables/useTribe.ts b/app/composables/useTribe.ts new file mode 100644 index 0000000..7a0c4aa --- /dev/null +++ b/app/composables/useTribe.ts @@ -0,0 +1,39 @@ +import type { TribeListRow, TribePage } from '~/types/game' + +/** The player's tribe on one world: its page, every tribe of the world, and the changes. */ +export function useTribe(worldId: number) { + const page = ref(null) + const tribes = ref(null) + const error = ref(null) + const busy = ref(false) + const errorText = useErrorText() + const v = useVillageContextOrNull() + + async function call(event: string, data: object = {}): Promise { + busy.value = true + error.value = null + try { + return await ws.send(event, { world_id: worldId, ...data }) + } catch (e) { + error.value = errorText(e) + return null + } finally { + busy.value = false + } + } + + const load = async () => (page.value = (await call('tribe.show')) ?? page.value) + const loadList = async () => (tribes.value = (await call('tribe.list')) ?? tribes.value) + + /** A change answers with the fresh page; the village header learns about it too. */ + async function act(intent: string, data: object = {}) { + const next = await call(`tribe.${intent}`, data) + if (!next) return false + page.value = next + v?.refresh() + if (tribes.value) loadList() + return true + } + + return { page, tribes, error, busy, load, loadList, act } +} diff --git a/app/mock/server.ts b/app/mock/server.ts index c47714d..f4669a7 100644 --- a/app/mock/server.ts +++ b/app/mock/server.ts @@ -5,6 +5,7 @@ import { onUnmounted, ref } from 'vue' import type { + TribePage, RankingPage, RankingRow, Specialisation, @@ -158,6 +159,8 @@ interface MockState { reports?: MockReport[] /** Claimed quests per `user:world`. */ quests?: Record + /** Tribes, each led by the one user who founded it: the mock has no other players. */ + tribes?: { id: number; world_id: number; user_id: number; name: string; tag: string; description: string | null; events: { type: string; payload: Record; at: string }[] }[] } const STORAGE_KEY = 'vp.mock.v4' @@ -335,6 +338,32 @@ function refund(v: MockVillage, amount: Cost) { v.resources.iron = Math.min(cap, v.resources.iron + amount.iron) } +// Tribes in the mock: one player, so one member, no invites and no diplomacy partners. +const mockTribe = (state: MockState, userId: number, worldId: number) => (state.tribes ?? []).find((t) => t.user_id === userId && t.world_id === worldId) ?? null +const tribeRef = (t: { id: number; name: string; tag: string }) => ({ id: t.id, name: t.name, tag: t.tag }) + +function tribePage(state: MockState, userId: number, worldId: number): TribePage { + const t = mockTribe(state, userId, worldId) + const user = state.users.find((u) => u.id === userId) + return { + tribe: t + ? { + ...tribeRef(t), + description: t.description, + role: 'leader', + player_id: userId, + members: [{ player: { id: userId, name: user?.name ?? '?' }, role: 'leader', points: 0, villages: state.villages.filter((v) => v.user_id === userId && v.world_id === worldId).length }], + invites: [], + relations: [], + events: t.events, + } + : null, + invites: [], + max_members: 25, + notice_seconds: 86400, + } +} + /** Village roles as the backend defines them (world.village.specialisation); the mock grants no bonus. */ const ROLES: Specialisation['roles'] = [ { role: 'resources', buildings: ['timber_camp', 'clay_pit', 'iron_mine'], bonuses: { production: 0.2 } }, @@ -565,6 +594,7 @@ function view(state: MockState, v: MockVillage, now: number): VillageState { points: points(v), space: { used: spaceUsed(v), max: spaceMax(v) }, specialisation: specialisation(v), + tribe: { tribe: mockTribe(state, v.user_id, v.world_id) ? { ...tribeRef(mockTribe(state, v.user_id, v.world_id)!), role: 'leader' } : null, invites: 0 }, resources: { ...v.resources }, resources_at: iso(v.resources_at), rates: rates(v), @@ -1401,6 +1431,35 @@ const handlers: Record = { return { book: questBook(state, v), village: view(state, v, now) } }, + 'tribe.show': (state, user, data) => tribePage(state, user.id, Number(data.world_id)), + 'tribe.list': (state, user, data) => + (state.tribes ?? []) + .filter((t) => t.world_id === Number(data.world_id)) + .map((t) => ({ ...tribeRef(t), members: 1, points: 0, relation: t.user_id === user.id ? ('tribe' as const) : ('none' as const) })), + 'tribe.found': (state, user, data) => { + const worldId = Number(data.world_id) + if (mockTribe(state, user.id, worldId)) fail('in_tribe') + const name = String(data.name ?? '').trim() + const tag = String(data.tag ?? '').trim() + if (name.length < 3 || name.length > 32 || tag.length < 2 || tag.length > 6) fail('tribe_name') + if ((state.tribes ?? []).some((t) => t.world_id === worldId && (t.name === name || t.tag === tag))) fail('tribe_taken') + ;(state.tribes ??= []).push({ id: nextId(state), world_id: worldId, user_id: user.id, name, tag, description: null, events: [{ type: 'founded', payload: { player: user.name }, at: iso(Date.now()) }] }) + return tribePage(state, user.id, worldId) + }, + 'tribe.describe': (state, user, data) => { + const tribe = mockTribe(state, user.id, Number(data.world_id)) ?? fail('no_tribe') + tribe.description = String(data.description ?? '').trim() || null + return tribePage(state, user.id, Number(data.world_id)) + }, + 'tribe.leave': (state, user, data) => { + state.tribes = (state.tribes ?? []).filter((t) => !(t.user_id === user.id && t.world_id === Number(data.world_id))) + return tribePage(state, user.id, Number(data.world_id)) + }, + 'tribe.disband': (state, user, data) => { + state.tribes = (state.tribes ?? []).filter((t) => !(t.user_id === user.id && t.world_id === Number(data.world_id))) + return tribePage(state, user.id, Number(data.world_id)) + }, + // 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' @@ -1409,9 +1468,9 @@ const handlers: Record = { 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 }] + ? [{ rank: 1, player: { ...player, tribe: null }, 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 })) + ? mine.map((v, i) => ({ rank: i + 1, player: { ...player, tribe: null }, 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 } }, diff --git a/app/pages/ranking/[worldId].vue b/app/pages/ranking/[worldId].vue index 365e19f..293114a 100644 --- a/app/pages/ranking/[worldId].vue +++ b/app/pages/ranking/[worldId].vue @@ -10,7 +10,7 @@ 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 KINDS = ['players', 'tribes', 'villages', 'attack', 'defense'] as const const kind = computed(() => (KINDS.includes(route.query.kind as RankingKind) ? (route.query.kind as RankingKind) : 'players')) watch(kind, (k) => load(k), { immediate: true }) @@ -44,20 +44,25 @@ const mapLink = (v: { x: number; y: number }) => ({ path: `/map/${worldId}`, que {{ $t('ranking.rank') }} {{ $t('ranking.village') }} - {{ $t('ranking.player') }} + {{ $t(kind === 'tribes' ? 'ranking.tribe' : 'ranking.player') }} {{ $t(`ranking.value.${kind}`) }} {{ $t('ranking.villages') }} + {{ $t('ranking.members') }} - + {{ r.rank }} {{ villageLabel({ ...r.village, player: null }) }} - {{ r.player.name }} + {{ r.tribe.name }} [{{ r.tribe.tag }}] + + {{ r.player.name }}[{{ r.player.tribe.tag }}] + {{ r.value.toLocaleString() }} {{ r.villages }} + {{ r.members }} diff --git a/app/pages/tribe/[worldId].vue b/app/pages/tribe/[worldId].vue new file mode 100644 index 0000000..dc9b154 --- /dev/null +++ b/app/pages/tribe/[worldId].vue @@ -0,0 +1,286 @@ + + +