Code Farm economy rebalance: market saturation, infrastructure/defense/cosmetics, mastery track, secondary leaderboards, admin eras, underdog bonus and weekly contracts
Every purchase/upgrade path (new and pre-existing) is now race-safe against concurrent requests via atomic conditional SQL updates. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -225,3 +225,177 @@ def test_is_golden_deterministic_and_bounded():
|
||||
def test_is_golden_requires_both_args():
|
||||
assert economy.is_golden("", "2026-06-20T00:00:00+00:00") is False
|
||||
assert economy.is_golden("plot-7", "") is False
|
||||
|
||||
|
||||
# --- market saturation --------------------------------------------------------
|
||||
|
||||
|
||||
def test_market_saturation_factor_is_full_when_fresh():
|
||||
assert economy.market_saturation_factor(0) == 1.0
|
||||
|
||||
|
||||
def test_market_saturation_factor_steps_down():
|
||||
assert economy.market_saturation_factor(40) < 1.0
|
||||
assert economy.market_saturation_factor(600) == 0.40
|
||||
assert economy.market_saturation_factor(40) > economy.market_saturation_factor(600)
|
||||
|
||||
|
||||
def test_market_buff_factor_only_applies_to_starter_crops():
|
||||
assert economy.market_buff_factor("kernel", 0.40) == 1.0
|
||||
assert economy.market_buff_factor("shell", 1.0) == 1.0
|
||||
assert economy.market_buff_factor("shell", 0.40) > 1.0
|
||||
assert economy.market_buff_factor("shell", 0.0) <= economy.MARKET_BUFF_CAP
|
||||
|
||||
|
||||
def test_effective_reward_coins_applies_market_factor():
|
||||
crop = economy.crop_for("shell")
|
||||
full = economy.effective_reward_coins(crop, 0, 0, 0, 1.0)
|
||||
saturated = economy.effective_reward_coins(crop, 0, 0, 0, 0.5)
|
||||
assert saturated < full
|
||||
assert saturated == round(crop.reward_coins * 0.5)
|
||||
|
||||
|
||||
def test_effective_reward_coins_underdog_multiplier():
|
||||
crop = economy.crop_for("shell")
|
||||
plain = economy.effective_reward_coins(crop, 0, 0, 0, 1.0, False)
|
||||
boosted = economy.effective_reward_coins(crop, 0, 0, 0, 1.0, True)
|
||||
assert boosted == round(crop.reward_coins * economy.UNDERDOG_MULTIPLIER)
|
||||
assert boosted > plain
|
||||
|
||||
|
||||
# --- infrastructure and defense ------------------------------------------------
|
||||
|
||||
|
||||
def test_infra_for_known_and_unknown():
|
||||
assert economy.infra_for("registry") is not None
|
||||
assert economy.infra_for("does_not_exist") is None
|
||||
|
||||
|
||||
def test_registry_boost_speeds_only_named_crops():
|
||||
kernel = economy.crop_for("kernel")
|
||||
shell = economy.crop_for("shell")
|
||||
assert economy.grow_seconds_for(kernel, 1, 0, 0, True) < economy.grow_seconds_for(
|
||||
kernel, 1, 0, 0, False
|
||||
)
|
||||
assert economy.grow_seconds_for(shell, 1, 0, 0, True) == economy.grow_seconds_for(
|
||||
shell, 1, 0, 0, False
|
||||
)
|
||||
|
||||
|
||||
def test_defense_tier_lookup_and_progression():
|
||||
assert economy.defense_tier(0).name == "Undefended"
|
||||
assert economy.defense_tier(0).upkeep_daily == 0
|
||||
top = economy.defense_tier(economy.MAX_DEFENSE_LEVEL)
|
||||
assert economy.next_defense_tier(economy.MAX_DEFENSE_LEVEL) is None
|
||||
assert economy.next_defense_tier(0) is not None
|
||||
assert top.steal_fraction_floor < economy.defense_tier(0).steal_fraction_floor
|
||||
|
||||
|
||||
def test_daily_upkeep_scales_with_wealth():
|
||||
tier = economy.defense_tier(1)
|
||||
assert economy.daily_upkeep(tier, 0) == tier.upkeep_daily
|
||||
rich = economy.daily_upkeep(tier, 10_000_000)
|
||||
assert rich > tier.upkeep_daily
|
||||
assert rich == round(10_000_000 * economy.UPKEEP_WEALTH_PCT)
|
||||
|
||||
|
||||
def test_effective_steal_fraction_floor_overridden_by_building():
|
||||
assert economy.effective_steal_fraction(99, floor=0.30) == 0.30
|
||||
assert economy.effective_steal_fraction(0, floor=0.30) == economy.STEAL_FRACTION
|
||||
|
||||
|
||||
def test_effective_steal_grace_extra_seconds_from_building():
|
||||
base = economy.effective_steal_grace(0, 0)
|
||||
with_building = economy.effective_steal_grace(0, 30)
|
||||
assert with_building == base + 30
|
||||
|
||||
|
||||
# --- cosmetics ------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_cosmetic_for_known_and_unknown():
|
||||
assert economy.cosmetic_for("title_architect") is not None
|
||||
assert economy.cosmetic_for("does_not_exist") is None
|
||||
|
||||
|
||||
def test_cosmetic_title_name_only_resolves_titles():
|
||||
assert economy.cosmetic_title_name("title_architect") == "The Architect"
|
||||
assert economy.cosmetic_title_name("skin_neon") == ""
|
||||
assert economy.cosmetic_title_name("") == ""
|
||||
assert economy.cosmetic_title_name("does_not_exist") == ""
|
||||
|
||||
|
||||
# --- mastery --------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_mastery_points_awarded_first_point_at_unlock():
|
||||
assert economy.mastery_points_awarded(49, 50) == 1
|
||||
assert economy.mastery_points_awarded(0, 49) == 0
|
||||
assert economy.mastery_points_awarded(55, 56) == 0
|
||||
assert economy.mastery_points_awarded(59, 60) == 1
|
||||
assert economy.mastery_points_awarded(50, 70) == 2
|
||||
|
||||
|
||||
def test_mastery_for_known_and_unknown():
|
||||
assert economy.mastery_for("autoreplant") is not None
|
||||
assert economy.mastery_for("does_not_exist") is None
|
||||
|
||||
|
||||
def test_mastery_cost_grows_with_level():
|
||||
m = economy.mastery_for("contracts")
|
||||
assert economy.mastery_cost(m, 0) == m.base_cost
|
||||
|
||||
|
||||
def test_unlocked_crops_gates_on_mastery_and_level():
|
||||
base = economy.unlocked_crops(economy.MAX_LEVEL, mastery_earned=0)
|
||||
with_mastery = economy.unlocked_crops(economy.MAX_LEVEL, mastery_earned=1)
|
||||
assert "distsys" not in {c.key for c in base}
|
||||
assert "distsys" in {c.key for c in with_mastery}
|
||||
|
||||
|
||||
def test_crop_payload_locked_by_mastery():
|
||||
distsys = economy.crop_for("distsys")
|
||||
assert economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=0)["locked"] is True
|
||||
assert economy.crop_payload(distsys, 1, economy.MAX_LEVEL, mastery_earned=1)["locked"] is False
|
||||
|
||||
|
||||
def test_secfort_crop_is_steal_immune():
|
||||
secfort = economy.crop_for("secfort")
|
||||
assert secfort.steal_immune is True
|
||||
assert economy.crop_for("shell").steal_immune is False
|
||||
|
||||
|
||||
# --- secondary leaderboard scoring -----------------------------------------------
|
||||
|
||||
|
||||
def test_fair_play_score_rewards_activity_and_penalizes_hoarding():
|
||||
active = economy.fair_play_score(harvests_week=20, coins=0)
|
||||
hoarder = economy.fair_play_score(harvests_week=0, coins=10_000_000)
|
||||
assert active > hoarder
|
||||
|
||||
|
||||
# --- era --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_era_score_gives_prestige_partial_weight():
|
||||
veteran = economy.era_score(era_coins=0, era_harvests=0, prestige=10)
|
||||
rookie = economy.era_score(era_coins=0, era_harvests=0, prestige=0)
|
||||
assert veteran > rookie
|
||||
assert veteran == round(10 * economy.SCORE_PRESTIGE * economy.ERA_PRESTIGE_CARRYOVER_PCT)
|
||||
|
||||
|
||||
def test_era_reward_stars_by_rank():
|
||||
assert economy.era_reward_stars(1) == economy.ERA_REWARD_STARS_BY_RANK[0]
|
||||
assert economy.era_reward_stars(len(economy.ERA_REWARD_STARS_BY_RANK) + 1) == 0
|
||||
assert economy.era_reward_stars(0) == 0
|
||||
|
||||
|
||||
# --- weekly contracts ---------------------------------------------------------------
|
||||
|
||||
|
||||
def test_weekly_contract_deterministic():
|
||||
first = economy.weekly_contract("user-xyz", "2026-W10")
|
||||
second = economy.weekly_contract("user-xyz", "2026-W10")
|
||||
assert first == second
|
||||
assert first["kind"] in economy.QUEST_DEFS
|
||||
assert first["reward_stars"] > 0
|
||||
|
||||
Reference in New Issue
Block a user