-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.py
102 lines (82 loc) · 2.84 KB
/
item.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
from __future__ import annotations
from openciv.engine.managers.i18n import T_TranslationOrStr
from openciv.engine.saving import SaveAble
from openciv.gameplay.effect import Effects
from openciv.gameplay.combat.stats import Stats
from openciv.engine.requires import Requires
from typing import Iterator, List, Any
class Item(SaveAble):
def __init__(
self,
key: str,
name: T_TranslationOrStr,
description: T_TranslationOrStr,
icon: str,
effects: Effects = Effects(),
requires: Requires = Requires(),
*args: Any,
**kwargs: Any,
):
self.key: str = key
self.name: T_TranslationOrStr = name
self.description: T_TranslationOrStr = description
self.icon: str = icon
self.active: bool = False
self.effects: Effects = effects
self.requires: Requires = requires
self.combat_stats: Stats = Stats()
self._setup_saveable()
class Items(SaveAble):
def __init__(
self,
key: str | None = None,
name: T_TranslationOrStr | None = None,
description: T_TranslationOrStr | None = None,
icon: str | None = None,
*args: Any,
**kwargs: Any,
) -> None:
SaveAble.__init__(self, *args, **kwargs)
self.key: str | None = key
self.name: T_TranslationOrStr | None = name
self.description: T_TranslationOrStr | None = description
self.icon: str | None = icon
self.items: List[Item] = []
self._setup_saveable()
def add_item(self, item: Item) -> Items:
self.items.append(item)
return self
def remove_item(self, item: Item) -> Items:
self.items.remove(item)
return self
def __add__(self, b: Items | Item) -> Items:
# Add item to items
if isinstance(b, Item):
self.items.append(b)
# Merge items
elif isinstance(b, Items): # type: ignore
self.items.extend(b.items)
return self
def __radd__(self, b: Items | Item) -> Items:
return self.__add__(b)
def __sub__(self, b: Items | Item) -> Items:
# Remove item from items
if isinstance(b, Item):
self.items.remove(b)
# Remove all items from items
elif isinstance(b, Items): # type: ignore
for item in b.items:
self.items.remove(item)
return self
def __rsub__(self, b: Items | Item) -> Items:
return self.__sub__(b)
def __getitem__(self, key: int) -> Item:
return self.items[key]
def __setitem__(self, index: int, value: Item) -> None:
self.items[index] = value
def __delitem__(self, index: int) -> None:
del self.items[index]
def __iter__(self) -> Iterator[Item]:
return iter(self.items)
def __len__(self) -> int:
return len(self.items)