Skip to content

Commit 7628231

Browse files
authored
Merge pull request #8 from tldrwtf/feature-dev-mp
Add comprehensive tests for PokeDo FastAPI server functionality
2 parents 7beac02 + 781f968 commit 7628231

28 files changed

+7565
-159
lines changed

README.md

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A Pokemon-themed CLI task manager and wellbeing tracker. Complete tasks to catch Pokemon, build your collection, and track your mental and physical wellbeing.
44

5-
**Version:** 0.3.3 | **License:** MIT | **Python:** 3.10+
5+
**Version:** 0.4.0 | **License:** MIT | **Python:** 3.10+
66

77
![CI](https://github.com/tldrwtf/pokedo/actions/workflows/ci.yml/badge.svg?branch=main) ![Version](https://badgen.net/github/release/tldrwtf/pokedo/stable)
88

@@ -12,7 +12,8 @@ A Pokemon-themed CLI task manager and wellbeing tracker. Complete tasks to catch
1212
- [Installation](#installation)
1313
- [Quick Start](#quick-start)
1414
- [Usage](#usage)
15-
- [Server Usage (Optional)](#server-usage-optional)
15+
- [Multiplayer (PvP Battles)](#multiplayer-pvp-battles)
16+
- [Server Usage](#server-usage)
1617
- [How It Works](#how-it-works)
1718
- [Data Storage](#data-storage)
1819
- [Development](#development)
@@ -61,6 +62,20 @@ A Pokemon-themed CLI task manager and wellbeing tracker. Complete tasks to catch
6162
- Achievement badges
6263
- Inventory system (Pokeballs, evolution items)
6364

65+
### Multiplayer (PvP)
66+
67+
- **Async turn-based battles** against other trainers via a self-hosted server
68+
- **Three battle formats**: 1v1, 3v3, and 6v6 singles
69+
- **Gen V+ damage formula** with STAB, type effectiveness, critical hits
70+
- **18-type chart** with full dual-type support (0x through 4x)
71+
- **25 natures** affecting stat calculations
72+
- **Status effects**: burn, poison, badly poisoned, paralysis, sleep, freeze
73+
- **Move mechanics**: priority, drain, recoil, Protect, secondary effects
74+
- **ELO rating system** (K=32) with rank progression (Youngster to Pokemon Master)
75+
- **Global leaderboard** sortable by ELO, wins, or losses
76+
- Server-authoritative -- clients send intents, server resolves outcomes
77+
- Snapshot-based teams -- local roster changes don't affect in-flight battles
78+
6479
### Terminal User Interface (TUI)
6580

6681
- Interactive dashboard with trainer profile, team, and task summaries
@@ -264,38 +279,92 @@ The task screen provides full CRUD operations with tabbed filtering:
264279

265280
Completing a task in the TUI triggers the full encounter flow with XP rewards, streak updates, and Pokemon catching, just like the CLI.
266281

282+
### Multiplayer (PvP Battles)
283+
284+
Requires a running PokeDo server. See [Server Usage](#server-usage-optional) below.
285+
286+
```bash
287+
# Register an account
288+
pokedo battle register -u myname -p mypass
289+
290+
# Challenge another trainer
291+
pokedo battle challenge opponent_name -u myname -p mypass
292+
293+
# Accept a challenge (as the opponent)
294+
pokedo battle accept <battle-id> -u opponent -p theirpass
295+
296+
# Submit your team
297+
pokedo battle team <battle-id> -u myname -p mypass
298+
299+
# Attack with a move (index 0-3)
300+
pokedo battle move <battle-id> -m 0 -u myname -p mypass
301+
302+
# Switch to a different Pokemon (team slot index)
303+
pokedo battle switch <battle-id> 1 -u myname -p mypass
304+
305+
# Forfeit
306+
pokedo battle forfeit <battle-id> -u myname -p mypass
307+
308+
# Check battle status
309+
pokedo battle status <battle-id> -u myname -p mypass
310+
311+
# View battle history
312+
pokedo battle history -u myname -p mypass
313+
```
314+
315+
### Leaderboard
316+
317+
```bash
318+
# Global leaderboard (sorted by ELO by default)
319+
pokedo leaderboard show
320+
321+
# Sort by wins
322+
pokedo leaderboard show --sort battle_wins
323+
324+
# Your own profile
325+
pokedo leaderboard me -u myname
326+
```
327+
267328
### Server Usage (Optional)
268329

269-
PokeDo is developing a FastAPI server to enable cloud synchronization and multi-user features. This system uses `requests` for client-side pushing and `bcrypt` for secure authentication.
330+
PokeDo includes a FastAPI server for multiplayer battles, leaderboard tracking,
331+
and cloud synchronization. The server uses PostgreSQL for persistent state and
332+
the `lifespan` context manager pattern.
270333

271-
1. **Run the Server:**
334+
1. **Start with Docker Compose (recommended):**
272335

273336
```bash
274-
uvicorn pokedo.server:app --reload --port 8000
337+
docker-compose up -d
275338
```
276339

277-
(Ensure you have installed development dependencies: `pip install -e ".[dev]"`)
340+
This starts both PostgreSQL and the PokeDo server.
278341

279-
2. **Register a User:**
342+
2. **Or run manually:**
280343

281344
```bash
282-
curl -X POST http://localhost:8000/register -H "Content-Type: application/json" -d "{\"username\": \"testuser\", \"password\": \"testpassword\"}"
345+
# Start PostgreSQL separately, then:
346+
uvicorn pokedo.server:app --reload --port 8000
283347
```
284348

285-
3. **Login and Get an Access Token:**
349+
(Ensure you have installed development dependencies: `pip install -e ".[dev]"`)
286350

287-
```bash
288-
curl -X POST http://localhost:8000/token -F "username=testuser" -F "password=testpassword"
289-
```
351+
3. **Environment variables:**
352+
353+
| Variable | Default | Description |
354+
|----------|---------|-------------|
355+
| `POKEDO_DATABASE_URL` | `postgresql://pokedo:pokedopass@localhost:5432/pokedo` | Postgres connection |
356+
| `POKEDO_SECRET_KEY` | `your-secret-key-keep-it-secret` | JWT signing secret |
357+
| `POKEDO_SERVER_URL` | `http://localhost:8000` | Server URL for CLI |
290358

291-
This will return a JSON object containing your `access_token`.
359+
4. **Register and battle:**
292360

293-
4. **Access Protected Endpoints (e.g., /users/me or /sync):**
294-
Replace `<YOUR_ACCESS_TOKEN>` with the token received from the login step.
295361
```bash
296-
curl -X GET http://localhost:8000/users/me -H "Authorization: Bearer <YOUR_ACCESS_TOKEN>"
362+
pokedo battle register -u myname -p mypassword
363+
pokedo battle challenge opponent -u myname -p mypassword
297364
```
298365

366+
See [MULTIPLAYER.md](docs/MULTIPLAYER.md) for the full battle flow.
367+
299368
## How It Works
300369

301370
### Catching Pokemon
@@ -376,10 +445,12 @@ Wellbeing actions also affect type encounters:
376445
377446
## Recent Fixes & Behavior Notes
378447
379-
- **EV/IV persistence**: Pokemon now persist their EV/IV dictionaries, so every completion permanently boosts the lead Pokémon’s stats (see `pokedo/data/database.py` and `pokedo/core/pokemon.py`).
380-
- **Affinity-aware encounters**: The reward engine filters rarity pools by task/wellbeing affinities, backfills missing type data (`_ensure_pokedex_entry_types`), and consumes the best available ball (master/ultra/great) for each attempt (`pokedo/core/rewards.py`).
381-
- **Pokedex tracking parity**: Every catch or evolution increments `pokedex_seen`/`pokedex_caught` (with first-caught timestamps and shiny flags), keeping trainer completion metrics in sync (`pokedo/cli/commands/tasks.py`, `pokedo/cli/commands/pokemon.py`).
382-
- **Priority ordering & streak sync**: Task listings now sort using explicit numeric weights, and streak best counters update immediately on first-day or resumed streaks (`pokedo/data/database.py`, `pokedo/core/trainer.py`).
448+
- **Multiplayer system**: Full async turn-based PvP battles with ELO-based leaderboard. FastAPI server backed by PostgreSQL, using the `lifespan` context manager (not deprecated `on_event`). Server-authoritative battle resolution with snapshot-based teams.
449+
- **Battle engine**: Gen V+ damage formula, 18-type effectiveness chart, 25 natures, move priority system, status effects (burn/poison/paralysis/sleep/freeze/badly poisoned), drain/recoil mechanics, Protect, mutual KO draw support.
450+
- **EV/IV persistence**: Pokemon now persist their EV/IV dictionaries, so every completion permanently boosts the lead Pokemon's stats.
451+
- **Affinity-aware encounters**: The reward engine filters rarity pools by task/wellbeing affinities, backfills missing type data, and consumes the best available ball for each attempt.
452+
- **Pokedex tracking parity**: Every catch or evolution increments `pokedex_seen`/`pokedex_caught` (with first-caught timestamps and shiny flags).
453+
- **Priority ordering & streak sync**: Task listings sort using explicit numeric weights, and streak best counters update immediately on first-day or resumed streaks.
383454

384455
### EV/IV System
385456

@@ -403,7 +474,6 @@ This system provides RPG mechanics for training your Pokemon's stats:
403474

404475
| Difficulty | EV Yield |
405476
| ---------- | -------- |
406-
|------------|----------|
407477
| Easy | 1 EV |
408478
| Medium | 2 EVs |
409479
| Hard | 4 EVs |
@@ -419,25 +489,29 @@ All data is stored locally in `~/.pokedo/`:
419489

420490
## Development
421491

422-
The project includes a FastAPI server (`pokedo/server.py`) for future cloud synchronization and currently provides **user authentication (registration/login with JWT)**.
492+
The project includes a FastAPI server (`pokedo/server.py`) for multiplayer battles, a global leaderboard, and cloud synchronization. The server uses PostgreSQL via SQLModel and JWT-based authentication.
423493

424494
```bash
425495
# Install with dev dependencies
426496
pip install -e ".[dev]"
427497
428-
# Run tests
498+
# Run all tests (556 tests)
429499
pytest
430500
431501
# Run tests with coverage
432502
pytest --cov=pokedo
433503
434504
# Run specific test file
435-
pytest tests/test_tasks.py
505+
pytest tests/test_battle.py
506+
507+
# Run multiplayer tests only
508+
pytest tests/test_moves.py tests/test_battle.py tests/test_server.py -v
436509
```
437510

438511
For more development information, see:
439512

440513
- [Architecture Documentation](docs/ARCHITECTURE.md) - System design and code structure
514+
- [Multiplayer Guide](docs/MULTIPLAYER.md) - PvP battles, leaderboard, and server API
441515
- [Contributing Guide](docs/CONTRIBUTING.md) - How to contribute to PokeDo
442516
- [API Reference](docs/API.md) - Internal API documentation
443517

@@ -518,6 +592,18 @@ A: Yes. Each trainer profile is stored in the same local database. The CLI uses
518592
**Q: Does wellbeing tracking affect gameplay?**
519593
A: Yes! Good sleep improves catch rates, hydration goals boost Water-type encounters, and meditation increases Psychic/Fairy encounters.
520594
595+
**Q: How do I battle other players?**
596+
A: Start the PokeDo server (see [Multiplayer Guide](docs/MULTIPLAYER.md)), register an account, and use the `pokedo battle` commands. Battles are async and turn-based -- you and your opponent submit actions independently, and the server resolves each turn.
597+
598+
**Q: Do I need a server to play?**
599+
A: No. All single-player features (tasks, Pokemon, wellbeing) work fully offline with a local SQLite database. The server is only needed for PvP battles and the leaderboard.
600+
601+
**Q: What battle formats are available?**
602+
A: Three formats: `singles_1v1` (1 Pokemon), `singles_3v3` (3 Pokemon, one active), and `singles_6v6` (full team, one active). Doubles and tournaments are planned for the future.
603+
604+
**Q: How does the ELO rating work?**
605+
A: Starting rating is 1000 with K-factor 32. Winning against higher-rated opponents earns more points. Ranks range from Youngster (below 1100) to Pokemon Master (2100+).
606+
521607
**Q: How do I evolve Pokemon?**
522608
A: Level up your Pokemon by completing tasks. When evolution requirements are met, use `pokedo pokemon evolve <id>`.
523609
@@ -530,15 +616,16 @@ A: The CLI (Command Line Interface) uses typed commands like `pokedo task add`.
530616
531617
```
532618
pokedo/
533-
├── cli/ # Command-line interface
534-
├── core/ # Business logic and models
535-
├── data/ # Database and API clients
619+
├── cli/ # Command-line interface (tasks, pokemon, battle, leaderboard)
620+
├── core/ # Business logic, models, battle engine, move system
621+
├── data/ # Database, API clients, server models
536622
├── tui/ # Terminal user interface (Textual)
537623
│ ├── app.py # Main TUI application
538624
│ ├── screens/ # Screen classes (tasks, etc.)
539625
│ ├── widgets/ # Reusable UI components
540626
│ └── styles/ # Textual CSS styling
541-
└── utils/ # Configuration and helpers
627+
├── server.py # FastAPI server (auth, battles, leaderboard, sync)
628+
└── utils/ # Configuration, helpers, sprite rendering
542629
```
543630
544631
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for detailed documentation.

docker-compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,16 @@ services:
1111
volumes:
1212
- pokedo_pgdata:/var/lib/postgresql/data
1313

14+
server:
15+
build: .
16+
command: uvicorn pokedo.server:app --host 0.0.0.0 --port 8000
17+
ports:
18+
- "8000:8000"
19+
environment:
20+
POKEDO_DATABASE_URL: "postgresql://pokedo:pokedopass@db:5432/pokedo"
21+
POKEDO_SECRET_KEY: "change-me-in-production"
22+
depends_on:
23+
- db
24+
1425
volumes:
1526
pokedo_pgdata:

0 commit comments

Comments
 (0)