Load materials from EDCD data files
Remove hard-coded material definitions
This commit is contained in:
parent
c100de9064
commit
4a2d5d5cb7
2 changed files with 34 additions and 180 deletions
|
@ -1,156 +1,18 @@
|
|||
import collections
|
||||
import csv
|
||||
import dataclasses
|
||||
import enum
|
||||
import fractions
|
||||
import importlib.resources
|
||||
import typing
|
||||
|
||||
from elite_engineering import trade
|
||||
|
||||
|
||||
class MaterialType(enum.Enum):
|
||||
raw = "Raw"
|
||||
manufactured = "Manufactured"
|
||||
encoded = "Encoded"
|
||||
|
||||
|
||||
categorized = {
|
||||
MaterialType.raw: {
|
||||
"Raw Material Category 1": ["Carbon", "Vanadium", "Niobium", "Yttrium"],
|
||||
"Raw Material Category 2": [
|
||||
"Phosphorus",
|
||||
"Chromium",
|
||||
"Molybdenum",
|
||||
"Technetium",
|
||||
],
|
||||
"Raw Material Category 3": ["Sulphur", "Manganese", "Cadmium", "Ruthenium"],
|
||||
"Raw Material Category 4": ["Iron", "Zinc", "Tin", "Selenium"],
|
||||
"Raw Material Category 5": ["Nickel", "Germanium", "Tungsten", "Tellurium"],
|
||||
"Raw Material Category 6": ["Rhenium", "Arsenic", "Mercury", "Polonium"],
|
||||
"Raw Material Category 7": ["Lead", "Zirconium", "Boron", "Antimony"],
|
||||
},
|
||||
MaterialType.manufactured: {
|
||||
"Chemical": [
|
||||
"Chemical Storage Units",
|
||||
"Chemical Processors",
|
||||
"Chemical Distillery",
|
||||
"Chemical Manipulators",
|
||||
"Pharmaceutical Isolators",
|
||||
],
|
||||
"Thermic": [
|
||||
"Tempered Alloys",
|
||||
"Heat Resistant Ceramics",
|
||||
"Precipitated Alloys",
|
||||
"Thermic Alloys",
|
||||
"Military Grade Alloys",
|
||||
],
|
||||
"Heat": [
|
||||
"Heat Conduction Wiring",
|
||||
"Heat Dispersion Plate",
|
||||
"Heat Exchangers",
|
||||
"Heat Vanes",
|
||||
"Proto Heat Radiators",
|
||||
],
|
||||
"Conductive": [
|
||||
"Basic Conductors",
|
||||
"Conductive Components",
|
||||
"Conductive Ceramics",
|
||||
"Conductive Polymers",
|
||||
"Biotech Conductors",
|
||||
],
|
||||
"Mechanical Components": [
|
||||
"Mechanical Scrap",
|
||||
"Mechanical Equipment",
|
||||
"Mechanical Components",
|
||||
"Configurable Components",
|
||||
"Improvised Components",
|
||||
],
|
||||
"Capacitors": [
|
||||
"Grid Resistors",
|
||||
"Hybrid Capacitors",
|
||||
"Electrochemical Arrays",
|
||||
"Polymer Capacitors",
|
||||
"Military Supercapacitors",
|
||||
],
|
||||
"Shielding": [
|
||||
"Worn Shield Emitters",
|
||||
"Shield Emitters",
|
||||
"Shielding Sensors",
|
||||
"Compound Shielding",
|
||||
"Imperial Shielding",
|
||||
],
|
||||
"Composite": [
|
||||
"Compact Composites",
|
||||
"Filament Composites",
|
||||
"High Density Composites",
|
||||
"Proprietary Composites",
|
||||
"Core Dynamics Composites",
|
||||
],
|
||||
"Crystals": [
|
||||
"Crystal Shards",
|
||||
"Flawed Focus Crystals",
|
||||
"Focus Crystals",
|
||||
"Refined Focus Crystals",
|
||||
"Exquisite Focus Crystals",
|
||||
],
|
||||
"Alloys": [
|
||||
"Salvaged Alloys",
|
||||
"Galvanising Alloys",
|
||||
"Phase Alloys",
|
||||
"Proto Light Alloys",
|
||||
"Proto Radiolic Alloys",
|
||||
],
|
||||
},
|
||||
MaterialType.encoded: {
|
||||
"Emission Data": [
|
||||
"Exceptional Scrambled Emission Data",
|
||||
"Irregular Emission Data",
|
||||
"Unexpected Emission Data",
|
||||
"Decoded Emission Data",
|
||||
"Abnormal Compact Emissions Data",
|
||||
],
|
||||
"Wake Scans": [
|
||||
"Atypical Disrupted Wake Echoes",
|
||||
"Anomalous FSD Telemetry",
|
||||
"Strange Wake Solutions",
|
||||
"Eccentric Hyperspace Trajectories",
|
||||
"Datamined Wake Exceptions",
|
||||
],
|
||||
"Shield Data": [
|
||||
"Distorted Shield Cycle Recordings",
|
||||
"Inconsistent Shield Soak Analysis",
|
||||
"Untypical Shield Scans",
|
||||
"Aberrant Shield Pattern Analysis",
|
||||
"Peculiar Shield Frequency Data",
|
||||
],
|
||||
"Encryption Files": [
|
||||
"Unusual Encrypted Files",
|
||||
"Tagged Encryption Codes",
|
||||
"Open Symmetric Keys",
|
||||
"Atypical Encryption Archives",
|
||||
"Adaptive Encryptors Capture",
|
||||
],
|
||||
"Data Archives": [
|
||||
"Anomalous Bulk Scan Data",
|
||||
"Unidentified Scan Archives",
|
||||
"Classified Scan Databanks",
|
||||
"Divergent Scan Data",
|
||||
"Classified Scan Fragment",
|
||||
],
|
||||
"Encoded Firmware": [
|
||||
"Specialised Legacy Firmware",
|
||||
"Modified Consumer Firmware",
|
||||
"Cracked Industrial Firmware",
|
||||
"Security Firmware Patch",
|
||||
"Modified Embedded Firmware",
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Material:
|
||||
name: str
|
||||
type_: MaterialType
|
||||
type_: str
|
||||
category: str
|
||||
grade: int
|
||||
|
||||
|
@ -160,12 +22,17 @@ class Material:
|
|||
return trade.ratio(self.grade, other.grade, self.category != other.category)
|
||||
|
||||
|
||||
materials: typing.Dict[str, Material] = {
|
||||
name: Material(name, type_, category, grade)
|
||||
for type_, categories in categorized.items()
|
||||
for category, names in categories.items()
|
||||
for grade, name in enumerate(names)
|
||||
}
|
||||
def _load() -> typing.Dict[str, Material]:
|
||||
path = importlib.resources.files(__package__).joinpath("data/fdevids/material.csv")
|
||||
lines = path.read_text().splitlines()
|
||||
reader = csv.reader(lines[1:])
|
||||
return {
|
||||
symbol: Material(name, type_, category, int(grade))
|
||||
for fdevid, symbol, grade, type_, category, name in reader
|
||||
}
|
||||
|
||||
|
||||
materials: typing.Dict[str, Material] = _load()
|
||||
|
||||
|
||||
class Inventory(collections.UserDict):
|
||||
|
|
|
@ -23,19 +23,14 @@ class TradeCalculationTests(unittest.TestCase):
|
|||
actual = [
|
||||
[
|
||||
self.format_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.raw, "category", grade_in
|
||||
).trade_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.raw, "category", grade_out
|
||||
)
|
||||
materials.Material("name", "raw", "category", grade_in).trade_ratio(
|
||||
materials.Material("name", "raw", "category", grade_out)
|
||||
)
|
||||
)
|
||||
for grade_in in [1, 2, 3, 4, 5]
|
||||
]
|
||||
for grade_out in [1, 2, 3, 4, 5]
|
||||
]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_different_category_exchange(self) -> None:
|
||||
|
@ -50,12 +45,8 @@ class TradeCalculationTests(unittest.TestCase):
|
|||
actual = [
|
||||
[
|
||||
self.format_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.raw, "category", grade_in
|
||||
).trade_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.raw, "other", grade_out
|
||||
)
|
||||
materials.Material("name", "raw", "category", grade_in).trade_ratio(
|
||||
materials.Material("name", "raw", "other", grade_out)
|
||||
)
|
||||
)
|
||||
for grade_in in [1, 2, 3, 4, 5]
|
||||
|
@ -77,12 +68,8 @@ class TradeCalculationTests(unittest.TestCase):
|
|||
actual = [
|
||||
[
|
||||
self.format_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.raw, "category", grade_in
|
||||
).trade_ratio(
|
||||
materials.Material(
|
||||
"name", materials.MaterialType.encoded, "other", grade_out
|
||||
)
|
||||
materials.Material("name", "raw", "category", grade_in).trade_ratio(
|
||||
materials.Material("name", "encoded", "other", grade_out)
|
||||
)
|
||||
)
|
||||
for grade_in in [1, 2, 3, 4, 5]
|
||||
|
@ -104,9 +91,9 @@ class InventoryTests(unittest.TestCase):
|
|||
self.assertEqual(set(materials.materials.keys()), set(inventory.keys()))
|
||||
|
||||
def test_create_populated(self) -> None:
|
||||
inventory = self.make({"Carbon": 5, "Shield Emitters": 3})
|
||||
inventory = self.make({"Carbon": 5, "ShieldEmitters": 3})
|
||||
self.assertEqual(5, inventory["Carbon"])
|
||||
self.assertEqual(3, inventory["Shield Emitters"])
|
||||
self.assertEqual(3, inventory["ShieldEmitters"])
|
||||
self.assertEqual(8, sum(inventory.values()))
|
||||
|
||||
def test_create_with_invalid_materials(self) -> None:
|
||||
|
@ -120,13 +107,13 @@ class InventoryTests(unittest.TestCase):
|
|||
self.assertEqual(exc.exception.args, (-5,))
|
||||
|
||||
def test_has_materials(self) -> None:
|
||||
inventory = self.make({"Carbon": 5, "Shield Emitters": 3})
|
||||
self.assertTrue(inventory.has({"Carbon": 1, "Shield Emitters": 3}))
|
||||
inventory = self.make({"Carbon": 5, "ShieldEmitters": 3})
|
||||
self.assertTrue(inventory.has({"Carbon": 1, "ShieldEmitters": 3}))
|
||||
|
||||
def test_lacks_materials(self) -> None:
|
||||
inventory = self.make({"Carbon": 5, "Shield Emitters": 3})
|
||||
inventory = self.make({"Carbon": 5, "ShieldEmitters": 3})
|
||||
self.assertTrue(not inventory.has({"Nickel": 1}))
|
||||
self.assertTrue(not inventory.has({"Carbon": 6, "Shield Emitters": 3}))
|
||||
self.assertTrue(not inventory.has({"Carbon": 6, "ShieldEmitters": 3}))
|
||||
|
||||
def test_has_raises_on_invalid_materials(self) -> None:
|
||||
inventory = self.make()
|
||||
|
@ -135,20 +122,20 @@ class InventoryTests(unittest.TestCase):
|
|||
self.assertEqual(exc.exception.args, ("Pizza",))
|
||||
|
||||
def test_addition(self) -> None:
|
||||
a = self.make({"Carbon": 5, "Shield Emitters": 3})
|
||||
b = self.make({"Iron": 2, "Shield Emitters": 3})
|
||||
expected = self.make({"Carbon": 5, "Iron": 2, "Shield Emitters": 6})
|
||||
a = self.make({"Carbon": 5, "ShieldEmitters": 3})
|
||||
b = self.make({"Iron": 2, "ShieldEmitters": 3})
|
||||
expected = self.make({"Carbon": 5, "Iron": 2, "ShieldEmitters": 6})
|
||||
self.assertEqual(expected, a + b)
|
||||
|
||||
def test_subtraction(self) -> None:
|
||||
a = self.make({"Carbon": 5, "Iron": 2, "Shield Emitters": 6})
|
||||
b = self.make({"Carbon": 5, "Shield Emitters": 3})
|
||||
expected = self.make({"Iron": 2, "Shield Emitters": 3})
|
||||
a = self.make({"Carbon": 5, "Iron": 2, "ShieldEmitters": 6})
|
||||
b = self.make({"Carbon": 5, "ShieldEmitters": 3})
|
||||
expected = self.make({"Iron": 2, "ShieldEmitters": 3})
|
||||
self.assertEqual(expected, a - b)
|
||||
|
||||
def test_subtraction_cannot_result_in_negative_quantities(self) -> None:
|
||||
a = self.make({"Carbon": 5, "Iron": 2, "Shield Emitters": 6})
|
||||
b = self.make({"Carbon": 6, "Shield Emitters": 3})
|
||||
a = self.make({"Carbon": 5, "Iron": 2, "ShieldEmitters": 6})
|
||||
b = self.make({"Carbon": 6, "ShieldEmitters": 3})
|
||||
with self.assertRaises(ValueError) as exc:
|
||||
a - b
|
||||
self.assertEqual(exc.exception.args, (-1,))
|
||||
|
|
Loading…
Reference in a new issue