shared-code-check

Ensures shared logic goes to loh-libs instead of being duplicated

Shared Code Guardrail

Code Placement Decision Tree

When writing new code, ask:

1. Is this SHARED between loh-game AND loh-backend?
   └─> YES → loh-libs/rust/shared-protocol/

2. Is this UI-specific (Egui, Bevy rendering, input handling)?
   └─> YES → loh-game/src/systems/ui/ or relevant system

3. Is this backend-specific (API handlers, database, auth)?
   └─> YES → loh-backend/

Infrastructure Context

PhaseBackend StackNotes
CurrentCloudflare Workers (D1, KV, R2)Free tier, rapid iteration
AlphaRust microservices + game monolithWhen game is alpha-ready

What Goes to loh-libs

  • DTOs (Data Transfer Objects) for API communication
  • Enums shared between client/server (e.g., OrderSide, SkillType)
  • Validation logic that must be consistent
  • Math/calculation utilities used by both
  • Combat damage formulas, XP tables, loot calculations

What Stays in loh-game

  • Bevy-specific: Component, Resource, SystemParam
  • UI rendering (Egui widgets, panels, themes)
  • Input handling, camera controls
  • VFX, audio, rendering systems
  • Game loop, player state

What Stays in loh-backend

  • sqlx derives (FromRow, Type)
  • Cloudflare Worker handlers
  • Database migrations, API routes
  • Authentication (JWT, sessions)
  • Payment processing

Check Before Adding Types

  1. Does this type appear in both codebases?
  2. Does it need to serialize/deserialize for network communication?
  3. Is validation logic duplicated?
If YES to any → move to loh-libs/rust/shared-protocol/src/types/

Directory Structure

loh-libs/rust/shared-protocol/src/types/
├── combat.rs    # Combat stats, damage types
├── economy.rs   # Market orders, snapshots (OrderSide, MarketSnapshot)
├── item.rs      # Item definitions
├── loot.rs      # Loot tables
├── npc.rs       # NPC data
└── skill.rs     # Skill types

Adding New Shared Types

  1. Create/edit file in shared-protocol/src/types/
  2. Add pub mod X; to types/mod.rs
  3. Add pub use types::X::*; to lib.rs
  4. Update both loh-game/Cargo.toml and loh-backend/*/Cargo.toml if needed

Automated Check

Run the guardrail script before committing:
// turbo
python3 scripts/check_shared_code.py
This script scans for duplicate file names across loh-game and loh-backend. If duplicates are found, consider moving shared logic to loh-libs.