1- from dataclasses import dataclass , asdict , fields , astuple
1+ from dataclasses import dataclass
22from typing import Self
33
44from .BasicResources import BasicResources
@@ -11,23 +11,35 @@ class AllResources(BasicResources):
1111 def __sub__ (self , other : BasicResources ) -> Self :
1212 if not isinstance (other , BasicResources ):
1313 raise ValueError (f"Other element must be resource is { other .__class__ } " )
14- self_dict = asdict (self )
15- other_dict = asdict (other )
16- resources = AllResources (
17- ** dict (
18- (key , value - other_dict .get (key , 0 ))
19- for key , value in self_dict .items ()
20- )
21- )
22- resources = AllResources (
23- * tuple (max (0 , resource ) for resource in astuple (resources )[:- 1 ]),
24- resources .gold
14+ return AllResources (
15+ max (0 , self .red - other .red ),
16+ max (0 , self .green - other .green ),
17+ max (0 , self .blue - other .blue ),
18+ max (0 , self .black - other .black ),
19+ max (0 , self .white - other .white ),
20+ self .gold
2521 + sum (
26- min (0 , getattr (resources , field .name ))
27- for field in fields (BasicResources )
22+ (
23+ min (0 , self .red - other .red ),
24+ min (0 , self .green - other .green ),
25+ min (0 , self .blue - other .blue ),
26+ min (0 , self .black - other .black ),
27+ min (0 , self .white - other .white ),
28+ )
2829 ),
2930 )
30- return resources
31+
32+ def __add__ (self , other : Self ) -> Self :
33+ if not isinstance (other , BasicResources ):
34+ raise ValueError (f"Other element must be resource is { other .__class__ } " )
35+ return AllResources (
36+ self .red + other .red ,
37+ self .green + other .green ,
38+ self .blue + other .blue ,
39+ self .black + other .black ,
40+ self .white + other .white ,
41+ self .gold + getattr (other , "gold" , 0 ),
42+ )
3143
3244 def __rsub__ (self , other : BasicResources ) -> Self :
3345 return self .__sub__ (other )
0 commit comments