Initial commit.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
# Copy to .env and adjust as needed. Real environment variables win over
|
||||
# values in this file.
|
||||
|
||||
# Directory holding the SQLite database (created if missing).
|
||||
DATA_DIR=data
|
||||
|
||||
# HTTP port for the server.
|
||||
PORT=8080
|
||||
|
||||
# Built frontend to serve. Leave as-is unless you move the build output.
|
||||
STATIC_DIR=web/dist
|
||||
@@ -0,0 +1,7 @@
|
||||
/bin/
|
||||
/data/
|
||||
.env
|
||||
|
||||
# frontend
|
||||
web/node_modules/
|
||||
web/dist/
|
||||
@@ -0,0 +1,77 @@
|
||||
# Super Auto Pets: The Board Game — Online
|
||||
|
||||
A web app for playing the Super Auto Pets board game remotely. 1v1 for now;
|
||||
the engine is built to grow to more players.
|
||||
|
||||
## Stack
|
||||
|
||||
- **Backend**: Go (stdlib-first). Pure game engine in `internal/game`,
|
||||
WebSocket sync via `coder/websocket`, persistence as JSON blobs in SQLite
|
||||
(`modernc.org/sqlite`, no cgo).
|
||||
- **Frontend**: React + Vite + TypeScript in `web/`. DOM/CSS card rendering
|
||||
and battle animations.
|
||||
- **Tasks**: [mise](https://mise.jdx.dev) (`mise.toml`).
|
||||
|
||||
## Quick start
|
||||
|
||||
```sh
|
||||
mise run install-web # once: npm install
|
||||
mise run serve # build frontend + server, run on :8080
|
||||
```
|
||||
|
||||
Open http://localhost:8080, host a game, and join from another browser (or
|
||||
incognito window) with the 5-letter code.
|
||||
|
||||
### Development
|
||||
|
||||
```sh
|
||||
mise run dev # Go server on :8080 + Vite hot reload on :5173
|
||||
mise run test # Go tests (game engine)
|
||||
mise run check # go vet + frontend type-check
|
||||
```
|
||||
|
||||
During development open the Vite URL (http://localhost:5173); it proxies
|
||||
`/api` (including the WebSocket) to the Go server.
|
||||
|
||||
## Configuration
|
||||
|
||||
Set via environment or a `.env` file (see `.env.example`):
|
||||
|
||||
| Variable | Default | Purpose |
|
||||
| ------------ | ---------- | ------------------------------------ |
|
||||
| `DATA_DIR` | `data` | Directory holding the SQLite DB |
|
||||
| `PORT` | `8080` | HTTP port |
|
||||
| `STATIC_DIR` | `web/dist` | Built frontend to serve |
|
||||
|
||||
## Rules implemented
|
||||
|
||||
Six rounds, each with its own shop tier deck. Per round:
|
||||
|
||||
1. **Shop** — each player has 3 coins and players alternate actions, 1 coin
|
||||
each: **buy** one of 4 face-up cards; **discard** any number of hand cards
|
||||
(each becomes an 🍎 apple, +1 power food); or **trade in** 3 same-suit pets
|
||||
to pick 1 of the top 2 cards of the next tier's deck (the other goes under
|
||||
that deck). Passing forfeits remaining coins.
|
||||
2. **Cleanup** — anyone holding more than 5 pets must discard down to 5
|
||||
(discards become apples).
|
||||
3. **Arrange** — players secretly order their decks. Food cards apply to the
|
||||
next pet after them; trailing foods are wasted.
|
||||
4. **Battle** — automatic. Front pets simultaneously deal their full power to
|
||||
each other as damage markers; a pet with markers ≥ power dies (attack
|
||||
power never drops while wounded). Last player with pets standing wins the
|
||||
round: 1 trophy for rounds 1–5, 2 trophies for round 6. Draws award
|
||||
nothing.
|
||||
|
||||
Most trophies after round 6 wins. Pet effects are not yet implemented (the
|
||||
card model carries an `effect` field for when they land).
|
||||
|
||||
## Layout
|
||||
|
||||
```
|
||||
cmd/server/ entrypoint
|
||||
internal/game/ rules engine (pure, fully tested)
|
||||
internal/server/ HTTP + WebSocket rooms
|
||||
internal/store/ SQLite persistence
|
||||
internal/env/ .env loading
|
||||
web/ React frontend
|
||||
```
|
||||
@@ -0,0 +1,61 @@
|
||||
// Command server runs the Super Auto Pets board game server: game API,
|
||||
// WebSocket sync, and the built web frontend.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/env"
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/server"
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := env.Load(".env"); err != nil {
|
||||
slog.Error("failed to read .env", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
dataDir := env.Get("DATA_DIR", "data")
|
||||
port := env.Get("PORT", "8080")
|
||||
staticDir := env.Get("STATIC_DIR", "web/dist")
|
||||
|
||||
st, err := store.Open(dataDir)
|
||||
if err != nil {
|
||||
slog.Error("failed to open store", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
if _, err := os.Stat(staticDir); err != nil {
|
||||
slog.Warn("static dir missing; only the API will be served", "dir", staticDir)
|
||||
staticDir = ""
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: server.New(st, staticDir).Handler(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
slog.Info("listening", "addr", "http://localhost:"+port, "dataDir", dataDir)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("server failed", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
<-stop
|
||||
slog.Info("shutting down")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
srv.Shutdown(ctx)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
module github.com/greyson/super-auto-pets-board-game
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/coder/websocket v1.8.15
|
||||
modernc.org/sqlite v1.54.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/sys v0.46.0 // indirect
|
||||
modernc.org/libc v1.74.1 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,53 @@
|
||||
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
||||
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
|
||||
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
|
||||
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
|
||||
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q=
|
||||
golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA=
|
||||
modernc.org/cc/v4 v4.29.0 h1:CXgwL8cvxmyzBQZzbSl/6xFtMCryb6u8IOqDci39cgc=
|
||||
modernc.org/cc/v4 v4.29.0/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
|
||||
modernc.org/ccgo/v4 v4.34.6 h1:sBgfIwyN0TQ9C5hwIeuqyeAKyMWnbvj2fvpF4L11uzU=
|
||||
modernc.org/ccgo/v4 v4.34.6/go.mod h1:SZ8YcN9NG7XVsQYdm6jYBvi8PQP1qi+kqB6OhjqI3Fk=
|
||||
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
|
||||
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/gc/v3 v3.1.4 h1:2g65LGVSmFQrXeITAw97x7hCRvZFcyE1uDP+7Vng7JI=
|
||||
modernc.org/gc/v3 v3.1.4/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.74.1 h1:bdR4VTKFMC4966QSNZ05XLGI/VwzVa2kTUX51Dm0riQ=
|
||||
modernc.org/libc v1.74.1/go.mod h1:uH4t5bOx3G3g9Xcmj10YKlTcVISlRDwv8VoQJG9n8Os=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
|
||||
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.54.0 h1:JCxR4qwkJvOaqAoYcgDoO25Nc+ROg6EJ2LfBVzdrgog=
|
||||
modernc.org/sqlite v1.54.0/go.mod h1:4ntCLuNmnH8+GNqjka1wNg7KJd5/Hi5FYp8K+XQ7GZw=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
// Package env provides .env file loading and typed lookups using only the
|
||||
// standard library.
|
||||
package env
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Load reads KEY=VALUE pairs from the given file into the process
|
||||
// environment. Existing environment variables win over file values. A
|
||||
// missing file is not an error, so .env stays optional.
|
||||
func Load(path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
key, value, ok := strings.Cut(line, "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key = strings.TrimSpace(strings.TrimPrefix(key, "export "))
|
||||
value = strings.TrimSpace(value)
|
||||
if len(value) >= 2 {
|
||||
if (value[0] == '"' && value[len(value)-1] == '"') ||
|
||||
(value[0] == '\'' && value[len(value)-1] == '\'') {
|
||||
value = value[1 : len(value)-1]
|
||||
}
|
||||
}
|
||||
if _, exists := os.LookupEnv(key); !exists {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
// Get returns the environment variable or a default.
|
||||
func Get(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package game
|
||||
|
||||
// BattleUnit is a pet on the battle line with its attached foods applied.
|
||||
// Power (attack) is unaffected by damage; a unit dies when Damage >= Power.
|
||||
type BattleUnit struct {
|
||||
Card Card `json:"card"`
|
||||
Foods []Card `json:"foods"`
|
||||
Bonus int `json:"bonus"` // total power added by foods
|
||||
Damage int `json:"damage"` // damage markers accumulated this battle
|
||||
}
|
||||
|
||||
func (u *BattleUnit) Power() int { return u.Card.Power + u.Bonus }
|
||||
func (u *BattleUnit) Alive() bool { return u.Damage < u.Power() }
|
||||
|
||||
// BattleEvent is one step of the battle, in order, for clients to animate.
|
||||
type BattleEvent struct {
|
||||
Type string `json:"type"` // "clash"
|
||||
// Indexes into the initial lineups (per seat) of the two front pets.
|
||||
Units []int `json:"units"` // one entry per seat
|
||||
// Damage each front pet has accumulated after the clash, per seat.
|
||||
Damage []int `json:"damage"`
|
||||
// Whether each front pet died in the clash, per seat.
|
||||
Died []bool `json:"died"`
|
||||
}
|
||||
|
||||
// BattleResult is the full, public record of one round's battle.
|
||||
type BattleResult struct {
|
||||
Round int `json:"round"`
|
||||
Lineups [][]BattleUnit `json:"lineups"` // initial lineups per seat
|
||||
WastedFood [][]Card `json:"wastedFoods"` // foods with no pet beneath them, per seat
|
||||
Events []BattleEvent `json:"events"`
|
||||
WinnerSeat int `json:"winnerSeat"` // -1 = draw
|
||||
Trophies int `json:"trophies"` // awarded to the winner
|
||||
}
|
||||
|
||||
// buildLineup walks a deck top-to-bottom, attaching each run of foods to the
|
||||
// next pet below it. Foods after the last pet affect nothing and are wasted.
|
||||
func buildLineup(deck []Card) (units []BattleUnit, wasted []Card) {
|
||||
var pendingFoods []Card
|
||||
for _, c := range deck {
|
||||
if c.IsFood() {
|
||||
pendingFoods = append(pendingFoods, c)
|
||||
continue
|
||||
}
|
||||
u := BattleUnit{Card: c, Foods: pendingFoods}
|
||||
for _, f := range pendingFoods {
|
||||
if f.Food == FoodApple {
|
||||
u.Bonus++
|
||||
}
|
||||
}
|
||||
pendingFoods = nil
|
||||
units = append(units, u)
|
||||
}
|
||||
return units, pendingFoods
|
||||
}
|
||||
|
||||
// resolveBattle simulates the battle from the players' arranged decks,
|
||||
// records the event log, awards trophies, and moves to PhaseBattle.
|
||||
//
|
||||
// Combat: the two front pets deal their full Power to each other
|
||||
// simultaneously as damage markers. A pet with Damage >= Power dies. Since
|
||||
// remaining health never exceeds Power, at least one pet dies every clash,
|
||||
// so the loop always terminates (until effects say otherwise).
|
||||
func (g *Game) resolveBattle() {
|
||||
res := &BattleResult{
|
||||
Round: g.Round,
|
||||
Lineups: make([][]BattleUnit, len(g.Players)),
|
||||
WastedFood: make([][]Card, len(g.Players)),
|
||||
WinnerSeat: -1,
|
||||
}
|
||||
live := make([][]BattleUnit, len(g.Players)) // working copies
|
||||
front := make([]int, len(g.Players)) // index of each seat's front pet
|
||||
for _, p := range g.Players {
|
||||
units, wasted := buildLineup(p.Deck)
|
||||
res.Lineups[p.Seat] = units
|
||||
res.WastedFood[p.Seat] = wasted
|
||||
live[p.Seat] = append([]BattleUnit(nil), units...)
|
||||
}
|
||||
|
||||
// Two-player combat. Effects and >2 player battle formats come later;
|
||||
// the surrounding state (lineups, events) is already per-seat.
|
||||
a, b := 0, 1
|
||||
for front[a] < len(live[a]) && front[b] < len(live[b]) {
|
||||
ua, ub := &live[a][front[a]], &live[b][front[b]]
|
||||
ua.Damage += ub.Power()
|
||||
ub.Damage += ua.Power()
|
||||
ev := BattleEvent{
|
||||
Type: "clash",
|
||||
Units: []int{front[a], front[b]},
|
||||
Damage: []int{ua.Damage, ub.Damage},
|
||||
Died: []bool{!ua.Alive(), !ub.Alive()},
|
||||
}
|
||||
res.Events = append(res.Events, ev)
|
||||
if !ua.Alive() {
|
||||
front[a]++
|
||||
}
|
||||
if !ub.Alive() {
|
||||
front[b]++
|
||||
}
|
||||
}
|
||||
|
||||
trophies := 1
|
||||
if g.Round == MaxRounds {
|
||||
trophies = 2
|
||||
}
|
||||
switch {
|
||||
case front[a] < len(live[a]):
|
||||
res.WinnerSeat = a
|
||||
case front[b] < len(live[b]):
|
||||
res.WinnerSeat = b
|
||||
}
|
||||
if res.WinnerSeat >= 0 {
|
||||
res.Trophies = trophies
|
||||
g.Players[res.WinnerSeat].Trophies += trophies
|
||||
}
|
||||
|
||||
g.Battle = res
|
||||
g.Phase = PhaseBattle
|
||||
for _, p := range g.Players {
|
||||
p.Ready = false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package game
|
||||
|
||||
import "testing"
|
||||
|
||||
// testGame builds a started 2-player game without going through the lobby.
|
||||
func testGame(t *testing.T) (*Game, *Player, *Player) {
|
||||
t.Helper()
|
||||
g := New()
|
||||
p1, err := g.AddPlayer("Alice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p2, err := g.AddPlayer("Bob")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g.Phase != PhaseShop {
|
||||
t.Fatalf("expected shop phase after both players join, got %s", g.Phase)
|
||||
}
|
||||
return g, p1, p2
|
||||
}
|
||||
|
||||
func (g *Game) pet(name string, power int) Card {
|
||||
return Card{ID: g.newCardID(), Kind: KindPet, Name: name, Tier: 1, Power: power, Suit: SuitSun}
|
||||
}
|
||||
|
||||
// forceBattle sets both decks, arranges them in current order, and resolves.
|
||||
func forceBattle(t *testing.T, g *Game, d1, d2 []Card) *BattleResult {
|
||||
t.Helper()
|
||||
g.Players[0].Deck = d1
|
||||
g.Players[1].Deck = d2
|
||||
g.Phase = PhaseArrange
|
||||
g.Players[0].Ready = false
|
||||
g.Players[1].Ready = false
|
||||
for _, p := range g.Players {
|
||||
ids := make([]string, len(p.Deck))
|
||||
for i, c := range p.Deck {
|
||||
ids[i] = c.ID
|
||||
}
|
||||
if err := g.SubmitOrder(p.ID, ids); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if g.Phase != PhaseBattle {
|
||||
t.Fatalf("expected battle phase, got %s", g.Phase)
|
||||
}
|
||||
return g.Battle
|
||||
}
|
||||
|
||||
// The worked example from the rules: a 3-power pet fights a 5-power pet. The
|
||||
// 3 dies, the 5 survives with 3 damage markers (2 health left, 5 attack).
|
||||
// Then a 2-power pet trades with it: both die.
|
||||
func TestBattleDamageMarkers(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
res := forceBattle(t, g,
|
||||
[]Card{g.pet("Three", 3), g.pet("Two", 2)},
|
||||
[]Card{g.pet("Five", 5)},
|
||||
)
|
||||
if len(res.Events) != 2 {
|
||||
t.Fatalf("expected 2 clashes, got %d", len(res.Events))
|
||||
}
|
||||
first := res.Events[0]
|
||||
if !first.Died[0] || first.Died[1] {
|
||||
t.Fatalf("first clash: 3-power should die, 5-power should survive: %+v", first)
|
||||
}
|
||||
if first.Damage[1] != 3 {
|
||||
t.Fatalf("5-power pet should carry 3 damage, has %d", first.Damage[1])
|
||||
}
|
||||
second := res.Events[1]
|
||||
if !second.Died[0] || !second.Died[1] {
|
||||
t.Fatalf("second clash: both should die (5 attack kills the 2; 3+2 damage kills the 5): %+v", second)
|
||||
}
|
||||
if res.WinnerSeat != -1 {
|
||||
t.Fatalf("battle should be a draw, winner=%d", res.WinnerSeat)
|
||||
}
|
||||
if g.Players[0].Trophies != 0 || g.Players[1].Trophies != 0 {
|
||||
t.Fatal("no trophies on a draw")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBattleEqualPowerBothDie(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
res := forceBattle(t, g,
|
||||
[]Card{g.pet("A", 4)},
|
||||
[]Card{g.pet("B", 4)},
|
||||
)
|
||||
ev := res.Events[0]
|
||||
if !ev.Died[0] || !ev.Died[1] {
|
||||
t.Fatalf("equal power pets should both die: %+v", ev)
|
||||
}
|
||||
if res.WinnerSeat != -1 {
|
||||
t.Fatal("expected a draw")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBattleWinnerGetsTrophy(t *testing.T) {
|
||||
g, _, p2 := testGame(t)
|
||||
res := forceBattle(t, g,
|
||||
[]Card{g.pet("Small", 1)},
|
||||
[]Card{g.pet("Big", 5)},
|
||||
)
|
||||
if res.WinnerSeat != 1 {
|
||||
t.Fatalf("seat 1 should win, got %d", res.WinnerSeat)
|
||||
}
|
||||
if res.Trophies != 1 || p2.Trophies != 1 {
|
||||
t.Fatalf("round 1 win should award 1 trophy, got %d/%d", res.Trophies, p2.Trophies)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBattleFinalRoundWorthTwoTrophies(t *testing.T) {
|
||||
g, _, p2 := testGame(t)
|
||||
g.Round = MaxRounds
|
||||
res := forceBattle(t, g,
|
||||
[]Card{g.pet("Small", 1)},
|
||||
[]Card{g.pet("Big", 5)},
|
||||
)
|
||||
if res.Trophies != 2 || p2.Trophies != 2 {
|
||||
t.Fatalf("final round win should award 2 trophies, got %d/%d", res.Trophies, p2.Trophies)
|
||||
}
|
||||
}
|
||||
|
||||
// Foods stack onto the next pet beneath them; each apple adds 1 power.
|
||||
// Trailing foods with no pet under them are wasted.
|
||||
func TestBattleApplesBuffNextPet(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
apple1, apple2, apple3 := g.newApple(), g.newApple(), g.newApple()
|
||||
res := forceBattle(t, g,
|
||||
[]Card{apple1, apple2, g.pet("Buffed", 3), apple3}, // 3+2=5 power; apple3 wasted
|
||||
[]Card{g.pet("Enemy", 5)},
|
||||
)
|
||||
u := res.Lineups[0][0]
|
||||
if u.Bonus != 2 || u.Power() != 5 {
|
||||
t.Fatalf("expected 2 apples for 5 total power, got bonus=%d power=%d", u.Bonus, u.Power())
|
||||
}
|
||||
if len(res.WastedFood[0]) != 1 || res.WastedFood[0][0].ID != apple3.ID {
|
||||
t.Fatalf("trailing apple should be wasted: %+v", res.WastedFood[0])
|
||||
}
|
||||
if res.WinnerSeat != -1 {
|
||||
t.Fatal("5 vs 5 should draw")
|
||||
}
|
||||
}
|
||||
|
||||
// A player with an empty lineup loses immediately with zero clashes.
|
||||
func TestBattleEmptyLineupLoses(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
res := forceBattle(t, g,
|
||||
[]Card{g.pet("Solo", 1)},
|
||||
[]Card{g.newApple()}, // food only, no pets
|
||||
)
|
||||
if len(res.Events) != 0 {
|
||||
t.Fatalf("expected no clashes, got %d", len(res.Events))
|
||||
}
|
||||
if res.WinnerSeat != 0 {
|
||||
t.Fatalf("seat 0 should win by default, got %d", res.WinnerSeat)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package game
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Suit is the trade-in symbol printed on pet cards. Three pets of the same
|
||||
// suit can be traded for a pick of the next tier's deck.
|
||||
type Suit string
|
||||
|
||||
const (
|
||||
SuitSun Suit = "sun"
|
||||
SuitMoon Suit = "moon"
|
||||
SuitStar Suit = "star"
|
||||
SuitLeaf Suit = "leaf"
|
||||
)
|
||||
|
||||
var suits = []Suit{SuitSun, SuitMoon, SuitStar, SuitLeaf}
|
||||
|
||||
// CardKind distinguishes pets from foods.
|
||||
type CardKind string
|
||||
|
||||
const (
|
||||
KindPet CardKind = "pet"
|
||||
KindFood CardKind = "food"
|
||||
)
|
||||
|
||||
// Food identifiers. Only apples exist for now; more foods come later.
|
||||
const FoodApple = "apple"
|
||||
|
||||
// Card is a single physical card instance. IDs are unique per game.
|
||||
type Card struct {
|
||||
ID string `json:"id"`
|
||||
Kind CardKind `json:"kind"`
|
||||
Name string `json:"name"`
|
||||
Tier int `json:"tier"`
|
||||
Power int `json:"power,omitempty"`
|
||||
Suit Suit `json:"suit,omitempty"`
|
||||
// Effect is display text for the pet's ability. Effects are not yet
|
||||
// implemented mechanically; the field keeps card data forward-compatible.
|
||||
Effect string `json:"effect,omitempty"`
|
||||
Food string `json:"food,omitempty"`
|
||||
}
|
||||
|
||||
func (c Card) IsPet() bool { return c.Kind == KindPet }
|
||||
func (c Card) IsFood() bool { return c.Kind == KindFood }
|
||||
|
||||
// petTemplate is the printed definition of a pet; each template appears as
|
||||
// multiple card copies in its tier's shop deck.
|
||||
type petTemplate struct {
|
||||
Name string
|
||||
Power int
|
||||
}
|
||||
|
||||
const copiesPerPet = 2
|
||||
|
||||
// petTiers defines the shop decks. Index 0 is tier 1 (round 1) through
|
||||
// index 5 for tier 6 (round 6). Suits are assigned round-robin per tier so
|
||||
// every tier contains every suit.
|
||||
var petTiers = [MaxRounds][]petTemplate{
|
||||
{ // Tier 1
|
||||
{"Ant", 1}, {"Cricket", 1}, {"Fish", 2}, {"Horse", 1},
|
||||
{"Beaver", 2}, {"Otter", 1}, {"Pig", 3}, {"Mosquito", 2},
|
||||
},
|
||||
{ // Tier 2
|
||||
{"Crab", 3}, {"Swan", 2}, {"Hedgehog", 3}, {"Peacock", 4},
|
||||
{"Flamingo", 3}, {"Rat", 2}, {"Shrimp", 2}, {"Spider", 3},
|
||||
},
|
||||
{ // Tier 3
|
||||
{"Dog", 4}, {"Badger", 4}, {"Camel", 3}, {"Giraffe", 3},
|
||||
{"Kangaroo", 4}, {"Ox", 5}, {"Rabbit", 3}, {"Sheep", 4},
|
||||
},
|
||||
{ // Tier 4
|
||||
{"Skunk", 5}, {"Hippo", 6}, {"Bison", 6}, {"Deer", 4},
|
||||
{"Squirrel", 4}, {"Whale", 5}, {"Worm", 4}, {"Penguin", 5},
|
||||
},
|
||||
{ // Tier 5
|
||||
{"Scorpion", 5}, {"Rhino", 7}, {"Monkey", 6}, {"Cow", 6},
|
||||
{"Seal", 6}, {"Shark", 7}, {"Turkey", 5}, {"Crocodile", 8},
|
||||
},
|
||||
{ // Tier 6
|
||||
{"Leopard", 8}, {"Boar", 9}, {"Fly", 7}, {"Gorilla", 9},
|
||||
{"Mammoth", 10}, {"Snake", 8}, {"Tiger", 9}, {"Dragon", 10},
|
||||
},
|
||||
}
|
||||
|
||||
// newCardID mints a unique card ID within the game.
|
||||
func (g *Game) newCardID() string {
|
||||
g.NextCardID++
|
||||
return fmt.Sprintf("c%d", g.NextCardID)
|
||||
}
|
||||
|
||||
// buildShopDecks creates all six tier decks (unshuffled).
|
||||
func (g *Game) buildShopDecks() {
|
||||
g.ShopDecks = make([][]Card, MaxRounds)
|
||||
for tierIdx, templates := range petTiers {
|
||||
deck := make([]Card, 0, len(templates)*copiesPerPet)
|
||||
for i, t := range templates {
|
||||
for range copiesPerPet {
|
||||
deck = append(deck, Card{
|
||||
ID: g.newCardID(),
|
||||
Kind: KindPet,
|
||||
Name: t.Name,
|
||||
Tier: tierIdx + 1,
|
||||
Power: t.Power,
|
||||
Suit: suits[i%len(suits)],
|
||||
})
|
||||
}
|
||||
}
|
||||
g.ShopDecks[tierIdx] = deck
|
||||
}
|
||||
}
|
||||
|
||||
// newApple mints an apple food card (from discarding, etc.).
|
||||
func (g *Game) newApple() Card {
|
||||
return Card{
|
||||
ID: g.newCardID(),
|
||||
Kind: KindFood,
|
||||
Name: "Apple",
|
||||
Food: FoodApple,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,533 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// Tunable rules. The engine supports any player count >= 2; MinPlayers /
|
||||
// MaxPlayers gate when a lobby can start (2 for now, more later).
|
||||
const (
|
||||
MaxRounds = 6
|
||||
CoinsPerRound = 3
|
||||
ShopRowSize = 4
|
||||
MaxPets = 5
|
||||
TradeInCount = 3
|
||||
MinPlayers = 2
|
||||
MaxPlayers = 2
|
||||
)
|
||||
|
||||
// Phase is the game's top-level state.
|
||||
type Phase string
|
||||
|
||||
const (
|
||||
PhaseLobby Phase = "lobby" // waiting for players
|
||||
PhaseShop Phase = "shop" // players take turns spending coins
|
||||
PhaseCleanup Phase = "cleanup" // forced discard down to MaxPets pets
|
||||
PhaseArrange Phase = "arrange" // players order their decks for battle
|
||||
PhaseBattle Phase = "battle" // battle resolved; players review the log
|
||||
PhaseGameOver Phase = "gameover" // all rounds played
|
||||
)
|
||||
|
||||
// Player holds everything about one seat. All fields are exported so a Game
|
||||
// serializes to JSON for persistence.
|
||||
type Player struct {
|
||||
ID string `json:"id"`
|
||||
Token string `json:"token"` // secret; never sent in views
|
||||
Name string `json:"name"`
|
||||
Seat int `json:"seat"`
|
||||
Coins int `json:"coins"`
|
||||
Deck []Card `json:"deck"`
|
||||
Trophies int `json:"trophies"`
|
||||
Ready bool `json:"ready"` // arrange submitted / battle acknowledged
|
||||
Connected bool `json:"connected"`
|
||||
}
|
||||
|
||||
// PetCount counts pet cards in the player's deck.
|
||||
func (p *Player) PetCount() int {
|
||||
n := 0
|
||||
for _, c := range p.Deck {
|
||||
if c.IsPet() {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (p *Player) cardIndex(cardID string) int {
|
||||
return slices.IndexFunc(p.Deck, func(c Card) bool { return c.ID == cardID })
|
||||
}
|
||||
|
||||
// PendingTrade is an in-progress trade-in: the trading player has paid and
|
||||
// must now pick one of two revealed cards from the next tier's deck.
|
||||
type PendingTrade struct {
|
||||
PlayerID string `json:"playerId"`
|
||||
Tier int `json:"tier"` // 1-based tier the options came from
|
||||
Options [2]Card `json:"options"`
|
||||
}
|
||||
|
||||
// Game is the complete authoritative state. It is a pure state machine: no
|
||||
// goroutines, no clocks, no I/O. Callers are responsible for locking.
|
||||
type Game struct {
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Phase Phase `json:"phase"`
|
||||
Round int `json:"round"` // 1-based
|
||||
Players []*Player `json:"players"`
|
||||
ShopDecks [][]Card `json:"shopDecks"` // index 0 = tier 1
|
||||
ShopRow []Card `json:"shopRow"` // empty ID = empty slot
|
||||
Turn int `json:"turn"` // seat with the current shop turn
|
||||
Pending *PendingTrade `json:"pending,omitempty"`
|
||||
Battle *BattleResult `json:"battle,omitempty"` // most recent battle
|
||||
NextCardID int `json:"nextCardId"`
|
||||
WinnerSeat int `json:"winnerSeat"` // set at gameover; -1 = tie
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotYourTurn = errors.New("not your turn")
|
||||
ErrWrongPhase = errors.New("action not allowed in this phase")
|
||||
ErrNoCoins = errors.New("no coins remaining")
|
||||
ErrInvalidAction = errors.New("invalid action")
|
||||
)
|
||||
|
||||
func randomID(n int) string {
|
||||
b := make([]byte, n)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func randomCode() string {
|
||||
const letters = "ABCDEFGHJKMNPQRSTUVWXYZ23456789" // no easily-confused chars
|
||||
code := make([]byte, 5)
|
||||
for i := range code {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
code[i] = letters[n.Int64()]
|
||||
}
|
||||
return string(code)
|
||||
}
|
||||
|
||||
func randInt(n int) int {
|
||||
v, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return int(v.Int64())
|
||||
}
|
||||
|
||||
func shuffle[T any](s []T) {
|
||||
for i := len(s) - 1; i > 0; i-- {
|
||||
j := randInt(i + 1)
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a game in the lobby phase with its shop decks built and
|
||||
// shuffled. All later "randomness" is just drawing from these decks, so the
|
||||
// state is fully deterministic (and serializable) after this point.
|
||||
func New() *Game {
|
||||
g := &Game{
|
||||
ID: randomID(16),
|
||||
Code: randomCode(),
|
||||
Phase: PhaseLobby,
|
||||
WinnerSeat: -1,
|
||||
}
|
||||
g.buildShopDecks()
|
||||
for i := range g.ShopDecks {
|
||||
shuffle(g.ShopDecks[i])
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
// AddPlayer seats a new player during the lobby phase and returns them (with
|
||||
// their secret token). The game starts automatically once full.
|
||||
func (g *Game) AddPlayer(name string) (*Player, error) {
|
||||
if g.Phase != PhaseLobby {
|
||||
return nil, fmt.Errorf("%w: game already started", ErrWrongPhase)
|
||||
}
|
||||
if len(g.Players) >= MaxPlayers {
|
||||
return nil, errors.New("game is full")
|
||||
}
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("Player %d", len(g.Players)+1)
|
||||
}
|
||||
p := &Player{
|
||||
ID: randomID(8),
|
||||
Token: randomID(16),
|
||||
Name: name,
|
||||
Seat: len(g.Players),
|
||||
}
|
||||
g.Players = append(g.Players, p)
|
||||
if len(g.Players) == MaxPlayers {
|
||||
g.start()
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// PlayerByID returns the player, or nil.
|
||||
func (g *Game) PlayerByID(id string) *Player {
|
||||
for _, p := range g.Players {
|
||||
if p.ID == id {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) start() {
|
||||
g.Round = 1
|
||||
g.startShopRound()
|
||||
}
|
||||
|
||||
// startShopRound resets coins, deals the shop row from this round's tier
|
||||
// deck, and rotates the starting player.
|
||||
func (g *Game) startShopRound() {
|
||||
g.Phase = PhaseShop
|
||||
g.Pending = nil
|
||||
for _, p := range g.Players {
|
||||
p.Coins = CoinsPerRound
|
||||
p.Ready = false
|
||||
}
|
||||
g.ShopRow = make([]Card, ShopRowSize)
|
||||
for i := range g.ShopRow {
|
||||
g.ShopRow[i] = g.drawFromTier(g.Round)
|
||||
}
|
||||
g.Turn = (g.Round - 1) % len(g.Players)
|
||||
}
|
||||
|
||||
// drawFromTier pops the top card of the given tier's deck (1-based tier).
|
||||
// Returns a zero Card if the deck is empty.
|
||||
func (g *Game) drawFromTier(tier int) Card {
|
||||
deck := g.ShopDecks[tier-1]
|
||||
if len(deck) == 0 {
|
||||
return Card{}
|
||||
}
|
||||
top := deck[0]
|
||||
g.ShopDecks[tier-1] = deck[1:]
|
||||
return top
|
||||
}
|
||||
|
||||
// requireShopTurn validates that playerID may act in the shop right now.
|
||||
func (g *Game) requireShopTurn(playerID string) (*Player, error) {
|
||||
if g.Phase != PhaseShop {
|
||||
return nil, ErrWrongPhase
|
||||
}
|
||||
p := g.PlayerByID(playerID)
|
||||
if p == nil {
|
||||
return nil, errors.New("unknown player")
|
||||
}
|
||||
if g.Players[g.Turn].ID != playerID {
|
||||
return nil, ErrNotYourTurn
|
||||
}
|
||||
if g.Pending != nil {
|
||||
return nil, fmt.Errorf("%w: finish your trade first", ErrInvalidAction)
|
||||
}
|
||||
if p.Coins <= 0 {
|
||||
return nil, ErrNoCoins
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Buy spends one coin to take the card at rowIdx into the player's deck.
|
||||
// The slot refills from the current round's tier deck.
|
||||
func (g *Game) Buy(playerID string, rowIdx int) error {
|
||||
p, err := g.requireShopTurn(playerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowIdx < 0 || rowIdx >= len(g.ShopRow) || g.ShopRow[rowIdx].ID == "" {
|
||||
return fmt.Errorf("%w: no card in that shop slot", ErrInvalidAction)
|
||||
}
|
||||
p.Coins--
|
||||
p.Deck = append(p.Deck, g.ShopRow[rowIdx])
|
||||
g.ShopRow[rowIdx] = g.drawFromTier(g.Round)
|
||||
g.advanceShopTurn()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Discard spends one coin to convert any number (>=1) of the player's cards
|
||||
// into that many apples.
|
||||
func (g *Game) Discard(playerID string, cardIDs []string) error {
|
||||
p, err := g.requireShopTurn(playerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(cardIDs) == 0 {
|
||||
return fmt.Errorf("%w: choose at least one card to discard", ErrInvalidAction)
|
||||
}
|
||||
if err := g.convertToApples(p, cardIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
p.Coins--
|
||||
g.advanceShopTurn()
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertToApples removes the given cards from p's deck and adds one apple
|
||||
// per removed card. It validates before mutating.
|
||||
func (g *Game) convertToApples(p *Player, cardIDs []string) error {
|
||||
if hasDuplicates(cardIDs) {
|
||||
return fmt.Errorf("%w: duplicate card", ErrInvalidAction)
|
||||
}
|
||||
for _, id := range cardIDs {
|
||||
if p.cardIndex(id) < 0 {
|
||||
return fmt.Errorf("%w: card not in your deck", ErrInvalidAction)
|
||||
}
|
||||
}
|
||||
for _, id := range cardIDs {
|
||||
p.Deck = slices.Delete(p.Deck, p.cardIndex(id), p.cardIndex(id)+1)
|
||||
}
|
||||
for range cardIDs {
|
||||
p.Deck = append(p.Deck, g.newApple())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TradeStart spends one coin and three same-suit pets from the player's deck
|
||||
// to reveal the top two cards of the next tier's deck. The player must then
|
||||
// call TradeChoose before anything else happens.
|
||||
func (g *Game) TradeStart(playerID string, cardIDs []string) error {
|
||||
p, err := g.requireShopTurn(playerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if g.Round >= MaxRounds {
|
||||
return fmt.Errorf("%w: no higher tier to trade into", ErrInvalidAction)
|
||||
}
|
||||
if len(cardIDs) != TradeInCount || hasDuplicates(cardIDs) {
|
||||
return fmt.Errorf("%w: trade in exactly %d cards", ErrInvalidAction, TradeInCount)
|
||||
}
|
||||
var suit Suit
|
||||
for i, id := range cardIDs {
|
||||
idx := p.cardIndex(id)
|
||||
if idx < 0 {
|
||||
return fmt.Errorf("%w: card not in your deck", ErrInvalidAction)
|
||||
}
|
||||
c := p.Deck[idx]
|
||||
if !c.IsPet() {
|
||||
return fmt.Errorf("%w: only pets have suits", ErrInvalidAction)
|
||||
}
|
||||
if i == 0 {
|
||||
suit = c.Suit
|
||||
} else if c.Suit != suit {
|
||||
return fmt.Errorf("%w: cards must share a suit", ErrInvalidAction)
|
||||
}
|
||||
}
|
||||
nextTier := g.Round + 1
|
||||
if len(g.ShopDecks[nextTier-1]) < 2 {
|
||||
return fmt.Errorf("%w: next tier deck is exhausted", ErrInvalidAction)
|
||||
}
|
||||
// Validated; commit.
|
||||
for _, id := range cardIDs {
|
||||
p.Deck = slices.Delete(p.Deck, p.cardIndex(id), p.cardIndex(id)+1)
|
||||
}
|
||||
p.Coins--
|
||||
g.Pending = &PendingTrade{
|
||||
PlayerID: playerID,
|
||||
Tier: nextTier,
|
||||
Options: [2]Card{g.drawFromTier(nextTier), g.drawFromTier(nextTier)},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TradeChoose resolves a pending trade: pick (0 or 1) joins the player's
|
||||
// deck, the other goes to the bottom of its tier deck.
|
||||
func (g *Game) TradeChoose(playerID string, pick int) error {
|
||||
if g.Phase != PhaseShop || g.Pending == nil || g.Pending.PlayerID != playerID {
|
||||
return fmt.Errorf("%w: no trade waiting on you", ErrInvalidAction)
|
||||
}
|
||||
if pick != 0 && pick != 1 {
|
||||
return fmt.Errorf("%w: pick 0 or 1", ErrInvalidAction)
|
||||
}
|
||||
p := g.PlayerByID(playerID)
|
||||
chosen, other := g.Pending.Options[pick], g.Pending.Options[1-pick]
|
||||
p.Deck = append(p.Deck, chosen)
|
||||
tierIdx := g.Pending.Tier - 1
|
||||
g.ShopDecks[tierIdx] = append(g.ShopDecks[tierIdx], other)
|
||||
g.Pending = nil
|
||||
g.advanceShopTurn()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pass forfeits the player's remaining coins and ends their shopping.
|
||||
func (g *Game) Pass(playerID string) error {
|
||||
p, err := g.requireShopTurn(playerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Coins = 0
|
||||
g.advanceShopTurn()
|
||||
return nil
|
||||
}
|
||||
|
||||
// advanceShopTurn hands the turn to the next player who still has coins, or
|
||||
// moves the game onward when everyone is spent.
|
||||
func (g *Game) advanceShopTurn() {
|
||||
for i := 1; i <= len(g.Players); i++ {
|
||||
seat := (g.Turn + i) % len(g.Players)
|
||||
if g.Players[seat].Coins > 0 {
|
||||
g.Turn = seat
|
||||
return
|
||||
}
|
||||
}
|
||||
g.endShop()
|
||||
}
|
||||
|
||||
// endShop moves to forced discard if anyone is over the pet limit, otherwise
|
||||
// straight to arranging.
|
||||
func (g *Game) endShop() {
|
||||
over := false
|
||||
for _, p := range g.Players {
|
||||
p.Ready = p.PetCount() <= MaxPets
|
||||
if !p.Ready {
|
||||
over = true
|
||||
}
|
||||
}
|
||||
if over {
|
||||
g.Phase = PhaseCleanup
|
||||
return
|
||||
}
|
||||
g.beginArrange()
|
||||
}
|
||||
|
||||
// CleanupDiscard performs the forced end-of-shop discard: the player must
|
||||
// convert exactly their excess pets into apples.
|
||||
func (g *Game) CleanupDiscard(playerID string, cardIDs []string) error {
|
||||
if g.Phase != PhaseCleanup {
|
||||
return ErrWrongPhase
|
||||
}
|
||||
p := g.PlayerByID(playerID)
|
||||
if p == nil {
|
||||
return errors.New("unknown player")
|
||||
}
|
||||
excess := p.PetCount() - MaxPets
|
||||
if excess <= 0 {
|
||||
return fmt.Errorf("%w: you are not over the pet limit", ErrInvalidAction)
|
||||
}
|
||||
if len(cardIDs) != excess {
|
||||
return fmt.Errorf("%w: discard exactly %d pets", ErrInvalidAction, excess)
|
||||
}
|
||||
for _, id := range cardIDs {
|
||||
idx := p.cardIndex(id)
|
||||
if idx < 0 || !p.Deck[idx].IsPet() {
|
||||
return fmt.Errorf("%w: pick pets from your deck", ErrInvalidAction)
|
||||
}
|
||||
}
|
||||
if err := g.convertToApples(p, cardIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
p.Ready = true
|
||||
if g.allReady() {
|
||||
g.beginArrange()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) allReady() bool {
|
||||
for _, p := range g.Players {
|
||||
if !p.Ready {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *Game) beginArrange() {
|
||||
g.Phase = PhaseArrange
|
||||
for _, p := range g.Players {
|
||||
p.Ready = false
|
||||
}
|
||||
}
|
||||
|
||||
// SubmitOrder records the player's battle ordering (a permutation of their
|
||||
// deck's card IDs, top of deck first). When everyone has submitted, the
|
||||
// battle resolves.
|
||||
func (g *Game) SubmitOrder(playerID string, orderedIDs []string) error {
|
||||
if g.Phase != PhaseArrange {
|
||||
return ErrWrongPhase
|
||||
}
|
||||
p := g.PlayerByID(playerID)
|
||||
if p == nil {
|
||||
return errors.New("unknown player")
|
||||
}
|
||||
if p.Ready {
|
||||
return fmt.Errorf("%w: order already submitted", ErrInvalidAction)
|
||||
}
|
||||
if len(orderedIDs) != len(p.Deck) || hasDuplicates(orderedIDs) {
|
||||
return fmt.Errorf("%w: order must include each of your cards exactly once", ErrInvalidAction)
|
||||
}
|
||||
ordered := make([]Card, 0, len(p.Deck))
|
||||
for _, id := range orderedIDs {
|
||||
idx := p.cardIndex(id)
|
||||
if idx < 0 {
|
||||
return fmt.Errorf("%w: card not in your deck", ErrInvalidAction)
|
||||
}
|
||||
ordered = append(ordered, p.Deck[idx])
|
||||
}
|
||||
p.Deck = ordered
|
||||
p.Ready = true
|
||||
if g.allReady() {
|
||||
g.resolveBattle()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AcknowledgeBattle marks the player done reviewing the battle. When all
|
||||
// players acknowledge, the next round starts (or the game ends).
|
||||
func (g *Game) AcknowledgeBattle(playerID string) error {
|
||||
if g.Phase != PhaseBattle {
|
||||
return ErrWrongPhase
|
||||
}
|
||||
p := g.PlayerByID(playerID)
|
||||
if p == nil {
|
||||
return errors.New("unknown player")
|
||||
}
|
||||
p.Ready = true
|
||||
if !g.allReady() {
|
||||
return nil
|
||||
}
|
||||
if g.Round >= MaxRounds {
|
||||
g.finish()
|
||||
return nil
|
||||
}
|
||||
g.Round++
|
||||
g.startShopRound()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) finish() {
|
||||
g.Phase = PhaseGameOver
|
||||
best, bestSeat, tie := -1, -1, false
|
||||
for _, p := range g.Players {
|
||||
switch {
|
||||
case p.Trophies > best:
|
||||
best, bestSeat, tie = p.Trophies, p.Seat, false
|
||||
case p.Trophies == best:
|
||||
tie = true
|
||||
}
|
||||
}
|
||||
if tie {
|
||||
g.WinnerSeat = -1
|
||||
} else {
|
||||
g.WinnerSeat = bestSeat
|
||||
}
|
||||
}
|
||||
|
||||
func hasDuplicates(ids []string) bool {
|
||||
seen := make(map[string]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if _, ok := seen[id]; ok {
|
||||
return true
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func deckIDs(p *Player, filter func(Card) bool) []string {
|
||||
var ids []string
|
||||
for _, c := range p.Deck {
|
||||
if filter == nil || filter(c) {
|
||||
ids = append(ids, c.ID)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func current(g *Game) *Player { return g.Players[g.Turn] }
|
||||
|
||||
func TestLobbyStartsWhenFull(t *testing.T) {
|
||||
g := New()
|
||||
if g.Phase != PhaseLobby {
|
||||
t.Fatalf("new game should be in lobby, got %s", g.Phase)
|
||||
}
|
||||
if _, err := g.AddPlayer("Alice"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g.Phase != PhaseLobby {
|
||||
t.Fatal("game should wait for second player")
|
||||
}
|
||||
if _, err := g.AddPlayer("Bob"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g.Phase != PhaseShop || g.Round != 1 {
|
||||
t.Fatalf("game should start round 1 shop, got phase=%s round=%d", g.Phase, g.Round)
|
||||
}
|
||||
if _, err := g.AddPlayer("Carol"); err == nil {
|
||||
t.Fatal("third player should be rejected while MaxPlayers=2")
|
||||
}
|
||||
for _, p := range g.Players {
|
||||
if p.Coins != CoinsPerRound {
|
||||
t.Fatalf("player should start with %d coins", CoinsPerRound)
|
||||
}
|
||||
}
|
||||
if len(g.ShopRow) != ShopRowSize {
|
||||
t.Fatalf("shop row should have %d cards", ShopRowSize)
|
||||
}
|
||||
for _, c := range g.ShopRow {
|
||||
if c.Tier != 1 {
|
||||
t.Fatalf("round 1 shop should deal tier 1 cards, got tier %d", c.Tier)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuyTakesCardAndRefills(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
p := current(g)
|
||||
want := g.ShopRow[0]
|
||||
deckBefore := len(g.ShopDecks[0])
|
||||
if err := g.Buy(p.ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.Coins != CoinsPerRound-1 {
|
||||
t.Fatalf("buy should cost 1 coin, coins=%d", p.Coins)
|
||||
}
|
||||
if len(p.Deck) != 1 || p.Deck[0].ID != want.ID {
|
||||
t.Fatalf("bought card should be in deck")
|
||||
}
|
||||
if g.ShopRow[0].ID == "" || g.ShopRow[0].ID == want.ID {
|
||||
t.Fatal("shop slot should refill with a new card")
|
||||
}
|
||||
if len(g.ShopDecks[0]) != deckBefore-1 {
|
||||
t.Fatal("refill should come from the tier deck")
|
||||
}
|
||||
if current(g).ID == p.ID {
|
||||
t.Fatal("turn should pass after an action")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnValidation(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
other := g.Players[(g.Turn+1)%2]
|
||||
if err := g.Buy(other.ID, 0); err == nil {
|
||||
t.Fatal("acting out of turn should fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscardConvertsToApples(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
p := current(g)
|
||||
if err := g.Buy(p.ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Skip opponent back to p.
|
||||
if err := g.Buy(current(g).ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := g.Discard(p.ID, deckIDs(p, Card.IsPet)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.Coins != CoinsPerRound-2 {
|
||||
t.Fatalf("discard should cost 1 coin, coins=%d", p.Coins)
|
||||
}
|
||||
if p.PetCount() != 0 || len(p.Deck) != 1 || p.Deck[0].Food != FoodApple {
|
||||
t.Fatalf("discarded pet should become an apple: %+v", p.Deck)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTradeInThreeMatchingSuits(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
p := current(g)
|
||||
// Hand p three same-suit tier-1 pets directly.
|
||||
for range 3 {
|
||||
c := g.pet("Fodder", 1)
|
||||
c.Suit = SuitMoon
|
||||
p.Deck = append(p.Deck, c)
|
||||
}
|
||||
nextDeckBefore := len(g.ShopDecks[1])
|
||||
if err := g.TradeStart(p.ID, deckIDs(p, nil)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g.Pending == nil || g.Pending.Tier != 2 {
|
||||
t.Fatalf("trade should reveal two tier-2 cards: %+v", g.Pending)
|
||||
}
|
||||
if len(p.Deck) != 0 {
|
||||
t.Fatal("traded cards should leave the deck")
|
||||
}
|
||||
if current(g).ID != p.ID {
|
||||
t.Fatal("turn should not pass until the trade is chosen")
|
||||
}
|
||||
if err := g.Buy(p.ID, 0); err == nil {
|
||||
t.Fatal("other actions should be blocked while a trade is pending")
|
||||
}
|
||||
chosen := g.Pending.Options[0]
|
||||
rejected := g.Pending.Options[1]
|
||||
if err := g.TradeChoose(p.ID, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(p.Deck) != 1 || p.Deck[0].ID != chosen.ID {
|
||||
t.Fatal("chosen card should join the deck")
|
||||
}
|
||||
deck2 := g.ShopDecks[1]
|
||||
if len(deck2) != nextDeckBefore-1 {
|
||||
t.Fatalf("tier 2 deck should be down exactly one card, was %d now %d", nextDeckBefore, len(deck2))
|
||||
}
|
||||
if deck2[len(deck2)-1].ID != rejected.ID {
|
||||
t.Fatal("rejected card should go to the bottom of the tier deck")
|
||||
}
|
||||
if p.Coins != CoinsPerRound-1 {
|
||||
t.Fatalf("trade should cost 1 coin, coins=%d", p.Coins)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTradeRequiresMatchingSuit(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
p := current(g)
|
||||
a, b, c := g.pet("A", 1), g.pet("B", 1), g.pet("C", 1)
|
||||
a.Suit, b.Suit, c.Suit = SuitSun, SuitSun, SuitMoon
|
||||
p.Deck = append(p.Deck, a, b, c)
|
||||
if err := g.TradeStart(p.ID, []string{a.ID, b.ID, c.ID}); err == nil {
|
||||
t.Fatal("mismatched suits should be rejected")
|
||||
}
|
||||
if len(p.Deck) != 3 || p.Coins != CoinsPerRound {
|
||||
t.Fatal("failed trade must not mutate state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTradeBlockedOnFinalRound(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
g.Round = MaxRounds
|
||||
p := current(g)
|
||||
for range 3 {
|
||||
c := g.pet("Fodder", 1)
|
||||
c.Suit = SuitMoon
|
||||
p.Deck = append(p.Deck, c)
|
||||
}
|
||||
if err := g.TradeStart(p.ID, deckIDs(p, nil)); err == nil {
|
||||
t.Fatal("trading should be impossible in the final round")
|
||||
}
|
||||
}
|
||||
|
||||
// spendAllCoins has both players pass until the shop ends.
|
||||
func spendAllCoins(t *testing.T, g *Game) {
|
||||
t.Helper()
|
||||
for g.Phase == PhaseShop {
|
||||
if err := g.Pass(current(g).ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestShopEndsIntoArrange(t *testing.T) {
|
||||
g, _, _ := testGame(t)
|
||||
spendAllCoins(t, g)
|
||||
if g.Phase != PhaseArrange {
|
||||
t.Fatalf("shop should end into arrange when no one is over the pet limit, got %s", g.Phase)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForcedDiscardOverPetLimit(t *testing.T) {
|
||||
g, p1, _ := testGame(t)
|
||||
for range MaxPets + 2 {
|
||||
p1.Deck = append(p1.Deck, g.pet("Extra", 1))
|
||||
}
|
||||
spendAllCoins(t, g)
|
||||
if g.Phase != PhaseCleanup {
|
||||
t.Fatalf("player with %d pets must be forced to discard, got phase %s", MaxPets+2, g.Phase)
|
||||
}
|
||||
// Wrong count rejected.
|
||||
if err := g.CleanupDiscard(p1.ID, deckIDs(p1, Card.IsPet)[:1]); err == nil {
|
||||
t.Fatal("must discard exactly the excess")
|
||||
}
|
||||
if err := g.CleanupDiscard(p1.ID, deckIDs(p1, Card.IsPet)[:2]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p1.PetCount() != MaxPets {
|
||||
t.Fatalf("expected %d pets after cleanup, got %d", MaxPets, p1.PetCount())
|
||||
}
|
||||
apples := 0
|
||||
for _, c := range p1.Deck {
|
||||
if c.Food == FoodApple {
|
||||
apples++
|
||||
}
|
||||
}
|
||||
if apples != 2 {
|
||||
t.Fatalf("discarded pets should become apples, got %d", apples)
|
||||
}
|
||||
if g.Phase != PhaseArrange {
|
||||
t.Fatalf("cleanup should flow into arrange, got %s", g.Phase)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrangeRejectsBadPermutation(t *testing.T) {
|
||||
g, p1, _ := testGame(t)
|
||||
p1.Deck = append(p1.Deck, g.pet("A", 1), g.pet("B", 2))
|
||||
spendAllCoins(t, g)
|
||||
if err := g.SubmitOrder(p1.ID, []string{p1.Deck[0].ID}); err == nil {
|
||||
t.Fatal("partial order should be rejected")
|
||||
}
|
||||
if err := g.SubmitOrder(p1.ID, []string{p1.Deck[0].ID, p1.Deck[0].ID}); err == nil {
|
||||
t.Fatal("duplicate IDs should be rejected")
|
||||
}
|
||||
if err := g.SubmitOrder(p1.ID, []string{p1.Deck[1].ID, p1.Deck[0].ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p1.Deck[0].Name != "B" {
|
||||
t.Fatal("submitted order should be applied to the deck")
|
||||
}
|
||||
}
|
||||
|
||||
// Full game: six rounds of pass-through shops and battles, trophy totals,
|
||||
// and game over with a winner.
|
||||
func TestFullGameFlow(t *testing.T) {
|
||||
g, p1, p2 := testGame(t)
|
||||
p1.Deck = append(p1.Deck, g.pet("Champ", 9))
|
||||
p2.Deck = append(p2.Deck, g.pet("Chump", 1))
|
||||
for round := 1; round <= MaxRounds; round++ {
|
||||
if g.Round != round || g.Phase != PhaseShop {
|
||||
t.Fatalf("expected shop of round %d, got round %d phase %s", round, g.Round, g.Phase)
|
||||
}
|
||||
if g.Turn != (round-1)%len(g.Players) {
|
||||
t.Fatalf("round %d should rotate the starting player, turn=%d", round, g.Turn)
|
||||
}
|
||||
for _, c := range g.ShopRow {
|
||||
if c.ID != "" && c.Tier != round {
|
||||
t.Fatalf("round %d shop dealt tier %d card", round, c.Tier)
|
||||
}
|
||||
}
|
||||
spendAllCoins(t, g)
|
||||
for _, p := range g.Players {
|
||||
if err := g.SubmitOrder(p.ID, deckIDs(p, nil)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if g.Phase != PhaseBattle {
|
||||
t.Fatalf("expected battle after both arrange, got %s", g.Phase)
|
||||
}
|
||||
if g.Battle.WinnerSeat != p1.Seat {
|
||||
t.Fatalf("round %d: seat 0 should win", round)
|
||||
}
|
||||
for _, p := range g.Players {
|
||||
if err := g.AcknowledgeBattle(p.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if g.Phase != PhaseGameOver {
|
||||
t.Fatalf("game should be over after %d rounds, got %s", MaxRounds, g.Phase)
|
||||
}
|
||||
// 1 trophy for rounds 1-5, 2 for round 6.
|
||||
if p1.Trophies != 7 {
|
||||
t.Fatalf("winner should have 7 trophies, got %d", p1.Trophies)
|
||||
}
|
||||
if g.WinnerSeat != p1.Seat {
|
||||
t.Fatalf("winner seat should be %d, got %d", p1.Seat, g.WinnerSeat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestViewHidesSecrets(t *testing.T) {
|
||||
g, p1, p2 := testGame(t)
|
||||
p1.Deck = append(p1.Deck, g.pet("Secret", 3))
|
||||
v := g.ViewFor(p2.ID)
|
||||
if v.YouSeat != p2.Seat {
|
||||
t.Fatalf("view should identify the viewer's seat")
|
||||
}
|
||||
for _, pv := range v.Players {
|
||||
if pv.Seat == p1.Seat && pv.Deck != nil {
|
||||
t.Fatal("opponent deck contents must be hidden")
|
||||
}
|
||||
if pv.Seat == p1.Seat && pv.DeckSize != 1 {
|
||||
t.Fatal("opponent deck size should be visible")
|
||||
}
|
||||
}
|
||||
// Pending trade options hidden from the opponent.
|
||||
for range 3 {
|
||||
c := g.pet("Fodder", 1)
|
||||
c.Suit = SuitLeaf
|
||||
p1.Deck = append(p1.Deck, c)
|
||||
}
|
||||
g.Turn = p1.Seat
|
||||
var fodder []string
|
||||
for _, c := range p1.Deck {
|
||||
if c.Name == "Fodder" {
|
||||
fodder = append(fodder, c.ID)
|
||||
}
|
||||
}
|
||||
if err := g.TradeStart(p1.ID, fodder); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opts := g.ViewFor(p2.ID).Pending.Options; opts[0].ID != "" || opts[1].ID != "" {
|
||||
t.Fatal("trade options must be hidden from opponents")
|
||||
}
|
||||
if opts := g.ViewFor(p1.ID).Pending.Options; opts[0].ID == "" {
|
||||
t.Fatal("trade options must be visible to the trader")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package game
|
||||
|
||||
// PlayerView is what any player may know about a seat. Deck contents are
|
||||
// only included for the viewer's own seat; opponents see counts.
|
||||
type PlayerView struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Seat int `json:"seat"`
|
||||
Coins int `json:"coins"`
|
||||
Trophies int `json:"trophies"`
|
||||
Ready bool `json:"ready"`
|
||||
Connected bool `json:"connected"`
|
||||
DeckSize int `json:"deckSize"`
|
||||
PetCount int `json:"petCount"`
|
||||
Deck []Card `json:"deck,omitempty"` // self only
|
||||
}
|
||||
|
||||
// View is the full game state as seen by one player.
|
||||
type View struct {
|
||||
GameID string `json:"gameId"`
|
||||
Code string `json:"code"`
|
||||
Phase Phase `json:"phase"`
|
||||
Round int `json:"round"`
|
||||
MaxRounds int `json:"maxRounds"`
|
||||
MaxPets int `json:"maxPets"`
|
||||
YouSeat int `json:"youSeat"`
|
||||
Turn int `json:"turn"`
|
||||
ShopRow []Card `json:"shopRow"`
|
||||
DeckCounts []int `json:"deckCounts"` // remaining shop cards per tier
|
||||
Players []PlayerView `json:"players"`
|
||||
// Pending is included for everyone so opponents see a trade is in
|
||||
// progress, but the revealed options are only shown to the trader.
|
||||
Pending *PendingTrade `json:"pending,omitempty"`
|
||||
Battle *BattleResult `json:"battle,omitempty"`
|
||||
WinnerSeat int `json:"winnerSeat"`
|
||||
}
|
||||
|
||||
// ViewFor builds the state visible to the given player.
|
||||
func (g *Game) ViewFor(playerID string) View {
|
||||
v := View{
|
||||
GameID: g.ID,
|
||||
Code: g.Code,
|
||||
Phase: g.Phase,
|
||||
Round: g.Round,
|
||||
MaxRounds: MaxRounds,
|
||||
MaxPets: MaxPets,
|
||||
YouSeat: -1,
|
||||
Turn: g.Turn,
|
||||
ShopRow: g.ShopRow,
|
||||
WinnerSeat: g.WinnerSeat,
|
||||
}
|
||||
for _, deck := range g.ShopDecks {
|
||||
v.DeckCounts = append(v.DeckCounts, len(deck))
|
||||
}
|
||||
for _, p := range g.Players {
|
||||
pv := PlayerView{
|
||||
ID: p.ID,
|
||||
Name: p.Name,
|
||||
Seat: p.Seat,
|
||||
Coins: p.Coins,
|
||||
Trophies: p.Trophies,
|
||||
Ready: p.Ready,
|
||||
Connected: p.Connected,
|
||||
DeckSize: len(p.Deck),
|
||||
PetCount: p.PetCount(),
|
||||
}
|
||||
if p.ID == playerID {
|
||||
v.YouSeat = p.Seat
|
||||
pv.Deck = p.Deck
|
||||
}
|
||||
v.Players = append(v.Players, pv)
|
||||
}
|
||||
if g.Pending != nil {
|
||||
pending := *g.Pending
|
||||
if pending.PlayerID != playerID {
|
||||
pending.Options = [2]Card{} // hide the revealed cards
|
||||
}
|
||||
v.Pending = &pending
|
||||
}
|
||||
// Battle results (lineups, events) are public once resolved. Keep the
|
||||
// battle around during the following shop phase too, so late joiners /
|
||||
// reconnects can still see the last result.
|
||||
v.Battle = g.Battle
|
||||
return v
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// Package server exposes the game engine over HTTP + WebSockets. Each game
|
||||
// lives in a "room": the authoritative Game state, a lock, and the set of
|
||||
// connected clients. All mutations happen under the room lock and are
|
||||
// persisted to the store before being broadcast.
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/game"
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/store"
|
||||
)
|
||||
|
||||
// Server routes HTTP/WS traffic to game rooms.
|
||||
type Server struct {
|
||||
store *store.Store
|
||||
staticDir string
|
||||
|
||||
mu sync.Mutex
|
||||
rooms map[string]*room // by game ID
|
||||
}
|
||||
|
||||
// New creates a server. staticDir is the built frontend to serve (may be
|
||||
// empty or missing during backend-only development).
|
||||
func New(st *store.Store, staticDir string) *Server {
|
||||
return &Server{
|
||||
store: st,
|
||||
staticDir: staticDir,
|
||||
rooms: make(map[string]*room),
|
||||
}
|
||||
}
|
||||
|
||||
// Handler builds the full route table.
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("POST /api/games", s.handleCreate)
|
||||
mux.HandleFunc("POST /api/join", s.handleJoin)
|
||||
mux.HandleFunc("GET /api/ws", s.handleWS)
|
||||
mux.HandleFunc("/", s.handleStatic)
|
||||
return mux
|
||||
}
|
||||
|
||||
// room is one live game plus its connections.
|
||||
type room struct {
|
||||
mu sync.Mutex
|
||||
game *game.Game
|
||||
conns map[*client]struct{}
|
||||
}
|
||||
|
||||
// getRoom returns the room for a game ID, loading it from the store if it
|
||||
// isn't in memory (e.g. after a server restart).
|
||||
func (s *Server) getRoom(gameID string) (*room, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if r, ok := s.rooms[gameID]; ok {
|
||||
return r, nil
|
||||
}
|
||||
g, err := s.store.Load(gameID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r := &room{game: g, conns: make(map[*client]struct{})}
|
||||
s.rooms[gameID] = r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// getRoomByCode resolves a join code to a room.
|
||||
func (s *Server) getRoomByCode(code string) (*room, error) {
|
||||
code = strings.ToUpper(strings.TrimSpace(code))
|
||||
s.mu.Lock()
|
||||
for _, r := range s.rooms {
|
||||
if r.game.Code == code {
|
||||
s.mu.Unlock()
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
g, err := s.store.LoadByCode(code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.getRoom(g.ID)
|
||||
}
|
||||
|
||||
// persist saves the room's game; callers must hold r.mu.
|
||||
func (s *Server) persist(r *room) {
|
||||
if err := s.store.Save(r.game); err != nil {
|
||||
slog.Error("failed to persist game", "game", r.game.ID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
type joinResponse struct {
|
||||
GameID string `json:"gameId"`
|
||||
Code string `json:"code"`
|
||||
PlayerID string `json:"playerId"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func (s *Server) handleCreate(w http.ResponseWriter, req *http.Request) {
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
||||
httpError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
g := game.New()
|
||||
p, err := g.AddPlayer(strings.TrimSpace(body.Name))
|
||||
if err != nil {
|
||||
httpError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
r := &room{game: g, conns: make(map[*client]struct{})}
|
||||
s.mu.Lock()
|
||||
s.rooms[g.ID] = r
|
||||
s.mu.Unlock()
|
||||
|
||||
r.mu.Lock()
|
||||
s.persist(r)
|
||||
r.mu.Unlock()
|
||||
writeJSON(w, joinResponse{GameID: g.ID, Code: g.Code, PlayerID: p.ID, Token: p.Token})
|
||||
}
|
||||
|
||||
func (s *Server) handleJoin(w http.ResponseWriter, req *http.Request) {
|
||||
var body struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
||||
httpError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
r, err := s.getRoomByCode(body.Code)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
httpError(w, http.StatusNotFound, "no game with that code")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpError(w, http.StatusInternalServerError, "failed to load game")
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
p, err := r.game.AddPlayer(strings.TrimSpace(body.Name))
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
httpError(w, http.StatusConflict, err.Error())
|
||||
return
|
||||
}
|
||||
s.persist(r)
|
||||
resp := joinResponse{GameID: r.game.ID, Code: r.game.Code, PlayerID: p.ID, Token: p.Token}
|
||||
r.broadcastLocked()
|
||||
r.mu.Unlock()
|
||||
writeJSON(w, resp)
|
||||
}
|
||||
|
||||
// handleStatic serves the built frontend with an SPA fallback to index.html.
|
||||
func (s *Server) handleStatic(w http.ResponseWriter, req *http.Request) {
|
||||
if s.staticDir == "" {
|
||||
httpError(w, http.StatusNotFound, "frontend not built (run: mise run build-web)")
|
||||
return
|
||||
}
|
||||
path := filepath.Join(s.staticDir, filepath.Clean("/"+req.URL.Path))
|
||||
if info, err := os.Stat(path); err == nil && !info.IsDir() {
|
||||
http.ServeFile(w, req, path)
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, req, filepath.Join(s.staticDir, "index.html"))
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func httpError(w http.ResponseWriter, status int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": msg})
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/game"
|
||||
)
|
||||
|
||||
// client is one WebSocket connection bound to a player in a room.
|
||||
type client struct {
|
||||
ws *websocket.Conn
|
||||
playerID string
|
||||
send chan []byte
|
||||
}
|
||||
|
||||
// clientMessage is anything a player can ask the server to do. Type decides
|
||||
// which other fields matter.
|
||||
type clientMessage struct {
|
||||
Type string `json:"type"`
|
||||
Row int `json:"row"` // buy
|
||||
Cards []string `json:"cards"` // discard, trade
|
||||
Pick int `json:"pick"` // tradeChoose
|
||||
Order []string `json:"order"` // arrange
|
||||
}
|
||||
|
||||
type serverMessage struct {
|
||||
Type string `json:"type"`
|
||||
State *game.View `json:"state,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// handleWS upgrades the connection and pumps messages until the client
|
||||
// leaves. Query params: game, player, token.
|
||||
func (s *Server) handleWS(w http.ResponseWriter, req *http.Request) {
|
||||
q := req.URL.Query()
|
||||
gameID, playerID, token := q.Get("game"), q.Get("player"), q.Get("token")
|
||||
|
||||
r, err := s.getRoom(gameID)
|
||||
if err != nil {
|
||||
httpError(w, http.StatusNotFound, "game not found")
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
p := r.game.PlayerByID(playerID)
|
||||
r.mu.Unlock()
|
||||
if p == nil || p.Token != token {
|
||||
httpError(w, http.StatusForbidden, "bad player credentials")
|
||||
return
|
||||
}
|
||||
|
||||
ws, err := websocket.Accept(w, req, &websocket.AcceptOptions{
|
||||
// Same-origin in production; the Vite dev server proxies /api, so
|
||||
// cross-origin checks buy nothing here yet.
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c := &client{ws: ws, playerID: playerID, send: make(chan []byte, 16)}
|
||||
|
||||
ctx, cancel := context.WithCancel(req.Context())
|
||||
defer cancel()
|
||||
go c.writeLoop(ctx)
|
||||
|
||||
r.mu.Lock()
|
||||
r.conns[c] = struct{}{}
|
||||
p.Connected = true
|
||||
r.broadcastLocked()
|
||||
r.mu.Unlock()
|
||||
|
||||
defer func() {
|
||||
r.mu.Lock()
|
||||
delete(r.conns, c)
|
||||
// Only mark disconnected if no other tab/connection remains.
|
||||
stillHere := false
|
||||
for other := range r.conns {
|
||||
if other.playerID == playerID {
|
||||
stillHere = true
|
||||
}
|
||||
}
|
||||
if !stillHere {
|
||||
p.Connected = false
|
||||
}
|
||||
r.broadcastLocked()
|
||||
r.mu.Unlock()
|
||||
ws.Close(websocket.StatusNormalClosure, "")
|
||||
}()
|
||||
|
||||
for {
|
||||
_, data, err := ws.Read(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var msg clientMessage
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
c.sendError("invalid message")
|
||||
continue
|
||||
}
|
||||
s.apply(r, c, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// apply runs one player action against the game under the room lock, then
|
||||
// persists and broadcasts on success.
|
||||
func (s *Server) apply(r *room, c *client, msg clientMessage) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
g := r.game
|
||||
var err error
|
||||
switch msg.Type {
|
||||
case "buy":
|
||||
err = g.Buy(c.playerID, msg.Row)
|
||||
case "discard":
|
||||
if g.Phase == game.PhaseCleanup {
|
||||
err = g.CleanupDiscard(c.playerID, msg.Cards)
|
||||
} else {
|
||||
err = g.Discard(c.playerID, msg.Cards)
|
||||
}
|
||||
case "trade":
|
||||
err = g.TradeStart(c.playerID, msg.Cards)
|
||||
case "tradeChoose":
|
||||
err = g.TradeChoose(c.playerID, msg.Pick)
|
||||
case "pass":
|
||||
err = g.Pass(c.playerID)
|
||||
case "arrange":
|
||||
err = g.SubmitOrder(c.playerID, msg.Order)
|
||||
case "ready":
|
||||
err = g.AcknowledgeBattle(c.playerID)
|
||||
default:
|
||||
err = game.ErrInvalidAction
|
||||
}
|
||||
if err != nil {
|
||||
c.sendError(err.Error())
|
||||
return
|
||||
}
|
||||
s.persist(r)
|
||||
r.broadcastLocked()
|
||||
}
|
||||
|
||||
// broadcastLocked sends each connected client its own view of the game.
|
||||
// Callers must hold r.mu.
|
||||
func (r *room) broadcastLocked() {
|
||||
for c := range r.conns {
|
||||
view := r.game.ViewFor(c.playerID)
|
||||
data, err := json.Marshal(serverMessage{Type: "state", State: &view})
|
||||
if err != nil {
|
||||
slog.Error("failed to marshal view", "err", err)
|
||||
continue
|
||||
}
|
||||
c.trySend(data)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *client) sendError(msg string) {
|
||||
data, _ := json.Marshal(serverMessage{Type: "error", Error: msg})
|
||||
c.trySend(data)
|
||||
}
|
||||
|
||||
// trySend queues a message, dropping it if the client's buffer is full (a
|
||||
// stalled client will resync from the next state broadcast anyway).
|
||||
func (c *client) trySend(data []byte) {
|
||||
select {
|
||||
case c.send <- data:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (c *client) writeLoop(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case data := <-c.send:
|
||||
writeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
err := c.ws.Write(writeCtx, websocket.MessageText, data)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Package store persists game state as JSON blobs in SQLite.
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/greyson/super-auto-pets-board-game/internal/game"
|
||||
)
|
||||
|
||||
// ErrNotFound is returned when a game doesn't exist.
|
||||
var ErrNotFound = errors.New("game not found")
|
||||
|
||||
// Store wraps the SQLite database in the data directory.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// Open creates the data directory if needed and opens (or initializes) the
|
||||
// database inside it.
|
||||
func Open(dataDir string) (*Store, error) {
|
||||
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("create data dir: %w", err)
|
||||
}
|
||||
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)",
|
||||
filepath.Join(dataDir, "games.db"))
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// modernc.org/sqlite is happiest with a single writer connection.
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS games (
|
||||
id TEXT PRIMARY KEY,
|
||||
code TEXT NOT NULL UNIQUE,
|
||||
state TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
`); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("init schema: %w", err)
|
||||
}
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
// Save upserts the full game state.
|
||||
func (s *Store) Save(g *game.Game) error {
|
||||
blob, err := json.Marshal(g)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = s.db.Exec(`
|
||||
INSERT INTO games (id, code, state, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET state = excluded.state, updated_at = excluded.updated_at
|
||||
`, g.ID, g.Code, string(blob), now, now)
|
||||
return err
|
||||
}
|
||||
|
||||
// Load fetches a game by ID.
|
||||
func (s *Store) Load(id string) (*game.Game, error) {
|
||||
return s.loadWhere(`id = ?`, id)
|
||||
}
|
||||
|
||||
// LoadByCode fetches a game by its join code.
|
||||
func (s *Store) LoadByCode(code string) (*game.Game, error) {
|
||||
return s.loadWhere(`code = ?`, code)
|
||||
}
|
||||
|
||||
func (s *Store) loadWhere(cond string, arg any) (*game.Game, error) {
|
||||
var blob string
|
||||
err := s.db.QueryRow(`SELECT state FROM games WHERE `+cond, arg).Scan(&blob)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var g game.Game
|
||||
if err := json.Unmarshal([]byte(blob), &g); err != nil {
|
||||
return nil, fmt.Errorf("corrupt game state %v: %w", arg, err)
|
||||
}
|
||||
return &g, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
[tools]
|
||||
go = "latest"
|
||||
node = "latest"
|
||||
|
||||
[tasks.dev-server]
|
||||
description = "Run the Go server (serves API + built frontend on :8080)"
|
||||
run = "go run ./cmd/server"
|
||||
|
||||
[tasks.dev-web]
|
||||
description = "Run the Vite dev server with hot reload (proxies /api to :8080)"
|
||||
dir = "web"
|
||||
run = "npm run dev"
|
||||
|
||||
[tasks.dev]
|
||||
description = "Run backend and frontend dev servers together"
|
||||
depends = ["dev-server", "dev-web"]
|
||||
|
||||
[tasks.install-web]
|
||||
description = "Install frontend dependencies"
|
||||
dir = "web"
|
||||
run = "npm install"
|
||||
|
||||
[tasks.build-web]
|
||||
description = "Build the frontend into web/dist"
|
||||
dir = "web"
|
||||
run = "npm run build"
|
||||
|
||||
[tasks.build]
|
||||
description = "Build everything: frontend + server binary (bin/server)"
|
||||
depends = ["build-web"]
|
||||
run = "go build -o bin/server ./cmd/server"
|
||||
|
||||
[tasks.test]
|
||||
description = "Run all Go tests"
|
||||
run = "go test ./..."
|
||||
|
||||
[tasks.check]
|
||||
description = "Vet Go code and type-check the frontend"
|
||||
run = ["go vet ./...", "cd web && npx tsc -b"]
|
||||
|
||||
[tasks.serve]
|
||||
description = "Build everything and run the production server"
|
||||
depends = ["build"]
|
||||
run = "./bin/server"
|
||||
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Super Auto Pets: The Board Game</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+1849
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "super-auto-pets-board-game-web",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc -b && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fontsource/lilita-one": "^5.1.0",
|
||||
"@fontsource/nunito": "^5.1.0",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.0.0",
|
||||
"@types/react-dom": "^19.0.0",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
"typescript": "^5.7.0",
|
||||
"vite": "^6.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { useState } from 'react'
|
||||
import { clearSession, loadSession, saveSession } from './api'
|
||||
import type { Session } from './types'
|
||||
import { Home } from './components/Home'
|
||||
import { Table } from './components/Table'
|
||||
|
||||
export function App() {
|
||||
const [session, setSession] = useState<Session | null>(loadSession)
|
||||
|
||||
if (!session) {
|
||||
return (
|
||||
<Home
|
||||
onSession={(s) => {
|
||||
saveSession(s)
|
||||
setSession(s)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<Table
|
||||
session={session}
|
||||
onLeave={() => {
|
||||
clearSession()
|
||||
setSession(null)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { Session } from './types'
|
||||
|
||||
const SESSION_KEY = 'sapbg-session'
|
||||
|
||||
async function post(path: string, body: unknown): Promise<Session> {
|
||||
const res = await fetch(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data.error ?? 'request failed')
|
||||
return data as Session
|
||||
}
|
||||
|
||||
export function createGame(name: string): Promise<Session> {
|
||||
return post('/api/games', { name })
|
||||
}
|
||||
|
||||
export function joinGame(code: string, name: string): Promise<Session> {
|
||||
return post('/api/join', { code, name })
|
||||
}
|
||||
|
||||
export function loadSession(): Session | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(SESSION_KEY)
|
||||
return raw ? (JSON.parse(raw) as Session) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function saveSession(s: Session) {
|
||||
localStorage.setItem(SESSION_KEY, JSON.stringify(s))
|
||||
}
|
||||
|
||||
export function clearSession() {
|
||||
localStorage.removeItem(SESSION_KEY)
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { Card, ClientMessage, GameView, PlayerView } from '../types'
|
||||
import { CardView } from './CardView'
|
||||
|
||||
interface Props {
|
||||
view: GameView
|
||||
you: PlayerView
|
||||
send: (msg: ClientMessage) => void
|
||||
}
|
||||
|
||||
// ArrangePhase lets the player order their deck for battle. Leftmost card
|
||||
// fights first; food cards buff the next pet to their right... i.e. foods
|
||||
// apply "down" to the next pet later in the order. Drag cards or use the
|
||||
// arrow buttons to reorder, then lock in.
|
||||
export function ArrangePhase({ view, you, send }: Props) {
|
||||
const [order, setOrder] = useState<Card[]>(you.deck ?? [])
|
||||
const dragIndex = useRef<number | null>(null)
|
||||
const locked = you.ready
|
||||
|
||||
// If the server-side deck changes (shouldn't during arrange, but be safe),
|
||||
// resync.
|
||||
useEffect(() => {
|
||||
setOrder(you.deck ?? [])
|
||||
}, [you.deck])
|
||||
|
||||
function move(from: number, to: number) {
|
||||
if (to < 0 || to >= order.length) return
|
||||
setOrder((o) => {
|
||||
const next = [...o]
|
||||
const [c] = next.splice(from, 1)
|
||||
next.splice(to, 0, c)
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
// Which pets do the foods land on? Compute buff per card for preview.
|
||||
const bonuses = new Map<string, number>()
|
||||
{
|
||||
let pendingApples = 0
|
||||
for (const c of order) {
|
||||
if (c.kind === 'food') {
|
||||
if (c.food === 'apple') pendingApples++
|
||||
} else {
|
||||
bonuses.set(c.id, pendingApples)
|
||||
pendingApples = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
const trailingFoods = (() => {
|
||||
let n = 0
|
||||
for (let i = order.length - 1; i >= 0 && order[i].kind === 'food'; i--) n++
|
||||
return n
|
||||
})()
|
||||
|
||||
const opponent = view.players.find((p) => p.seat !== view.youSeat)
|
||||
|
||||
if (locked) {
|
||||
return (
|
||||
<div className="centered">
|
||||
<h2>Order locked in ⚔️</h2>
|
||||
<p className="muted">
|
||||
Waiting for {opponent?.name ?? 'your opponent'} to arrange their deck…
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="arrange">
|
||||
<div className="shop-status">
|
||||
<span className="status-hot">Arrange your battle line</span>
|
||||
</div>
|
||||
<p className="hint">
|
||||
The <strong>leftmost</strong> card fights first. Food cards power up the
|
||||
next pet to their <strong>right</strong>.
|
||||
{trailingFoods > 0 && (
|
||||
<span className="warn-text">
|
||||
{' '}
|
||||
⚠️ {trailingFoods} food card{trailingFoods > 1 ? 's' : ''} at the end
|
||||
will be wasted!
|
||||
</span>
|
||||
)}
|
||||
</p>
|
||||
|
||||
<div className="arrange-row">
|
||||
<div className="arrange-marker">⚔️ first</div>
|
||||
{order.map((c, i) => (
|
||||
<div
|
||||
key={c.id}
|
||||
className="arrange-card"
|
||||
draggable
|
||||
onDragStart={() => (dragIndex.current = i)}
|
||||
onDragOver={(e) => {
|
||||
e.preventDefault()
|
||||
if (dragIndex.current !== null && dragIndex.current !== i) {
|
||||
move(dragIndex.current, i)
|
||||
dragIndex.current = i
|
||||
}
|
||||
}}
|
||||
onDragEnd={() => (dragIndex.current = null)}
|
||||
>
|
||||
<CardView card={c} bonus={bonuses.get(c.id) ?? 0} />
|
||||
<div className="arrange-arrows">
|
||||
<button
|
||||
className="btn btn-ghost btn-sm"
|
||||
disabled={i === 0}
|
||||
onClick={() => move(i, i - 1)}
|
||||
aria-label="move earlier"
|
||||
>
|
||||
◀
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-ghost btn-sm"
|
||||
disabled={i === order.length - 1}
|
||||
onClick={() => move(i, i + 1)}
|
||||
aria-label="move later"
|
||||
>
|
||||
▶
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="actions">
|
||||
<button
|
||||
className="btn btn-primary btn-big"
|
||||
onClick={() => send({ type: 'arrange', order: order.map((c) => c.id) })}
|
||||
>
|
||||
Lock in & battle ⚔️
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import type { BattleEvent, BattleUnit, ClientMessage, GameView } from '../types'
|
||||
import { CardView } from './CardView'
|
||||
|
||||
interface Props {
|
||||
view: GameView
|
||||
send: (msg: ClientMessage) => void
|
||||
}
|
||||
|
||||
const STEP_MS = 1400
|
||||
|
||||
interface UnitState {
|
||||
unit: BattleUnit
|
||||
index: number
|
||||
damage: number
|
||||
dead: boolean // died in an earlier step (gone)
|
||||
dying: boolean // died in the step just played (animate out)
|
||||
}
|
||||
|
||||
// applyEvents replays the first `step` events onto a seat's lineup.
|
||||
function applyEvents(view: GameView, seat: number, step: number): UnitState[] {
|
||||
const battle = view.battle!
|
||||
const events = battle.events ?? []
|
||||
const states: UnitState[] = (battle.lineups[seat] ?? []).map((u, i) => ({
|
||||
unit: u,
|
||||
index: i,
|
||||
damage: 0,
|
||||
dead: false,
|
||||
dying: false,
|
||||
}))
|
||||
const seatPos = seat === 0 ? 0 : 1
|
||||
for (let k = 0; k < step && k < events.length; k++) {
|
||||
const ev = events[k]
|
||||
const s = states[ev.units[seatPos]]
|
||||
if (!s) continue
|
||||
s.damage = ev.damage[seatPos]
|
||||
if (ev.died[seatPos]) {
|
||||
s.dying = k === step - 1
|
||||
s.dead = k < step - 1
|
||||
}
|
||||
}
|
||||
return states
|
||||
}
|
||||
|
||||
// BattlePhase plays back the battle log: front pets clash, damage numbers
|
||||
// fly, the fallen fade out, then the round result lands.
|
||||
export function BattlePhase({ view, send }: Props) {
|
||||
const battle = view.battle!
|
||||
const events = battle.events ?? []
|
||||
const [step, setStep] = useState(0)
|
||||
const [acked, setAcked] = useState(false)
|
||||
const done = step >= events.length
|
||||
|
||||
useEffect(() => {
|
||||
if (done) return
|
||||
const t = window.setTimeout(() => setStep((s) => s + 1), STEP_MS)
|
||||
return () => window.clearTimeout(t)
|
||||
}, [step, done])
|
||||
|
||||
const youSeat = view.youSeat
|
||||
const oppSeat = view.players.find((p) => p.seat !== youSeat)?.seat ?? 1
|
||||
|
||||
const yourLine = useMemo(
|
||||
() => applyEvents(view, youSeat, step),
|
||||
[view, youSeat, step],
|
||||
)
|
||||
const oppLine = useMemo(
|
||||
() => applyEvents(view, oppSeat, step),
|
||||
[view, oppSeat, step],
|
||||
)
|
||||
|
||||
const lastEvent = step > 0 ? events[step - 1] : null
|
||||
|
||||
function renderSide(line: UnitState[], side: 'left' | 'right', seat: number) {
|
||||
const seatPos = seat === 0 ? 0 : 1
|
||||
const frontIdx = line.find((s) => !s.dead && !s.dying)?.index
|
||||
return (
|
||||
<div className={`battle-side battle-side-${side}`}>
|
||||
{line
|
||||
.filter((s) => !s.dead)
|
||||
.map((s) => {
|
||||
const isFront = s.index === frontIdx
|
||||
const clashing =
|
||||
!done && lastEvent !== null && lastEvent.units[seatPos] === s.index
|
||||
return (
|
||||
<div
|
||||
key={`${s.unit.card.id}-${clashing ? step : 'idle'}`}
|
||||
className={[
|
||||
'battle-unit',
|
||||
clashing ? `clash-${side}` : '',
|
||||
s.dying ? 'unit-dying' : '',
|
||||
isFront && !s.dying ? 'is-front' : '',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
>
|
||||
<CardView
|
||||
card={s.unit.card}
|
||||
bonus={s.unit.bonus}
|
||||
damage={s.damage}
|
||||
dead={s.dying}
|
||||
/>
|
||||
{clashing && (
|
||||
<div className="damage-pop">
|
||||
−{lastEvent!.damage[seatPos] -
|
||||
prevDamage(events, step - 1, seatPos, s.index)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const you = view.players[youSeat]
|
||||
const opp = view.players[oppSeat]
|
||||
const won = battle.winnerSeat === youSeat
|
||||
const draw = battle.winnerSeat < 0
|
||||
|
||||
return (
|
||||
<div className="battle">
|
||||
<div className="battle-header">
|
||||
<h2>Battle! Round {battle.round}</h2>
|
||||
{!done && (
|
||||
<button className="btn btn-ghost btn-sm" onClick={() => setStep(events.length)}>
|
||||
Skip ⏭
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="battle-names">
|
||||
<span>{you?.name} (you)</span>
|
||||
<span className="battle-vs">VS</span>
|
||||
<span>{opp?.name}</span>
|
||||
</div>
|
||||
|
||||
<div className="battlefield">
|
||||
{renderSide(yourLine, 'left', youSeat)}
|
||||
<div className="battle-center" aria-hidden>
|
||||
⚡
|
||||
</div>
|
||||
{renderSide(oppLine, 'right', oppSeat)}
|
||||
</div>
|
||||
|
||||
{done && (
|
||||
<div className={`battle-result ${draw ? 'is-draw' : won ? 'is-win' : 'is-loss'}`}>
|
||||
<div className="battle-result-title">
|
||||
{draw ? 'Draw!' : won ? 'Victory!' : 'Defeat…'}
|
||||
</div>
|
||||
{!draw && (
|
||||
<div className="battle-result-sub">
|
||||
{view.players[battle.winnerSeat]?.name} wins{' '}
|
||||
{'🏆'.repeat(battle.trophies)}
|
||||
</div>
|
||||
)}
|
||||
{draw && <div className="battle-result-sub">No trophies awarded</div>}
|
||||
{acked ? (
|
||||
<p className="muted">Waiting for opponent…</p>
|
||||
) : (
|
||||
<button
|
||||
className="btn btn-primary btn-big"
|
||||
onClick={() => {
|
||||
setAcked(true)
|
||||
send({ type: 'ready' })
|
||||
}}
|
||||
>
|
||||
{battle.round >= view.maxRounds ? 'See final results' : 'Next round →'}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// prevDamage finds the damage a unit had before the given event, so the
|
||||
// floating number shows just this clash's hit.
|
||||
function prevDamage(
|
||||
events: BattleEvent[],
|
||||
upto: number,
|
||||
seatPos: number,
|
||||
unitIndex: number,
|
||||
): number {
|
||||
let dmg = 0
|
||||
for (let k = 0; k < upto; k++) {
|
||||
const ev = events[k]
|
||||
if (ev.units[seatPos] === unitIndex) dmg = ev.damage[seatPos]
|
||||
}
|
||||
return dmg
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { Card } from '../types'
|
||||
import { artFor, SUIT_EMOJI } from '../petArt'
|
||||
|
||||
interface Props {
|
||||
card: Card
|
||||
size?: 'sm' | 'md' | 'lg'
|
||||
selected?: boolean
|
||||
disabled?: boolean
|
||||
onClick?: () => void
|
||||
// Battle decorations
|
||||
bonus?: number
|
||||
damage?: number
|
||||
dead?: boolean
|
||||
}
|
||||
|
||||
// CardView renders one physical card: pets get a power badge and suit stamp,
|
||||
// foods a description line. Battle mode layers on buffs and damage markers.
|
||||
export function CardView({
|
||||
card,
|
||||
size = 'md',
|
||||
selected,
|
||||
disabled,
|
||||
onClick,
|
||||
bonus = 0,
|
||||
damage = 0,
|
||||
dead,
|
||||
}: Props) {
|
||||
const power = (card.power ?? 0) + bonus
|
||||
const classes = [
|
||||
'card',
|
||||
`card-${size}`,
|
||||
card.kind === 'food' ? 'card-food' : 'card-pet',
|
||||
selected ? 'is-selected' : '',
|
||||
disabled ? 'is-disabled' : '',
|
||||
dead ? 'is-dead' : '',
|
||||
onClick && !disabled ? 'is-clickable' : '',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' ')
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classes}
|
||||
onClick={disabled ? undefined : onClick}
|
||||
role={onClick ? 'button' : undefined}
|
||||
>
|
||||
<div className="card-top">
|
||||
<span className="card-tier">T{card.tier || '–'}</span>
|
||||
{card.suit && (
|
||||
<span className="card-suit" title={`Suit: ${card.suit}`}>
|
||||
{SUIT_EMOJI[card.suit]}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="card-art" aria-hidden>
|
||||
{artFor(card.name)}
|
||||
</div>
|
||||
<div className="card-name">{card.name}</div>
|
||||
{card.kind === 'pet' ? (
|
||||
<div className="card-bottom">
|
||||
<span className={`card-power ${bonus > 0 ? 'is-buffed' : ''}`}>{power}</span>
|
||||
{damage > 0 && !dead && <span className="card-damage">−{damage}</span>}
|
||||
</div>
|
||||
) : (
|
||||
<div className="card-food-text">+1 power</div>
|
||||
)}
|
||||
{selected && <div className="card-check">✓</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { GameView } from '../types'
|
||||
|
||||
export function GameOver({ view, onLeave }: { view: GameView; onLeave: () => void }) {
|
||||
const winner = view.winnerSeat >= 0 ? view.players[view.winnerSeat] : null
|
||||
const youWon = view.winnerSeat === view.youSeat
|
||||
|
||||
return (
|
||||
<div className="gameover">
|
||||
<div className="gameover-emoji" aria-hidden>
|
||||
{winner ? (youWon ? '🎉' : '💀') : '🤝'}
|
||||
</div>
|
||||
<h1 className="gameover-title">
|
||||
{winner ? (youWon ? 'You win!' : `${winner.name} wins!`) : "It's a tie!"}
|
||||
</h1>
|
||||
<div className="gameover-scores">
|
||||
{[...view.players]
|
||||
.sort((a, b) => b.trophies - a.trophies)
|
||||
.map((p) => (
|
||||
<div key={p.id} className={`score-line ${p.seat === view.youSeat ? 'is-you' : ''}`}>
|
||||
<span className="score-name">
|
||||
{p.name}
|
||||
{p.seat === view.youSeat ? ' (you)' : ''}
|
||||
</span>
|
||||
<span className="score-trophies">
|
||||
{'🏆'.repeat(p.trophies) || '—'} <strong>{p.trophies}</strong>
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<button className="btn btn-primary btn-big" onClick={onLeave}>
|
||||
Back to the den
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { useState } from 'react'
|
||||
import { createGame, joinGame } from '../api'
|
||||
import type { Session } from '../types'
|
||||
|
||||
// Home is the create/join screen shown when there's no active session.
|
||||
export function Home({ onSession }: { onSession: (s: Session) => void }) {
|
||||
const [name, setName] = useState('')
|
||||
const [code, setCode] = useState('')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
async function run(fn: () => Promise<Session>) {
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
onSession(await fn())
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : 'something went wrong')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="home">
|
||||
<div className="home-pets" aria-hidden>
|
||||
🐷🦔🐶🦩🐉
|
||||
</div>
|
||||
<h1 className="home-title">
|
||||
Super Auto Pets
|
||||
<span className="home-subtitle">The Board Game</span>
|
||||
</h1>
|
||||
|
||||
<div className="home-card">
|
||||
<label className="field">
|
||||
<span>Your name</span>
|
||||
<input
|
||||
value={name}
|
||||
maxLength={20}
|
||||
placeholder="e.g. Greyson"
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
/>
|
||||
</label>
|
||||
|
||||
<button
|
||||
className="btn btn-primary btn-big"
|
||||
disabled={busy}
|
||||
onClick={() => run(() => createGame(name))}
|
||||
>
|
||||
Host a new game
|
||||
</button>
|
||||
|
||||
<div className="home-divider">
|
||||
<span>or join a friend</span>
|
||||
</div>
|
||||
|
||||
<div className="home-join">
|
||||
<input
|
||||
className="code-input"
|
||||
value={code}
|
||||
maxLength={5}
|
||||
placeholder="CODE"
|
||||
onChange={(e) => setCode(e.target.value.toUpperCase())}
|
||||
/>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
disabled={busy || code.length < 5}
|
||||
onClick={() => run(() => joinGame(code, name))}
|
||||
>
|
||||
Join
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && <div className="home-error">{error}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { GameView } from '../types'
|
||||
|
||||
export function Lobby({ view }: { view: GameView }) {
|
||||
return (
|
||||
<div className="lobby">
|
||||
<div className="lobby-bounce" aria-hidden>
|
||||
🐟
|
||||
</div>
|
||||
<h2>Waiting for an opponent…</h2>
|
||||
<p className="muted">Share this code so a friend can join:</p>
|
||||
<div className="lobby-code">{view.code}</div>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={() => navigator.clipboard?.writeText(view.code)}
|
||||
>
|
||||
Copy code
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import type { Card, ClientMessage, GameView, PlayerView } from '../types'
|
||||
import { CardView } from './CardView'
|
||||
import { SUIT_EMOJI } from '../petArt'
|
||||
|
||||
interface Props {
|
||||
view: GameView
|
||||
you: PlayerView
|
||||
send: (msg: ClientMessage) => void
|
||||
}
|
||||
|
||||
export function ShopPhase({ view, you, send }: Props) {
|
||||
const [selected, setSelected] = useState<string[]>([])
|
||||
const cleanup = view.phase === 'cleanup'
|
||||
const myTurn = !cleanup && view.turn === view.youSeat && you.coins > 0
|
||||
const deck = you.deck ?? []
|
||||
const opponent = view.players.find((p) => p.seat !== view.youSeat)
|
||||
const pending = view.pending
|
||||
const myPending = pending?.playerId === you.id
|
||||
|
||||
// Drop selections that no longer exist (bought/traded/discarded cards).
|
||||
useEffect(() => {
|
||||
setSelected((sel) => sel.filter((id) => deck.some((c) => c.id === id)))
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [you.deck])
|
||||
|
||||
function toggle(id: string) {
|
||||
setSelected((sel) =>
|
||||
sel.includes(id) ? sel.filter((s) => s !== id) : [...sel, id],
|
||||
)
|
||||
}
|
||||
|
||||
const selectedCards = selected
|
||||
.map((id) => deck.find((c) => c.id === id))
|
||||
.filter((c): c is Card => !!c)
|
||||
const sameSuit =
|
||||
selectedCards.length === 3 &&
|
||||
selectedCards.every((c) => c.suit && c.suit === selectedCards[0].suit)
|
||||
const excessPets = you.petCount - view.maxPets
|
||||
const cleanupReady =
|
||||
cleanup &&
|
||||
excessPets > 0 &&
|
||||
selectedCards.length === excessPets &&
|
||||
selectedCards.every((c) => c.kind === 'pet')
|
||||
|
||||
function act(msg: ClientMessage) {
|
||||
send(msg)
|
||||
setSelected([])
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="shop">
|
||||
{/* Status line */}
|
||||
<div className="shop-status">
|
||||
{cleanup ? (
|
||||
excessPets > 0 ? (
|
||||
<span className="status-hot">
|
||||
Too many pets! Discard <strong>{excessPets}</strong> — they become
|
||||
apples 🍎
|
||||
</span>
|
||||
) : (
|
||||
<span className="muted">
|
||||
Waiting for {opponent?.name ?? 'opponent'} to discard down to{' '}
|
||||
{view.maxPets} pets…
|
||||
</span>
|
||||
)
|
||||
) : pending && !myPending ? (
|
||||
<span className="muted">
|
||||
{opponent?.name ?? 'Opponent'} is trading up a tier…
|
||||
</span>
|
||||
) : myTurn ? (
|
||||
<span className="status-hot">Your turn — spend a coin 🪙</span>
|
||||
) : (
|
||||
<span className="muted">
|
||||
{view.players[view.turn]?.name ?? 'Opponent'}’s turn…
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Shop row */}
|
||||
{!cleanup && (
|
||||
<section className="shop-row-wrap">
|
||||
<div className="section-label">
|
||||
Shop · Tier {view.round}
|
||||
<span className="muted"> · {view.deckCounts[view.round - 1]} left in deck</span>
|
||||
</div>
|
||||
<div className="shop-row">
|
||||
{view.shopRow.map((c, i) =>
|
||||
c.id ? (
|
||||
<CardView
|
||||
key={c.id}
|
||||
card={c}
|
||||
size="lg"
|
||||
disabled={!myTurn}
|
||||
onClick={myTurn ? () => act({ type: 'buy', row: i }) : undefined}
|
||||
/>
|
||||
) : (
|
||||
<div key={`empty-${i}`} className="card-slot-empty" />
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
{myTurn && <div className="hint">Tap a card to buy it for 1 🪙</div>}
|
||||
</section>
|
||||
)}
|
||||
|
||||
{/* Your deck */}
|
||||
<section className="deck-wrap">
|
||||
<div className="section-label">
|
||||
Your deck
|
||||
<span className="muted">
|
||||
{' '}
|
||||
· {you.petCount}/{view.maxPets} pets
|
||||
</span>
|
||||
</div>
|
||||
{deck.length === 0 ? (
|
||||
<div className="muted deck-empty">No cards yet — buy something!</div>
|
||||
) : (
|
||||
<div className="deck-row">
|
||||
{deck.map((c) => (
|
||||
<CardView
|
||||
key={c.id}
|
||||
card={c}
|
||||
selected={selected.includes(c.id)}
|
||||
onClick={() => toggle(c.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="actions">
|
||||
{cleanup ? (
|
||||
excessPets > 0 && (
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
disabled={!cleanupReady}
|
||||
onClick={() => act({ type: 'discard', cards: selected })}
|
||||
>
|
||||
Discard {excessPets} pet{excessPets > 1 ? 's' : ''} → 🍎
|
||||
</button>
|
||||
)
|
||||
) : (
|
||||
<>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
disabled={!myTurn || selected.length === 0}
|
||||
onClick={() => act({ type: 'discard', cards: selected })}
|
||||
title="Convert selected cards into apples (+1 power each)"
|
||||
>
|
||||
Discard {selected.length > 0 ? selected.length : ''} → 🍎 (1 🪙)
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
disabled={!myTurn || !sameSuit || view.round >= view.maxRounds}
|
||||
onClick={() => act({ type: 'trade', cards: selected })}
|
||||
title="Trade 3 same-suit pets for a pick from the next tier"
|
||||
>
|
||||
Trade 3 {sameSuit && selectedCards[0].suit ? SUIT_EMOJI[selectedCards[0].suit] : 'matching'} ↑ Tier{' '}
|
||||
{Math.min(view.round + 1, view.maxRounds)} (1 🪙)
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-ghost"
|
||||
disabled={!myTurn}
|
||||
onClick={() => act({ type: 'pass' })}
|
||||
title="Give up your remaining coins"
|
||||
>
|
||||
Pass
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Trade picker */}
|
||||
{myPending && pending && (
|
||||
<div className="modal-backdrop">
|
||||
<div className="modal">
|
||||
<h3>Pick one — the other goes under the tier {pending.tier} deck</h3>
|
||||
<div className="modal-cards">
|
||||
{pending.options.map((c, i) => (
|
||||
<CardView
|
||||
key={c.id}
|
||||
card={c}
|
||||
size="lg"
|
||||
onClick={() => send({ type: 'tradeChoose', pick: i })}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { useGame } from '../useGame'
|
||||
import type { Session } from '../types'
|
||||
import { Lobby } from './Lobby'
|
||||
import { ShopPhase } from './ShopPhase'
|
||||
import { ArrangePhase } from './ArrangePhase'
|
||||
import { BattlePhase } from './BattlePhase'
|
||||
import { GameOver } from './GameOver'
|
||||
|
||||
// Table connects to the game and routes to the right phase screen.
|
||||
export function Table({ session, onLeave }: { session: Session; onLeave: () => void }) {
|
||||
const { view, error, connected, send } = useGame(session)
|
||||
|
||||
if (!view) {
|
||||
return (
|
||||
<div className="centered muted">
|
||||
{connected ? 'Loading game…' : 'Connecting…'}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const you = view.players[view.youSeat]
|
||||
const opponents = view.players.filter((p) => p.seat !== view.youSeat)
|
||||
|
||||
return (
|
||||
<div className="table">
|
||||
<header className="topbar">
|
||||
<div className="topbar-brand" title="Super Auto Pets: The Board Game">
|
||||
🐾 <span>SAP</span>
|
||||
</div>
|
||||
{view.phase !== 'gameover' && (
|
||||
<div className="topbar-round">
|
||||
Round <strong>{view.round}</strong> / {view.maxRounds}
|
||||
</div>
|
||||
)}
|
||||
<div className="topbar-players">
|
||||
{view.players.map((p) => (
|
||||
<div
|
||||
key={p.id}
|
||||
className={`topbar-player ${p.seat === view.youSeat ? 'is-you' : ''}`}
|
||||
>
|
||||
<span className={`conn-dot ${p.connected ? 'on' : 'off'}`} />
|
||||
<span className="topbar-name">{p.name}</span>
|
||||
<span className="chip">🏆 {p.trophies}</span>
|
||||
{(view.phase === 'shop' || view.phase === 'cleanup') && (
|
||||
<span className="chip">🪙 {p.coins}</span>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="topbar-code" title="Share this code with your opponent">
|
||||
{view.code}
|
||||
</div>
|
||||
<button className="btn btn-ghost btn-sm" onClick={onLeave}>
|
||||
Leave
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<main className="table-main">
|
||||
{view.phase === 'lobby' && <Lobby view={view} />}
|
||||
{(view.phase === 'shop' || view.phase === 'cleanup') && (
|
||||
<ShopPhase view={view} you={you} send={send} />
|
||||
)}
|
||||
{view.phase === 'arrange' && <ArrangePhase view={view} you={you} send={send} />}
|
||||
{view.phase === 'battle' && <BattlePhase view={view} send={send} />}
|
||||
{view.phase === 'gameover' && <GameOver view={view} onLeave={onLeave} />}
|
||||
</main>
|
||||
|
||||
{opponents.some((p) => !p.connected) && view.phase !== 'lobby' && (
|
||||
<div className="banner banner-warn">An opponent is disconnected…</div>
|
||||
)}
|
||||
{error && <div className="toast">{error}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import '@fontsource/lilita-one'
|
||||
import '@fontsource/nunito/400.css'
|
||||
import '@fontsource/nunito/700.css'
|
||||
import '@fontsource/nunito/900.css'
|
||||
import './styles.css'
|
||||
import { App } from './App'
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { Suit } from './types'
|
||||
|
||||
const PET_EMOJI: Record<string, string> = {
|
||||
// Tier 1
|
||||
Ant: '🐜', Cricket: '🦗', Fish: '🐟', Horse: '🐴',
|
||||
Beaver: '🦫', Otter: '🦦', Pig: '🐷', Mosquito: '🦟',
|
||||
// Tier 2
|
||||
Crab: '🦀', Swan: '🦢', Hedgehog: '🦔', Peacock: '🦚',
|
||||
Flamingo: '🦩', Rat: '🐀', Shrimp: '🦐', Spider: '🕷️',
|
||||
// Tier 3
|
||||
Dog: '🐶', Badger: '🦡', Camel: '🐫', Giraffe: '🦒',
|
||||
Kangaroo: '🦘', Ox: '🐂', Rabbit: '🐰', Sheep: '🐑',
|
||||
// Tier 4
|
||||
Skunk: '🦨', Hippo: '🦛', Bison: '🦬', Deer: '🦌',
|
||||
Squirrel: '🐿️', Whale: '🐳', Worm: '🪱', Penguin: '🐧',
|
||||
// Tier 5
|
||||
Scorpion: '🦂', Rhino: '🦏', Monkey: '🐒', Cow: '🐄',
|
||||
Seal: '🦭', Shark: '🦈', Turkey: '🦃', Crocodile: '🐊',
|
||||
// Tier 6
|
||||
Leopard: '🐆', Boar: '🐗', Fly: '🪰', Gorilla: '🦍',
|
||||
Mammoth: '🦣', Snake: '🐍', Tiger: '🐯', Dragon: '🐉',
|
||||
// Foods
|
||||
Apple: '🍎',
|
||||
}
|
||||
|
||||
export function artFor(name: string): string {
|
||||
return PET_EMOJI[name] ?? '🐾'
|
||||
}
|
||||
|
||||
export const SUIT_EMOJI: Record<Suit, string> = {
|
||||
sun: '☀️',
|
||||
moon: '🌙',
|
||||
star: '⭐',
|
||||
leaf: '🍃',
|
||||
}
|
||||
@@ -0,0 +1,957 @@
|
||||
/* ============================================================
|
||||
Super Auto Pets: The Board Game — cozy tabletop theme.
|
||||
Deep felt table, cream cards with cocoa borders, chunky type.
|
||||
============================================================ */
|
||||
|
||||
:root {
|
||||
--felt-900: #12351f;
|
||||
--felt-800: #1a4a2b;
|
||||
--felt-700: #226038;
|
||||
--wood: #5b3a1e;
|
||||
--wood-light: #7a5230;
|
||||
--cream: #fdf3dc;
|
||||
--cream-dark: #f3e3bd;
|
||||
--cocoa: #4a2c14;
|
||||
--ink: #33230f;
|
||||
--coral: #ff8a3d;
|
||||
--coral-dark: #e06a1b;
|
||||
--gold: #ffcf5c;
|
||||
--red: #d94a38;
|
||||
--teal: #2f9c8a;
|
||||
--font-display: 'Lilita One', system-ui, sans-serif;
|
||||
--font-body: 'Nunito', system-ui, sans-serif;
|
||||
--card-radius: 12px;
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
html,
|
||||
body,
|
||||
#root {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: var(--font-body);
|
||||
color: var(--cream);
|
||||
background-color: var(--felt-900);
|
||||
background-image:
|
||||
radial-gradient(ellipse at 50% -20%, rgba(255, 255, 255, 0.09), transparent 60%),
|
||||
radial-gradient(ellipse at 50% 120%, rgba(0, 0, 0, 0.45), transparent 60%),
|
||||
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)' opacity='0.05'/%3E%3C/svg%3E"),
|
||||
linear-gradient(160deg, var(--felt-800), var(--felt-900) 70%);
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3 {
|
||||
font-family: var(--font-display);
|
||||
font-weight: 400;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: rgba(253, 243, 220, 0.55);
|
||||
}
|
||||
|
||||
.centered {
|
||||
min-height: 60vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.hint {
|
||||
font-size: 0.9rem;
|
||||
color: rgba(253, 243, 220, 0.65);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.warn-text {
|
||||
color: var(--gold);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/* ---------- buttons ---------- */
|
||||
|
||||
.btn {
|
||||
font-family: var(--font-display);
|
||||
font-size: 1rem;
|
||||
letter-spacing: 0.03em;
|
||||
color: var(--cream);
|
||||
background: var(--wood);
|
||||
border: 3px solid rgba(0, 0, 0, 0.25);
|
||||
border-radius: 12px;
|
||||
padding: 10px 18px;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
|
||||
transition: transform 80ms ease, box-shadow 80ms ease, filter 120ms ease;
|
||||
}
|
||||
|
||||
.btn:hover:not(:disabled) {
|
||||
filter: brightness(1.1);
|
||||
}
|
||||
|
||||
.btn:active:not(:disabled) {
|
||||
transform: translateY(3px);
|
||||
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(180deg, var(--coral), var(--coral-dark));
|
||||
color: #fff;
|
||||
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: linear-gradient(180deg, var(--teal), #227465);
|
||||
}
|
||||
|
||||
.btn-ghost {
|
||||
background: transparent;
|
||||
border-color: rgba(253, 243, 220, 0.3);
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.btn-big {
|
||||
font-size: 1.25rem;
|
||||
padding: 14px 28px;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
font-size: 0.8rem;
|
||||
padding: 4px 10px;
|
||||
border-width: 2px;
|
||||
}
|
||||
|
||||
/* ---------- home ---------- */
|
||||
|
||||
.home {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 20px;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.home-pets {
|
||||
font-size: 2.6rem;
|
||||
letter-spacing: 0.3em;
|
||||
animation: float 3s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes float {
|
||||
0%,
|
||||
100% {
|
||||
transform: translateY(0);
|
||||
}
|
||||
50% {
|
||||
transform: translateY(-8px);
|
||||
}
|
||||
}
|
||||
|
||||
.home-title {
|
||||
font-size: clamp(2.2rem, 6vw, 3.6rem);
|
||||
text-align: center;
|
||||
color: var(--gold);
|
||||
text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
line-height: 1.05;
|
||||
}
|
||||
|
||||
.home-subtitle {
|
||||
font-size: 0.42em;
|
||||
color: var(--cream);
|
||||
letter-spacing: 0.25em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.home-card {
|
||||
background: rgba(0, 0, 0, 0.25);
|
||||
border: 2px solid rgba(253, 243, 220, 0.15);
|
||||
border-radius: 20px;
|
||||
padding: 28px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
width: min(380px, 92vw);
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-weight: 700;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.field input,
|
||||
.code-input {
|
||||
font-family: var(--font-body);
|
||||
font-size: 1.1rem;
|
||||
font-weight: 700;
|
||||
color: var(--ink);
|
||||
background: var(--cream);
|
||||
border: 3px solid var(--cocoa);
|
||||
border-radius: 10px;
|
||||
padding: 10px 12px;
|
||||
outline: none;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.code-input {
|
||||
font-family: var(--font-display);
|
||||
letter-spacing: 0.35em;
|
||||
text-transform: uppercase;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.home-divider {
|
||||
text-align: center;
|
||||
font-size: 0.85rem;
|
||||
color: rgba(253, 243, 220, 0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.home-divider::before,
|
||||
.home-divider::after {
|
||||
content: '';
|
||||
flex: 1;
|
||||
height: 1px;
|
||||
background: rgba(253, 243, 220, 0.2);
|
||||
}
|
||||
|
||||
.home-join {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.home-error {
|
||||
color: #ffb3a7;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* ---------- topbar ---------- */
|
||||
|
||||
.table {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.topbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
padding: 10px 16px;
|
||||
background: linear-gradient(180deg, var(--wood-light), var(--wood));
|
||||
border-bottom: 4px solid rgba(0, 0, 0, 0.35);
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.topbar-brand {
|
||||
font-family: var(--font-display);
|
||||
font-size: 1.2rem;
|
||||
color: var(--gold);
|
||||
}
|
||||
|
||||
.topbar-round {
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.topbar-players {
|
||||
display: flex;
|
||||
gap: 14px;
|
||||
flex: 1;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.topbar-player {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 999px;
|
||||
padding: 4px 12px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.topbar-player.is-you {
|
||||
outline: 2px solid var(--gold);
|
||||
}
|
||||
|
||||
.topbar-name {
|
||||
font-weight: 900;
|
||||
}
|
||||
|
||||
.chip {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.conn-dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.conn-dot.on {
|
||||
background: #6fe08b;
|
||||
}
|
||||
|
||||
.conn-dot.off {
|
||||
background: var(--red);
|
||||
}
|
||||
|
||||
.topbar-code {
|
||||
font-family: var(--font-display);
|
||||
letter-spacing: 0.25em;
|
||||
background: rgba(0, 0, 0, 0.25);
|
||||
border: 2px dashed rgba(253, 243, 220, 0.4);
|
||||
border-radius: 8px;
|
||||
padding: 4px 10px;
|
||||
}
|
||||
|
||||
.table-main {
|
||||
flex: 1;
|
||||
padding: 20px 16px 40px;
|
||||
max-width: 1100px;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
/* ---------- lobby ---------- */
|
||||
|
||||
.lobby {
|
||||
min-height: 60vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 14px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.lobby-bounce {
|
||||
font-size: 3rem;
|
||||
animation: float 2s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.lobby-code {
|
||||
font-family: var(--font-display);
|
||||
font-size: 3rem;
|
||||
letter-spacing: 0.35em;
|
||||
color: var(--gold);
|
||||
background: rgba(0, 0, 0, 0.25);
|
||||
border: 3px dashed rgba(255, 207, 92, 0.5);
|
||||
border-radius: 16px;
|
||||
padding: 12px 28px 12px 40px;
|
||||
}
|
||||
|
||||
/* ---------- cards ---------- */
|
||||
|
||||
.card {
|
||||
position: relative;
|
||||
width: 108px;
|
||||
height: 148px;
|
||||
background: linear-gradient(180deg, var(--cream), var(--cream-dark));
|
||||
border: 3px solid var(--cocoa);
|
||||
border-radius: var(--card-radius);
|
||||
color: var(--ink);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 6px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.35);
|
||||
flex-shrink: 0;
|
||||
transition: transform 120ms ease, box-shadow 120ms ease;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.card-lg {
|
||||
width: 128px;
|
||||
height: 176px;
|
||||
}
|
||||
|
||||
.card-sm {
|
||||
width: 84px;
|
||||
height: 116px;
|
||||
}
|
||||
|
||||
.card.is-clickable {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.card.is-clickable:hover {
|
||||
transform: translateY(-6px) rotate(-1deg);
|
||||
box-shadow: 0 10px 16px rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
|
||||
.card.is-selected {
|
||||
outline: 4px solid var(--gold);
|
||||
transform: translateY(-6px);
|
||||
}
|
||||
|
||||
.card.is-disabled {
|
||||
filter: saturate(0.6) brightness(0.85);
|
||||
}
|
||||
|
||||
.card.is-dead {
|
||||
filter: grayscale(1);
|
||||
}
|
||||
|
||||
.card-top {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
font-size: 0.72rem;
|
||||
}
|
||||
|
||||
.card-tier {
|
||||
font-weight: 900;
|
||||
color: rgba(51, 35, 15, 0.55);
|
||||
}
|
||||
|
||||
.card-suit {
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.card-art {
|
||||
font-size: 3rem;
|
||||
line-height: 1.25;
|
||||
filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.25));
|
||||
}
|
||||
|
||||
.card-lg .card-art {
|
||||
font-size: 3.6rem;
|
||||
}
|
||||
|
||||
.card-sm .card-art {
|
||||
font-size: 2.2rem;
|
||||
}
|
||||
|
||||
.card-name {
|
||||
font-family: var(--font-display);
|
||||
font-size: 0.85rem;
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.card-bottom {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
align-items: center;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.card-power {
|
||||
font-family: var(--font-display);
|
||||
background: radial-gradient(circle at 35% 30%, #ffb347, var(--coral-dark));
|
||||
color: #fff;
|
||||
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
|
||||
border: 2px solid rgba(0, 0, 0, 0.25);
|
||||
border-radius: 50%;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.card-power.is-buffed {
|
||||
background: radial-gradient(circle at 35% 30%, #7be495, #1f9e55);
|
||||
}
|
||||
|
||||
.card-damage {
|
||||
font-family: var(--font-display);
|
||||
color: #fff;
|
||||
background: var(--red);
|
||||
border: 2px solid rgba(0, 0, 0, 0.25);
|
||||
border-radius: 8px;
|
||||
padding: 1px 6px;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
.card-food-text {
|
||||
font-size: 0.72rem;
|
||||
font-weight: 700;
|
||||
color: #7a2e1e;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.card-food {
|
||||
background: linear-gradient(180deg, #ffe9e0, #ffd4c2);
|
||||
}
|
||||
|
||||
.card-check {
|
||||
position: absolute;
|
||||
top: -10px;
|
||||
right: -10px;
|
||||
background: var(--gold);
|
||||
color: var(--ink);
|
||||
font-weight: 900;
|
||||
border: 2px solid var(--cocoa);
|
||||
border-radius: 50%;
|
||||
width: 26px;
|
||||
height: 26px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
.card-slot-empty {
|
||||
width: 128px;
|
||||
height: 176px;
|
||||
border: 3px dashed rgba(253, 243, 220, 0.25);
|
||||
border-radius: var(--card-radius);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* ---------- shop ---------- */
|
||||
|
||||
.shop {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 22px;
|
||||
}
|
||||
|
||||
.shop-status {
|
||||
text-align: center;
|
||||
font-size: 1.15rem;
|
||||
font-weight: 900;
|
||||
min-height: 1.6em;
|
||||
}
|
||||
|
||||
.status-hot {
|
||||
color: var(--gold);
|
||||
animation: pulse 1.6s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%,
|
||||
100% {
|
||||
opacity: 1;
|
||||
}
|
||||
50% {
|
||||
opacity: 0.65;
|
||||
}
|
||||
}
|
||||
|
||||
.section-label {
|
||||
font-family: var(--font-display);
|
||||
font-size: 1rem;
|
||||
margin-bottom: 10px;
|
||||
color: rgba(253, 243, 220, 0.9);
|
||||
}
|
||||
|
||||
.shop-row-wrap,
|
||||
.deck-wrap {
|
||||
background: rgba(0, 0, 0, 0.18);
|
||||
border: 2px solid rgba(253, 243, 220, 0.1);
|
||||
border-radius: 18px;
|
||||
padding: 14px 16px;
|
||||
}
|
||||
|
||||
.shop-row,
|
||||
.deck-row {
|
||||
display: flex;
|
||||
gap: 14px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.deck-empty {
|
||||
text-align: center;
|
||||
padding: 20px 0;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
justify-content: center;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
/* ---------- modal ---------- */
|
||||
|
||||
.modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
z-index: 50;
|
||||
}
|
||||
|
||||
.modal {
|
||||
background: linear-gradient(180deg, var(--felt-700), var(--felt-800));
|
||||
border: 3px solid rgba(253, 243, 220, 0.25);
|
||||
border-radius: 20px;
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 18px;
|
||||
max-width: 92vw;
|
||||
}
|
||||
|
||||
.modal-cards {
|
||||
display: flex;
|
||||
gap: 18px;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
/* ---------- arrange ---------- */
|
||||
|
||||
.arrange {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.arrange-row {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
background: rgba(0, 0, 0, 0.18);
|
||||
border: 2px solid rgba(253, 243, 220, 0.1);
|
||||
border-radius: 18px;
|
||||
padding: 18px 16px;
|
||||
min-height: 220px;
|
||||
}
|
||||
|
||||
.arrange-marker {
|
||||
font-family: var(--font-display);
|
||||
color: var(--gold);
|
||||
writing-mode: vertical-rl;
|
||||
transform: rotate(180deg);
|
||||
font-size: 0.9rem;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.arrange-card {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
cursor: grab;
|
||||
}
|
||||
|
||||
.arrange-card:active {
|
||||
cursor: grabbing;
|
||||
}
|
||||
|
||||
.arrange-arrows {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
/* ---------- battle ---------- */
|
||||
|
||||
.battle {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.battle-header {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.battle-names {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 18px;
|
||||
font-weight: 900;
|
||||
}
|
||||
|
||||
.battle-vs {
|
||||
font-family: var(--font-display);
|
||||
color: var(--gold);
|
||||
}
|
||||
|
||||
.battlefield {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
background:
|
||||
radial-gradient(ellipse at center, rgba(255, 207, 92, 0.07), transparent 65%),
|
||||
rgba(0, 0, 0, 0.2);
|
||||
border: 2px solid rgba(253, 243, 220, 0.1);
|
||||
border-radius: 18px;
|
||||
padding: 26px 12px;
|
||||
min-height: 240px;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.battle-center {
|
||||
font-size: 1.6rem;
|
||||
opacity: 0.5;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.battle-side {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
/* Front units meet in the middle: left side is reversed so index 0 sits
|
||||
next to the center. */
|
||||
.battle-side-left {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.battle-side-right {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.battle-unit {
|
||||
position: relative;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.battle-unit .card {
|
||||
width: 96px;
|
||||
height: 132px;
|
||||
}
|
||||
|
||||
.battle-unit.is-front .card {
|
||||
outline: 3px solid rgba(255, 207, 92, 0.6);
|
||||
}
|
||||
|
||||
@keyframes clash-left {
|
||||
0% {
|
||||
transform: translateX(0);
|
||||
}
|
||||
35% {
|
||||
transform: translateX(26px) rotate(4deg);
|
||||
}
|
||||
60% {
|
||||
transform: translateX(-6px);
|
||||
}
|
||||
100% {
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes clash-right {
|
||||
0% {
|
||||
transform: translateX(0);
|
||||
}
|
||||
35% {
|
||||
transform: translateX(-26px) rotate(-4deg);
|
||||
}
|
||||
60% {
|
||||
transform: translateX(6px);
|
||||
}
|
||||
100% {
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
.battle-unit.clash-left {
|
||||
animation: clash-left 500ms ease;
|
||||
}
|
||||
|
||||
.battle-unit.clash-right {
|
||||
animation: clash-right 500ms ease;
|
||||
}
|
||||
|
||||
@keyframes dying {
|
||||
to {
|
||||
opacity: 0;
|
||||
transform: translateY(30px) rotate(12deg) scale(0.85);
|
||||
}
|
||||
}
|
||||
|
||||
.battle-unit.unit-dying {
|
||||
animation: dying 700ms ease 500ms forwards;
|
||||
}
|
||||
|
||||
@keyframes damage-pop {
|
||||
0% {
|
||||
opacity: 0;
|
||||
transform: translate(-50%, 0) scale(0.6);
|
||||
}
|
||||
25% {
|
||||
opacity: 1;
|
||||
transform: translate(-50%, -16px) scale(1.15);
|
||||
}
|
||||
100% {
|
||||
opacity: 0;
|
||||
transform: translate(-50%, -44px) scale(1);
|
||||
}
|
||||
}
|
||||
|
||||
.damage-pop {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
font-family: var(--font-display);
|
||||
font-size: 1.4rem;
|
||||
color: #ff6b52;
|
||||
text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5);
|
||||
animation: damage-pop 1100ms ease 250ms forwards;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
z-index: 5;
|
||||
}
|
||||
|
||||
.battle-result {
|
||||
text-align: center;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
animation: result-in 400ms ease;
|
||||
}
|
||||
|
||||
@keyframes result-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(16px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.battle-result-title {
|
||||
font-family: var(--font-display);
|
||||
font-size: 2.4rem;
|
||||
}
|
||||
|
||||
.battle-result.is-win .battle-result-title {
|
||||
color: var(--gold);
|
||||
}
|
||||
|
||||
.battle-result.is-loss .battle-result-title {
|
||||
color: #ff8f7a;
|
||||
}
|
||||
|
||||
.battle-result.is-draw .battle-result-title {
|
||||
color: var(--teal);
|
||||
}
|
||||
|
||||
.battle-result-sub {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/* ---------- game over ---------- */
|
||||
|
||||
.gameover {
|
||||
min-height: 60vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 18px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.gameover-emoji {
|
||||
font-size: 4rem;
|
||||
}
|
||||
|
||||
.gameover-title {
|
||||
font-size: 3rem;
|
||||
color: var(--gold);
|
||||
text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
|
||||
}
|
||||
|
||||
.gameover-scores {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
background: rgba(0, 0, 0, 0.22);
|
||||
border-radius: 16px;
|
||||
padding: 18px 26px;
|
||||
min-width: min(360px, 90vw);
|
||||
}
|
||||
|
||||
.score-line {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 24px;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.score-line.is-you .score-name {
|
||||
color: var(--gold);
|
||||
font-weight: 900;
|
||||
}
|
||||
|
||||
/* ---------- toasts & banners ---------- */
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
bottom: 24px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: var(--red);
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
border-radius: 12px;
|
||||
padding: 10px 20px;
|
||||
box-shadow: 0 6px 14px rgba(0, 0, 0, 0.4);
|
||||
animation: result-in 200ms ease;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.banner {
|
||||
position: fixed;
|
||||
top: 64px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
border-radius: 10px;
|
||||
padding: 6px 16px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 700;
|
||||
z-index: 90;
|
||||
}
|
||||
|
||||
.banner-warn {
|
||||
background: rgba(217, 74, 56, 0.9);
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.card {
|
||||
width: 92px;
|
||||
height: 128px;
|
||||
}
|
||||
.card-lg {
|
||||
width: 104px;
|
||||
height: 146px;
|
||||
}
|
||||
.battle-unit .card {
|
||||
width: 78px;
|
||||
height: 110px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// Mirrors of the Go view types (internal/game/view.go).
|
||||
|
||||
export type Suit = 'sun' | 'moon' | 'star' | 'leaf'
|
||||
export type CardKind = 'pet' | 'food'
|
||||
export type Phase = 'lobby' | 'shop' | 'cleanup' | 'arrange' | 'battle' | 'gameover'
|
||||
|
||||
export interface Card {
|
||||
id: string
|
||||
kind: CardKind
|
||||
name: string
|
||||
tier: number
|
||||
power?: number
|
||||
suit?: Suit
|
||||
effect?: string
|
||||
food?: string
|
||||
}
|
||||
|
||||
export interface PlayerView {
|
||||
id: string
|
||||
name: string
|
||||
seat: number
|
||||
coins: number
|
||||
trophies: number
|
||||
ready: boolean
|
||||
connected: boolean
|
||||
deckSize: number
|
||||
petCount: number
|
||||
deck?: Card[]
|
||||
}
|
||||
|
||||
export interface PendingTrade {
|
||||
playerId: string
|
||||
tier: number
|
||||
options: [Card, Card]
|
||||
}
|
||||
|
||||
export interface BattleUnit {
|
||||
card: Card
|
||||
foods: Card[] | null
|
||||
bonus: number
|
||||
damage: number
|
||||
}
|
||||
|
||||
export interface BattleEvent {
|
||||
type: 'clash'
|
||||
units: number[]
|
||||
damage: number[]
|
||||
died: boolean[]
|
||||
}
|
||||
|
||||
export interface BattleResult {
|
||||
round: number
|
||||
lineups: BattleUnit[][]
|
||||
wastedFoods: (Card[] | null)[]
|
||||
events: BattleEvent[] | null
|
||||
winnerSeat: number
|
||||
trophies: number
|
||||
}
|
||||
|
||||
export interface GameView {
|
||||
gameId: string
|
||||
code: string
|
||||
phase: Phase
|
||||
round: number
|
||||
maxRounds: number
|
||||
maxPets: number
|
||||
youSeat: number
|
||||
turn: number
|
||||
shopRow: Card[]
|
||||
deckCounts: number[]
|
||||
players: PlayerView[]
|
||||
pending?: PendingTrade
|
||||
battle?: BattleResult
|
||||
winnerSeat: number
|
||||
}
|
||||
|
||||
export type ClientMessage =
|
||||
| { type: 'buy'; row: number }
|
||||
| { type: 'discard'; cards: string[] }
|
||||
| { type: 'trade'; cards: string[] }
|
||||
| { type: 'tradeChoose'; pick: number }
|
||||
| { type: 'pass' }
|
||||
| { type: 'arrange'; order: string[] }
|
||||
| { type: 'ready' }
|
||||
|
||||
export interface Session {
|
||||
gameId: string
|
||||
code: string
|
||||
playerId: string
|
||||
token: string
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import type { ClientMessage, GameView, Session } from './types'
|
||||
|
||||
interface ServerMessage {
|
||||
type: 'state' | 'error'
|
||||
state?: GameView
|
||||
error?: string
|
||||
}
|
||||
|
||||
// useGame owns the WebSocket for a session: it keeps the latest server view,
|
||||
// reconnects with backoff, and exposes send() for actions. Server-rejected
|
||||
// actions surface as a transient `error`.
|
||||
export function useGame(session: Session) {
|
||||
const [view, setView] = useState<GameView | null>(null)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [connected, setConnected] = useState(false)
|
||||
const wsRef = useRef<WebSocket | null>(null)
|
||||
const errorTimer = useRef<number>(undefined)
|
||||
|
||||
useEffect(() => {
|
||||
let ws: WebSocket
|
||||
let closed = false
|
||||
let retryDelay = 500
|
||||
let retryTimer: number | undefined
|
||||
|
||||
function connect() {
|
||||
const proto = location.protocol === 'https:' ? 'wss' : 'ws'
|
||||
const params = new URLSearchParams({
|
||||
game: session.gameId,
|
||||
player: session.playerId,
|
||||
token: session.token,
|
||||
})
|
||||
ws = new WebSocket(`${proto}://${location.host}/api/ws?${params}`)
|
||||
wsRef.current = ws
|
||||
ws.onopen = () => {
|
||||
retryDelay = 500
|
||||
setConnected(true)
|
||||
}
|
||||
ws.onmessage = (ev) => {
|
||||
const msg = JSON.parse(ev.data) as ServerMessage
|
||||
if (msg.type === 'state' && msg.state) setView(msg.state)
|
||||
if (msg.type === 'error' && msg.error) showError(msg.error)
|
||||
}
|
||||
ws.onclose = () => {
|
||||
setConnected(false)
|
||||
if (closed) return
|
||||
retryTimer = window.setTimeout(connect, retryDelay)
|
||||
retryDelay = Math.min(retryDelay * 2, 8000)
|
||||
}
|
||||
}
|
||||
|
||||
function showError(msg: string) {
|
||||
setError(msg)
|
||||
window.clearTimeout(errorTimer.current)
|
||||
errorTimer.current = window.setTimeout(() => setError(null), 3500)
|
||||
}
|
||||
|
||||
connect()
|
||||
return () => {
|
||||
closed = true
|
||||
window.clearTimeout(retryTimer)
|
||||
window.clearTimeout(errorTimer.current)
|
||||
ws.close()
|
||||
}
|
||||
}, [session.gameId, session.playerId, session.token])
|
||||
|
||||
const send = useCallback((msg: ClientMessage) => {
|
||||
wsRef.current?.send(JSON.stringify(msg))
|
||||
}, [])
|
||||
|
||||
return { view, error, connected, send }
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"isolatedModules": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"root":["./src/App.tsx","./src/api.ts","./src/main.tsx","./src/petArt.ts","./src/types.ts","./src/useGame.ts","./src/components/ArrangePhase.tsx","./src/components/BattlePhase.tsx","./src/components/CardView.tsx","./src/components/GameOver.tsx","./src/components/Home.tsx","./src/components/Lobby.tsx","./src/components/ShopPhase.tsx","./src/components/Table.tsx"],"version":"5.9.3"}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
// The Go server owns /api (including the WebSocket); Vite proxies to it in
|
||||
// dev so the frontend can be served with hot reload on :5173.
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:8080',
|
||||
ws: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user