1import math
2import copy
3
4class Potion:
5
6 def __init__(self, effects, duration, is_part_of_bigger = False, is_applied = False):
7 self.duration = duration
8 self.is_part_of_bigger = is_part_of_bigger
9 self.is_applied = is_applied
10
11 self.effects = {}
12 for name, func in effects.items():
13 if name in self.effects:
14 self.effects[name].intensity += 1
15 else:
16 self.effects[name] = Effect(name, func)
17
18 def check_potions(self, other = None):
19 if self.is_applied is True:
20 raise TypeError("Potion is depleted")
21 if self.is_part_of_bigger is True:
22 raise TypeError("Potion is now part of something bigger than itself.")
23
24 if other is not None:
25 if other.is_part_of_bigger is True:
26 raise TypeError("Potion is now part of something bigger than itself.")
27 if other.is_applied is True:
28 raise TypeError("Potion is depleted")
29
30 def __getattr__(self, name):
31 if name in self.effects:
32 self.check_potions()
33 return self.effects[name]
34 else:
35 raise AttributeError
36
37 def __add__(self, other):
38 self.check_potions(other)
39
40 self.is_part_of_bigger = True
41 other.is_part_of_bigger = True
42
43 if self.duration > other.duration:
44 new_duration = self.duration
45 else:
46 new_duration = other.duration
47
48 new_potion = Potion({}, new_duration)
49 for name, func in other.effects.items():
50 new_potion.effects[name] = copy.deepcopy(other.effects[name])
51
52 for name, func in self.effects.items():
53 if name in new_potion.effects:
54 new_potion.effects[name].intensity += self.effects[name].intensity
55 else:
56 new_potion.effects[name] = copy.deepcopy(self.effects[name])
57
58 return new_potion
59
60
61 def __sub__(self, other):
62 self.check_potions(other)
63
64 self.is_part_of_bigger = True
65 other.is_part_of_bigger = True
66 new_potion = Potion({}, self.duration)
67
68 for name, func in self.effects.items():
69 new_potion.effects[name] = copy.deepcopy(self.effects[name])
70
71 for name in other.effects.keys():
72 if name in new_potion.effects:
73 new_potion.effects[name].intensity -= other.effects[name].intensity
74 if new_potion.effects[name].intensity <= 0:
75 del new_potion.effects[name]
76 else:
77 raise TypeError("Invalid purification")
78
79 return new_potion
80
81
82 def __mul__(self, num):
83 self.check_potions()
84
85 self.is_part_of_bigger = True
86 new_potion = Potion({}, self.duration)
87
88 for name, func in self.effects.items():
89 new_potion.effects[name] = copy.deepcopy(self.effects[name])
90
91 if isinstance(num, int):
92 for name in self.effects.keys():
93 new_potion.effects[name].intensity *= num
94 else:
95 for name in self.effects.keys():
96 new_intensity = new_potion.effects[name].intensity * num
97 if new_intensity - int(new_intensity) <= 0.5:
98 new_intensity = math.floor(new_intensity)
99 else:
100 new_intensity = math.ceil(new_intensity)
101 new_potion.effects[name].intensity = new_intensity
102
103 return new_potion
104
105 def __truediv__(self, num):
106 self.check_potions()
107
108 self.is_part_of_bigger = True
109
110 new_potion = Potion({}, self.duration)
111
112 for name, func in self.effects.items():
113 new_potion.effects[name] = copy.deepcopy(self.effects[name])
114
115 for name in self.effects.keys():
116 new_intensity = new_potion.effects[name].intensity / num
117 if new_intensity - int(new_intensity) <= 0.5:
118 new_intensity = math.floor(new_intensity)
119 else:
120 new_intensity = math.ceil(new_intensity)
121 new_potion.effects[name].intensity = new_intensity
122
123 return tuple(new_potion for _ in range(num))
124
125 def __eq__(self, other):
126 self.check_potions(other)
127
128 if self.effects.keys() != other.effects.keys():
129 return False
130
131 for name in self.effects.keys():
132 if not (self.effects[name] == other.effects[name]):
133 return False
134
135 return True
136
137 def __lt__(self, other):
138 self.check_potions(other)
139
140 my_int = 0
141 other_int = 0
142
143 for name in self.effects.keys():
144 my_int += self.effects[name].intensity
145
146 for name in other.effects.keys():
147 other_int += other.effects[name].intensity
148
149 return my_int < other_int
150
151 def __gt__(self, other):
152 self.check_potions(other)
153 return not self.__lt__(other) and not self.__eq__(other)
154
155
156class Effect:
157
158 def __init__(self, name, func, intensity = 1, used = False):
159 self.name = name
160 self.func = func
161 self.intensity = intensity
162 self.used = used
163 self.molecule = sum(ord(char) for char in name)
164
165 def __call__(self, args):
166 if self.used is True:
167 raise TypeError("Effect is depleted")
168 for count in range(self.intensity):
169 self.func(args)
170
171 self.used = True
172
173 def __eq__(self, other):
174 if self.name != other.name:
175 return False
176 if self.func != other.func:
177 return False
178 if self.intensity != other.intensity:
179 return False
180 return True
181
182class ГоспожатаПоХимия:
183
184 #def __init__(self)
185
186 def tick(self):
187 #to do
188 pass
189
190 def apply(self, target, potion):
191 potion.check_potion()
192
193 potion.effects = dict(sorted(potion.effects.items(), key=lambda item: item[1].molecule))
194
195 for name in potion.effects.keys():
196 if potion.effects[name].used == False:
197 for count in range(potion.effects[name].intensity):
198 potion.effects[name](target)
199
200 potion.is_applied = True
201
.F..F......EEEEEEEEE
======================================================================
ERROR: test_separation (test.TestPotionOperations)
Test separation of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 218, in test_separation
potion2.int_attr_fun(self._target)
File "/tmp/solution.py", line 167, in __call__
raise TypeError("Effect is depleted")
TypeError: Effect is depleted
======================================================================
ERROR: test_applying_depleted_potion (test.TestГоспожатаПоХимия)
Test applying a depleted potion or a potion that was used in a reaction.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 379, in test_applying_depleted_potion
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_applying_normal_case (test.TestГоспожатаПоХимия)
Test applying a normal potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 350, in test_applying_normal_case
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_applying_order (test.TestГоспожатаПоХимия)
Test applying order of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 404, in test_applying_order
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_applying_part_of_potion (test.TestГоспожатаПоХимия)
Test applying only a part of a potion.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 365, in test_applying_part_of_potion
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_ticking_immutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with immutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 424, in test_ticking_immutable
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_ticking_multiple_potions (test.TestГоспожатаПоХимия)
Test ticking after applying multiple potions which affect the same attribute.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 454, in test_ticking_multiple_potions
self._dimitrichka.apply(self._target, potion1)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_ticking_multiple_targets (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 485, in test_ticking_multiple_targets
self._dimitrichka.apply(target1, potion1)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
ERROR: test_ticking_mutable (test.TestГоспожатаПоХимия)
Test ticking after applying a potion with mutable attributes.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 438, in test_ticking_mutable
self._dimitrichka.apply(self._target, potion)
File "/tmp/solution.py", line 191, in apply
potion.check_potion()
File "/tmp/solution.py", line 35, in __getattr__
raise AttributeError
AttributeError
======================================================================
FAIL: test_depletion (test.TestBasicPotion)
Test depletion of a potion effect.
----------------------------------------------------------------------
TypeError: Effect is depleted
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 78, in test_depletion
with self.assertRaisesRegex(TypeError, 'Effect is depleted\.'):
AssertionError: "Effect is depleted\." does not match "Effect is depleted"
======================================================================
FAIL: test_superbness (test.TestPotionComparison)
Test superbness of potions.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 329, in test_superbness
self.assertFalse(potion1 > potion2)
AssertionError: True is not false
----------------------------------------------------------------------
Ran 20 tests in 0.003s
FAILED (failures=2, errors=9)
f | 1 | import math | f | 1 | import math |
2 | import copy | 2 | import copy | ||
3 | 3 | ||||
4 | class Potion: | 4 | class Potion: | ||
5 | 5 | ||||
6 | def __init__(self, effects, duration, is_part_of_bigger = False, is_applied = False): | 6 | def __init__(self, effects, duration, is_part_of_bigger = False, is_applied = False): | ||
7 | self.duration = duration | 7 | self.duration = duration | ||
8 | self.is_part_of_bigger = is_part_of_bigger | 8 | self.is_part_of_bigger = is_part_of_bigger | ||
9 | self.is_applied = is_applied | 9 | self.is_applied = is_applied | ||
10 | 10 | ||||
11 | self.effects = {} | 11 | self.effects = {} | ||
12 | for name, func in effects.items(): | 12 | for name, func in effects.items(): | ||
13 | if name in self.effects: | 13 | if name in self.effects: | ||
14 | self.effects[name].intensity += 1 | 14 | self.effects[name].intensity += 1 | ||
15 | else: | 15 | else: | ||
16 | self.effects[name] = Effect(name, func) | 16 | self.effects[name] = Effect(name, func) | ||
17 | 17 | ||||
18 | def check_potions(self, other = None): | 18 | def check_potions(self, other = None): | ||
19 | if self.is_applied is True: | 19 | if self.is_applied is True: | ||
20 | raise TypeError("Potion is depleted") | 20 | raise TypeError("Potion is depleted") | ||
21 | if self.is_part_of_bigger is True: | 21 | if self.is_part_of_bigger is True: | ||
22 | raise TypeError("Potion is now part of something bigger than itself.") | 22 | raise TypeError("Potion is now part of something bigger than itself.") | ||
23 | 23 | ||||
24 | if other is not None: | 24 | if other is not None: | ||
25 | if other.is_part_of_bigger is True: | 25 | if other.is_part_of_bigger is True: | ||
26 | raise TypeError("Potion is now part of something bigger than itself.") | 26 | raise TypeError("Potion is now part of something bigger than itself.") | ||
27 | if other.is_applied is True: | 27 | if other.is_applied is True: | ||
28 | raise TypeError("Potion is depleted") | 28 | raise TypeError("Potion is depleted") | ||
29 | 29 | ||||
30 | def __getattr__(self, name): | 30 | def __getattr__(self, name): | ||
31 | if name in self.effects: | 31 | if name in self.effects: | ||
32 | self.check_potions() | 32 | self.check_potions() | ||
33 | return self.effects[name] | 33 | return self.effects[name] | ||
34 | else: | 34 | else: | ||
35 | raise AttributeError | 35 | raise AttributeError | ||
36 | 36 | ||||
37 | def __add__(self, other): | 37 | def __add__(self, other): | ||
38 | self.check_potions(other) | 38 | self.check_potions(other) | ||
39 | 39 | ||||
40 | self.is_part_of_bigger = True | 40 | self.is_part_of_bigger = True | ||
41 | other.is_part_of_bigger = True | 41 | other.is_part_of_bigger = True | ||
42 | 42 | ||||
43 | if self.duration > other.duration: | 43 | if self.duration > other.duration: | ||
44 | new_duration = self.duration | 44 | new_duration = self.duration | ||
45 | else: | 45 | else: | ||
46 | new_duration = other.duration | 46 | new_duration = other.duration | ||
47 | 47 | ||||
48 | new_potion = Potion({}, new_duration) | 48 | new_potion = Potion({}, new_duration) | ||
49 | for name, func in other.effects.items(): | 49 | for name, func in other.effects.items(): | ||
50 | new_potion.effects[name] = copy.deepcopy(other.effects[name]) | 50 | new_potion.effects[name] = copy.deepcopy(other.effects[name]) | ||
51 | 51 | ||||
52 | for name, func in self.effects.items(): | 52 | for name, func in self.effects.items(): | ||
53 | if name in new_potion.effects: | 53 | if name in new_potion.effects: | ||
54 | new_potion.effects[name].intensity += self.effects[name].intensity | 54 | new_potion.effects[name].intensity += self.effects[name].intensity | ||
55 | else: | 55 | else: | ||
56 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 56 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
57 | 57 | ||||
58 | return new_potion | 58 | return new_potion | ||
59 | 59 | ||||
60 | 60 | ||||
61 | def __sub__(self, other): | 61 | def __sub__(self, other): | ||
62 | self.check_potions(other) | 62 | self.check_potions(other) | ||
63 | 63 | ||||
64 | self.is_part_of_bigger = True | 64 | self.is_part_of_bigger = True | ||
65 | other.is_part_of_bigger = True | 65 | other.is_part_of_bigger = True | ||
66 | new_potion = Potion({}, self.duration) | 66 | new_potion = Potion({}, self.duration) | ||
67 | 67 | ||||
68 | for name, func in self.effects.items(): | 68 | for name, func in self.effects.items(): | ||
69 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 69 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
70 | 70 | ||||
71 | for name in other.effects.keys(): | 71 | for name in other.effects.keys(): | ||
72 | if name in new_potion.effects: | 72 | if name in new_potion.effects: | ||
73 | new_potion.effects[name].intensity -= other.effects[name].intensity | 73 | new_potion.effects[name].intensity -= other.effects[name].intensity | ||
74 | if new_potion.effects[name].intensity <= 0: | 74 | if new_potion.effects[name].intensity <= 0: | ||
75 | del new_potion.effects[name] | 75 | del new_potion.effects[name] | ||
76 | else: | 76 | else: | ||
77 | raise TypeError("Invalid purification") | 77 | raise TypeError("Invalid purification") | ||
78 | 78 | ||||
79 | return new_potion | 79 | return new_potion | ||
80 | 80 | ||||
81 | 81 | ||||
82 | def __mul__(self, num): | 82 | def __mul__(self, num): | ||
83 | self.check_potions() | 83 | self.check_potions() | ||
84 | 84 | ||||
85 | self.is_part_of_bigger = True | 85 | self.is_part_of_bigger = True | ||
86 | new_potion = Potion({}, self.duration) | 86 | new_potion = Potion({}, self.duration) | ||
87 | 87 | ||||
88 | for name, func in self.effects.items(): | 88 | for name, func in self.effects.items(): | ||
89 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 89 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
90 | 90 | ||||
91 | if isinstance(num, int): | 91 | if isinstance(num, int): | ||
92 | for name in self.effects.keys(): | 92 | for name in self.effects.keys(): | ||
93 | new_potion.effects[name].intensity *= num | 93 | new_potion.effects[name].intensity *= num | ||
94 | else: | 94 | else: | ||
95 | for name in self.effects.keys(): | 95 | for name in self.effects.keys(): | ||
96 | new_intensity = new_potion.effects[name].intensity * num | 96 | new_intensity = new_potion.effects[name].intensity * num | ||
97 | if new_intensity - int(new_intensity) <= 0.5: | 97 | if new_intensity - int(new_intensity) <= 0.5: | ||
98 | new_intensity = math.floor(new_intensity) | 98 | new_intensity = math.floor(new_intensity) | ||
99 | else: | 99 | else: | ||
100 | new_intensity = math.ceil(new_intensity) | 100 | new_intensity = math.ceil(new_intensity) | ||
101 | new_potion.effects[name].intensity = new_intensity | 101 | new_potion.effects[name].intensity = new_intensity | ||
102 | 102 | ||||
103 | return new_potion | 103 | return new_potion | ||
104 | 104 | ||||
105 | def __truediv__(self, num): | 105 | def __truediv__(self, num): | ||
106 | self.check_potions() | 106 | self.check_potions() | ||
107 | 107 | ||||
108 | self.is_part_of_bigger = True | 108 | self.is_part_of_bigger = True | ||
109 | 109 | ||||
110 | new_potion = Potion({}, self.duration) | 110 | new_potion = Potion({}, self.duration) | ||
111 | 111 | ||||
112 | for name, func in self.effects.items(): | 112 | for name, func in self.effects.items(): | ||
113 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 113 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
114 | 114 | ||||
115 | for name in self.effects.keys(): | 115 | for name in self.effects.keys(): | ||
116 | new_intensity = new_potion.effects[name].intensity / num | 116 | new_intensity = new_potion.effects[name].intensity / num | ||
117 | if new_intensity - int(new_intensity) <= 0.5: | 117 | if new_intensity - int(new_intensity) <= 0.5: | ||
118 | new_intensity = math.floor(new_intensity) | 118 | new_intensity = math.floor(new_intensity) | ||
119 | else: | 119 | else: | ||
120 | new_intensity = math.ceil(new_intensity) | 120 | new_intensity = math.ceil(new_intensity) | ||
121 | new_potion.effects[name].intensity = new_intensity | 121 | new_potion.effects[name].intensity = new_intensity | ||
122 | 122 | ||||
123 | return tuple(new_potion for _ in range(num)) | 123 | return tuple(new_potion for _ in range(num)) | ||
124 | 124 | ||||
125 | def __eq__(self, other): | 125 | def __eq__(self, other): | ||
126 | self.check_potions(other) | 126 | self.check_potions(other) | ||
127 | 127 | ||||
128 | if self.effects.keys() != other.effects.keys(): | 128 | if self.effects.keys() != other.effects.keys(): | ||
129 | return False | 129 | return False | ||
130 | 130 | ||||
131 | for name in self.effects.keys(): | 131 | for name in self.effects.keys(): | ||
132 | if not (self.effects[name] == other.effects[name]): | 132 | if not (self.effects[name] == other.effects[name]): | ||
133 | return False | 133 | return False | ||
134 | 134 | ||||
135 | return True | 135 | return True | ||
136 | 136 | ||||
137 | def __lt__(self, other): | 137 | def __lt__(self, other): | ||
138 | self.check_potions(other) | 138 | self.check_potions(other) | ||
139 | 139 | ||||
140 | my_int = 0 | 140 | my_int = 0 | ||
141 | other_int = 0 | 141 | other_int = 0 | ||
142 | 142 | ||||
143 | for name in self.effects.keys(): | 143 | for name in self.effects.keys(): | ||
144 | my_int += self.effects[name].intensity | 144 | my_int += self.effects[name].intensity | ||
145 | 145 | ||||
146 | for name in other.effects.keys(): | 146 | for name in other.effects.keys(): | ||
147 | other_int += other.effects[name].intensity | 147 | other_int += other.effects[name].intensity | ||
148 | 148 | ||||
149 | return my_int < other_int | 149 | return my_int < other_int | ||
150 | 150 | ||||
151 | def __gt__(self, other): | 151 | def __gt__(self, other): | ||
152 | self.check_potions(other) | 152 | self.check_potions(other) | ||
153 | return not self.__lt__(other) and not self.__eq__(other) | 153 | return not self.__lt__(other) and not self.__eq__(other) | ||
154 | 154 | ||||
155 | 155 | ||||
156 | class Effect: | 156 | class Effect: | ||
157 | 157 | ||||
158 | def __init__(self, name, func, intensity = 1, used = False): | 158 | def __init__(self, name, func, intensity = 1, used = False): | ||
159 | self.name = name | 159 | self.name = name | ||
160 | self.func = func | 160 | self.func = func | ||
161 | self.intensity = intensity | 161 | self.intensity = intensity | ||
162 | self.used = used | 162 | self.used = used | ||
n | n | 163 | self.molecule = sum(ord(char) for char in name) | ||
163 | 164 | ||||
164 | def __call__(self, args): | 165 | def __call__(self, args): | ||
165 | if self.used is True: | 166 | if self.used is True: | ||
166 | raise TypeError("Effect is depleted") | 167 | raise TypeError("Effect is depleted") | ||
167 | for count in range(self.intensity): | 168 | for count in range(self.intensity): | ||
168 | self.func(args) | 169 | self.func(args) | ||
169 | 170 | ||||
170 | self.used = True | 171 | self.used = True | ||
171 | 172 | ||||
172 | def __eq__(self, other): | 173 | def __eq__(self, other): | ||
173 | if self.name != other.name: | 174 | if self.name != other.name: | ||
174 | return False | 175 | return False | ||
175 | if self.func != other.func: | 176 | if self.func != other.func: | ||
176 | return False | 177 | return False | ||
177 | if self.intensity != other.intensity: | 178 | if self.intensity != other.intensity: | ||
178 | return False | 179 | return False | ||
179 | return True | 180 | return True | ||
180 | 181 | ||||
181 | class ГоспожатаПоХимия: | 182 | class ГоспожатаПоХимия: | ||
182 | 183 | ||||
183 | #def __init__(self) | 184 | #def __init__(self) | ||
184 | 185 | ||||
185 | def tick(self): | 186 | def tick(self): | ||
186 | #to do | 187 | #to do | ||
187 | pass | 188 | pass | ||
188 | 189 | ||||
189 | def apply(self, target, potion): | 190 | def apply(self, target, potion): | ||
190 | potion.check_potion() | 191 | potion.check_potion() | ||
n | 191 | n | 192 | ||
193 | potion.effects = dict(sorted(potion.effects.items(), key=lambda item: item[1].molecule)) | ||||
194 | |||||
192 | for name in potion.effects.keys(): | 195 | for name in potion.effects.keys(): | ||
193 | if potion.effects[name].used == False: | 196 | if potion.effects[name].used == False: | ||
t | 194 | for count in potion.effects[name].intensity: | t | 197 | for count in range(potion.effects[name].intensity): |
195 | potion.effects[name](target) | 198 | potion.effects[name](target) | ||
196 | 199 | ||||
197 | potion.is_applied = True | 200 | potion.is_applied = True | ||
198 | 201 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
2 | import copy | 2 | import copy | ||
3 | 3 | ||||
4 | class Potion: | 4 | class Potion: | ||
5 | 5 | ||||
6 | def __init__(self, effects, duration, is_part_of_bigger = False, is_applied = False): | 6 | def __init__(self, effects, duration, is_part_of_bigger = False, is_applied = False): | ||
7 | self.duration = duration | 7 | self.duration = duration | ||
8 | self.is_part_of_bigger = is_part_of_bigger | 8 | self.is_part_of_bigger = is_part_of_bigger | ||
9 | self.is_applied = is_applied | 9 | self.is_applied = is_applied | ||
10 | 10 | ||||
11 | self.effects = {} | 11 | self.effects = {} | ||
12 | for name, func in effects.items(): | 12 | for name, func in effects.items(): | ||
13 | if name in self.effects: | 13 | if name in self.effects: | ||
14 | self.effects[name].intensity += 1 | 14 | self.effects[name].intensity += 1 | ||
15 | else: | 15 | else: | ||
16 | self.effects[name] = Effect(name, func) | 16 | self.effects[name] = Effect(name, func) | ||
17 | 17 | ||||
18 | def check_potions(self, other = None): | 18 | def check_potions(self, other = None): | ||
19 | if self.is_applied is True: | 19 | if self.is_applied is True: | ||
20 | raise TypeError("Potion is depleted") | 20 | raise TypeError("Potion is depleted") | ||
21 | if self.is_part_of_bigger is True: | 21 | if self.is_part_of_bigger is True: | ||
22 | raise TypeError("Potion is now part of something bigger than itself.") | 22 | raise TypeError("Potion is now part of something bigger than itself.") | ||
23 | 23 | ||||
24 | if other is not None: | 24 | if other is not None: | ||
25 | if other.is_part_of_bigger is True: | 25 | if other.is_part_of_bigger is True: | ||
26 | raise TypeError("Potion is now part of something bigger than itself.") | 26 | raise TypeError("Potion is now part of something bigger than itself.") | ||
27 | if other.is_applied is True: | 27 | if other.is_applied is True: | ||
28 | raise TypeError("Potion is depleted") | 28 | raise TypeError("Potion is depleted") | ||
29 | 29 | ||||
30 | def __getattr__(self, name): | 30 | def __getattr__(self, name): | ||
31 | if name in self.effects: | 31 | if name in self.effects: | ||
32 | self.check_potions() | 32 | self.check_potions() | ||
33 | return self.effects[name] | 33 | return self.effects[name] | ||
34 | else: | 34 | else: | ||
35 | raise AttributeError | 35 | raise AttributeError | ||
36 | 36 | ||||
37 | def __add__(self, other): | 37 | def __add__(self, other): | ||
38 | self.check_potions(other) | 38 | self.check_potions(other) | ||
39 | 39 | ||||
40 | self.is_part_of_bigger = True | 40 | self.is_part_of_bigger = True | ||
41 | other.is_part_of_bigger = True | 41 | other.is_part_of_bigger = True | ||
42 | 42 | ||||
43 | if self.duration > other.duration: | 43 | if self.duration > other.duration: | ||
44 | new_duration = self.duration | 44 | new_duration = self.duration | ||
45 | else: | 45 | else: | ||
46 | new_duration = other.duration | 46 | new_duration = other.duration | ||
47 | 47 | ||||
48 | new_potion = Potion({}, new_duration) | 48 | new_potion = Potion({}, new_duration) | ||
49 | for name, func in other.effects.items(): | 49 | for name, func in other.effects.items(): | ||
50 | new_potion.effects[name] = copy.deepcopy(other.effects[name]) | 50 | new_potion.effects[name] = copy.deepcopy(other.effects[name]) | ||
51 | 51 | ||||
52 | for name, func in self.effects.items(): | 52 | for name, func in self.effects.items(): | ||
53 | if name in new_potion.effects: | 53 | if name in new_potion.effects: | ||
54 | new_potion.effects[name].intensity += self.effects[name].intensity | 54 | new_potion.effects[name].intensity += self.effects[name].intensity | ||
55 | else: | 55 | else: | ||
56 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 56 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
57 | 57 | ||||
58 | return new_potion | 58 | return new_potion | ||
59 | 59 | ||||
60 | 60 | ||||
61 | def __sub__(self, other): | 61 | def __sub__(self, other): | ||
62 | self.check_potions(other) | 62 | self.check_potions(other) | ||
63 | 63 | ||||
64 | self.is_part_of_bigger = True | 64 | self.is_part_of_bigger = True | ||
65 | other.is_part_of_bigger = True | 65 | other.is_part_of_bigger = True | ||
66 | new_potion = Potion({}, self.duration) | 66 | new_potion = Potion({}, self.duration) | ||
67 | 67 | ||||
68 | for name, func in self.effects.items(): | 68 | for name, func in self.effects.items(): | ||
69 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 69 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
70 | 70 | ||||
71 | for name in other.effects.keys(): | 71 | for name in other.effects.keys(): | ||
72 | if name in new_potion.effects: | 72 | if name in new_potion.effects: | ||
73 | new_potion.effects[name].intensity -= other.effects[name].intensity | 73 | new_potion.effects[name].intensity -= other.effects[name].intensity | ||
74 | if new_potion.effects[name].intensity <= 0: | 74 | if new_potion.effects[name].intensity <= 0: | ||
75 | del new_potion.effects[name] | 75 | del new_potion.effects[name] | ||
76 | else: | 76 | else: | ||
77 | raise TypeError("Invalid purification") | 77 | raise TypeError("Invalid purification") | ||
78 | 78 | ||||
79 | return new_potion | 79 | return new_potion | ||
80 | 80 | ||||
81 | 81 | ||||
82 | def __mul__(self, num): | 82 | def __mul__(self, num): | ||
83 | self.check_potions() | 83 | self.check_potions() | ||
84 | 84 | ||||
85 | self.is_part_of_bigger = True | 85 | self.is_part_of_bigger = True | ||
86 | new_potion = Potion({}, self.duration) | 86 | new_potion = Potion({}, self.duration) | ||
87 | 87 | ||||
88 | for name, func in self.effects.items(): | 88 | for name, func in self.effects.items(): | ||
89 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 89 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
90 | 90 | ||||
91 | if isinstance(num, int): | 91 | if isinstance(num, int): | ||
92 | for name in self.effects.keys(): | 92 | for name in self.effects.keys(): | ||
93 | new_potion.effects[name].intensity *= num | 93 | new_potion.effects[name].intensity *= num | ||
94 | else: | 94 | else: | ||
95 | for name in self.effects.keys(): | 95 | for name in self.effects.keys(): | ||
96 | new_intensity = new_potion.effects[name].intensity * num | 96 | new_intensity = new_potion.effects[name].intensity * num | ||
97 | if new_intensity - int(new_intensity) <= 0.5: | 97 | if new_intensity - int(new_intensity) <= 0.5: | ||
98 | new_intensity = math.floor(new_intensity) | 98 | new_intensity = math.floor(new_intensity) | ||
99 | else: | 99 | else: | ||
100 | new_intensity = math.ceil(new_intensity) | 100 | new_intensity = math.ceil(new_intensity) | ||
101 | new_potion.effects[name].intensity = new_intensity | 101 | new_potion.effects[name].intensity = new_intensity | ||
102 | 102 | ||||
103 | return new_potion | 103 | return new_potion | ||
104 | 104 | ||||
105 | def __truediv__(self, num): | 105 | def __truediv__(self, num): | ||
106 | self.check_potions() | 106 | self.check_potions() | ||
107 | 107 | ||||
108 | self.is_part_of_bigger = True | 108 | self.is_part_of_bigger = True | ||
109 | 109 | ||||
110 | new_potion = Potion({}, self.duration) | 110 | new_potion = Potion({}, self.duration) | ||
111 | 111 | ||||
112 | for name, func in self.effects.items(): | 112 | for name, func in self.effects.items(): | ||
113 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | 113 | new_potion.effects[name] = copy.deepcopy(self.effects[name]) | ||
114 | 114 | ||||
115 | for name in self.effects.keys(): | 115 | for name in self.effects.keys(): | ||
116 | new_intensity = new_potion.effects[name].intensity / num | 116 | new_intensity = new_potion.effects[name].intensity / num | ||
117 | if new_intensity - int(new_intensity) <= 0.5: | 117 | if new_intensity - int(new_intensity) <= 0.5: | ||
118 | new_intensity = math.floor(new_intensity) | 118 | new_intensity = math.floor(new_intensity) | ||
119 | else: | 119 | else: | ||
120 | new_intensity = math.ceil(new_intensity) | 120 | new_intensity = math.ceil(new_intensity) | ||
121 | new_potion.effects[name].intensity = new_intensity | 121 | new_potion.effects[name].intensity = new_intensity | ||
122 | 122 | ||||
123 | return tuple(new_potion for _ in range(num)) | 123 | return tuple(new_potion for _ in range(num)) | ||
124 | 124 | ||||
125 | def __eq__(self, other): | 125 | def __eq__(self, other): | ||
126 | self.check_potions(other) | 126 | self.check_potions(other) | ||
127 | 127 | ||||
128 | if self.effects.keys() != other.effects.keys(): | 128 | if self.effects.keys() != other.effects.keys(): | ||
129 | return False | 129 | return False | ||
130 | 130 | ||||
131 | for name in self.effects.keys(): | 131 | for name in self.effects.keys(): | ||
132 | if not (self.effects[name] == other.effects[name]): | 132 | if not (self.effects[name] == other.effects[name]): | ||
133 | return False | 133 | return False | ||
134 | 134 | ||||
135 | return True | 135 | return True | ||
136 | 136 | ||||
137 | def __lt__(self, other): | 137 | def __lt__(self, other): | ||
138 | self.check_potions(other) | 138 | self.check_potions(other) | ||
139 | 139 | ||||
140 | my_int = 0 | 140 | my_int = 0 | ||
141 | other_int = 0 | 141 | other_int = 0 | ||
142 | 142 | ||||
143 | for name in self.effects.keys(): | 143 | for name in self.effects.keys(): | ||
144 | my_int += self.effects[name].intensity | 144 | my_int += self.effects[name].intensity | ||
145 | 145 | ||||
146 | for name in other.effects.keys(): | 146 | for name in other.effects.keys(): | ||
147 | other_int += other.effects[name].intensity | 147 | other_int += other.effects[name].intensity | ||
148 | 148 | ||||
149 | return my_int < other_int | 149 | return my_int < other_int | ||
150 | 150 | ||||
151 | def __gt__(self, other): | 151 | def __gt__(self, other): | ||
152 | self.check_potions(other) | 152 | self.check_potions(other) | ||
153 | return not self.__lt__(other) and not self.__eq__(other) | 153 | return not self.__lt__(other) and not self.__eq__(other) | ||
154 | 154 | ||||
155 | 155 | ||||
156 | class Effect: | 156 | class Effect: | ||
157 | 157 | ||||
158 | def __init__(self, name, func, intensity = 1, used = False): | 158 | def __init__(self, name, func, intensity = 1, used = False): | ||
159 | self.name = name | 159 | self.name = name | ||
160 | self.func = func | 160 | self.func = func | ||
161 | self.intensity = intensity | 161 | self.intensity = intensity | ||
162 | self.used = used | 162 | self.used = used | ||
163 | 163 | ||||
164 | def __call__(self, args): | 164 | def __call__(self, args): | ||
165 | if self.used is True: | 165 | if self.used is True: | ||
166 | raise TypeError("Effect is depleted") | 166 | raise TypeError("Effect is depleted") | ||
167 | for count in range(self.intensity): | 167 | for count in range(self.intensity): | ||
168 | self.func(args) | 168 | self.func(args) | ||
169 | 169 | ||||
170 | self.used = True | 170 | self.used = True | ||
171 | 171 | ||||
172 | def __eq__(self, other): | 172 | def __eq__(self, other): | ||
173 | if self.name != other.name: | 173 | if self.name != other.name: | ||
174 | return False | 174 | return False | ||
175 | if self.func != other.func: | 175 | if self.func != other.func: | ||
176 | return False | 176 | return False | ||
177 | if self.intensity != other.intensity: | 177 | if self.intensity != other.intensity: | ||
178 | return False | 178 | return False | ||
179 | return True | 179 | return True | ||
180 | 180 | ||||
n | 181 | class ГоспожатаПоХимия: | n | 181 | class ГоспожатаПоХимия: |
182 | |||||
183 | #def __init__(self) | ||||
184 | |||||
185 | def tick(self): | ||||
186 | #to do | ||||
187 | pass | ||||
182 | 188 | ||||
n | 183 | #def __init__(self) | n | ||
184 | |||||
185 | def tick(self): | ||||
186 | #to do | ||||
187 | pass | ||||
188 | |||||
189 | def apply(self, target, potion): | 189 | def apply(self, target, potion): | ||
190 | potion.check_potion() | 190 | potion.check_potion() | ||
191 | 191 | ||||
n | 192 | for name in potion.effects.keys(): | n | 192 | for name in potion.effects.keys(): |
193 | if potion.effects[name].used == False: | 193 | if potion.effects[name].used == False: | ||
194 | for count in potion.effects[name].intensity: | 194 | for count in potion.effects[name].intensity: | ||
195 | potion.effects[name](target) | 195 | potion.effects[name](target) | ||
196 | 196 | ||||
t | 197 | potion.is_applied = True | t | 197 | potion.is_applied = True |
198 | 198 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
06.12.2023 09:50
06.12.2023 09:56
06.12.2023 09:52
06.12.2023 09:53
06.12.2023 09:54