-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequipment.py
More file actions
84 lines (60 loc) · 2.16 KB
/
Copy pathequipment.py
File metadata and controls
84 lines (60 loc) · 2.16 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
from dataclasses import dataclass, field
import random
from typing import List
import marshmallow
import marshmallow_dataclass
import json
@dataclass
class Weapon:
id: int
name: str
min_damage: float
max_damage: float
stamina_per_hit: float
@property
def random_damage(self) -> float:
return random.uniform(self.min_damage, self.max_damage)
@dataclass
class Armor:
id: int
name: str
defence: float
stamina_per_turn: float
WeaponSchema = marshmallow_dataclass.class_schema(Weapon)
def random_damage(min_damage: float, max_damage: float) -> float:
return random.uniform(min_damage, max_damage)
@dataclass
class EquiremenrData:
weapons: List[Weapon]
armors: list[Armor]
class Meta:
unkwnown = marshmallow.EXCLUDE
EquiremenrdataSchema = marshmallow_dataclass.class_schema(EquiremenrData)
class Equired():
def __init__(self):
self._get_equipmentdata_class()
def _get_equipmentdata_class(self) -> List[EquiremenrData]:
try:
with open("data/equirement.json", "r", encoding="utf-8") as f:
result = EquiremenrdataSchema().load(json.load(f))
return result
except:
raise "ошибка в открытие файла "
def get_weapon(self, name_weapon: str) -> [Weapon, str]:
result = [weapon for weapon in self._get_equipmentdata_class().weapons if name_weapon in weapon.name]
if not result:
return f"{name_weapon} не найдено"
return result[0]
def get_armor(self, name_armor: str)->[Armor,str]:
result = [armor for armor in self._get_equipmentdata_class().armors if name_armor in armor.name]
if not result:
return f"{name_armor} не найдено"
return result[0]
def get_weapon_names(self)->list:
list = [weapon for weapon in self._get_equipmentdata_class().weapons]
results = [weapon.name for weapon in list]
return results
def get_armors_names(self)->list:
list = [armor for armor in self._get_equipmentdata_class().armors]
results = [armor.name for armor in list]
return results