forked from reidcooper/PythonBaseballSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
173 lines (131 loc) · 5.84 KB
/
Copy pathapp.py
File metadata and controls
173 lines (131 loc) · 5.84 KB
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
#!/usr/bin/python
from bottle import *
import json
import re
import sys
import os
import time
import datetime
from baseballGamePackage.Game import Game
''' Global Settings '''
historical_games_location = "simulations/"
debug = False # Change for production to false
port = 9999 # Typically 80 for websites
@route('/')
@route('/index.html')
def homepage():
'''Routes for index page'''
return template('index')
@route('/about')
def about():
'''Routes for about page'''
return template('about')
@route('/historicGame')
def historicGame():
'''When the historical page loads, the method obtainDirectoryListing() is called to create a JSON file listing of all the files in the simulations directory'''
obtainDirectoryListing()
return template('historicGame')
@route('/submitHistoricGame', method='POST')
def submitHistoricGame():
'''When user selects a previously simulated game and selects Play Ball! the webpage will call this route and then return the displayGame page'''
# Play Ball with a selected JSON game file
if request.forms.get('play_ball_btn'):
# Temp Until We Read File Now
home_team = "HomeTeam"
away_team = "AwayTeam"
json_game = request.forms.get('gameFile')
print "Game Selected: " + json_game
home_team = re.search('([^_]*)_([^_]*)_([^_]*)', json_game).group(2)
away_team = re.search('([^_]*)_([^_]*)_([^_]*)', json_game).group(3)
return template('displayGame', home_team=home_team, away_team=away_team, game_file=json_game)
# If the User wants to upload a JSON game file
elif request.forms.get('upload_btn'):
if request.files.get('upload'):
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if re.match('.json', ext):
var = os.path.abspath(os.path.dirname(__file__))+"/static/"+historical_games_location
file_path = "{path}/{file}".format(path=var, file=upload.filename)
upload.save(file_path, overwrite=True)
home_team = "HomeTeam"
away_team = "AwayTeam"
home_team = re.search('([^_]*)_([^_]*)_([^_]*)', upload.filename).group(2)
away_team = re.search('([^_]*)_([^_]*)_([^_]*)', upload.filename).group(3)
return template('displayGame', home_team=home_team, away_team=away_team, game_file=upload.filename)
else:
print "Error - Not a JSON file"
return template('historicGame')
else:
print "Error - No file uploaded"
return template('historicGame')
else:
print "Did not select Play Ball or Upload"
return template('historicGame')
# Submit Teams to Form
@route('/submitTeams', method='POST')
def submitTeams():
'''Submits the two baseball teams ready for simulation, obtains those two teams, and runs the Python Simulator'''
# For using JSON and Javascript
# home_team = request.json['myDict']['selectHomeTeam']
# away_team = request.json['myDict']['selectAwayTeam']
home_team = request.forms.get('selectHomeTeam')
away_team = request.forms.get('selectAwayTeam')
print "Home Team: " + str(home_team) + " Away Team: " + str(away_team)
# Run Simulation ======
g = Game(str(home_team), str(away_team))
g.playGame()
game_file = g.getJSONData()
print game_file
# For A Dynamic Page
# <center><h3>Game 1: {{ home_team }} vs. {{away_team}}</h3></center>
return template('displayGame', home_team=home_team, away_team=away_team, game_file=game_file)
# return template('displayGame')
@route('/download/<filename>')
def static(filename):
'''Download Simulation Files'''
print "DOWNLOAD FROM "+historical_games_location
return static_file(filename, root='static/' + historical_games_location + '/', download=filename)
@route('/static/<directory>/<filename>')
def static(filename, directory):
'''Serve Directory Files from the Static Directory <> are wildcards'''
return static_file(filename, root='static/'+directory+'/')
''' ====== FILE TREE ======= '''
''' These next four methods serve the purpose of creating the jQuery File Tree shown on the historical game page'''
@route('/static/js/dist/<filename>')
def static(filename):
return static_file(filename, root='static/js/dist/')
@route('/static/js/dist/themes/default/style.min.css')
def static():
return static_file('style.min.css', root='static/js/dist/themes/default/')
@route('/static/js/dist/themes/default/<filename>')
def static(filename):
return static_file(filename, root='static/js/dist/themes/default/')
# Creates JSON file of all the historical games simulated
def obtainDirectoryListing():
'''Creates a JSON file listing all files within the scanned directory. Typically it will be scanning the /simulations directory '''
var = os.path.abspath(os.path.dirname(__file__))+"/static/"+historical_games_location
files = os.listdir(var)
record_array = []
count = 0
for file in files:
# Regex to detect .dotFiles, we do not want them
if (not re.match('\.\w*', file)) and (not re.match('file_tree.json', file)):
record = {'id': str(file), 'parent' : '#', 'text': str(file), "icon":"glyphicon glyphicon-leaf"}
record_array.append(record)
with open(var+'file_tree.json', 'w') as outfile:
json.dump(record_array, outfile)
print "CREATED JSON DIRECTORY LISTING"
''' ====== FILE TREE ======= '''
''' 404 returns index page'''
@error(404)
def error404(error):
return template('index')
''' 405 returns index page'''
@error(405)
def error405(error):
return template('index')
# Set debug to false for production
if __name__ == '__main__':
print "\nServinging Up PythonBaseballSimulator...\n"
port = int(os.environ.get('PORT', port))
run(host='0.0.0.0', port=port, debug=debug)