Skip to content

Commit

Permalink
chore: fixes average cost, flows, and improves inventory report
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed May 24, 2024
1 parent 49ae929 commit fbd6f9f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
13 changes: 13 additions & 0 deletions examples/inventory_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ItemReport:
consumption: float = 0
export: float = 0
sale: float = 0
expenses: float = 0
revenue: float = 0


async def main():
Expand All @@ -44,14 +46,18 @@ async def main():
"Consumption",
"Export",
"Sale",
"Expenses",
"Revenue",
"Balance",
]

reports = []
for item in player.storehouse.items:
if item.value in Transport:
continue

report = generate_report(player, item)
reports.append(report)
table.append(
[
item.name,
Expand All @@ -61,11 +67,16 @@ async def main():
report.consumption,
report.export,
report.sale,
report.expenses,
report.revenue,
report.balance,
]
)

print(tabulate(table, headers, floatfmt=".2f"))
print("Total expenses:", sum([report.expenses for report in reports]))
print("Total revenue:", sum([report.revenue for report in reports]))
print("Total balance:", sum([report.balance for report in reports]))


def generate_report(player: Player, target_item: Item) -> ItemReport:
Expand Down Expand Up @@ -103,6 +114,8 @@ def generate_report(player: Player, target_item: Item) -> ItemReport:
output += item.sale_value
report.sale = item.sale_value

report.expenses = 0 - input
report.revenue = output
report.balance = round(output - input, 2)
return report

Expand Down
35 changes: 17 additions & 18 deletions pymerc/game/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,21 @@
class Building:
"""A higher level representation of a building in the game."""

data: BuildingModel

def __init__(self, client: Client, id: int):
self._client = client
self.id = id
self.data: Optional[BuildingModel] = None
self.total_flow: Optional[dict[common.Item, common.InventoryFlow]] = None
self.operations: Optional[list[common.Operation]] = None

async def load(self):
"""Loads the data for the building."""
self.data = await self._client.buildings_api.get(self.id)
building_operations = await self._client.buildings_api.get_operations(self.id)
self.total_flow = building_operations.total_flow
self.operations = building_operations.operations
self.operations_data = await self._client.buildings_api.get_operations(self.id)

@property
def flows(self) -> Optional[dict[common.Item, common.InventoryFlow]]:
"""The flows of the building."""
if self.total_flow:
return self.total_flow
else:
return None

@property
def previous_flows(self) -> Optional[dict[common.Item, common.InventoryFlow]]:
"""The flows of the building."""
if self.data.storage:
return self.data.storage.inventory.previous_flows
else:
return None
return self.operations_data.total_flow

@property
def inventory(self) -> Optional[common.Inventory]:
Expand All @@ -64,6 +50,19 @@ def managers(self) -> dict[common.Item, common.InventoryManager]:
"""The managers of the building."""
return self.data.storage.inventory.managers

@property
def operations(self) -> Optional[list[common.Operation]]:
"""The operations of the building."""
return self.operations_data.operations

@property
def previous_flows(self) -> Optional[dict[common.Item, common.InventoryFlow]]:
"""The flows of the building."""
if self.data.storage:
return self.data.storage.inventory.previous_flows
else:
return None

@property
def production(self) -> Optional[common.Producer]:
"""Returns the production of the building."""
Expand Down
46 changes: 36 additions & 10 deletions pymerc/game/storehouse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from pymerc.api.models import common
from pymerc.game.building import Building
Expand Down Expand Up @@ -39,9 +39,24 @@ async def load(self):
exports=self.player.exports.get(item, ExportsList()),
imports=self.player.imports.get(item, ImportsList()),
manager=self.data.inventory.managers.get(item, None),
flow=self.data.inventory.previous_flows.get(item, None),
flow=self.flows.get(item, None),
)

@property
def flows(self) -> dict[common.Item, common.InventoryFlow]:
"""The flows of the storehouse."""
return self.data.flows

@property
def operations(self) -> Optional[list[common.Operation]]:
"""The operations of the storehouse."""
return self.data.operations

@property
def previous_flows(self) -> dict[common.Item, common.InventoryFlow]:
"""The previous flows of the storehouse."""
return self.data.previous_flows


@dataclass
class StorehouseItem:
Expand All @@ -56,18 +71,29 @@ class StorehouseItem:
@property
def average_cost(self) -> float:
"""The average cost of the item across production, imports, and purchases."""
costs = []
total_cost = 0
total_volume = 0
if self.produced:
costs.append(self.production_cost / self.produced)
total_cost += self.production_cost
total_volume += self.produced
if self.imported:
costs.append(self.import_cost_flowed / self.imported)
total_cost += self.import_cost_flowed
total_volume += self.imported
if self.purchased:
costs.append(self.purchased_cost / self.purchased)
total_cost += self.purchased_cost
total_volume += self.purchased

if costs:
return sum(costs) / len(costs)
else:
return 0
return total_cost / total_volume if total_volume else 0

@property
def balance(self) -> int:
"""The current balance of the item."""
return self.asset.balance

@property
def capacity(self) -> int:
"""The maximum capacity of the item."""
return self.asset.capacity

@property
def consumed(self) -> float:
Expand Down

0 comments on commit fbd6f9f

Please sign in to comment.