feat: map links from the header, worlds and village; world, continent and points in the village state

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 19:58:42 +01:00
parent a4185b66a7
commit 657e2d331e
8 changed files with 88 additions and 4 deletions

View File

@ -2,6 +2,8 @@
const { t } = useI18n() const { t } = useI18n()
const { user, logout } = useAuth() const { user, logout } = useAuth()
const notices = useNotices() const notices = useNotices()
/** The world of the village or map on screen, for the Map link. */
const currentWorld = useState<number | null>('world.current', () => null)
const connection = computed(() => const connection = computed(() =>
isMockServer isMockServer
@ -32,11 +34,15 @@ useHead({ title: () => t('app.title') })
<span class="hidden font-display text-lg font-semibold sm:inline">{{ t('app.title') }}</span> <span class="hidden font-display text-lg font-semibold sm:inline">{{ t('app.title') }}</span>
</NuxtLink> </NuxtLink>
<nav v-if="user" class="ml-1"> <nav v-if="user" class="ml-1 flex items-center gap-1">
<NuxtLink to="/worlds" class="btn btn-ghost" active-class="bg-parchment-200/70"> <NuxtLink to="/worlds" class="btn btn-ghost" active-class="bg-parchment-200/70">
<IconUi name="globe" class="size-4" /> <IconUi name="globe" class="size-4" />
{{ t('app.worlds') }} {{ t('app.worlds') }}
</NuxtLink> </NuxtLink>
<NuxtLink v-if="currentWorld" :to="`/map/${currentWorld}`" class="btn btn-ghost" active-class="bg-parchment-200/70">
<IconUi name="pin" class="size-4" />
{{ t('app.map') }}
</NuxtLink>
</nav> </nav>
<div class="ml-auto flex min-w-0 items-center gap-2"> <div class="ml-auto flex min-w-0 items-center gap-2">

View File

@ -19,6 +19,7 @@ import type {
VillageState, VillageState,
WorldSummary, WorldSummary,
} from '~/types/game' } from '~/types/game'
import type { MapVillage, MapView } from '~/types/map'
import type { GameAuth, GameWs } from '~/utils/networking' import type { GameAuth, GameWs } from '~/utils/networking'
import { import {
BASE_TRICKLE, BASE_TRICKLE,
@ -324,9 +325,12 @@ function view(v: MockVillage, now: number): VillageState {
const pop = villagers(v) const pop = villagers(v)
return { return {
id: v.id, id: v.id,
world_id: v.world_id,
name: v.name, name: v.name,
x: v.x, x: v.x,
y: v.y, y: v.y,
continent: continentOf(v.x, v.y),
points: points(v),
space: { used: spaceUsed(v), max: v.space_max }, space: { used: spaceUsed(v), max: v.space_max },
resources: { ...v.resources }, resources: { ...v.resources },
resources_at: iso(v.resources_at), resources_at: iso(v.resources_at),
@ -375,6 +379,58 @@ function view(v: MockVillage, now: number): VillageState {
} }
} }
const continentOf = (x: number, y: number) => `K${Math.floor(y / 100)}${Math.floor(x / 100)}`
/** Placeholder points: 5 per building level, counting level 0. */
const points = (v: MockVillage) => v.buildings.reduce((sum, b) => sum + 5 * (b.level + 1), 0)
/** Mock map: the stored player villages plus barbarians made up from a hash of the field. */
function mapView(state: MockState, user: AuthUser, data: any): MapView {
const size = 1000
const own = state.villages.find((v) => v.world_id === Number(data.world_id) && v.user_id === user.id)
const cx = Number.isFinite(Number(data.x)) && data.x !== undefined ? Number(data.x) : (own?.x ?? 500)
const cy = Number.isFinite(Number(data.y)) && data.y !== undefined ? Number(data.y) : (own?.y ?? 500)
const w = Math.max(1, Math.min(41, Number(data.w) || 15))
const h = Math.max(1, Math.min(41, Number(data.h) || 15))
const x0 = Math.max(0, Math.min(size - w, cx - Math.floor(w / 2)))
const y0 = Math.max(0, Math.min(size - h, cy - Math.floor(h / 2)))
const villages: MapVillage[] = []
for (let y = y0; y < y0 + h; y++) {
for (let x = x0; x < x0 + w; x++) {
const player = state.villages.find((v) => v.world_id === Number(data.world_id) && v.x === x && v.y === y)
if (player) {
const owner = state.users.find((u) => u.id === player.user_id)
villages.push({
id: player.id,
x,
y,
name: player.name,
points: points(player),
continent: continentOf(x, y),
player: { id: player.user_id, name: owner?.name ?? '?' },
own: player.user_id === user.id,
})
continue
}
const r = (Math.imul(x * 73856093 ^ y * 19349663, 1274126177) >>> 0) / 4294967296
const near = Math.abs(x - 500) <= 25 && Math.abs(y - 500) <= 25
if (near && r < 0.08) {
villages.push({
id: -(y * size + x),
x,
y,
name: 'Barbarian village',
points: 20 + Math.floor(r * 1000),
continent: continentOf(x, y),
player: null,
own: false,
})
}
}
}
return { world_id: Number(data.world_id), size, center: { x: cx, y: cy }, x: x0, y: y0, w, h, villages }
}
function createVillage(state: MockState, user: AuthUser, worldId: number, now: number): MockVillage { function createVillage(state: MockState, user: AuthUser, worldId: number, now: number): MockVillage {
const taken = new Set(state.villages.filter((v) => v.world_id === worldId).map((v) => `${v.x}|${v.y}`)) const taken = new Set(state.villages.filter((v) => v.world_id === worldId).map((v) => `${v.x}|${v.y}`))
let x = 500 let x = 500
@ -436,6 +492,8 @@ const handlers: Record<string, Handler> = {
'village.show': (state, user, data, now) => view(ownVillage(state, user, data.village_id, now), now), 'village.show': (state, user, data, now) => view(ownVillage(state, user, data.village_id, now), now),
'map.view': (state, user, data) => mapView(state, user, data),
'village.build': (state, user, data, now) => { 'village.build': (state, user, data, now) => {
const v = ownVillage(state, user, data.village_id, now) const v = ownVillage(state, user, data.village_id, now)
const type = data.type as BuildingType const type = data.type as BuildingType

View File

@ -12,6 +12,11 @@ watch(selectedBuilding, (b) => {
const spaceShare = computed(() => (village.value ? village.value.space.used / village.value.space.max : 0)) const spaceShare = computed(() => (village.value ? village.value.space.used / village.value.space.max : 0))
const currentWorld = useState<number | null>('world.current', () => null)
watch(village, (v) => {
if (v) currentWorld.value = v.world_id
})
v.refresh() v.refresh()
function showInVillage(id: number) { function showInVillage(id: number) {
@ -29,10 +34,15 @@ function showInVillage(id: number) {
<div class="rounded-xl bg-white/85 px-4 py-3 shadow-sm ring-1 ring-parchment-300/70 backdrop-blur sm:pointer-events-auto"> <div class="rounded-xl bg-white/85 px-4 py-3 shadow-sm ring-1 ring-parchment-300/70 backdrop-blur sm:pointer-events-auto">
<p class="text-xs font-medium tracking-wide text-ink-500 uppercase">{{ $t('village.title') }}</p> <p class="text-xs font-medium tracking-wide text-ink-500 uppercase">{{ $t('village.title') }}</p>
<h1 class="font-display text-xl font-semibold sm:text-2xl">{{ village.name }}</h1> <h1 class="font-display text-xl font-semibold sm:text-2xl">{{ village.name }}</h1>
<p class="mt-0.5 flex items-center gap-1 text-xs text-ink-500"> <NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: village.x, y: village.y } }"
class="mt-0.5 inline-flex items-center gap-1 text-xs text-ink-500 hover:text-amber-800 hover:underline"
:title="$t('village.on_map')"
>
<IconUi name="pin" class="size-3.5" /> <IconUi name="pin" class="size-3.5" />
{{ village.x }}|{{ village.y }} <span class="tabular-nums">{{ village.x }}|{{ village.y }} · {{ village.continent }}</span>
</p> <span>· {{ $t('village.points', { points: village.points }) }}</span>
</NuxtLink>
<div class="mt-2.5 sm:w-52"> <div class="mt-2.5 sm:w-52">
<div class="mb-1 flex justify-between gap-3 text-xs"> <div class="mb-1 flex justify-between gap-3 text-xs">
<span class="text-ink-500">{{ $t('village.diorama.space') }}</span> <span class="text-ink-500">{{ $t('village.diorama.space') }}</span>

View File

@ -48,6 +48,7 @@ async function resetMock() {
> >
{{ w.village_id ? $t('worlds.open') : $t('worlds.join') }} {{ w.village_id ? $t('worlds.open') : $t('worlds.join') }}
</button> </button>
<NuxtLink v-if="w.village_id" :to="`/map/${w.id}`" class="btn btn-outline">{{ $t('worlds.map') }}</NuxtLink>
</li> </li>
</ul> </ul>
</div> </div>

View File

@ -43,9 +43,14 @@ export interface WorldSummary {
/** `village.show`, and the reply to every `village.*` mutation. */ /** `village.show`, and the reply to every `village.*` mutation. */
export interface VillageState { export interface VillageState {
id: number id: number
world_id: number
name: string name: string
x: number x: number
y: number y: number
/** Map continent, e.g. K55 for 500|500. */
continent: string
/** Sum of the buildings' level points, as shown on the map. */
points: number
/** Building space: what buildings and queued new buildings take, out of the village maximum. */ /** Building space: what buildings and queued new buildings take, out of the village maximum. */
space: { used: number; max: number } space: { used: number; max: number }
/** Amounts stored at `resources_at`. */ /** Amounts stored at `resources_at`. */

View File

@ -2,6 +2,7 @@ export default {
app: { app: {
title: 'Village Politics', title: 'Village Politics',
worlds: 'Worlds', worlds: 'Worlds',
map: 'Map',
logout: 'Log out', logout: 'Log out',
mock: 'Mock server, no backend', mock: 'Mock server, no backend',
connected: 'Connected', connected: 'Connected',

View File

@ -4,6 +4,8 @@ export default {
en: { en: {
village: { village: {
title: 'Village', title: 'Village',
on_map: 'Show on the world map',
points: '{points} points',
resources: { resources: {
title: 'Resources', title: 'Resources',
cap: 'Storage cap', cap: 'Storage cap',

View File

@ -4,6 +4,7 @@ export default {
en: { en: {
worlds: { worlds: {
title: 'Worlds', title: 'Worlds',
map: 'Map',
subtitle: 'Each world has its own map, speed and players.', subtitle: 'Each world has its own map, speed and players.',
name: 'World', name: 'World',
speed: 'Speed', speed: 'Speed',