forked from retoor/devplacepy
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
|
||||
|
||||
@@ -25,6 +25,15 @@ def _user(username):
|
||||
return row
|
||||
|
||||
|
||||
def _clear_game_caches():
|
||||
from devplacepy.services.game.store import farm as farm_store
|
||||
from devplacepy.services.game.store import market as market_store
|
||||
|
||||
farm_store._leaderboard_cache.clear()
|
||||
farm_store._board_cache.clear()
|
||||
market_store._saturation_cache.clear()
|
||||
|
||||
|
||||
def _reset(username, coins=100000):
|
||||
user = _user(username)
|
||||
farm = store.get_farm(user["uid"])
|
||||
@@ -34,6 +43,9 @@ def _reset(username, coins=100000):
|
||||
get_table("game_farms").delete(uid=farm["uid"])
|
||||
get_table("game_steals").delete(thief_uid=user["uid"])
|
||||
get_table("game_steals").delete(owner_uid=user["uid"])
|
||||
get_table("game_cosmetics").delete(user_uid=user["uid"])
|
||||
get_table("game_market_ticks").delete()
|
||||
_clear_game_caches()
|
||||
farm = store.ensure_farm(user["uid"])
|
||||
get_table("game_farms").update({"uid": farm["uid"], "coins": coins}, ["uid"])
|
||||
return user
|
||||
@@ -644,3 +656,293 @@ def test_leaderboard_entry_exposes_score_and_prestige(local_db):
|
||||
assert entry["prestige"] == 3
|
||||
assert entry["score"] == economy.farm_score(farm)
|
||||
assert {"rank", "username", "level", "xp", "coins", "total_harvests", "prestige", "score"} <= set(entry)
|
||||
|
||||
|
||||
# --- market saturation ---------------------------------------------------------
|
||||
|
||||
|
||||
def test_record_and_recent_harvests_roundtrip(local_db):
|
||||
_reset("unit_a")
|
||||
from devplacepy.services.game.store import market as market_store
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
market_store.record_harvest_tick("shell", now)
|
||||
market_store.record_harvest_tick("shell", now)
|
||||
assert store.recent_harvests("shell", economy.MARKET_WINDOW_HOURS) == 2
|
||||
assert store.recent_harvests("python", economy.MARKET_WINDOW_HOURS) == 0
|
||||
|
||||
|
||||
def test_harvest_records_market_tick_and_saturates_reward(local_db):
|
||||
user = _reset("unit_a")
|
||||
from devplacepy.services.game.store import market as market_store
|
||||
from devplacepy.utils import generate_uid
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
worst_tier_count = economy.MARKET_SATURATION_TIERS[-1][0]
|
||||
get_table("game_market_ticks").insert(
|
||||
{
|
||||
"uid": generate_uid(),
|
||||
"crop_key": "shell",
|
||||
"hour_bucket": market_store._hour_bucket(now),
|
||||
"harvests": worst_tier_count,
|
||||
"updated_at": now.isoformat(),
|
||||
}
|
||||
)
|
||||
store.plant(user, 0, "shell")
|
||||
_warp(user)
|
||||
crop = economy.crop_for("shell")
|
||||
result = store.harvest(user, 0)
|
||||
assert result["coins"] < crop.reward_coins
|
||||
|
||||
|
||||
def test_harvest_records_a_market_tick_for_its_own_crop(local_db):
|
||||
user = _reset("unit_a")
|
||||
store.plant(user, 0, "shell")
|
||||
_warp(user)
|
||||
store.harvest(user, 0)
|
||||
assert store.recent_harvests("shell", economy.MARKET_WINDOW_HOURS) == 1
|
||||
|
||||
|
||||
# --- infrastructure --------------------------------------------------------------
|
||||
|
||||
|
||||
def test_buy_infrastructure_success(local_db):
|
||||
cost = economy.infra_for("registry").cost
|
||||
user = _reset("unit_a", coins=cost + 100000)
|
||||
_set(user, prestige=5)
|
||||
result = store.buy_infrastructure(user, "registry")
|
||||
assert result["key"] == "registry"
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["infra_registry"]) == 1
|
||||
assert _coins(user) == 100000
|
||||
|
||||
|
||||
def test_buy_infrastructure_requires_prestige(local_db):
|
||||
user = _reset("unit_a")
|
||||
with pytest.raises(GameError):
|
||||
store.buy_infrastructure(user, "registry")
|
||||
|
||||
|
||||
def test_buy_infrastructure_already_owned_blocked(local_db):
|
||||
cost = economy.infra_for("registry").cost
|
||||
user = _reset("unit_a", coins=cost * 2)
|
||||
_set(user, prestige=5)
|
||||
store.buy_infrastructure(user, "registry")
|
||||
with pytest.raises(GameError):
|
||||
store.buy_infrastructure(user, "registry")
|
||||
|
||||
|
||||
def test_buy_infrastructure_unknown_key(local_db):
|
||||
user = _reset("unit_a")
|
||||
with pytest.raises(GameError):
|
||||
store.buy_infrastructure(user, "does_not_exist")
|
||||
|
||||
|
||||
# --- defense -----------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_upgrade_defense_success(local_db):
|
||||
user = _reset("unit_a")
|
||||
result = store.upgrade_defense(user)
|
||||
assert result["defense_level"] == 1
|
||||
assert int(store.get_farm(user["uid"])["defense_level"]) == 1
|
||||
|
||||
|
||||
def test_upgrade_defense_insufficient_coins(local_db):
|
||||
user = _reset("unit_a", coins=0)
|
||||
with pytest.raises(GameError):
|
||||
store.upgrade_defense(user)
|
||||
|
||||
|
||||
def test_charge_upkeep_decays_when_unaffordable(local_db):
|
||||
user = _reset("unit_a", coins=0)
|
||||
_set(user, defense_level=1, defense_last_upkeep_at=(datetime.now(timezone.utc) - timedelta(days=3)).isoformat())
|
||||
farm = store.get_farm(user["uid"])
|
||||
updated = store.charge_upkeep(farm, datetime.now(timezone.utc))
|
||||
assert int(updated["defense_level"]) == 0
|
||||
assert int(updated["coins"]) == 0
|
||||
|
||||
|
||||
def test_charge_upkeep_deducts_coins_when_affordable(local_db):
|
||||
user = _reset("unit_a", coins=100000)
|
||||
_set(user, defense_level=1, defense_last_upkeep_at=(datetime.now(timezone.utc) - timedelta(days=1)).isoformat())
|
||||
farm = store.get_farm(user["uid"])
|
||||
updated = store.charge_upkeep(farm, datetime.now(timezone.utc))
|
||||
assert int(updated["defense_level"]) == 1
|
||||
assert int(updated["coins"]) < 100000
|
||||
|
||||
|
||||
# --- cosmetics ---------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_buy_and_equip_cosmetic(local_db):
|
||||
user = _reset("unit_a", coins=economy.cosmetic_for("title_architect").cost_coins * 2)
|
||||
store.buy_cosmetic(user, "title_architect")
|
||||
assert "title_architect" in store.owned_cosmetic_keys(user["uid"])
|
||||
result = store.equip_title(user, "title_architect")
|
||||
assert result["active_title"] == "title_architect"
|
||||
assert store.get_farm(user["uid"])["active_title"] == "title_architect"
|
||||
|
||||
|
||||
def test_buy_cosmetic_twice_blocked(local_db):
|
||||
user = _reset("unit_a", coins=economy.cosmetic_for("title_architect").cost_coins * 2)
|
||||
store.buy_cosmetic(user, "title_architect")
|
||||
with pytest.raises(GameError):
|
||||
store.buy_cosmetic(user, "title_architect")
|
||||
|
||||
|
||||
def test_equip_unowned_cosmetic_blocked(local_db):
|
||||
user = _reset("unit_a")
|
||||
with pytest.raises(GameError):
|
||||
store.equip_title(user, "title_architect")
|
||||
|
||||
|
||||
def test_equip_non_title_cosmetic_blocked(local_db):
|
||||
user = _reset("unit_a", coins=economy.cosmetic_for("skin_neon").cost_coins * 2)
|
||||
store.buy_cosmetic(user, "skin_neon")
|
||||
with pytest.raises(GameError):
|
||||
store.equip_title(user, "skin_neon")
|
||||
|
||||
|
||||
# --- mastery -------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_upgrade_mastery_success(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, mastery_points=10)
|
||||
result = store.upgrade_mastery(user, "autoreplant")
|
||||
assert result["level"] == 1
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["mastery_autoreplant"]) == 1
|
||||
assert int(farm["mastery_points"]) == 10 - economy.mastery_cost(economy.mastery_for("autoreplant"), 0)
|
||||
|
||||
|
||||
def test_upgrade_mastery_insufficient_points(local_db):
|
||||
user = _reset("unit_a")
|
||||
with pytest.raises(GameError):
|
||||
store.upgrade_mastery(user, "autoreplant")
|
||||
|
||||
|
||||
def test_upgrade_mastery_unknown_key(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, mastery_points=1000)
|
||||
with pytest.raises(GameError):
|
||||
store.upgrade_mastery(user, "does_not_exist")
|
||||
|
||||
|
||||
def test_farm_analytics_hidden_until_unlocked(local_db):
|
||||
user = _reset("unit_a")
|
||||
state = store.serialize_farm(store.get_farm(user["uid"]), viewer=user, owner=user)
|
||||
assert state["mastery_analytics_unlocked"] is False
|
||||
|
||||
|
||||
def test_farm_analytics_tracks_lifetime_totals_once_unlocked(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, mastery_points=1000)
|
||||
store.upgrade_mastery(user, "analytics")
|
||||
store.plant(user, 0, "shell")
|
||||
_warp(user)
|
||||
result = store.harvest(user, 0)
|
||||
state = store.serialize_farm(store.get_farm(user["uid"]), viewer=user, owner=user)
|
||||
assert state["mastery_analytics_unlocked"] is True
|
||||
assert state["lifetime_coins_earned"] == result["coins"]
|
||||
assert state["lifetime_harvests"] == 1
|
||||
|
||||
|
||||
def test_prestige_awards_mastery_at_threshold(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, xp=economy.xp_threshold(economy.PRESTIGE_MIN_LEVEL), prestige=49)
|
||||
result = store.prestige(user)
|
||||
assert result["mastery_awarded"] == 1
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["mastery_points"]) == 1
|
||||
assert int(farm["mastery_points_earned_total"]) == 1
|
||||
|
||||
|
||||
def test_prestige_below_mastery_threshold_awards_nothing(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, xp=economy.xp_threshold(economy.PRESTIGE_MIN_LEVEL), prestige=0)
|
||||
result = store.prestige(user)
|
||||
assert result["mastery_awarded"] == 0
|
||||
|
||||
|
||||
# --- eras --------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_start_era_resets_visible_counters_not_real_balances(local_db):
|
||||
user = _reset("unit_a", coins=12345)
|
||||
_set(user, era_coins=999, era_harvests=5, prestige=3, stars=7)
|
||||
if store.active_era():
|
||||
store.end_era()
|
||||
store.start_era("TestEra", 7)
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["era_coins"]) == 0
|
||||
assert int(farm["era_harvests"]) == 0
|
||||
assert int(farm["coins"]) == 12345
|
||||
assert int(farm["prestige"]) == 3
|
||||
assert int(farm["stars"]) == 7
|
||||
store.end_era()
|
||||
|
||||
|
||||
def test_start_era_twice_blocked(local_db):
|
||||
if store.active_era():
|
||||
store.end_era()
|
||||
store.start_era("TestEra", 7)
|
||||
with pytest.raises(GameError):
|
||||
store.start_era("AnotherEra", 7)
|
||||
store.end_era()
|
||||
|
||||
|
||||
def test_end_era_without_active_blocked(local_db):
|
||||
if store.active_era():
|
||||
store.end_era()
|
||||
with pytest.raises(GameError):
|
||||
store.end_era()
|
||||
|
||||
|
||||
def test_end_era_awards_stars_to_top_participant(local_db):
|
||||
if store.active_era():
|
||||
store.end_era()
|
||||
user = _reset("unit_era_top")
|
||||
store.start_era("TestEra", 7)
|
||||
_set(user, era_coins=50000, era_harvests=10)
|
||||
before_stars = int(store.get_farm(user["uid"])["stars"])
|
||||
store.end_era()
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["stars"]) > before_stars
|
||||
assert int(farm["era_coins"]) == 0
|
||||
|
||||
|
||||
def test_era_gates_new_crops_and_leaderboard(local_db):
|
||||
if store.active_era():
|
||||
store.end_era()
|
||||
_clear_game_caches()
|
||||
assert store.leaderboard_for("era") == []
|
||||
|
||||
|
||||
# --- weekly contracts ------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_weekly_contract_requires_mastery_upgrade(local_db):
|
||||
user = _reset("unit_a")
|
||||
with pytest.raises(GameError):
|
||||
store.claim_quest(user, "harvest", "weekly")
|
||||
|
||||
|
||||
def test_weekly_contract_claim_pays_stars_and_boost(local_db):
|
||||
user = _reset("unit_a")
|
||||
_set(user, mastery_points=1000)
|
||||
store.upgrade_mastery(user, "contracts")
|
||||
farm = store.get_farm(user["uid"])
|
||||
from devplacepy.services.game.store.quests import ensure_weekly_contract
|
||||
from devplacepy.services.game.store.common import _iso_week
|
||||
|
||||
iso_week = _iso_week(datetime.now(timezone.utc))
|
||||
ensure_weekly_contract(farm, iso_week)
|
||||
row = get_table("game_quests").find_one(farm_uid=farm["uid"], day=iso_week, scope="weekly")
|
||||
get_table("game_quests").update({"uid": row["uid"], "progress": row["goal"]}, ["uid"])
|
||||
result = store.claim_quest(user, row["kind"], "weekly")
|
||||
assert result["reward_stars"] > 0
|
||||
farm = store.get_farm(user["uid"])
|
||||
assert int(farm["stars"]) == result["reward_stars"]
|
||||
assert farm.get("contract_boost_until")
|
||||
|
||||
Reference in New Issue
Block a user