forked from yigitbey/soyouhaveanidea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
100 lines (80 loc) · 2.62 KB
/
entity.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
import copy
from exceptions import *
class EntityMeta(type):
def __new__(mcs, clsname, superclasses, attributedict):
clss = type.__new__(mcs, clsname, superclasses, attributedict)
if 'cost' in attributedict:
clss.message = "{} (${} monthly)".format(attributedict['formatted'], attributedict['cost'])
else:
clss.message = "{}".format(attributedict['formatted'])
return clss
class Entity(object, metaclass=EntityMeta):
current_amount = 0
initial_cost = 0
limit = -1
unlocked = False
unlocks_entities = []
locks_entities = []
cost = 0
formatted = "Entity"
drains = {}
replenishes = {}
inventory = {'money': 0}
project = None
increases = {}
decreases = {}
productivity_modifier = 0
detail_fields = []
def __init__(self, project=None, inventory=inventory, draining=drains, replenishing=replenishes):
if self.__class__.limit >= 0:
if self.__class__.current_amount >= self.__class__.limit:
raise TooManyEntitiesException("No limit for {}".format(self.__class__.__name__))
self.inventory = copy.deepcopy(inventory)
self.draining = copy.deepcopy(draining)
self.replenishing = copy.deepcopy(replenishing)
self.project = project
self.__class__.current_amount += 1
self.__class__.unlocks()
self.__class__.locks()
def __repr__(self):
return self.__class__.__name__
def trade(self, to_entity, item, value):
item1 = getattr(self, item)
item2 = getattr(to_entity, item)
setattr(self, item, item1-value)
setattr(to_entity, item, item2+value)
def turn(self):
self.drain()
self.replenish()
def drain(self):
for key, value in self.draining.items():
self.inventory[key] -= value
def replenish(self):
for key, value in self.replenishing.items():
self.inventory[key] += value
@property
def money(self):
return self.inventory['money']
@money.setter
def money(self, value):
self.inventory['money'] = value
@classmethod
def unlocks(cls):
for entity in cls.unlocks_entities:
entity.unlock()
@classmethod
def locks(cls):
for entity in cls.locks_entities:
entity.lock()
@classmethod
def unlock(cls):
cls.unlocked = True
@classmethod
def lock(cls):
cls.unlocked = False
@classmethod
def limit_reached(cls):
if cls.limit >= 0:
if cls.current_amount >= cls.limit:
return True
return False