-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.py
786 lines (614 loc) · 20.5 KB
/
game.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
import random
from copy import deepcopy
import ui
from entity import Entity
from resources import UsedResources
from exceptions import *
import logging
logger = logging.getLogger('soyu')
hdlr = logging.FileHandler('/tmp/soyu.log')
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
class Event(Entity):
productivity_modifier = 0
initial_cost = 0
formatted = "Event"
action_str = "Organize"
drains = {}
def __init__(self, project,*args, **kwargs):
super().__init__(*args, **kwargs)
project.productivity += self.productivity_modifier / 100
project.money -= self.initial_cost
class TeamEvent(Event):
productivity_modifier = 10
initial_cost = 1000
formatted = "Team Event (Productivity: +{}, Initial Cost: {})".format(productivity_modifier, initial_cost)
unlocked = False
class TempEntity(Entity):
lasts = -1
temporary_increases = {}
temporary_decreases = {}
permanent_increases = {}
permanent_decreases = {}
formatted = "TempEntity"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.lasts = self.__class__.lasts
for key, value in self.__class__.temporary_increases.items():
val = getattr(Game.project, key)
setattr(Game.project, key, val + value)
for key, value in self.__class__.temporary_decreases.items():
val = getattr(Game.project, key)
setattr(Game.project, key, val - value)
for key, value in self.__class__.permanent_increases.items():
val = getattr(Game.project, key)
setattr(Game.project, key, val + value)
for key, value in self.__class__.permanent_decreases.items():
val = getattr(Game, key)
setattr(Game.project, key, val - value)
def turn(self):
super().turn()
#TODO: move this block to entity logic
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
for key, value in self.decreases.items():
value *= Game.project.productivity
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key - value)
if self.lasts:
self.lasts -= 1
else:
self.reset_effects()
Game.objects.remove(self)
def reset_effects(self):
for key, value in self.__class__.temporary_increases.items():
val = getattr(Game.project, key)
setattr(Game.project, key, val - value)
for key, value in self.__class__.temporary_decreases.items():
val = getattr(Game.project, key)
setattr(Game.project, key, val - value)
class Burst(TempEntity):
lasts = 20
limit = 5
temporary_increases = {'productivity': 0.5}
increases = {
'bugs': 10,
'technical_debt': 10
}
unlocked = True
formatted = "Temporary Burst"
action_str = "Start"
detail_fields = [('lasts', "Increases productivity by %50 for {} turns")]
class MarketingCampaign(TempEntity):
unlocked = False
lasts = 20
increases = {'influence':5}
initial_cost = 1000
formatted = "Marketing Campaign"
action_str = "Start"
detail_fields = [('lasts', "Increases project influence by 5 for {} turns")]
class Resignment(object):
def __init__(self, person, cause):
logger.error("A")
self.person = person
self.cause = cause
self.message = "{} has resigned because of {}".format(person, cause)
def __repr__(self):
return self.message
class AlertEvent(object):
def __init__(self, message):
self.message = message
def __repr__(self):
return self.message
class Person(Entity):
limit = -1
cost = 0
formatted = "Person"
action_str = "Hire"
shares = 0
drains_from = None
def turn(self):
super().turn()
self.drains_from.trade(self, 'money', self.cost)
def resign(self, cause):
Game.objects.remove(self)
Game.project.turn_events.append(Resignment(self, cause))
def fire(self):
self.drains_from.trade(self, 'money', self.cost * 5)
Game.objects.remove(self)
del self
class ProjectEmployee(Person):
formatted = "Employee"
def __init__(self, project, *args, **kwargs):
super().__init__(*args, **kwargs)
self.drains_from = project
class Developer(ProjectEmployee):
limit = -1
formatted = "Developer"
cost = 0
action_str = "Hire"
inventory = {'money': 0}
productivity_modifier = 0
increases = {}
decreases = {}
resign_prob = 0
def turn(self):
super().turn()
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
for key, value in self.decreases.items():
value *= Game.project.productivity
project_key = getattr(Game.project, key)
#if there are no waiting features, fix bugs.
if key == 'features' and project_key <= 0:
project_key = getattr(Game.project, 'bugs')
setattr(Game.project, 'bugs', project_key - value/2)
else:
if getattr(Game.project, key) > 0:
setattr(Game.project, key, project_key - value)
threshold = self.resign_prob*Game.project.technical_debt/100*Game.project.bugs/1000
if random.random() < threshold:
self.resign("bugs and technical debt")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Game.project.productivity *= (1 + self.productivity_modifier/100)
self.increases['server_maintenance'] = self.decreases['features'] / 30
class CoffeeMachine(Entity):
limit = -1
formatted = "Coffee Machine ☕"
action_str = "Buy"
initial_cost = 0
productivity_modifier = 0
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Game.project.money -= self.initial_cost
Game.project.productivity += self.productivity_modifier / 100
class ShittyCoffeeMachine(CoffeeMachine):
limit = 1
cost = 2
initial_cost = 10
productivity_modifier = 2
formatted = u"Shitty Coffee Machine ☕(Productivity: +{}, Initial Cost: {})".format(productivity_modifier, initial_cost)
class GoodCoffeeMachine(CoffeeMachine):
limit = 1
formatted = "Good Coffee Machine ☕"
cost = 4
initial_cost = 25
productivity_modifier = 5
class ArtisanCoffeeMachine(CoffeeMachine):
limit = 1
formatted = "Artisan Coffee Machine ☕"
cost = 10
initial_cost = 100
productivity_modifier = 10
class Designer(ProjectEmployee):
limit = -1
formatted = "Designer"
cost = 0
productivity_modifier = 0.1
action_str = "Hire"
inventory = {'money': 0}
increases = {}
decreases = {}
def turn(self):
super().turn()
for key, value in self.decreases.items():
value *= Game.project.productivity
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key - value)
class StudentDesigner(Designer):
formatted = "Student Designer"
cost = 0
decreases = {'design_need': 4}
class ShittyDesigner(Designer):
formatted = "Shitty Designer 💩"
cost = 5
decreases = {'design_need': 6}
unlocks_entities = [ShittyCoffeeMachine]
class MediocreDesigner(Designer):
formatted = "Mediocre Designer"
cost = 10
decreases = {'design_need': 8}
unlocks_entities = [GoodCoffeeMachine]
class SeniorDesigner(Designer):
formatted = "Senior Designer"
cost = 20
decreases = {'design_need': 12}
unlocks_entities = [ArtisanCoffeeMachine]
class ProjectManager(ProjectEmployee):
limit = 1
formatted = "Project Manager"
cost = 10
increases = {
'design_need': 5
}
decreases = {
"features": 15
}
unlocks_entities = [Designer, ShittyDesigner, MediocreDesigner, SeniorDesigner, TeamEvent]
def turn(self):
super().turn()
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
for key, value in self.decreases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key - value)
class StudentDeveloper(Developer):
limit = -1
formatted = "Student Developer 👦"
cost = 0
productivity_modifier = -20
increases = {
"bugs": 2,
"technical_debt": 3,
'security_issues': 3,
}
decreases = {
"features": 1,
"documentation": -1,
}
class ShittyDeveloper(Developer):
limit = -1
formatted = u"Shitty Developer 💩"
cost = 5
productivity_modifier = -15
increases = {
"bugs": 1,
"technical_debt": 4,
"security_issues": 3,
}
decreases = {"features": 2}
class MediocreDeveloper(Developer):
limit = -1
formatted = "Mediocre Developer 👱"
cost = 10
productivity_modifier = -10
increases = {
"bugs": 1,
"technical_debt": 3,
"documentation": 1,
"security_issues": 1,
}
decreases = {
"features": 3,
}
class SeniorDeveloper(Developer):
limit = -1
formatted = "Senior Developer 👴"
cost = 20
productivity_modifier = -5
increases = {
"bugs": 1,
"documentation": 6,
"security_issues": 1,
}
decreases = {
"features": 6,
"technical_debt": 1
}
resign_prob = 0.01
class GeniusDeveloper(Developer):
limit = 1
formatted = "Genius Developer 🕵"
cost = 100
productivity_modifier = 0
increases = {
"bugs": 1,
"documentation": 10
}
decreases = {
"features": 10,
"technical_debt": 5,
}
resign_prob = 0.1
class SecurityEngineer(Developer):
limit = -1
formatted = "Security Engineer"
cost = 20
productivity_modifier = -10
increases = {
"server_maintenance": 20
}
decreases = {
"security_issues": 3,
"technical_debt": 1,
"features": 1,
}
resign_prob = 0.01
class DevopsEngineer(Developer):
limit = -1
formatted = "Devops Engineer"
productivity_modifier = 5
increases = {
"documentation": 5,
}
decreases = {
"server_maintenance": 100,
"technical_debt": 2,
"features": 2,
}
cost = 20
resign_prob = 0.01
class SecurityBreach(object):
unlocked = False
def __init__(self):
if self.unlocked:
Game.project.turn_events.append(AlertEvent("There has been a security breach and some customers are affected."))
customers = Game.get_customers()
for x in range(int(Game.project.security_issues/100)):
c = random.choice(customers)
c.unsubscribe(reason="Security Breach")
class SetPrice(Entity):
unlocked = True
limit = -1
increases={}
decreases={}
action_str = "Set"
formatted = "Price"
artikel = ""
def __init__(self, *args, **kwargs):
super(SetPrice, self).__init__(*args, **kwargs)
price = int(ui.dialog("Set monthly price to:"))
Game.project.price = price
class Project(Entity):
name = "Project 1"
limit = 1
unlocked = True
bugs = 0
features = 1000
initial_features = features
technical_debt = 0
documentation = 0
server_maintenance = 0
security_issues = 0
productivity = 1
design_need = 0
initial_design_need = design_need
influence = 0
action_str = "Start"
formatted = "Project"
price = 0
unlocks_entities = [StudentDeveloper, ShittyDeveloper, MediocreDeveloper, SeniorDeveloper, GeniusDeveloper, SetPrice]
increases = {
'features': 5,
'design_need': 5
}
turn_events = []
@property
def score(self):
score = 1000 * ((self.features * -1 / 2) - self.bugs - self.technical_debt + self.documentation * 3 - self.server_maintenance - self.design_need + self.money) * self.productivity
return score
@property
def number_of_customers(self):
return len(Game.get_customers())
@property
def each_turn_payment(self):
payment = 0
customers = Game.get_customers()
for customer in customers:
payment += customer.price_multiplier * Game.project.price
return payment
def turn(self):
super().turn()
for e in Game.entities:
if e.unlocked:
e.unlocked_age += 1
Game.project.money -= Game.project.server_maintenance
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
Game.project.money += Game.project.each_turn_payment
Game.project.productivity *= (1 - (Game.project.technical_debt / 20000))
if Game.project.features/Game.project.initial_features <= 0.9:
ProjectManager.unlock()
if Game.project.features/Game.project.initial_features <= 0.8:
COO.unlock()
if Game.project.features <= 500:
BetaRelease.unlock()
#Security Breach
if random.random() < Game.project.security_issues / 10000:
SecurityBreach()
r = random.random()
k = Game.project.influence / (5*(Game.project.price+1))
logger.error("adsa" + str(r) + " " + str(k))
if r < k:
logger.error("customer1")
Game.objects.append(Customer())
if Game.project.money <= Game.project.cash_flow * -3 :
e = AlertEvent("Your project is running out of money. Try to get investors.")
Game.project.turn_events.append(e)
if Game.objects[0].money <= 50:
Game.project.turn_events.append(AlertEvent("You are starving!"))
if Game.project.money <= 0:
raise NotEnoughFundsException
if Game.project.features <= 0 and Game.project.bugs <= 0 and Game.project.cash_flow > 0 and random.random() < 0.01:
raise WinException
# self.turn_events = []
def __repr__(self):
return self.name
@property
def cash_flow(self):
expense = sum([o.cost for o in Game.objects])
income = sum([o.increases['money'] for o in Game.objects if 'money' in o.increases])
return income - expense - Game.project.server_maintenance + Game.project.each_turn_payment
class Investor(Entity):
shares = 0
limit = 1
money = 0
formatted = "Investor"
action_str = "Get investment from"
detail_fields = [('shares', "Will get {} shares"), ('money', 'Will bring ${}')]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Game.project.money += self.money
self.money = 0
Game.objects[0].shares -= self.shares
def turn(self):
super().turn()
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
class SmallInvestor(Investor):
limit = 5
shares = 2
money = 1000
formatted = "Small Investor"
increases = {'features': 2,
'design_need': 3}
class BigInvestor(Investor):
limit = 2
shares = 40
money = 20000
formatted = "Big Investor"
increases = {'features': 30,
'design_need': 40}
class COO(ProjectEmployee):
limit = 1
cost = 25
unlocks_entities = [SmallInvestor, BigInvestor]
formatted = "COO"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
#hack to pay ceo
Game.objects[0].cost = 26
Game.project.turn_events.append(AlertEvent("Your COO suggested to pay the CEO(you) $26 each month."))
class Customer(Entity):
limit = -1
price_multiplier = 1
increases = {
'features': 3,
'design_need': 3,
'influence': 1,
}
formatted = "Customer"
action_str = "Get"
detail_fields = [('tolerance', 'Tolerance: %{}')]
tolerance = "%20-%80"
def __init__(self, *args, **kwargs):
super(Customer, self).__init__(*args, **kwargs)
logger.error("customer created")
self.tolerance = random.randrange(20, 80)
SecurityBreach.unlocked = True
def turn(self):
super().turn()
for key, value in self.increases.items():
project_key = getattr(Game.project, key)
setattr(Game.project, key, project_key + value)
if Game.project.features / Game.project.initial_features > self.tolerance/100:
self.unsubscribe(reason="Underdelivery of promises")
if Game.project.bugs > 1000 * self.tolerance/100:
self.unsubscribe(reason="High amount of bugs")
if Game.project.design_need > 1000 * self.tolerance/100:
self.unsubscribe(reason="Bad UX")
def unsubscribe(self, reason):
try:
Game.objects.remove(self)
except Exception:
pass
event = AlertEvent("Your Customer decided to stop using your services.\nReason: {}".format(reason))
Game.project.turn_events.append(event)
Game.project.influence -= 6
class BetaCustomer(Customer):
limit = 5
price_multiplier = 0.8
increases = {
'features': 1,
'design_need': 1,
'influence': 5,
}
formatted = "Beta Customer"
tolerance = 80
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tolerance = 80
class Release(Entity):
limit = -1
unlocks_entities = [Customer]
formatted = "Release"
action_str = "Release"
class PublicRelease(Release):
limit = 1
increases = {
'design_need': 50,
'server_maintenance': 100,
}
unlocks_entities = [MarketingCampaign]
formatted = "Golden Version"
action_str = "Release"
locks_entities = [BetaCustomer]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Game.project.influence += 50
class BetaRelease(Release):
limit = 1
increases = {
'design_need': 50,
'server_maintenance': 100,
}
unlocks_entities = [PublicRelease, BetaCustomer, SecurityEngineer, DevopsEngineer]
formatted = "Beta Version"
action_str = "Release"
class Boss(Person):
limit = 1
formatted = "Boss"
cost = 0
unlocked = True
unlocks_entities = [Project]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.drains_from = self
def turn(self):
super().turn()
if self.money <= 0:
raise NotEnoughFundsException
@property
def cash_flow(self):
return (self.draining['money'] - self.cost) * -1
def fire(self):
pass
class Game(object):
objects = []
used_resources = None
project = None
entities = [Boss, StudentDeveloper, ShittyDeveloper, MediocreDeveloper, SeniorDeveloper, GeniusDeveloper,
SecurityEngineer, DevopsEngineer,
MediocreDesigner, StudentDesigner, ShittyDesigner, SeniorDesigner, ProjectManager,
ShittyCoffeeMachine, GoodCoffeeMachine, ArtisanCoffeeMachine, TeamEvent,
COO, SmallInvestor, BigInvestor,
Burst,
BetaCustomer, Customer, PublicRelease, BetaRelease,
SetPrice, MarketingCampaign
]
last_state = None
@classmethod
def init_game(cls):
cls.used_resources = UsedResources()
initial_player_inventory = {
'money': 10000,
}
initial_player_drain = {
'money': 25
}
initial_player_replenish = {}
player = Boss(
inventory=initial_player_inventory,
draining=initial_player_drain,
replenishing=initial_player_replenish,
)
Boss.shares = 100
project_name, budget, idea = ui.initproject(player.inventory['money'])
cls.project = Project()
cls.project.name = project_name
cls.project.pitch = idea.pitch
cls.project.features = idea.features
cls.project.design_need = idea.design_need
player.trade(cls.project, 'money', budget)
cls.objects.append(player)
cls.objects.append(cls.project)
player.project = cls.project
@classmethod
def get_customers(cls):
return [o for o in cls.objects if isinstance(o, Customer)]