-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta_model.py
More file actions
90 lines (77 loc) · 2.65 KB
/
meta_model.py
File metadata and controls
90 lines (77 loc) · 2.65 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
85
86
87
88
89
90
import json
import os
from dataclasses import dataclass
from ..common import FILE_JSON, IMAGE_WEBP, PATH_META_DATA, URL_LINK, equipment_mapping
from ..exception import NFTAlreadyExist, NoValidBaseStatSelected, NoValidMetaData
@dataclass
class ImageMetaModel:
name: str = ""
description: str = ""
rarity: str = ""
equipment_type: str = ""
power: int = 0
attack_speed: int = 0
weight: int = 0
defense: int = 0
special_effect: str = ""
equipment_set: str = ""
equipment_range: int = 0
# only for app
index: int = 0
image_path: str = ""
def generate_meta_data(self) -> dict:
meta_data = {
"name": self.name,
"description": self.description,
"image_url": f"{URL_LINK}{self.name}",
"rarity": self.rarity,
"type": self.equipment_type,
"damage": self.power,
"attackSpeed": self.attack_speed,
"weight": self.weight,
"defense": self.defense,
"effect": self.special_effect,
"set": self.equipment_set,
"range": self.equipment_range,
}
return meta_data
def validate_meta_data(self, equipment):
equipment_stats = self.check_stats(equipment)
base_stats = self.check_stats("base")
result_equipment = any(not attr for attr in equipment_stats)
result_base = any(not attr for attr in base_stats)
return result_base or result_equipment
def check_stats(self, equipment):
if "armor" in equipment:
return [str(self.defense), self.equipment_set]
elif "shield" in equipment:
return [str(self.defense)]
elif "weapon" in equipment:
return [str(self.power), str(self.attack_speed)]
elif "base" in equipment:
return [
self.name,
self.description,
self.rarity,
self.equipment_type,
str(self.weight),
]
else:
raise NoValidBaseStatSelected(
"Select a correct base stat."
) # pragma no cover
def save(self):
if self.validate_meta_data(equipment_mapping[self.equipment_type]):
raise NoValidMetaData
data_json_format = self.generate_meta_data()
file_name = str(self.name)
path = PATH_META_DATA + file_name + FILE_JSON
if os.path.isfile(path):
raise NFTAlreadyExist("NFT name already awarded.")
with open(
path,
"w",
encoding="utf-8",
) as f:
json.dump(data_json_format, f, indent=2)
return True