-
Notifications
You must be signed in to change notification settings - Fork 0
/
equipment.py
71 lines (54 loc) · 1.73 KB
/
equipment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from dataclasses import dataclass
from random import uniform
from typing import Optional
import marshmallow_dataclass
import marshmallow
import json
@dataclass
class Armor:
name: str
defence: float
stamina_per_turn: float
class Meta:
unknown = marshmallow.EXCLUDE
@dataclass
class Weapon:
name: str
min_damage: float
max_damage: float
stamina_per_hit: float
class Meta:
unknown = marshmallow.EXCLUDE
@property
def damage(self) -> float:
return uniform(self.min_damage, self.max_damage)
@dataclass
class EquipmentData:
weapons: list[Weapon]
armors: list[Armor]
class Equipment:
def __init__(self) -> None:
self.equipment = self._get_equipment_data()
def get_weapon(self, weapon_name: str) -> Optional[Weapon]:
for weapon in self.equipment.weapons:
if weapon.name == weapon_name:
return weapon
return None
def get_armor(self, armor_name: str) -> Optional[Armor]:
for armor in self.equipment.armors:
if armor.name == armor_name:
return armor
return None
def get_weapons_names(self) -> list[str]:
return [weapon.name for weapon in self.equipment.weapons]
def get_armors_names(self) -> list[str]:
return [armor.name for armor in self.equipment.armors]
@staticmethod
def _get_equipment_data() -> EquipmentData:
with open("./data/equipment.json") as file:
data = json.load(file)
equipment_schema = marshmallow_dataclass.class_schema(EquipmentData)
try:
return equipment_schema().load(data)
except marshmallow.exceptions.ValidationError:
raise ValueError