259 lines
7.3 KiB
Vue
259 lines
7.3 KiB
Vue
<script setup lang="ts">
|
|
import { deckValidation, loadDecks, type SavedDeck } from '~/utils/decks'
|
|
import { loadPlaymatChoice, type PlaymatChoice } from '~/utils/playmat'
|
|
|
|
interface MatchSummary {
|
|
id: string
|
|
hostName: string
|
|
status: 'lobby' | 'active' | 'finished'
|
|
playerCount: number
|
|
maxPlayers: number
|
|
players: string[]
|
|
createdAt: number
|
|
}
|
|
|
|
definePageMeta({ middleware: 'auth' })
|
|
|
|
const { public: { apiBase } } = useRuntimeConfig()
|
|
const SESSION_PREFIX = 'commander-session-'
|
|
|
|
const playerName = ref('')
|
|
const maxPlayers = ref(4)
|
|
const decks = ref<SavedDeck[]>([])
|
|
const selectedDeckId = ref('')
|
|
const games = ref<MatchSummary[]>([])
|
|
const loading = ref(false)
|
|
const starting = ref(false)
|
|
const error = ref('')
|
|
let poll: ReturnType<typeof setInterval> | undefined
|
|
|
|
const selectedDeck = computed(() => decks.value.find(deck => deck.id === selectedDeckId.value))
|
|
const deckErrors = computed(() => selectedDeck.value ? deckValidation(selectedDeck.value) : ['Choose a deck.'])
|
|
const canStart = computed(() => Boolean(playerName.value.trim() && selectedDeck.value && !deckErrors.value.length))
|
|
|
|
const playmat = ref<PlaymatChoice>()
|
|
|
|
function deckPayload(deck: SavedDeck) {
|
|
return {
|
|
commander: deck.commander.oracleId,
|
|
cards: deck.cards.map(card => card.oracleId),
|
|
}
|
|
}
|
|
|
|
async function loadGames() {
|
|
try {
|
|
const data = await $fetch<{ games: MatchSummary[] }>(`${apiBase}/games`)
|
|
games.value = data.games
|
|
} catch (caught) {
|
|
error.value = errorMessage(caught)
|
|
}
|
|
}
|
|
|
|
async function createGame() {
|
|
if (!canStart.value || !selectedDeck.value) return
|
|
starting.value = true
|
|
error.value = ''
|
|
try {
|
|
const data = await $fetch<{ matchId: string; playerId: string; token: string }>(`${apiBase}/games`, {
|
|
method: 'POST',
|
|
body: {
|
|
name: playerName.value,
|
|
maxPlayers: maxPlayers.value,
|
|
deck: deckPayload(selectedDeck.value),
|
|
playmat: playmat.value,
|
|
},
|
|
})
|
|
enterGame(data.matchId, data)
|
|
} catch (caught) {
|
|
error.value = errorMessage(caught)
|
|
} finally {
|
|
starting.value = false
|
|
}
|
|
}
|
|
|
|
async function joinGame(game: MatchSummary) {
|
|
if (!canStart.value || !selectedDeck.value) {
|
|
error.value = 'Choose a valid deck and enter your name first.'
|
|
return
|
|
}
|
|
starting.value = true
|
|
error.value = ''
|
|
try {
|
|
const data = await $fetch<{ matchId: string; playerId: string; token: string }>(
|
|
`${apiBase}/games/${game.id}/join`,
|
|
{
|
|
method: 'POST',
|
|
body: { name: playerName.value, deck: deckPayload(selectedDeck.value), playmat: playmat.value },
|
|
},
|
|
)
|
|
enterGame(data.matchId, data)
|
|
} catch (caught) {
|
|
error.value = errorMessage(caught)
|
|
} finally {
|
|
starting.value = false
|
|
}
|
|
}
|
|
|
|
function enterGame(matchId: string, data: { playerId: string; token: string }) {
|
|
sessionStorage.setItem(`${SESSION_PREFIX}${matchId}`, JSON.stringify(data))
|
|
void navigateTo(`/games/${matchId}`)
|
|
}
|
|
|
|
function errorMessage(caught: unknown): string {
|
|
const data = (caught as { data?: { error?: string } })?.data
|
|
return data?.error ?? (caught instanceof Error ? caught.message : String(caught))
|
|
}
|
|
|
|
onMounted(async () => {
|
|
playerName.value = localStorage.getItem('commander-player-name') ?? ''
|
|
playmat.value = await loadPlaymatChoice(apiBase)
|
|
decks.value = (await loadDecks(apiBase)).sort((a, b) => b.updatedAt - a.updatedAt)
|
|
selectedDeckId.value = decks.value.find(deck => !deckValidation(deck).length)?.id
|
|
?? decks.value[0]?.id
|
|
?? ''
|
|
void loadGames()
|
|
poll = setInterval(loadGames, 5000)
|
|
})
|
|
|
|
watch(playerName, value => {
|
|
if (typeof localStorage !== 'undefined') localStorage.setItem('commander-player-name', value)
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
if (poll) clearInterval(poll)
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<main class="page">
|
|
<header>
|
|
<h1>Play Commander</h1>
|
|
<p class="muted">
|
|
Four-player free-for-all with 40 life, command zones, commander tax, and
|
|
21 commander damage. Games live in the API process and disappear on restart.
|
|
</p>
|
|
</header>
|
|
|
|
<p v-if="error" class="error">{{ error }}</p>
|
|
|
|
<div class="home-grid">
|
|
<section class="panel">
|
|
<h2>Start or join</h2>
|
|
<label class="field">
|
|
<span class="muted">Your name</span>
|
|
<input v-model="playerName" maxlength="30" placeholder="Player">
|
|
</label>
|
|
<label class="field">
|
|
<span class="muted">Deck</span>
|
|
<select v-model="selectedDeckId">
|
|
<option v-for="deck in decks" :key="deck.id" :value="deck.id">
|
|
{{ deck.name }} ({{ deck.cards.length + 1 }})
|
|
</option>
|
|
</select>
|
|
</label>
|
|
<p v-if="!decks.length" class="warn">
|
|
No decks yet. <NuxtLink to="/decks">Create or import one</NuxtLink>.
|
|
</p>
|
|
<ul v-else-if="deckErrors.length" class="errors">
|
|
<li v-for="message in deckErrors.slice(0, 3)" :key="message" class="warn">{{ message }}</li>
|
|
</ul>
|
|
<label v-else class="field">
|
|
<span class="muted">Seats</span>
|
|
<select v-model.number="maxPlayers">
|
|
<option :value="2">2 players</option>
|
|
<option :value="3">3 players</option>
|
|
<option :value="4">4 players</option>
|
|
</select>
|
|
</label>
|
|
<button type="button" class="primary" :disabled="!canStart || starting" @click="createGame">
|
|
{{ starting ? 'Starting…' : 'Create game' }}
|
|
</button>
|
|
</section>
|
|
|
|
<section class="panel">
|
|
<div class="row">
|
|
<h2 class="grow">Open games</h2>
|
|
<button type="button" class="mini" @click="loadGames">Refresh</button>
|
|
</div>
|
|
<p v-if="!games.length" class="muted">No games yet. Create one and send the link to your friends.</p>
|
|
<ul class="games">
|
|
<li v-for="game in games" :key="game.id" class="game-row">
|
|
<div class="grow">
|
|
<div class="row wrap">
|
|
<strong>{{ game.hostName }}'s game</strong>
|
|
<span class="tag">{{ game.status }}</span>
|
|
<span class="tag">{{ game.playerCount }}/{{ game.maxPlayers }}</span>
|
|
</div>
|
|
<p class="muted">{{ game.players.join(', ') || 'Waiting for players' }}</p>
|
|
</div>
|
|
<button
|
|
v-if="game.status === 'lobby' && game.playerCount < game.maxPlayers"
|
|
type="button"
|
|
:disabled="!canStart || starting"
|
|
@click="joinGame(game)"
|
|
>Join</button>
|
|
<span v-else class="muted">{{ game.status === 'active' ? 'In progress' : 'Full' }}</span>
|
|
</li>
|
|
</ul>
|
|
</section>
|
|
</div>
|
|
</main>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.home-grid {
|
|
display: grid;
|
|
grid-template-columns: minmax(18rem, 24rem) 1fr;
|
|
gap: 1rem;
|
|
align-items: start;
|
|
}
|
|
|
|
.field {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.3rem;
|
|
}
|
|
|
|
.field select,
|
|
.field input {
|
|
width: 100%;
|
|
}
|
|
|
|
.errors {
|
|
list-style: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
.games {
|
|
list-style: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.game-row {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 1rem;
|
|
padding: 0.6rem 0;
|
|
border-top: 1px solid var(--line);
|
|
}
|
|
|
|
.game-row:first-child {
|
|
border-top: 0;
|
|
}
|
|
|
|
.mini {
|
|
font-size: 0.75rem;
|
|
padding: 0.2rem 0.45rem;
|
|
}
|
|
|
|
@media (max-width: 800px) {
|
|
.home-grid {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
</style>
|