-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
946 lines (847 loc) · 34.7 KB
/
app.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
from flask import Flask, render_template, request
import random
import teams
import misc_funcs
app = Flask(__name__)
import pandas as pd
#Use these for the box score
team1scores = []
team2scores = []
scoring_plays_description = []
general_descriptions = []
def groundout_or_flyout(pitcher):
go_range_max = pitcher.go_pct
random_float = random.random()
if random_float <= go_range_max:
result = "Groundout"
else:
result = "Flyout"
return result
def at_bat(hitter, pitcher):
hitter_or_pitcher = random.random()
random_float = random.random()
if (hitter_or_pitcher < 0.5):
k_range_max = hitter.k_pct
single_range_max = k_range_max + hitter.single_pct
double_range_max = single_range_max + hitter.double_pct
triple_range_max = double_range_max + hitter.triple_pct
hr_range_max = triple_range_max + hitter.hr_pct
else:
k_range_max = pitcher.k_pct
mod_single_pct = hitter.single_pct*pitcher.hit_pct
mod_double_pct = hitter.double_pct*pitcher.hit_pct
mod_triple_pct = hitter.triple_pct*pitcher.hit_pct
mod_homer_pct = hitter.hr_pct*pitcher.hit_pct
single_range_max = k_range_max + mod_single_pct
double_range_max = single_range_max + mod_double_pct
triple_range_max = double_range_max + mod_triple_pct
hr_range_max = triple_range_max + mod_homer_pct
if random_float <= k_range_max:
result = "Strikeout"
elif random_float <= single_range_max:
result = "Single"
elif random_float <= double_range_max:
result = "Double"
elif random_float <= triple_range_max:
result = "Triple"
elif random_float <= hr_range_max:
result = "Home Run"
else:
result = groundout_or_flyout(pitcher)
return result
#Helper Function for the Description Creation Functions
def desc_helper(result):
if result == "Single":
desc = " singles, scoring "
elif result == "Double":
desc = " doubles, scoring "
elif result == "Triple":
desc = "triples, scoring "
elif result == "Home Run":
desc = " homers, scoring "
elif result == "Flyout":
desc = " run scores on a sacrifice fly by "
else:
desc = " run scores on a RBI groundout by "
return desc
#Run-Scoring Description Creation Function
def create_description(inning, hitter_name, result, runs_scored):
desc = desc_helper(result)
#If not a flyout and not a groundout
if result != "Flyout" and result != "Groundout":
scoring_plays_description.append("Inning " +str(inning) +
": " + hitter_name + desc + str(runs_scored) + " run(s).")
#If a flyout or a groundout
else:
scoring_plays_description.append("Inning " +str(inning)
+ ": " + str(runs_scored) + desc + hitter_name + ".")
#General Description Function (For Play-by-Play)
def general_description(inning, outs, hitter_name,
pitcher_name, result, runs_scored):
if runs_scored > 0:
desc = desc_helper(result)
#If not a flyout or groundout
if result != "Flyout" or result != "Groundout":
general_descriptions.append("Inning " +
str(inning) + ": " + hitter_name
+ desc + str(runs_scored) +
" run(s).")
else:
general_descriptions.append("Inning " +str(inning)
+ ": " + str(runs_scored)
+ desc + hitter_name)
else:
#inning, outs, hitter_name, pitcher_name, result, runs_scored
if result == "Strikeout":
result = "strikes out"
elif result == "Single":
result = "singles"
elif result == "Double":
result = "doubles"
elif result == "Triple":
result = "triples"
elif result == "Home Run":
result = "homers"
elif result == "Groundout":
result = "grounds out"
elif result == "Flyout":
result = "flies out"
general_descriptions.append("Inning " +str(inning) + ": " +str(outs)
+ "out(s): "+ hitter_name + " " + result
+ " on a pitch by " + pitcher_name +". ")
#Hitter Class
class Hitter:
def __init__(self, k_pct, single_pct, double_pct, triple_pct, hr_pct, batted_out_pct, walk_pct, name):
self.k_pct = k_pct
self.single_pct = single_pct
self.double_pct = double_pct
self.triple_pct = triple_pct
self.hr_pct = hr_pct
self.batted_out_pct = batted_out_pct
self.walk_pct = walk_pct
self.name = name
#Pitcher Class
class Pitcher:
def __init__(self, k_pct, hit_pct, go_ao_pct, walk_pct, name):
self.k_pct = k_pct
self.hit_pct = hit_pct
self.go_ao_pct = go_ao_pct
self.walk_pct = walk_pct
self.name = name
self.go_pct = go_ao_pct/(1 + go_ao_pct)
self.ao_pct = 1/(1 + go_ao_pct)
#Team Class
class Team:
def __init__(self, starter,lineup, name, reliever1=None, reliever2=None, reliever3=None):
self.starter = starter
self.reliever1 = reliever1
self.reliever2 = reliever2
self.reliever3 = reliever3
self.lineup = lineup
self.name = name
#Game Class
class Game:
def __init__(self, team1, team2, inning=1, lineup_position=0, outs=0, runner1pos=0, runner2pos=0, runner3pos=0):
self.team1 = team1
self.team2 = team2
self.inning = inning
self.outs = outs
self.lineup_position = lineup_position
self.current_hitter = team1.lineup[lineup_position]
self.current_pitcher = team2.starter
self.runner1pos = runner1pos
self.runner2pos = runner2pos
self.runner3pos = runner3pos
self.teamAtBat = team1
self.halfInningScore = 0
self.team1score = 0
self.team2score = 0
def updateGameSituation(game, result):
if (result=="Strikeout"):
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.lineup_position+=1
elif (result=="Single"):
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=1
game.runner2pos=2
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=1
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=1
game.runner2pos=2
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=1
game.runner2pos=3
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 3):
#Runner on Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=1
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 2):
#Runner on Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=1
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 1):
#Runner on Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.runner1pos=1
game.runner2pos=3
game.runner3pos=0
#No runners on base
else:
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.runner1pos = 1
game.runner2pos = 0
game.runner3pos = 0
elif (result=="Double"):
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=2
game.runner2pos=3
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=2
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=2
game.runner2pos=3
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=2
game.runner2pos=3
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 3):
#Runner on Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=2
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 2):
#Runner on Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=2
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 1):
#Runner on First
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.runner1pos=2
game.runner2pos=3
game.runner3pos=0
#No runners on base
else:
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.runner1pos = 2
game.runner2pos = 0
game.runner3pos = 0
elif (result=="Triple"):
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 3)
game.halfInningScore+=3
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 3)
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=3
game.runner2pos=3
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner1pos == 3):
#Runner on Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 2):
#Runner on Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif (game.runner1pos == 1):
#Runner on First
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
#No runners on base
else:
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.runner1pos = 3
game.runner2pos = 0
game.runner3pos = 0
elif (result=="Home Run"):
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 4)
game.halfInningScore+=4
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 4)
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 3)
game.halfInningScore+=3
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 3)
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 3)
game.halfInningScore+=3
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 3)
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 3)
game.halfInningScore+=3
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 3)
elif (game.runner1pos == 3):
#Runner on Third
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner1pos == 2):
#Runner on Second
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
elif (game.runner1pos == 1):
#Runner on First
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 2)
game.halfInningScore+=2
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 2)
#No runners on base
else:
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
game.runner1pos = 0
game.runner2pos = 0
game.runner3pos = 0
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
elif result== "Groundout":
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
if (game.outs < 2):
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
general_descriptions.append("Double Play!")
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
#General Description
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
general_descriptions.append("Double Play!")
game.outs+=2
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
general_descriptions.append("Double Play!")
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
general_descriptions.append("Double Play!")
game.outs+=2
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
general_descriptions.append("Double Play!")
game.outs+=2
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
elif (game.runner1pos == 3):
#Runner on Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
elif (game.runner1pos == 2):
#Runner on Second
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=2
game.runner2pos=0
game.runner3pos=0
elif (game.runner1pos == 1):
#Runner on First
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
general_descriptions.append("Double play!")
game.outs+=2
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
#No runners on base
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos = 0
game.runner2pos = 0
game.runner3pos = 0
elif result == "Flyout":
game.lineup_position+=1
if (game.runner3pos != 0):
#The Bases are Loaded
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=1
game.runner2pos=2
game.runner3pos=0
elif (game.runner2pos == 3 and game.runner1pos == 2):
#Runner on Second and Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
elif (game.runner2pos == 3 and game.runner1pos == 1):
#Runner on First and Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=1
game.runner2pos=0
game.runner3pos=0
elif (game.runner2pos == 2 and game.runner1pos == 1):
#Runner on First and Second
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=1
game.runner2pos=2
game.runner3pos=0
elif (game.runner1pos == 3):
#Runner on Third
if (game.outs < 2):
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 1)
game.halfInningScore+=1
#Scoring Play! Add Description
create_description(game.inning, game.current_hitter.name, result, 1)
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=0
game.runner2pos=0
game.runner3pos=0
elif (game.runner1pos == 2):
#Runner on Second
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=3
game.runner2pos=0
game.runner3pos=0
elif (game.runner1pos == 1):
#Runner on First
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos=1
game.runner2pos=0
game.runner3pos=0
#No runners on base
else:
general_description(game.inning, game.outs, game.current_hitter.name, game.current_pitcher.name, result, 0)
game.outs+=1
game.runner1pos = 0
game.runner2pos = 0
game.runner3pos = 0
else:
print("There is an error")
if (game.lineup_position == 9):
game.lineup_position = 0
game.current_hitter = game.teamAtBat.lineup[game.lineup_position]
def simulate_half_inning(game):
while game.outs < 3:
at_bat_result = at_bat(game.current_hitter, game.current_pitcher)
updateGameSituation(game, at_bat_result)
if game.teamAtBat == game.team2:
game.inning +=1
if game.teamAtBat == game.team1:
team1scores.append(game.halfInningScore)
game.team1score+=game.halfInningScore
if game.teamAtBat == game.team2:
team2scores.append(game.halfInningScore)
game.team2score+=game.halfInningScore
general_descriptions.append("\n")
if game.teamAtBat == game.team2:
general_descriptions.append("End of Inning " + str(game.inning-1) + ".<b>Score: </b>" + game.team1.name + " " + str(game.team1score) + " " + game.team2.name + " " + str(game.team2score) + ".")
general_descriptions.append("<br>")
game.outs = 0
if game.teamAtBat == game.team1:
game.current_pitcher = game.team1.starter
game.teamAtBat = game.team2
elif game.teamAtBat == game.team2:
game.current_pitcher = game.team2.starter
game.teamAtBat = game.team1
game.current_hitter = game.teamAtBat.lineup[game.lineup_position]
game.runner1pos = 0
game.runner2pos = 0
game.runner3pos = 0
game.halfInningScore = 0
def simulate_game(game):
is_game_over = False
while is_game_over == False:
simulate_half_inning(game)
if game.inning > 9 and game.teamAtBat == game.team1:
team1sum = 0
for num in team1scores:
team1sum+=num
team2sum = 0
for num in team2scores:
team2sum+=num
if (team1sum != team2sum):
is_game_over = True
#reset to default values for series and long sims
game.inning = 1
game.lineup_position = 0
game.outs = 0
game.runner1pos = 0
game.runner2pos = 0
game.runner3pos = 0
game.current_hitter = game.team1.lineup[game.lineup_position]
game.current_pitcher = game.team2.starter
game.teamAtBat = game.team1
game.halfInningScore = 0
game.team1score = 0
game.team2score = 0
def simulate_many_games(game, num_games):
team1wins = 0
team2wins = 0
for i in range(num_games):
simulate_game(game)
team1sum = 0
for num in team1scores:
team1sum+=num
team2sum = 0
for num in team2scores:
team2sum+=num
if team1sum > team2sum:
team1wins+=1
elif team1sum < team2sum:
team2wins+=1
for i in range(len(team1scores)):
team1scores.remove(team1scores[0])
team2scores.remove(team2scores[0])
team1pct = team1wins/num_games
team2pct = team2wins/num_games
return [team1pct, team2pct]
#Astros
verlander = Pitcher(0.278, hit_pct=0.186, walk_pct=0.041, go_ao_pct=(0.66), name="Justin Verlander")
#Phillies
wheeler = Pitcher(0.269, hit_pct=0.223, walk_pct = 0.050, go_ao_pct=1.11, name="Zach Wheeler")
burnes = Pitcher(0.378, hit_pct=0.186, walk_pct=0.041, go_ao_pct=(0.66), name="Corbin Burnes")
team_lineups = teams.get_lineups()
#define teams
astros = Team(starter=verlander, lineup=team_lineups["Astros"], name="Houston Astros")
phillies = Team(starter = wheeler, lineup = team_lineups["Phillies"], name = "Philadelphia Phillies")
orioles = Team(starter=burnes, lineup=team_lineups["Orioles"], name = "Baltimore Orioles")
teams = {"Astros": astros, "Phillies": phillies, "Orioles": orioles}
#Function for providing output to the user based on their choice of simulation mode
def printToUser(choice, home_team, away_team, num_games=0):
df_html = None
to_return = None
str_to_return = None
away_team = teams[away_team]
home_team = teams[home_team]
#Game Recap
if choice=="1":
to_return = ""
current_game = Game(away_team, home_team)
#define game
simulate_game(current_game)
team1sum = 0
for num in team1scores:
team1sum+=num
team2sum = 0
for num in team2scores:
team2sum+=num
box_score = {'1': [team1scores[0],team2scores[0]], '2': [team1scores[1],team2scores[1]], '3': [team1scores[2],team2scores[2]], '4': [team1scores[3],team2scores[3]], '5': [team1scores[4],team2scores[4]], '6': [team1scores[5],team2scores[5]], '7': [team1scores[6],team2scores[6]], '8': [team1scores[7],team2scores[7]], '9': [team1scores[8],team2scores[8]]}
#If there's extra innings, add them to the box score
if len(team1scores)>9:
extra_inning = 10
while extra_inning<=len(team1scores):
box_score[str(extra_inning)] = [team1scores[extra_inning-1], team2scores[extra_inning-1]]
extra_inning+=1
#Add the final score to the box score
box_score["Final"] = [team1sum, team2sum]
box_score_df = pd.DataFrame(data=box_score, index=[away_team.name, home_team.name])
df_html = box_score_df.to_html(classes='data', header="true")
#print(box_score_df)
to_return = "<b>Scoring Plays</b><br>"
for item in scoring_plays_description:
to_return += ("\n" + item)
scoring_plays_description.clear()
general_descriptions.clear()
team1scores.clear()
team2scores.clear()
return to_return, df_html
#Play by Play
if choice=="2":
current_game = Game(away_team, home_team)
#define game
simulate_game(current_game)
str_to_return = "<b>Full play by play</b><br>"
#print("Full play by play:")
for item in general_descriptions:
str_to_return += (item + "\n")
#print("\n")
team1sum = 0
for num in team1scores:
team1sum+=num
team2sum = 0
for num in team2scores:
team2sum+=num
box_score = {'1': [team1scores[0],team2scores[0]], '2': [team1scores[1],team2scores[1]], '3': [team1scores[2],team2scores[2]], '4': [team1scores[3],team2scores[3]], '5': [team1scores[4],team2scores[4]], '6': [team1scores[5],team2scores[5]], '7': [team1scores[6],team2scores[6]], '8': [team1scores[7],team2scores[7]], '9': [team1scores[8],team2scores[8]]}
#If there's extra innings, add them to the box score
if len(team1scores)>9:
extra_inning = 10
while extra_inning<=len(team1scores):
box_score[str(extra_inning)] = [team1scores[extra_inning-1], team2scores[extra_inning-1]]
extra_inning+=1
#Add the final score to the box score
box_score["Final"] = [team1sum, team2sum]
box_score_df = pd.DataFrame(data=box_score, index=[away_team.name, home_team.name])
#Convert df to html and display
df_html = box_score_df.to_html(classes='data', header="true")
scoring_plays_description.clear()
general_descriptions.clear()
team1scores.clear()
team2scores.clear()
return str_to_return, df_html
#Many Games
if choice=="3":
current_game = Game(away_team, home_team)
#num_games = int(input("How many games do you want to simulate? "))
win_pcts = simulate_many_games(current_game, num_games)
to_return = "<b>You simulated " + str(num_games) + " games.</b>"
to_return += "\n" + ("The " + current_game.team1.name +
" won " + str(round(win_pcts[0]*100, 2)) + " percent of the time.")
to_return += "\n" + ("The " + current_game.team2.name +
" won " + str(round(win_pcts[1]*100, 2)) + " percent of the time.")
scoring_plays_description.clear()
general_descriptions.clear()
team1scores.clear()
team2scores.clear()
return to_return, df_html
@app.route('/')
def index():
return render_template('index.html')
@app.route('/simulate', methods=['POST'])
def simulate():
mode = request.form.get('mode')
num_games = request.form.get('num_games')
home_team = request.form.get('home_team')
away_team = request.form.get('away_team')
# Convert num_games to integer if provided
num_games = int(num_games) if num_games.isdigit() else None
result = None
result, df_html = printToUser(mode, home_team, away_team, num_games)
if (mode != "3"):
result = result.replace('.', '.<br>')
else:
result = result.replace("s.", "s.<br>")
return render_template('index.html', result=result, table=df_html)
if __name__ == '__main__':
app.run(debug=True)