-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.py
214 lines (175 loc) · 8.5 KB
/
cat.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
from datetime import datetime, timezone
import json
import os
class Cat:
def __init__(self, name: str,
max_food=100.0,
food_gain_modifier=10.0,
food_drain_modifier=100.0,
max_energy=100.0,
energy_gain_modifier=100.0,
energy_drain_modifier=5.0,
max_excitement=100.0,
excitement_gain_modifier=5.0,
excitement_drain_modifier=100.0,
evolution_thresholds=None,
look='cat1'
):
"""
:param name: Name of the cat. Purely cosmetic
:param max_food: Maximum value of the food meter
:param food_gain_modifier: Food gained per commit
:param food_drain_modifier: Food lost per day
:param max_energy: Maximum value of the energy meter
:param energy_gain_modifier: Energy gained per day
:param energy_drain_modifier: Energy lost per file touched
:param max_excitement: Maximum value of the excitement meter
:param excitement_gain_modifier: Excitement gained per line added
:param excitement_drain_modifier: Excitement lost per day
:param evolution_thresholds: List of thresholds for the cat to evolve. If None, the cat will always stay at evolution stage 0
"""
self.name = name
self.look = look
self.max_food = max_food
self.food_gain_modifier = food_gain_modifier
self.food_drain_modifier = food_drain_modifier
self.food = self.max_food
self.max_energy = max_energy
self.energy_gain_modifier = energy_gain_modifier
self.energy_drain_modifier = energy_drain_modifier
self.energy = self.max_energy
self.max_excitement = max_excitement
self.excitement_gain_modifier = excitement_gain_modifier
self.excitement_drain_modifier = excitement_drain_modifier
self.excitement = self.max_excitement
self.evolution_thresholds = evolution_thresholds if evolution_thresholds is not None else []
self.evolution = 0.0
self.last_update = datetime.now(timezone.utc)
def pet(self):
"""A manual way to increase cat excitement. Besides, who doesn't like petting cats?"""
self.excite(10)
self.ascii_plot("petting")
self.save()
def nap(self):
"""A manual way to increase cat energy"""
self.recharge(1/24)
self.ascii_plot("tired")
self.save()
def update_by_time_passed(self, days: float) -> bool:
"""
Updates the cat needs by the given amount of days.
:param days: a float representing the amount of days that passed since the last update
:return: True if cat reached new evolution stage, False otherwise
"""
self.hunger(days)
self.bore(days)
self.recharge(days)
prev_stage = self.get_evolution_stage()
self.evolve(days)
new_stage = self.get_evolution_stage()
return prev_stage != new_stage
def feed(self, amount: float):
self.food = min(self.food + amount * self.food_gain_modifier, self.max_food)
def hunger(self, days: float):
self.food = max(0.0, self.food - days * self.food_drain_modifier)
def recharge(self, days: float):
self.energy = min(self.energy + days * self.energy_gain_modifier, self.max_energy)
def exhaust(self, amount: float):
self.energy = max(0.0, self.energy - amount * self.energy_drain_modifier)
def excite(self, amount: float):
self.excitement = min(self.excitement + amount + self.excitement_gain_modifier, self.max_excitement)
def bore(self, days: float):
self.excitement = max(0.0, self.excitement - days * self.excitement_drain_modifier)
def evolve(self, days: float):
self.evolution += days
def get_evolution_stage(self):
stage = 0
for threshold in self.evolution_thresholds:
if self.evolution >= threshold:
stage += 1
else:
break
return stage
@staticmethod
def load(name: str) -> 'Cat':
if not os.path.isdir(os.path.join('.gittycat', 'cats')):
raise FileNotFoundError('.gittycat folder missing or corrupted!')
try:
with open(os.path.join('.gittycat', 'cats', f'{name}.json'), 'r') as infile:
data = json.load(infile)
cat = Cat(name)
cat.name = data['name']
cat.max_food = data['max_food']
cat.food_gain_modifier = data['food_gain_modifier']
cat.food_drain_modifier = data['food_drain_modifier']
cat.food = data['food']
cat.max_energy = data['max_energy']
cat.energy_gain_modifier = data['energy_gain_modifier']
cat.energy_drain_modifier = data['energy_drain_modifier']
cat.energy = data['energy']
cat.max_excitement = data['max_excitement']
cat.excitement_gain_modifier = data['excitement_gain_modifier']
cat.excitement_drain_modifier = data['excitement_drain_modifier']
cat.excitement = data['excitement']
cat.evolution_thresholds = data['evolution_thresholds']
cat.evolution = data['evolution']
cat.last_update = datetime.fromtimestamp(data['last_update'], timezone.utc)
return cat
except FileNotFoundError:
raise FileNotFoundError(f'No cat with name {name} found!')
@staticmethod
def create_with_personality(name: str, personality: str):
"""Intializes a new cat with the given name and personality"""
if not os.path.isdir(os.path.join('.gittycat', 'cats')):
raise FileNotFoundError('.gittycat folder missing or corrupted!')
dirname = os.path.dirname(__file__)
# personality is saved in a json file relative to the gittycat installation
with open(os.path.join(dirname, 'personalities', f'{personality}.json'), 'r') as infile:
data = json.load(infile)
cat = Cat(name)
cat.look = data['look']
cat.max_food = data['max_food']
cat.food_gain_modifier = data['food_gain_modifier']
cat.food_drain_modifier = data['food_drain_modifier']
cat.food = cat.max_food
cat.max_energy = data['max_energy']
cat.energy_gain_modifier = data['energy_gain_modifier']
cat.energy_drain_modifier = data['energy_drain_modifier']
cat.energy = cat.max_energy
cat.max_excitement = data['max_excitement']
cat.excitement_gain_modifier = data['excitement_gain_modifier']
cat.excitement_drain_modifier = data['excitement_drain_modifier']
cat.excitement = cat.max_excitement
cat.evolution_thresholds = data['evolution_thresholds']
cat.evolution = 0.0
return cat
def save(self):
"""Saves the cat into the corresponding json file"""
if not os.path.isdir(os.path.join('.gittycat', 'cats')):
raise FileNotFoundError('.gittycat folder missing or corrupted!')
with open(os.path.join('.gittycat', 'cats', f'{self.name}.json'), 'w') as outfile:
data = {
'name': self.name,
'look': self.look,
'max_food': self.max_food,
'food_gain_modifier': self.food_gain_modifier,
'food_drain_modifier': self.food_drain_modifier,
'food': self.food,
'max_energy': self.max_energy,
'energy_gain_modifier': self.energy_gain_modifier,
'energy_drain_modifier': self.energy_drain_modifier,
'energy': self.energy,
'max_excitement': self.max_excitement,
'excitement_gain_modifier': self.excitement_gain_modifier,
'excitement_drain_modifier': self.excitement_drain_modifier,
'excitement': self.excitement,
'evolution_thresholds': self.evolution_thresholds,
'evolution': self.evolution,
'last_update': self.last_update.timestamp()
}
json.dump(data, outfile, indent=2)
def ascii_plot(self, state):
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ascii", str(self.look), str(self.get_evolution_stage()), state + ".txt")
with open(path, 'r') as file:
content = file.read()
print(content)