Files
Code_of_Conquest/api
Phillip Tarrant a38906b445 feat(api): integrate equipment stats into combat damage system
Equipment-Combat Integration:
- Update Stats damage formula from STR//2 to int(STR*0.75) for better scaling
- Add spell_power system for magical weapons (staves, wands)
- Add spell_power_bonus field to Stats model with spell_power property
- Add spell_power field to Item model with is_magical_weapon() method
- Update Character.get_effective_stats() to populate spell_power_bonus

Combatant Model Updates:
- Add weapon property fields (crit_chance, crit_multiplier, damage_type)
- Add elemental weapon support (elemental_damage_type, physical_ratio, elemental_ratio)
- Update serialization to handle new weapon properties

DamageCalculator Refactoring:
- Remove weapon_damage parameter from calculate_physical_damage()
- Use attacker_stats.damage directly (includes weapon bonus)
- Use attacker_stats.spell_power for magical damage calculations

Combat Service Updates:
- Extract weapon properties in _create_combatant_from_character()
- Use stats.damage_bonus for enemy combatants from templates
- Remove hardcoded _get_weapon_damage() method
- Handle elemental weapons with split damage in _execute_attack()

Item Generation Updates:
- Add base_spell_power to BaseItemTemplate dataclass
- Add ARCANE damage type to DamageType enum
- Add magical weapon templates (wizard_staff, arcane_staff, wand, crystal_wand)

Test Updates:
- Update test_stats.py for new damage formula (0.75 scaling)
- Update test_character.py for equipment bonus calculations
- Update test_damage_calculator.py for new API signatures
- Update test_combat_service.py mock fixture for equipped attribute

Tests: 174 passing
2025-11-26 19:54:58 -06:00
..
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00
2025-11-24 23:10:55 -06:00

Code of Conquest - API Backend

Flask-based REST API backend for Code of Conquest, an AI-powered D&D-style game.

Overview

This is the API backend component of Code of Conquest. It provides:

  • RESTful API endpoints for game functionality
  • Business logic and game mechanics
  • Database operations (Appwrite)
  • AI integration (Claude, Replicate)
  • Background job processing (RQ + Redis)
  • Authentication and authorization

Architecture

Tech Stack:

  • Framework: Flask 3.x
  • Database: Appwrite (NoSQL)
  • Job Queue: RQ (Redis Queue)
  • Cache: Redis
  • AI: Anthropic Claude, Replicate
  • Logging: Structlog
  • WSGI Server: Gunicorn

Key Components:

  • /app/api - API endpoint blueprints
  • /app/models - Data models (dataclasses)
  • /app/services - Business logic and external integrations
  • /app/utils - Helper functions
  • /app/tasks - Background job handlers
  • /app/data - Game data (YAML files)

Setup

Prerequisites

  • Python 3.11+
  • Redis server
  • Appwrite instance (cloud or self-hosted)

Installation

  1. Create virtual environment:
python3 -m venv venv
source venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure environment:
cp .env.example .env
# Edit .env with your credentials
  1. Initialize database:
python scripts/init_database.py

Running Locally

Development mode:

# Make sure Redis is running
docker-compose up -d

# Activate virtual environment
source venv/bin/activate

# Set environment
export FLASK_ENV=development

# Run development server
python wsgi.py

The API will be available at http://localhost:5000

Production mode:

gunicorn --bind 0.0.0.0:5000 --workers 4 wsgi:app

Configuration

Environment-specific configs are in /config:

  • development.yaml - Local development settings
  • production.yaml - Production settings

Key settings:

  • Server: Port, workers
  • Redis: Connection settings
  • RQ: Queue configuration
  • AI: Model settings, rate limits
  • Auth: Session, password requirements
  • CORS: Allowed origins

API Documentation

See API_REFERENCE.md for complete endpoint documentation.

Base URL

  • Development: http://localhost:5000
  • Production: https://api.codeofconquest.com

Authentication

Uses Appwrite session-based authentication with HTTP-only cookies.

Response Format

All endpoints return standardized JSON responses:

{
  "app": "Code of Conquest",
  "version": "0.1.0",
  "status": 200,
  "timestamp": "2025-01-15T10:30:00Z",
  "result": {...},
  "error": null
}

Testing

Run tests with pytest:

# Activate virtual environment
source venv/bin/activate

# Run all tests
pytest

# Run with coverage
pytest --cov=app tests/

# Run specific test file
pytest tests/test_character.py

Background Jobs

The API uses RQ for background processing:

Start RQ worker:

rq worker ai_tasks combat_tasks marketplace_tasks --url redis://localhost:6379

Monitor jobs:

rq info --url redis://localhost:6379

Directory Structure

api/
├── app/                    # Application code
│   ├── api/               # API endpoint blueprints
│   ├── models/            # Data models
│   ├── services/          # Business logic
│   ├── utils/             # Utilities
│   ├── tasks/             # Background jobs
│   ├── ai/                # AI integration
│   ├── game_logic/        # Game mechanics
│   └── data/              # Game data (YAML)
├── config/                # Configuration files
├── tests/                 # Test suite
├── scripts/               # Utility scripts
├── logs/                  # Application logs
├── docs/                  # API documentation
├── requirements.txt       # Python dependencies
├── wsgi.py               # WSGI entry point
├── docker-compose.yml    # Redis service
└── .env.example          # Environment template

Development

See CLAUDE.md in the project root for:

  • Coding standards
  • Development workflow
  • Project structure guidelines
  • Git conventions

Deployment

See DEPLOYMENT.md for production deployment instructions.

  • Public Web: /public_web - Traditional web frontend
  • Godot Client: /godot_client - Native game client

Both frontends consume this API backend.

License

Proprietary - All rights reserved