Handle invalid material conversions

This commit is contained in:
Correl Roush 2022-01-20 11:57:53 -05:00
parent acfb0ab8f5
commit eb2f2ef13c
3 changed files with 26 additions and 17 deletions

View file

@ -1,6 +1,8 @@
import dataclasses
import fractions
from returns.maybe import Maybe, Some, Nothing
materials = {
"raw": {
"Raw Material Category 1": ["Carbon", "Vanadium", "Niobium", "Yttrium"],
@ -141,7 +143,9 @@ class Material:
category: str
grade: int
def trade_ratio(self, other: "Material") -> fractions.Fraction:
MAX_CAPACITY: int = 300
def trade_ratio(self, other: "Material") -> Maybe[fractions.Fraction]:
grade_base = 3 if self.grade <= other.grade else 6
grade_ratio = fractions.Fraction(
grade_base ** self.grade, grade_base ** other.grade
@ -151,4 +155,8 @@ class Material:
if self.category == other.category
else fractions.Fraction(6, 1)
)
return grade_ratio * category_ratio
ratio = grade_ratio * category_ratio
if ratio.numerator > self.MAX_CAPACITY or ratio.denominator > self.MAX_CAPACITY:
return Nothing
else:
return Some(ratio)

View file

@ -7,6 +7,7 @@ license = "MIT"
[tool.poetry.dependencies]
python = "^3.9"
returns = "^0.18.0"
[tool.poetry.dev-dependencies]
black = "^21.12b0"

View file

@ -1,6 +1,8 @@
import fractions
import unittest
from returns.maybe import Maybe
from elite_engineering import trade
@ -8,22 +10,21 @@ class TradeCalculationTests(unittest.TestCase):
def format_ratio(self, ratio: fractions.Fraction) -> str:
return "{}{}".format(ratio.numerator, ratio.denominator)
def test_same_category_exchange(self):
def test_same_category_exchange(self) -> None:
expected = [
["1 → 1", "1 → 3", "1 → 9", "1 → 27", "1 → 81"],
["6 → 1", "1 → 1", "1 → 3", "1 → 9", "1 → 27"],
["36 → 1", "6 → 1", "1 → 1", "1 → 3", "1 → 9"],
["216 → 1", "36 → 1", "6 → 1", "1 → 1", "1 → 3"],
["1296 → 1", "216 → 1", "36 → 1", "6 → 1", "1 → 1"],
["-", "216 → 1", "36 → 1", "6 → 1", "1 → 1"],
]
actual = [
[
self.format_ratio(
trade.Material("name", "category", grade_in).trade_ratio(
trade.Material("name", "category", grade_out)
)
)
trade.Material("name", "category", grade_in)
.trade_ratio(trade.Material("name", "category", grade_out))
.map(self.format_ratio)
.value_or("-")
for grade_out in [1, 2, 3, 4, 5]
]
for grade_in in [1, 2, 3, 4, 5]
@ -31,22 +32,21 @@ class TradeCalculationTests(unittest.TestCase):
self.assertEqual(expected, actual)
def test_different_category_exchange(self):
def test_different_category_exchange(self) -> None:
expected = [
["6 → 1", "2 → 1", "2 → 3", "2 → 9", "2 → 27"],
["36 → 1", "6 → 1", "2 → 1", "2 → 3", "2 → 9"],
["216 → 1", "36 → 1", "6 → 1", "2 → 1", "2 → 3"],
["1296 → 1", "216 → 1", "36 → 1", "6 → 1", "2 → 1"],
["7776 → 1", "1296 → 1", "216 → 1", "36 → 1", "6 → 1"],
["-", "216 → 1", "36 → 1", "6 → 1", "2 → 1"],
["-", "-", "216 → 1", "36 → 1", "6 → 1"],
]
actual = [
[
self.format_ratio(
trade.Material("name", "category", grade_in).trade_ratio(
trade.Material("name", "other", grade_out)
)
)
trade.Material("name", "category", grade_in)
.trade_ratio(trade.Material("name", "other", grade_out))
.map(self.format_ratio)
.value_or("-")
for grade_out in [1, 2, 3, 4, 5]
]
for grade_in in [1, 2, 3, 4, 5]