2021-01-03 21:48:51 +00:00
|
|
|
import asyncio, time, datetime, games, json, threading, jinja2, leagues
|
2020-12-29 00:48:50 +00:00
|
|
|
from flask import Flask, url_for, Response, render_template, request, jsonify
|
|
|
|
from flask_socketio import SocketIO, emit
|
2021-01-06 01:38:02 +00:00
|
|
|
import database as db
|
2020-12-28 08:21:47 +00:00
|
|
|
|
|
|
|
app = Flask("the-prestige")
|
2020-12-29 00:48:50 +00:00
|
|
|
app.config['SECRET KEY'] = 'dev'
|
2021-01-01 20:28:12 +00:00
|
|
|
#app.config['SERVER_NAME'] = '0.0.0.0:5000'
|
2020-12-29 00:48:50 +00:00
|
|
|
socketio = SocketIO(app)
|
2020-12-28 08:21:47 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
2020-12-29 00:48:50 +00:00
|
|
|
def index():
|
2021-01-02 16:51:47 +00:00
|
|
|
if ('league' in request.args):
|
|
|
|
return render_template("index.html", league=request.args['league'])
|
2020-12-29 00:48:50 +00:00
|
|
|
return render_template("index.html")
|
2020-12-28 08:21:47 +00:00
|
|
|
|
2021-01-02 11:21:53 +00:00
|
|
|
@app.route('/game')
|
|
|
|
def game_page():
|
|
|
|
return render_template("game.html")
|
|
|
|
|
|
|
|
|
2021-01-01 20:28:12 +00:00
|
|
|
thread2 = threading.Thread(target=socketio.run,args=(app,'0.0.0.0'))
|
2020-12-28 08:21:47 +00:00
|
|
|
thread2.start()
|
|
|
|
|
|
|
|
master_games_dic = {} #key timestamp : (game game, {} state)
|
2021-01-01 10:04:10 +00:00
|
|
|
data_to_send = []
|
2020-12-29 00:48:50 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
@socketio.on("recieved")
|
2021-01-01 08:32:47 +00:00
|
|
|
def handle_new_conn(data):
|
2021-01-01 10:04:10 +00:00
|
|
|
socketio.emit("states_update", data_to_send, room=request.sid)
|
2020-12-28 08:21:47 +00:00
|
|
|
|
|
|
|
def update_loop():
|
|
|
|
while True:
|
2021-01-01 10:04:10 +00:00
|
|
|
game_states = {}
|
2020-12-28 08:21:47 +00:00
|
|
|
game_times = iter(master_games_dic.copy().keys())
|
|
|
|
for game_time in game_times:
|
2020-12-31 18:41:32 +00:00
|
|
|
this_game, state, discrim_string = master_games_dic[game_time]
|
2020-12-28 08:21:47 +00:00
|
|
|
test_string = this_game.gamestate_display_full()
|
2020-12-31 18:41:32 +00:00
|
|
|
state["leagueoruser"] = discrim_string
|
2020-12-28 08:21:47 +00:00
|
|
|
state["display_inning"] = this_game.inning #games need to be initialized with the following keys in state:
|
2020-12-31 22:06:56 +00:00
|
|
|
#is_league, bool
|
2020-12-28 08:21:47 +00:00
|
|
|
state["outs"] = this_game.outs #away_name
|
|
|
|
state["pitcher"] = this_game.get_pitcher().name #home_name
|
|
|
|
state["batter"] = this_game.get_batter().name #max_innings
|
|
|
|
state["away_score"] = this_game.teams["away"].score #top_of_inning = True
|
|
|
|
state["home_score"] = this_game.teams["home"].score #update_pause = 0
|
|
|
|
#victory_lap = False
|
|
|
|
if test_string == "Game not started.": #weather_emoji
|
|
|
|
state["update_emoji"] = "🍿" #weather_text
|
|
|
|
state["update_text"] = "Play blall!" #they also need a timestamp
|
2020-12-31 08:32:01 +00:00
|
|
|
state["start_delay"] -= 1
|
2021-01-06 01:38:02 +00:00
|
|
|
|
2020-12-30 07:48:39 +00:00
|
|
|
state["display_top_of_inning"] = state["top_of_inning"]
|
|
|
|
|
2020-12-31 08:32:01 +00:00
|
|
|
if state["start_delay"] <= 0:
|
2020-12-28 08:21:47 +00:00
|
|
|
if this_game.top_of_inning != state["top_of_inning"]:
|
|
|
|
state["update_pause"] = 2
|
|
|
|
state["pitcher"] = "-"
|
|
|
|
state["batter"] = "-"
|
2020-12-29 00:48:50 +00:00
|
|
|
if not state["top_of_inning"]:
|
2020-12-28 08:21:47 +00:00
|
|
|
state["display_inning"] -= 1
|
2020-12-30 07:48:39 +00:00
|
|
|
state["display_top_of_inning"] = False
|
2020-12-28 08:21:47 +00:00
|
|
|
|
|
|
|
if state["update_pause"] == 1:
|
|
|
|
state["update_emoji"] = "🍿"
|
2020-12-31 02:32:07 +00:00
|
|
|
if this_game.over:
|
2021-01-06 05:02:46 +00:00
|
|
|
print(this_game)
|
|
|
|
#db.cache_history(this_game.teams['home'].name, this_game.teams["home"].score, this_game.teams['away'].name, this_game.teams['away'].score,
|
2021-01-06 04:52:23 +00:00
|
|
|
this_game.weather_text)
|
2020-12-31 08:32:01 +00:00
|
|
|
state["display_inning"] -= 1
|
|
|
|
state["display_top_of_inning"] = False
|
2020-12-31 02:32:07 +00:00
|
|
|
winning_team = this_game.teams['home'].name if this_game.teams['home'].score > this_game.teams['away'].score else this_game.teams['away'].name
|
2020-12-31 08:32:01 +00:00
|
|
|
if this_game.victory_lap and winning_team == this_game.teams['home'].name:
|
|
|
|
state["update_text"] = f"{winning_team} wins with a victory lap!"
|
|
|
|
elif winning_team == this_game.teams['home'].name:
|
|
|
|
state["update_text"] = f"{winning_team} wins, shaming {this_game.teams['away'].name}!"
|
|
|
|
else:
|
|
|
|
state["update_text"] = f"{winning_team} wins!"
|
2020-12-31 02:32:07 +00:00
|
|
|
state["pitcher"] = "-"
|
|
|
|
state["batter"] = "-"
|
|
|
|
elif this_game.top_of_inning:
|
2020-12-28 08:21:47 +00:00
|
|
|
state["update_text"] = f"Top of {this_game.inning}. {this_game.teams['away'].name} batting!"
|
|
|
|
else:
|
|
|
|
if this_game.inning >= this_game.max_innings:
|
|
|
|
if this_game.teams["home"].score > this_game.teams["away"].score:
|
2020-12-31 08:32:01 +00:00
|
|
|
this_game.victory_lap = True
|
2020-12-28 08:21:47 +00:00
|
|
|
state["update_text"] = f"Bottom of {this_game.inning}. {this_game.teams['home'].name} batting!"
|
|
|
|
|
|
|
|
elif state["update_pause"] != 1 and test_string != "Game not started.":
|
|
|
|
if "steals" in this_game.last_update[0].keys():
|
|
|
|
updatestring = ""
|
|
|
|
for attempt in this_game.last_update[0]["steals"]:
|
|
|
|
updatestring += attempt + "\n"
|
|
|
|
|
2021-01-06 01:38:02 +00:00
|
|
|
state["update_emoji"] = "💎"
|
2020-12-28 08:21:47 +00:00
|
|
|
state["update_text"] = updatestring
|
|
|
|
|
2021-01-02 10:42:14 +00:00
|
|
|
elif "mulligan" in this_game.last_update[0].keys():
|
|
|
|
updatestring = ""
|
|
|
|
punc = ""
|
|
|
|
if this_game.last_update[0]["defender"] != "":
|
|
|
|
punc = ", "
|
|
|
|
|
|
|
|
state["update_emoji"] = "🏌️♀️"
|
|
|
|
state["update_text"] = f"{this_game.last_update[0]['batter']} would have gone out, but they took a mulligan!"
|
|
|
|
|
|
|
|
elif "snow_atbat" in this_game.last_update[0].keys():
|
|
|
|
state["update_emoji"] = "❄"
|
|
|
|
state["update_text"] = this_game.last_update[0]["text"]
|
|
|
|
|
2020-12-28 08:21:47 +00:00
|
|
|
else:
|
|
|
|
updatestring = ""
|
|
|
|
punc = ""
|
|
|
|
if this_game.last_update[0]["defender"] != "":
|
|
|
|
punc = ". "
|
|
|
|
|
|
|
|
if "fc_out" in this_game.last_update[0].keys():
|
|
|
|
name, base_string = this_game.last_update[0]['fc_out']
|
|
|
|
updatestring = f"{this_game.last_update[0]['batter']} {this_game.last_update[0]['text'].value.format(name, base_string)} {this_game.last_update[0]['defender']}{punc}"
|
|
|
|
else:
|
|
|
|
updatestring = f"{this_game.last_update[0]['batter']} {this_game.last_update[0]['text'].value} {this_game.last_update[0]['defender']}{punc}"
|
|
|
|
if this_game.last_update[1] > 0:
|
|
|
|
updatestring += f"{this_game.last_update[1]} runs scored!"
|
|
|
|
|
2020-12-30 06:31:19 +00:00
|
|
|
state["update_emoji"] = "🏏"
|
2020-12-28 08:21:47 +00:00
|
|
|
state["update_text"] = updatestring
|
|
|
|
|
|
|
|
state["bases"] = this_game.named_bases()
|
|
|
|
|
2021-01-06 01:38:02 +00:00
|
|
|
state["top_of_inning"] = this_game.top_of_inning
|
2020-12-28 08:21:47 +00:00
|
|
|
|
2021-01-01 08:32:47 +00:00
|
|
|
game_states[game_time] = state
|
2020-12-28 08:21:47 +00:00
|
|
|
|
2020-12-31 08:32:01 +00:00
|
|
|
if state["update_pause"] <= 1 and state["start_delay"] < 0:
|
2020-12-31 02:32:07 +00:00
|
|
|
if this_game.over:
|
2020-12-31 08:32:01 +00:00
|
|
|
state["update_pause"] = 2
|
|
|
|
if state["end_delay"] < 0:
|
|
|
|
master_games_dic.pop(game_time)
|
2021-01-01 17:08:11 +00:00
|
|
|
else:
|
|
|
|
state["end_delay"] -= 1
|
|
|
|
master_games_dic[game_time][1]["end_delay"] -= 1
|
2020-12-31 02:32:07 +00:00
|
|
|
else:
|
|
|
|
this_game.gamestate_update_full()
|
2020-12-28 08:21:47 +00:00
|
|
|
|
|
|
|
state["update_pause"] -= 1
|
2020-12-31 02:32:07 +00:00
|
|
|
|
2021-01-01 10:04:10 +00:00
|
|
|
global data_to_send
|
|
|
|
data_to_send = []
|
2021-01-02 11:21:53 +00:00
|
|
|
template = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')).get_template('game_box.html')
|
2021-01-06 01:38:02 +00:00
|
|
|
|
2021-01-01 10:04:10 +00:00
|
|
|
for timestamp in game_states:
|
|
|
|
data_to_send.append({
|
|
|
|
'timestamp' : timestamp,
|
|
|
|
'league' : game_states[timestamp]['leagueoruser'] if game_states[timestamp]['is_league'] else '',
|
2021-01-01 21:30:13 +00:00
|
|
|
'state' : game_states[timestamp],
|
2021-01-02 11:21:53 +00:00
|
|
|
'html' : template.render(state=game_states[timestamp], timestamp=timestamp)
|
2021-01-01 10:04:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
socketio.emit("states_update", data_to_send)
|
2021-01-04 04:18:45 +00:00
|
|
|
time.sleep(8)
|