diff --git a/tests/test_persistence.py b/tests/test_persistence.py index a3da1fa..1cdc3ff 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -1,4 +1,4 @@ -import time +import pytest from untitled import content, migration, model, persistence, rules @@ -47,7 +47,7 @@ def test_migration(): assert "last_updated" in result["cat"] -def test_decay_and_feed(): +def test_decay_and_replenish(): cat = model.Save( version=content.SAVE_VERSION, cat=model.Cat( @@ -58,12 +58,16 @@ def test_decay_and_feed(): "eyes": "blue", "personality": "judges you silently", }, + last_updated=0, ), ) - rules.reconcile(cat.cat, time.time() + 3600) + rules.reconcile(cat.cat, 3600 * 2) + assert cat.cat.happiness < 98 assert cat.cat.fullness < 98 rules.feed(cat.cat) - assert cat.cat.fullness > 98 and cat.cat.fullness <= 100 + assert cat.cat.fullness == pytest.approx(100) + rules.reconcile(cat.cat, 7200 + 20 * 3600) + assert cat.cat.happiness < 96 - (content.BASE_HAPPINESS_DECAY_PER_HOUR * 20) def test_list_saves(tmp_path): diff --git a/untitled/content.py b/untitled/content.py index 76a1186..bbcb926 100644 --- a/untitled/content.py +++ b/untitled/content.py @@ -4,8 +4,9 @@ GAME_NAME = "Untitled Cat Game" # Titled Cat Game SAVE_VERSION = 2 HUNGER_DECAY_PER_HOUR = 5 -BASE_HAPPINESS_DECAY_PER_HOUR = 5 - +BASE_HAPPINESS_DECAY_PER_HOUR = 2 +HUNGER_SADNESS_THRESHOLD = 30 +HUNGER_SADNESS_PENALTY_PER_HOUR = 5 CAT_COLORS = [ "orange tabby", "black", diff --git a/untitled/migration.py b/untitled/migration.py index 514b307..abf628e 100644 --- a/untitled/migration.py +++ b/untitled/migration.py @@ -3,6 +3,7 @@ import time def _v1_to_v2(data): data["cat"]["fullness"] = 100 + data["cat"]["happiness"] = 100 data["cat"]["last_updated"] = time.time() data["version"] = 2 return data diff --git a/untitled/rules.py b/untitled/rules.py index bb8d7dd..ac2d984 100644 --- a/untitled/rules.py +++ b/untitled/rules.py @@ -17,20 +17,34 @@ def validate_cat_name(name, auto_gen=False): return "Your cat's name needs at least 1 letter." +def _clamp(value, low=0, high=100): + if value < low: + return low + if value > high: + return high + return value + + def reconcile(cat: model.Cat, now): elapsed_hours = (now - cat.last_updated) / 3600 if elapsed_hours <= 0: return cat.fullness -= content.HUNGER_DECAY_PER_HOUR * elapsed_hours - if cat.fullness < 0: - cat.fullness = 0 + cat.fullness = _clamp(cat.fullness) + cat.happiness -= content.BASE_HAPPINESS_DECAY_PER_HOUR * elapsed_hours - if cat.happiness < 0: - cat.happiness = 0 + if cat.fullness < content.HUNGER_SADNESS_THRESHOLD: + cat.happiness -= content.HUNGER_SADNESS_PENALTY_PER_HOUR * elapsed_hours + cat.happiness = _clamp(cat.happiness) + cat.last_updated = now def feed(cat, amount=100): cat.fullness += amount - if cat.fullness > 100: - cat.fullness = 100 + cat.fullness = _clamp(cat.fullness) + + +def excite(cat, amount=100): + cat.happiness += amount + cat.happiness = _clamp(cat.happiness) diff --git a/untitled/screens/house.py b/untitled/screens/house.py index 3eb44e5..1061be5 100644 --- a/untitled/screens/house.py +++ b/untitled/screens/house.py @@ -8,17 +8,22 @@ def house(save: model.Save): print("Welcome to your house!") while True: match ui.select( - "What do you want to do?", ["Check on your cat", "Feed your cat", "Menu"] + "What do you want to do?", + ["Check on your cat", "Feed your cat", "Pet your cat", "Menu"], ): case "Check on your cat": rules.reconcile(save.cat, time.time()) print( - f"{save.cat.name}, {generation.generate_trait_sentence(save.cat.traits).lower()}\nFullness: {round(save.cat.fullness,1)}" + f"{save.cat.name}, {generation.generate_trait_sentence(save.cat.traits).lower()}\nFullness: {round(save.cat.fullness,1)}\nHappiness: {round(save.cat.happiness,1)}" ) case "Feed your cat": rules.reconcile(save.cat, time.time()) rules.feed(save.cat) print(f"You feed {save.cat.name}, {save.cat.name} is now full.") + case "Pet your cat": + rules.reconcile(save.cat, time.time()) + rules.excite(save.cat) + print(f"You pet {save.cat.name}, {save.cat.name} is now happy.") case "Menu": result = options() match result: