Fix #148: Add risk, defense systems, and penalties to Code Farm raiding #149
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "typosaurus/ticket-148"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Resolves #148.
Plan
Implementation Plan: Risk, Defense Systems, and Penalties for Code Farm Raiding
Objective
Extend the existing Code Farm raiding mechanic with purchasable defense items, failure probability, raider fines, insurance payout for defenders, and a pre-raid risk assessment. Maintain backward compatibility with the existing
DefenseTiersystem – the new items operate as orthogonal modifiers on top of the tier’s base steal fraction.Database Changes (
devplacepy/database/schema.py)Add
game_farm_defense_itemstableColumns:
uid(UUID PK),farm_uid(FK →game_farms.uid),item_type(ENUM:mines,cameras,guards,insurance),purchased_at(ISO timestamp).Index on
(farm_uid, item_type).Add
game_failed_stealstableColumns:
uid(UUID PK),thief_uid(FK → users),owner_uid(FK → users),farm_uid(FK →game_farms.uid),failure_type(ENUM:mine,camera,guard),fine_applied(int, coins),cooldown_until(ISO timestamp),occurred_at(ISO timestamp).Add
insurance_payoutcolumn togame_stealstable (nullable int).When insurance is active on the owner’s farm, half the stolen coins are credited back and stored here.
Service Layer Changes (
devplacepy/services/game/ordevplacepy/services/game.py– exact location to be determined from project structure)New service function:
compute_defense_items_effect(farm_uid)Returns a dict with:
failure_chance: sum of base probabilities – 0.15 per mine item, 0.10 per camera, 0.20 per guard (caps at 0.80).catch_chance: cameras add 0.10 per unit to catch probability (used if not killed by mine/guard).auto_catch_prob: guards add 0.15 per unit (immediate failure if rolled).fine_base: mine=100, camera=50, guard=75.has_insurance: boolean.Modify
steal()(core raid logic)After computing
effective_steal_fractionfrom existing tier formula, apply defense item effects:game_failed_stealswith fine =fine_base * escalation_multiplier, deduct from raider’scoins(set to 0 if insufficient), apply per-farm cooldown (1 hour).1 + 0.05 * (num_mines + num_cameras + num_guards). Cap total steal fraction at 0.50 (50% of crop value).insurance_payoutcolumn).Escalating penalties
Query
game_failed_stealsfor the raider in the last 24 hours.escalation_multiplier = 1 + (count_failures_last_day - 1) * 0.5(i.e., second failure 1.5x, third 2x, etc.).After a failure, set a per-farm cooldown
cooldown_until(current time + 1 hour). The raider cannot attempt that farm again until expiry.Insurance purchase
Use existing coins. Cost: 500 coins per purchase (one-time, not recurring). Add a new action
game_insurance_purchasethat inserts intogame_farm_defense_itemswith typeinsurance.API / Action Changes (
devplacepy/services/devii/actions/catalog/game.py)New action:
game_defense_item_purchasePOST, requires auth. Parameters:
farm_uid,item_type(enum). Validates farm ownership, sufficient coins, no duplicate forinsurance(only one allowed). Inserts row, deducts coins. Returns updated defense profile.Extended serialization for
game_view_farmIn addition to
defense_level,defense_tier_name, add:defense_items: list of{item_type: str, purchased_at: str}raid_risk: computed{failure_chance: float, fine_min: int, fine_max: int, has_insurance: bool}Frontend / UX Considerations (non‑code but necessary for completeness)
raid_riskdata in the farm detail view before clickingSteal.Concurrency and Safety
SELECT … FOR UPDATEpattern in the service layer).Test Plan (
tests/api/game/mutations.pyand unit tests)New unit tests (in
tests/unit/services/game/economy.pyor a new file):test_defense_items_failure_probability– assert correct probability sums.test_failed_raid_fine_deduction– raider coins decrease.test_escalating_penalty_multiplier– second failure has higher fine.test_insurance_payout_on_success– owner receives half.test_per_farm_cooldown_after_failure– block re‑raid within 1 hour.New integration tests (in
tests/api/game/mutations.py):test_steal_fails_on_guard_farm– raid fails when guards present.test_steal_succeeds_with_risk_bonus– higher reward on defended farm.test_defense_item_purchase_success– item inserted, coins deducted.test_insurance_purchase_duplicate_rejected– 400 response.test_view_farm_shows_raid_risk– response includes new fields.Regression: existing Playwright e2e tests must still pass. The earlier failures were infrastructure issues (Playwright browser setup); we should ensure our changes do not modify any HTML/UI elements that those tests rely on. If any tests are flaky due to timing, we may need to add a stable wait in tests. However, for this plan we assume we do not touch e2e test code.
Definition of Done
game_stealaction correctly models failure probability from defense items, applies fines, and records failures.game_stealaction applies a risk bonus to the steal fraction when defense items are present, capped at 50% of crop value.game_insurance_purchaseendpoint, and when a successful raid occurs against an insured farm, the owner automatically receives 50% of stolen coins.game_view_farm) returns araid_riskobject with failure chance, fine range, and insurance status.pip install -e '.[dev]' -q && python -m pytest(including all Playwright e2e tests).Opened automatically by Typosaurus.
Checkout
From your project repository, check out a new branch and test the changes.