-
Notifications
You must be signed in to change notification settings - Fork 4
/
LemonadeMain.py
371 lines (300 loc) · 11.6 KB
/
LemonadeMain.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# Lemonade stand is Licensed under the Don't Be A Dick License
# (dbad-license). This license is an extension of the Apache License.
#
# If you do not wish to comply with the restrictions of the Don't Be A Dick
# License, this software is also available under the terms of the
# GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# The text of the Don't Be A Dick License is available at at
# <http://dbad-license.org/license>, while the GNU General Public License
# is available at <http://www.gnu.org/licenses/>.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Authors:
# Justin Lewis <jlew.blackout@gmail.com>
# Nathaniel Case <Qalthos@gmail.com>
from random import randint
from gettext import gettext as _
from operator import itemgetter
from constants import STARTING_MONEY, STARTING_PRICE, MAX_MSG, EVENTS,\
ITEMS, CURRENCY, RECIPES, DIFFICULTY, format_money
class LemonadeMain:
"""
The main class for the Lemonade Game.
This class holds all the logic of the game
"""
def __init__(self, difficulty_level=0):
self.splash = True
self.__day = 1
self.__difficulty = difficulty_level
self.__resources = {
'money': STARTING_MONEY,
'last_income': 0,
'last_profit': 0,
'price': STARTING_PRICE,
'recipe': RECIPES['basic']
}
# Populate resources with item keys
for item_key in list(ITEMS.keys()):
self.__resources[item_key] = []
self.__weather = 1
self.__msg_queue = []
# run weather
self.weather_change()
# run random
self.random_event()
@property
def money(self):
return self.__resources['money']
@property
def day(self):
return self.__day
@property
def weather(self):
return self.__weather
def get_resource(self, key):
return self.count_item(key)
@property
def resource_list(self):
resources = {}
for item_key in list(ITEMS.keys()):
resources[item_key] = self.count_item(item_key)
return resources
def recipe(self, ingredient):
return self.__resources['recipe'].get(ingredient, 0)
@property
def messages(self):
return self.__msg_queue
def add_msg(self, mesg):
self.__msg_queue.append(mesg)
if len(self.__msg_queue) > MAX_MSG:
self.__msg_queue.pop(0)
def clear_queue(self):
self.__msg_queue = []
def weather_change(self):
"""
Randomly change the weather, but not more than one unit away
"""
self.__weather += randint(-1, 1)
# It looks like its going to rain tomorrow
if self.__weather <= 0:
self.__weather = 0
# Tomorrow looks to be very hot
elif self.__weather >= 2:
self.__weather = 2
def random_event(self):
"""
Attempt to run random events
"""
event_num = randint(0, 10)
if event_num < len(EVENTS):
event = EVENTS[event_num]
itemcount = self.count_item(event['item'])
if event['change'] < 0:
remove = abs(event['change'])
if itemcount > remove:
self.remove_item(event['item'], remove)
else:
self.remove_item(event['item'], itemcount)
else:
self.add_item(event['item'], event['change'])
self.add_msg(event['text'])
def process_day_logic(self, items):
self.clear_queue()
self.__day += 1
start_money = self.__resources['money']
self.add_msg(_("Starting Money: %s" % format_money(start_money)))
# Process Item Payment
for item in items:
status = self.buy_item(item, items[item])
if status == -1:
self.add_msg(_("You can't afford any units of %s.") %
ITEMS[item]['name'])
else:
self.add_msg(_("Bought %d units of %s.") %
(items[item], ITEMS[item]['name']))
# Calculate how many can be bought
inventory_hold = []
for item_key in list(ITEMS.keys()):
if self.recipe(item_key) == 0:
continue
inventory_hold.append(
self.count_item(item_key) / self.recipe(item_key))
sales = min(inventory_hold)
# Calculate how many will be bought by weather
if sales != 0:
if self.__weather == 0:
sales = int(sales * .8)
elif self.__weather == -1:
sales = int(sales * .6)
# Remove items required per cup sold
for item_key in list(ITEMS.keys()):
self.remove_item(item_key, sales * self.recipe(item_key))
self.__resources['last_income'] = sales * self.__resources['price']
self.add_msg(_("Sold %d cups, at %s each") %
(sales, format_money(self.__resources['price'])))
# Show profit and expenses if the difficuly is less than impossible
if self.__difficulty < DIFFICULTY.index("Impossible"):
self.add_msg("You spent %s on supplies" %
format_money(self.__resources['money'] - start_money))
self.add_msg("and made %s in sales" %
format_money(self.__resources['last_income']))
profit_to_calculate = (self.__resources['money'] - start_money)\
+ self.__resources['last_income']
self.__resources['last_profit'] = profit_to_calculate
if profit_to_calculate > 0:
# Show the net porfit if difficulty is less than normal
if self.__difficulty < DIFFICULTY.index("Hard"):
self.add_msg("That comes to %s in profit" %
(format_money(self.__resources['last_profit'])))
return True
else:
self.__resources['money'] += self.__resources['last_income']
self.process_day_end()
return False
def process_change(self, mini_game_key):
"""
Processes the counting game effects
@param mini_game_key: A dictionary of keys and values of the
mini game
"""
if self.__resources['last_profit'] > 0:
mini_game_success = self.count_game(
mini_game_key, self.__resources['last_profit'])
if mini_game_success:
# Give them the money if they added
self.__resources['money'] += self.__resources['last_income']
else:
self.add_msg(
_("That is the incorrect amount of money. Try again."))
return False
return True
def process_day_end(self):
"""
Processes the end of the day events.
"""
# Decay items
self.decay_items()
# Weather
self.weather_change()
# Random event
self.random_event()
def buy_item(self, key, quanity):
"""
Attempts to buy as many (up to max quantity) items from
the inventory.
@param key: The key of the item being added
@param quanity: The number of units to buy (before bulk)
@return: Returns total bought, -1 if you can't
afford any
"""
the_item = ITEMS[key]
total = quanity * the_item['bulk']
cost = the_item['cost'] * total
if cost < self.__resources['money']:
self.__resources['money'] -= cost
self.add_item(key, total)
return total
else:
bulk_price = the_item['bulk'] * the_item['cost']
# Lets try to buy as many as we can
can_buy = self.__resources['money'] // bulk_price
if can_buy != 0:
total = can_buy * the_item['bulk']
self.__resources['money'] -= can_buy * bulk_price
self.add_item(key, total)
return total
else:
return -1
def add_item(self, key, quantity):
"""
Adds item to inventory with correct decay flag
@param key: The key of the item being added
@param quantity: The total quantity to add
"""
total = quantity
self.__resources[key].append([ITEMS[key]['decay'], total])
def remove_item(self, key, quantity):
"""
Removes item from inventory
@param key: The key of the items to remove
@param quantity: The amount to remove
@return: Returns true if successful, false if
they don't have enough to remove.
"""
to_remove = quantity
# Make copy of resource in case fail
resource = self.__resources[key][:]
while to_remove > 0:
try:
item = resource.pop(0)
except BaseException:
return False
if item[1] > to_remove:
item[1] -= to_remove
resource.insert(0, item)
break
else:
to_remove -= item[1]
self.__resources[key] = resource
return True
def decay_items(self):
"""
Decays items and removes expired items.
"""
# Loop through all items
for item_key in list(ITEMS.keys()):
new_list = []
# Loops through all items stored in item list
for item in self.__resources[item_key]:
# Decrement decay and add to new list
# ignore it if has expired
if item[0] != 1:
# If item is 0, then it doesn't decay
if item[0] == 0:
new_list.append([item[0], item[1]])
else:
new_list.append([item[0] - 1, item[1]])
elif item[1] != 0:
self.add_msg("%d %ss have gone bad" % (item[1], item_key))
# Place item back into resource list
self.__resources[item_key] = new_list
def count_item(self, key):
"""
Returns the items owned under the given key
@param key: The key of the item to lookup
@return: Returns the items in inventory
"""
count = 0
for item in self.__resources[key]:
count += item[1]
return count
def count_game(self, values, target):
"""
Verifies the values of the counting game.
@param values: A dictionary (keys must match CURRENCY) and
values are how many of each currency type
are required to make the optimum change
@return: Returns true if they pass the mini-game
"""
currency_values = sorted(list(CURRENCY.items()), key=itemgetter(1),
reverse=True)
# Set previous_value to target so it always accepts the first key
previous_value = target
for key, value in currency_values:
cal_val = (value * values[key])
if cal_val > previous_value:
return False
target -= cal_val
previous_value = value
if target == 0:
return True
else:
return False