From 5f1999caac903dcaaaf7abf86a664dd5d05b05af Mon Sep 17 00:00:00 2001 From: Nate Bowman Date: Sat, 7 Nov 2026 19:56:09 -0500 Subject: [PATCH] Add market transactions --- aoe/costs.py | 30 ++++++++++++ aoe/exceptions.py | 5 ++ aoe/gameplay.py | 12 +++++ aoe/gather.py | 5 ++ aoe/world.py | 30 +++++++++++- tests/test.py | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 195 insertions(+), 1 deletion(-) diff --git a/aoe/costs.py b/aoe/costs.py index c513544..5afd127 100644 --- a/aoe/costs.py +++ b/aoe/costs.py @@ -9,6 +9,26 @@ class Costs(): return self.costs[purchase] + def compute_market_price(self, resource, buy=True): + rate = self.market_rates[resource] + if buy: + cost = rate*(1 + self.market_fee) + else: + cost = rate*(1 - self.market_fee) + return cost + + + def adjust_market_price(self, resource, buy=True): + rate = self.market_rates[resource] + if buy: + new_rate = min(self.maximum_market_rate, + rate + self.market_increment_amount) + else: + new_rate = max(self.minimum_market_rate, + rate - self.market_increment_amount) + self.market_rates[resource] = new_rate + + def __init__(self): self.costs = { Ages.DARK: (0, 0, 0, 0), @@ -142,3 +162,13 @@ class Costs(): Units.FISHINGSHIP: (0, 75, 0, 0), Units.TRADECOG: (0, 100, 50, 0), } + self.market_rates = { + Resources.FOOD: 115, + Resources.WOOD: 100, + Resources.STONE: 130, + } + self.market_fee = 0.3 + self.minimum_market_rate = 20 + self.maximum_market_rate = 9999 + self.market_increment_amount = 2 + self.market_transaction_amount = 100 diff --git a/aoe/exceptions.py b/aoe/exceptions.py index 46ddc0b..edaffef 100644 --- a/aoe/exceptions.py +++ b/aoe/exceptions.py @@ -1,3 +1,8 @@ +class InvalidMarketAmountException(Exception): + pass + + + class CannotChangeThePastException(Exception): pass diff --git a/aoe/gameplay.py b/aoe/gameplay.py index 58c3091..db25496 100644 --- a/aoe/gameplay.py +++ b/aoe/gameplay.py @@ -37,6 +37,18 @@ class Gameplay(): villagers_to,))) + def market_buy(self, action_time, resource, amount): + self._check_time(action_time) + self.actions[action_time].append((self.world.market_buy, + (resource, amount))) + + + def market_sell(self, action_time, resource, amount): + self._check_time(action_time) + self.actions[action_time].append((self.world.market_sell, + (resource, amount))) + + def queue_research(self, action_time, research): self._check_time(action_time) self.actions[action_time].append((self.world.queue_research, diff --git a/aoe/gather.py b/aoe/gather.py index 48e38e8..10ef0f7 100644 --- a/aoe/gather.py +++ b/aoe/gather.py @@ -171,6 +171,11 @@ class Gather(): self.resources[i] -= cost[i] + def acquire_resources_without_gather(self, gained): + for i in range(len(self.resources)): + self.resources[i] += gained[i] + + def permanently_modify_gather_rate(self, task, rate_multiplier): self.gather_rates[task] *= rate_multiplier self.original_gather_rates[task] *= rate_multiplier diff --git a/aoe/world.py b/aoe/world.py index 63bec34..cd6cc52 100644 --- a/aoe/world.py +++ b/aoe/world.py @@ -48,7 +48,6 @@ class World(): # reseeding of farms # queuing of villagers # possibly other things - # TODO use market for buy/sell MAX_WORLD_TIME = 3*60*60 # 3 hours @@ -60,6 +59,35 @@ class World(): self.gather.spend_resources(cost) + def market_buy(self, resource, amount): + self._use_market(resource, amount, buy=True) + + + def market_sell(self, resource, amount): + self._use_market(resource, amount, buy=False) + + + def _use_market(self, resource, amount, buy=True): + if amount % self.costs.market_transaction_amount != 0: + raise InvalidMarketAmountException + + for i in range(amount//self.costs.market_transaction_amount): + cost = self.costs.compute_market_price(resource, buy=buy) + gold_amount = [0, 0, cost, 0] + + resource_amount = [0, 0, 0, 0] + resource_amount[int(resource)] = self.costs.market_transaction_amount + + if buy: + self.gather.spend_resources(gold_amount) + self.gather.acquire_resources_without_gather(resource_amount) + else: + self.gather.spend_resources(resource_amount) + self.gather.acquire_resources_without_gather(gold_amount) + + self.costs.adjust_market_price(resource, buy=buy) + + def has_building(self, building): return building in self.buildings diff --git a/tests/test.py b/tests/test.py index 146b46b..f04fac9 100644 --- a/tests/test.py +++ b/tests/test.py @@ -341,3 +341,117 @@ class TestWorld(unittest.TestCase): game.play_until(training + 60) self.assertResourcesMatch([500, 400, 450 + gathered, 500], game.world) + + + def test_market_buy(self): + game = Gameplay() + + game.world.age = Ages.FEUDAL + game.world.gather.resources = [500, 500, 1000, 500] + game.world._add_building(Buildings.MARKET, {}, {}) + + food_rate = 115 + wood_rate = 100 + new_gold = game.world.gather.resources[2] + fee = 0.3 + inc = 2 + + game.market_buy(0, Resources.FOOD, 100) + new_gold -= food_rate*(1 + fee) + food_rate += 2 + game.play_until(1) + self.assertResourcesMatch([600, 500, new_gold, 500], game.world) + + game.market_buy(5, Resources.FOOD, 100) + new_gold -= food_rate*(1 + fee) + food_rate += 2 + game.play_until(6) + self.assertResourcesMatch([700, 500, new_gold, 500], game.world) + + game.market_buy(7, Resources.FOOD, 300) + for i in range(3): + new_gold -= food_rate*(1 + fee) + food_rate += 2 + game.play_until(8) + self.assertResourcesMatch([1000, 500, new_gold, 500], game.world) + + game.market_buy(9, Resources.WOOD, 100) + new_gold -= wood_rate*(1 + fee) + wood_rate += 2 + game.play_until(10) + self.assertResourcesMatch([1000, 600, new_gold, 500], game.world) + + + def test_market_sell(self): + game = Gameplay() + + game.world.age = Ages.FEUDAL + game.world.gather.resources = [500, 500, 1000, 500] + game.world._add_building(Buildings.MARKET, {}, {}) + + food_rate = 115 + wood_rate = 100 + new_gold = game.world.gather.resources[2] + fee = 0.3 + inc = 2 + + game.market_sell(0, Resources.FOOD, 100) + new_gold += food_rate*(1 - fee) + food_rate -= 2 + game.play_until(1) + self.assertResourcesMatch([400, 500, new_gold, 500], game.world) + + game.market_sell(5, Resources.FOOD, 100) + new_gold += food_rate*(1 - fee) + food_rate -= 2 + game.play_until(6) + self.assertResourcesMatch([300, 500, new_gold, 500], game.world) + + game.market_sell(7, Resources.FOOD, 300) + for i in range(3): + new_gold += food_rate*(1 - fee) + food_rate -= 2 + game.play_until(8) + self.assertResourcesMatch([0, 500, new_gold, 500], game.world) + + game.market_sell(9, Resources.WOOD, 100) + new_gold += wood_rate*(1 - fee) + wood_rate -= 2 + game.play_until(10) + self.assertResourcesMatch([0, 400, new_gold, 500], game.world) + + + def test_market_buy_too_much(self): + game = Gameplay() + + game.world.age = Ages.FEUDAL + game.world.gather.resources = [500, 500, 1000, 500] + game.world._add_building(Buildings.MARKET, {}, {}) + + with self.assertRaises(NotEnoughResourcesException): + game.market_buy(0, Resources.FOOD, 1000) + game.play_until(1) + + + def test_market_sell_too_much(self): + game = Gameplay() + + game.world.age = Ages.FEUDAL + game.world.gather.resources = [500, 500, 1000, 500] + game.world._add_building(Buildings.MARKET, {}, {}) + + with self.assertRaises(NotEnoughResourcesException): + game.market_sell(0, Resources.FOOD, 1000) + game.play_until(1) + + + def test_market_sell_bad_amount(self): + game = Gameplay() + + game.world.age = Ages.FEUDAL + game.world.gather.resources = [500, 500, 1000, 500] + game.world._add_building(Buildings.MARKET, {}, {}) + + with self.assertRaises(InvalidMarketAmountException): + game.market_sell(0, Resources.FOOD, 50) + game.play_until(1)