-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource.py
280 lines (219 loc) · 9.82 KB
/
resource.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from __future__ import annotations
from enum import Enum
from typing import Union, List, Dict, Any, Self, Type
from openciv.engine.saving import SaveAble
from openciv.engine.managers.i18n import T_TranslationOrStr, t_
from openciv.engine.exceptions.resource_exception import ResourceTypeException
class ResourceType(Enum):
MECHANIC = -1
BASIC = 0
BONUS = 1
STRATEGIC = 2
LUXURY = 3
class ResourceValueType(Enum):
FLOAT = 0
INT = 1
class ResourceTypeBase:
def __init__(self, name: T_TranslationOrStr, description: T_TranslationOrStr, type_: ResourceType):
self.type_name: T_TranslationOrStr = name
self.type_description: T_TranslationOrStr = description
self.type_num: ResourceType = type_
class ResourceTypeMechanic(ResourceTypeBase):
def __init__(self) -> None:
super().__init__(
name=t_("content.resources.core.types.mechanic.name"),
description=t_("content.resources.core.types.mechanic.description"),
type_=ResourceType.MECHANIC,
)
class ResourceTypeBonus(ResourceTypeBase):
def __init__(self) -> None:
super().__init__(
name=t_("content.resources.core.types.bonus.name"),
description=t_("content.resources.core.types.bonus.description"),
type_=ResourceType.BONUS,
)
class ResourceTypeStrategic(ResourceTypeBase):
def __init__(self) -> None:
super().__init__(
t_("content.resources.core.types.strategic.name"),
t_("content.resources.core.types.strategic.description"),
ResourceType.STRATEGIC,
)
class ResourceTypeLuxury(ResourceTypeBase):
def __init__(self) -> None:
super().__init__(
name=t_("content.resources.core.types.luxury.name"),
description=t_("content.resources.core.types.luxury.description"),
type_=ResourceType.LUXURY,
)
class ResourceTypeBasic(ResourceTypeBase):
def __init__(self):
super().__init__(
name=t_("content.resources.core.types.basic.name"),
description=t_("content.resources.core.types.basic.description"),
type_=ResourceType.BASIC,
)
class Resource(SaveAble):
def __init__(
self,
key: str,
name: T_TranslationOrStr,
type_: Type[ResourceTypeBase],
value: Union[float, int] = 0,
configure_as_float_or_int: ResourceValueType = ResourceValueType.INT,
icon: T_TranslationOrStr | None = None,
description: T_TranslationOrStr | None = None,
*args: Any,
**kwargs: Any,
) -> None:
super().__init__()
self.key: str = key
self.name: T_TranslationOrStr = name
self.description: T_TranslationOrStr | None = description
self.value: Union[float, int] = value
self.value_storage: ResourceValueType = configure_as_float_or_int
self.icon: T_TranslationOrStr | None = icon
self.type = type_
self._setup_saveable()
# Overloaded operators
def __add__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return self.value + other.value
return self.value + other
def __radd__(self, other: Union[Resource, float, int]) -> Union[float, int]:
return self.__add__(other)
def __sub__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return self.value - other.value
return self.value - other
def __rsub__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return other.value - self.value
return other - self.value
def __mul__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return self.value * other.value
return self.value * other
def __rmul__(self, other: Union[Resource, float, int]) -> Union[float, int]:
return self.__mul__(other)
def __truediv__(self, other: Union[Resource, float, int]) -> float:
if isinstance(other, Resource):
return self.value / other.value
return self.value / other
def __rtruediv__(self, other: Union[Resource, float, int]) -> float:
if isinstance(other, Resource):
return other.value / self.value
return other / self.value
def __floordiv__(self, other: Union[Resource, float, int]) -> int:
if isinstance(other, Resource):
return int(self.value // other.value)
return int(self.value // other)
def __rfloordiv__(self, other: Union[Resource, float, int]) -> int:
return self.__floordiv__(other)
def __mod__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return self.value % other.value
return self.value % other
def __rmod__(self, other: Union[Resource, float, int]) -> Union[float, int]:
return self.__mod__(other)
def __pow__(self, other: Union[Resource, float, int]) -> Union[float, int]:
if isinstance(other, Resource):
return self.value**other.value
return self.value**other
def __rpow__(self, other: Union[Resource, float, int]) -> Union[float, int]:
return self.__pow__(other)
def __eq__(self, other: object) -> bool:
if isinstance(other, Resource):
return self.value == other.value
return False
def __ne__(self, other: object) -> bool:
return not self.__eq__(other)
def __lt__(self, other: Union[Resource, float, int]) -> bool:
if isinstance(other, Resource):
return self.value < other.value
return self.value < other
def __le__(self, other: Union[Resource, float, int]) -> bool:
if isinstance(other, Resource):
return self.value <= other.value
return self.value <= other
def __gt__(self, other: Union[Resource, float, int]) -> bool:
if isinstance(other, Resource):
return self.value > other.value
return self.value > other
def __ge__(self, other: Union[Resource, float, int]) -> bool:
if isinstance(other, Resource):
return self.value >= other.value
return self.value >= other
def __repr__(self) -> str:
return f"{self.key}: {self.value}"
@classmethod
def strategic(cls, *args: Any, **kwargs: Any) -> Resource:
return cls(*args, **kwargs, type_=ResourceTypeStrategic)
@classmethod
def luxury(cls, *args: Any, **kwargs: Any) -> Resource:
return cls(*args, **kwargs, type_=ResourceTypeLuxury)
@classmethod
def bonus(cls, *args: Any, **kwargs: Any) -> Resource:
return cls(*args, **kwargs, type_=ResourceTypeBonus)
@classmethod
def basic(cls, *args: Any, **kwargs: Any) -> Resource:
return cls(*args, **kwargs, type_=ResourceTypeBasic)
@classmethod
def mechanic(cls, *args: Any, **kwargs: Any) -> Resource:
return cls(*args, **kwargs, type_=ResourceTypeMechanic)
mapping: Dict[ResourceType, Type[ResourceTypeBase]] = {
ResourceType.MECHANIC: ResourceTypeMechanic,
ResourceType.BASIC: ResourceTypeBasic,
ResourceType.BONUS: ResourceTypeBonus,
ResourceType.STRATEGIC: ResourceTypeStrategic,
ResourceType.LUXURY: ResourceTypeLuxury,
}
class Resources:
def __init__(self):
self.resources: Dict[Type[ResourceTypeBase], Dict[str, Resource]] = {}
self.define_types()
def define_types(self) -> None:
global mapping
for item in mapping.values():
if item not in self.resources.keys():
self.resources[item] = {}
def flatten(self) -> Dict[str, Resource]:
return {key: resource for sub_dict in self.resources.values() for key, resource in sub_dict.items()}
def get(
self, _type: Type[ResourceTypeBase] | None = None, key: str | None = None
) -> Dict[Type[ResourceTypeBase], Dict[str, Resource]] | Resource | Dict[str, Resource]:
if _type is None:
for sub_dict in self.resources.values():
for resource in sub_dict.values():
if resource.key == key:
return resource
raise KeyError(f"Key {key} not found in resources")
else:
sub: Dict[str, Resource] = self.resources[_type]
if key is not None:
if key not in sub:
raise KeyError(f"Key {key} not found in resources")
return sub[key]
return self.resources
def toDict(self) -> Dict[Type[ResourceTypeBase], Dict[str, Resource]]:
return self.resources
def add(self, resource: Union[Resource, List[Resource]], auto_instance: bool = True) -> None:
def _add(self: Self, tmp_resource: Resource) -> None:
resource_type: Type[ResourceTypeBase] = tmp_resource.type
if resource_type not in self.resources:
self.resources[resource_type] = {}
self.resources[resource_type][tmp_resource.key] = tmp_resource
if isinstance(resource, Resource):
_add(self=self, tmp_resource=resource)
elif isinstance(resource, list): # type: ignore | Pyright is wrong here. Its saying that its a un-needed check but it is needed because it can also be anything else.
for r in resource:
_add(self=self, tmp_resource=r)
else:
raise ResourceTypeException(f"Resource must be of type Resource, not {type(resource)}") # noqa: F821
def __add__(self, b: Resource) -> None:
self.add(b)
def __getitem__(self, key: str) -> Dict[str, Resource] | Resource:
return self.flatten()[key]
class Costs:
def __init__(self, costs: List[Resource] | Resource) -> None:
self.costs: List[Resource] = costs if isinstance(costs, list) else [costs]