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

287 lines
14 KiB
Vue

<script setup lang="ts">
import type { Pact, TribeRole } from '~/types/game'
definePageMeta({ layout: 'game' })
const route = useRoute()
const worldId = Number(route.params.worldId)
useCurrentWorld().enter(worldId)
const { page, tribes, error, busy, load, loadList, act } = useTribe(worldId)
const { t } = useI18n()
const clock = useNow()
load()
loadList()
const tribe = computed(() => page.value?.tribe ?? null)
const manages = computed(() => tribe.value?.role === 'leader' || tribe.value?.role === 'council')
const leads = computed(() => tribe.value?.role === 'leader')
const myId = computed(() => tribe.value?.player_id)
// Like the tribe screens of Die Stämme: the tribe and its news, members, invites, diplomacy,
// and every tribe of the world. Without a tribe: found one, the invites, the tribes.
type Tab = 'overview' | 'members' | 'invites' | 'diplomacy' | 'tribes'
const tab = ref<Tab>((route.query.tab as Tab) ?? 'overview')
const tabs = computed(() =>
(tribe.value ? (['overview', 'members', ...(manages.value ? ['invites'] : []), 'diplomacy', 'tribes'] as Tab[]) : (['overview', 'tribes'] as Tab[])).map((key) => ({
key,
label: t(`tribe.tabs.${key}`),
count: key === 'diplomacy' ? tribe.value?.relations.filter((r) => r.status === 'incoming').length : key === 'overview' && !tribe.value ? page.value?.invites.length : undefined,
})),
)
const found = reactive({ name: '', tag: '' })
const invitee = ref('')
const description = ref<string | null>(null)
const pact = reactive<{ tag: string; state: Pact | 'enemy' }>({ tag: '', state: 'ally' })
async function invite() {
if (await act('invite', { player: invitee.value })) invitee.value = ''
}
async function saveDescription() {
if (await act('describe', { description: description.value ?? '' })) description.value = null
}
async function offer() {
const ok = pact.state === 'enemy' ? await act('enemy', { tag: pact.tag }) : await act('propose', { tag: pact.tag, state: pact.state })
if (ok) pact.tag = ''
}
function leave() {
if (confirm(t(leads.value && tribe.value?.members.length === 1 ? 'tribe.confirm_last' : 'tribe.confirm_leave'))) act('leave')
}
function disband() {
if (confirm(t('tribe.confirm_disband'))) act('disband')
}
const canKick = (role: TribeRole, id: number) => id !== myId.value && (leads.value || (tribe.value?.role === 'council' && role === 'member'))
const secondsUntil = (iso: string) => Math.max(0, (Date.parse(iso) - clock.value) / 1000)
/** One line of the news, from its type and what happened. */
function event(e: { type: string; payload: Record<string, any> }) {
const p = { ...e.payload }
if (p.state) p.state = t(`tribe.states.${p.state}`)
if (p.role) p.role = t(`tribe.roles.${p.role}`)
if (p.between) p.between = p.between.join(' & ')
if (p.ends_at) p.ends_at = formatArrival(Date.parse(p.ends_at), clock.value, t)
return t(`tribe.events.${e.type}`, p)
}
const TONE: Record<string, string> = { ally: 'text-iron-700', nap: 'text-[#6b3f8f]', enemy: 'text-blood-700', tribe: 'text-iron-700', none: 'text-ink-500' }
</script>
<template>
<div class="space-y-2 p-2.5 text-sm">
<header class="flex flex-wrap items-baseline gap-x-3 gap-y-1">
<h1 class="font-display text-xl font-semibold">{{ tribe ? `${tribe.name} [${tribe.tag}]` : $t('tribe.none_title') }}</h1>
<span v-if="tribe" class="text-ink-500">{{ $t(`tribe.roles.${tribe.role}`) }} · {{ $t('tribe.member_count', { n: tribe.members.length, max: page?.max_members }) }}</span>
</header>
<UiSubTabs v-model="tab" :tabs="tabs" />
<p v-if="error" class="alert" role="alert">{{ error }}</p>
<p v-if="!page" class="py-6 text-center text-ink-500">{{ $t('common.loading') }}</p>
<!-- Without a tribe: found one, or take an invite. -->
<template v-else-if="tab === 'overview' && !tribe">
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.invites_received') }}</h2></div>
<table v-if="page.invites.length" class="grid-table">
<tbody>
<tr v-for="i in page.invites" :key="i.tribe.id">
<td class="font-semibold">{{ i.tribe.name }} [{{ i.tribe.tag }}]</td>
<td class="text-ink-500">{{ $t('tribe.members_n', { n: i.members }) }}</td>
<td class="text-right whitespace-nowrap">
<button type="button" class="btn btn-primary" :disabled="busy" @click="act('join', { tribe_id: i.tribe.id })">{{ $t('tribe.join') }}</button>
<button type="button" class="btn btn-outline ml-1" :disabled="busy" @click="act('refuse', { tribe_id: i.tribe.id })">{{ $t('tribe.refuse') }}</button>
</td>
</tr>
</tbody>
</table>
<p v-else class="panel-body text-ink-500">{{ $t('tribe.no_invites') }}</p>
</section>
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.found') }}</h2></div>
<form class="panel-body flex flex-wrap items-end gap-2" @submit.prevent="act('found', found)">
<label class="flex flex-col gap-0.5">
<span class="text-xs text-ink-500">{{ $t('tribe.name') }}</span>
<input v-model="found.name" class="input w-56" maxlength="32" required />
</label>
<label class="flex flex-col gap-0.5">
<span class="text-xs text-ink-500">{{ $t('tribe.tag') }}</span>
<input v-model="found.tag" class="input w-24" maxlength="6" required />
</label>
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('tribe.found_button') }}</button>
</form>
<p class="px-2.5 pb-2 text-xs text-ink-500">{{ $t('tribe.found_hint') }}</p>
</section>
</template>
<!-- The tribe: description, news, leaving. -->
<template v-else-if="tab === 'overview' && tribe">
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('tribe.about') }}</h2>
<button v-if="manages && description === null" type="button" class="btn btn-ghost" @click="description = tribe.description ?? ''">
<IconUi name="edit" class="size-4" />{{ $t('tribe.edit') }}
</button>
</div>
<form v-if="description !== null" class="panel-body space-y-2" @submit.prevent="saveDescription">
<textarea v-model="description" class="input h-28 w-full" maxlength="2000" />
<div class="flex gap-1">
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('tribe.save') }}</button>
<button type="button" class="btn btn-outline" @click="description = null">{{ $t('tribe.cancel') }}</button>
</div>
</form>
<p v-else class="panel-body whitespace-pre-line" :class="tribe.description ? '' : 'text-ink-500'">{{ tribe.description || $t('tribe.no_description') }}</p>
</section>
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.news') }}</h2></div>
<ul class="divide-y divide-parchment-300">
<li v-for="(e, i) in tribe.events" :key="i" class="flex gap-3 px-2.5 py-1">
<span class="w-40 shrink-0 text-ink-500 tabular-nums">{{ formatArrival(Date.parse(e.at), clock, t) }}</span>
<span>{{ event(e) }}</span>
</li>
</ul>
</section>
<div class="flex gap-2">
<button type="button" class="btn btn-outline" :disabled="busy" @click="leave">{{ $t('tribe.leave') }}</button>
<button v-if="leads" type="button" class="btn btn-attack" :disabled="busy" @click="disband">{{ $t('tribe.disband') }}</button>
</div>
</template>
<section v-else-if="tab === 'members' && tribe" class="panel">
<table class="grid-table">
<thead>
<tr>
<th>{{ $t('tribe.player') }}</th>
<th>{{ $t('tribe.role') }}</th>
<th class="text-right">{{ $t('tribe.points') }}</th>
<th class="text-right">{{ $t('tribe.villages') }}</th>
<th />
</tr>
</thead>
<tbody>
<tr v-for="m in tribe.members" :key="m.player.id">
<td class="font-semibold">{{ m.player.name }}</td>
<td>
<select
v-if="leads && m.player.id !== myId"
class="input w-auto py-0.5"
:value="m.role"
:disabled="busy"
@change="act('role', { player_id: m.player.id, role: ($event.target as HTMLSelectElement).value })"
>
<option v-for="r in ['leader', 'council', 'member']" :key="r" :value="r">{{ $t(`tribe.roles.${r}`) }}</option>
</select>
<span v-else>{{ $t(`tribe.roles.${m.role}`) }}</span>
</td>
<td class="text-right tabular-nums">{{ m.points }}</td>
<td class="text-right tabular-nums">{{ m.villages }}</td>
<td class="text-right">
<button v-if="canKick(m.role, m.player.id)" type="button" class="btn btn-outline py-0.5" :disabled="busy" @click="act('kick', { player_id: m.player.id })">
{{ $t('tribe.kick') }}
</button>
</td>
</tr>
</tbody>
</table>
</section>
<template v-else-if="tab === 'invites' && tribe">
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.invite') }}</h2></div>
<form class="panel-body flex gap-2" @submit.prevent="invite">
<input v-model="invitee" class="input w-56" :placeholder="$t('tribe.player_name')" required />
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('tribe.invite_button') }}</button>
</form>
</section>
<section class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.invites_sent') }}</h2></div>
<table v-if="tribe.invites.length" class="grid-table">
<tbody>
<tr v-for="i in tribe.invites" :key="i.player.id">
<td class="font-semibold">{{ i.player.name }}</td>
<td class="text-ink-500">{{ formatArrival(Date.parse(i.at), clock, t) }}</td>
<td class="text-right">
<button type="button" class="btn btn-outline py-0.5" :disabled="busy" @click="act('cancelInvite', { player_id: i.player.id })">{{ $t('tribe.withdraw') }}</button>
</td>
</tr>
</tbody>
</table>
<p v-else class="panel-body text-ink-500">{{ $t('tribe.no_invites_sent') }}</p>
</section>
</template>
<template v-else-if="tab === 'diplomacy' && tribe">
<section class="panel">
<table v-if="tribe.relations.length" class="grid-table">
<thead>
<tr>
<th>{{ $t('tribe.tribe') }}</th>
<th>{{ $t('tribe.relation') }}</th>
<th>{{ $t('tribe.status') }}</th>
<th />
</tr>
</thead>
<tbody>
<tr v-for="r in tribe.relations" :key="`${r.tribe.id}-${r.status}`">
<td class="font-semibold">{{ r.tribe.name }} [{{ r.tribe.tag }}]</td>
<td :class="TONE[r.state]">{{ $t(`tribe.states.${r.state}`) }}</td>
<td>
{{ $t(`tribe.status_${r.status}`) }}
<span v-if="r.ends_at" class="tabular-nums text-ink-500"> · {{ formatSpan(secondsUntil(r.ends_at), t) }}</span>
</td>
<td class="text-right whitespace-nowrap">
<template v-if="manages">
<button v-if="r.status === 'incoming'" type="button" class="btn btn-primary py-0.5" :disabled="busy" @click="act('accept', { tribe_id: r.tribe.id })">{{ $t('tribe.accept') }}</button>
<button
v-if="r.status !== 'hostile' && r.status !== 'ending'"
type="button"
class="btn btn-outline ml-1 py-0.5"
:disabled="busy"
@click="act('end', { tribe_id: r.tribe.id })"
>
{{ $t(`tribe.undo_${r.status}`) }}
</button>
</template>
</td>
</tr>
</tbody>
</table>
<p v-else class="panel-body text-ink-500">{{ $t('tribe.no_relations') }}</p>
</section>
<section v-if="manages" class="panel">
<div class="panel-head"><h2 class="panel-title">{{ $t('tribe.new_relation') }}</h2></div>
<form class="panel-body flex flex-wrap gap-2" @submit.prevent="offer">
<input v-model="pact.tag" class="input w-28" maxlength="6" :placeholder="$t('tribe.tag')" required />
<select v-model="pact.state" class="input w-auto">
<option value="ally">{{ $t('tribe.offer.ally') }}</option>
<option value="nap">{{ $t('tribe.offer.nap') }}</option>
<option value="enemy">{{ $t('tribe.offer.enemy') }}</option>
</select>
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('tribe.send') }}</button>
</form>
<p class="px-2.5 pb-2 text-xs text-ink-500">{{ $t('tribe.diplomacy_hint', { time: formatSpan(page.notice_seconds, t) }) }}</p>
</section>
</template>
<section v-else-if="tab === 'tribes'" class="panel">
<table v-if="tribes?.length" class="grid-table">
<thead>
<tr>
<th class="w-10 text-right">#</th>
<th>{{ $t('tribe.tribe') }}</th>
<th class="text-right">{{ $t('tribe.members') }}</th>
<th class="text-right">{{ $t('tribe.points') }}</th>
<th>{{ $t('tribe.relation') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(r, i) in tribes" :key="r.id" :class="r.relation === 'tribe' ? 'bg-gold-100 font-bold' : ''">
<td class="text-right tabular-nums">{{ i + 1 }}</td>
<td>{{ r.name }} [{{ r.tag }}]</td>
<td class="text-right tabular-nums">{{ r.members }}</td>
<td class="text-right tabular-nums">{{ r.points }}</td>
<td :class="TONE[r.relation]">{{ r.relation === 'none' ? '' : $t(`tribe.states.${r.relation}`) }}</td>
</tr>
</tbody>
</table>
<p v-else class="panel-body text-ink-500">{{ $t('tribe.no_tribes') }}</p>
</section>
</div>
</template>