diff --git a/main_controller.py b/main_controller.py index 19eb1ba..28959d5 100644 --- a/main_controller.py +++ b/main_controller.py @@ -14,32 +14,15 @@ thread2 = threading.Thread(target=socketio.run,args=(app,)) thread2.start() master_games_dic = {} #key timestamp : (game game, {} state) -game_states = {} - -def data_to_send(): - template = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')).get_template('game.html') - data = [] - for timestamp in game_states: - data.append({ - 'timestamp' : timestamp, - 'league' : game_states[timestamp]['leagueoruser'] if game_states[timestamp]['is_league'] else '', - 'html' : template.render( - state=game_states[timestamp], - base_filled="/static/img/base_filled.png", - base_empty="/static/img/base_empty.png", - out_out="/static/img/out_out.png", - out_in="/static/img/out_in.png" - ) - }) - return data +data_to_send = [] @socketio.on("recieved") def handle_new_conn(data): - socketio.emit("states_update", data_to_send(), room=request.sid) + socketio.emit("states_update", data_to_send, room=request.sid) def update_loop(): while True: - global game_states + game_states = {} game_times = iter(master_games_dic.copy().keys()) for game_time in game_times: this_game, state, discrim_string = master_games_dic[game_time] @@ -134,5 +117,21 @@ def update_loop(): state["update_pause"] -= 1 - socketio.emit("states_update", data_to_send()) + global data_to_send + template = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')).get_template('game.html') + data_to_send = [] + for timestamp in game_states: + data_to_send.append({ + 'timestamp' : timestamp, + 'league' : game_states[timestamp]['leagueoruser'] if game_states[timestamp]['is_league'] else '', + 'html' : template.render( + state=game_states[timestamp], + base_filled="/static/img/base_filled.png", + base_empty="/static/img/base_empty.png", + out_out="/static/img/out_out.png", + out_in="/static/img/out_in.png" + ) + }) + + socketio.emit("states_update", data_to_send) time.sleep(6) diff --git a/static/loader.js b/static/loader.js index a477198..fa83ceb 100644 --- a/static/loader.js +++ b/static/loader.js @@ -14,9 +14,9 @@ $(document).ready(function (){ //get all leagues leagues = [] - for (var i in json) { - if (json[i].league != "" && !leagues.includes(json[i].league)) { - leagues.push(json[i].league) + for (var game of json) { + if (game.league != "" && !leagues.includes(game.league)) { + leagues.push(game.league) } } @@ -32,15 +32,15 @@ $(document).ready(function (){ }) // add leagues not already present - for (var i in leagues) { // we removed the entries that are already there in the loop above - $('#filters').append(""); + for (var league of leagues) { // we removed the entries that are already there in the loop above + $('#filters').append(""); } //add click handlers to each filter $('#filters .filter').each(function(index) { $(this).click(function() { if ($('#filters #selected_filter').text() == 'All') { - updateGames(Object(), ""); // clear grid when switching off of All, to make games collapse to top + updateGames([], ""); // clear grid when switching off of All, to make games collapse to top } $('#filters #selected_filter').attr('id', ''); $(this).attr('id', 'selected_filter'); @@ -50,10 +50,11 @@ $(document).ready(function (){ }); const updateGames = (json, filter) => { + filterjson = []; - for (var i in json) { - if (json[i].league == filter || filter == "All") { - filterjson.push(json[i]); + for (var game of json) { + if (game.league == filter || filter == "All") { + filterjson.push(game); } } @@ -72,9 +73,16 @@ $(document).ready(function (){ } } - for (var i in filterjson) { + for (var game of filterjson) { + //updates game in list + for (var slotnum = 0; slotnum < grid.children.length; slotnum++) { + if (grid.children[slotnum].timestamp == game.timestamp) { + insertGame(slotnum, game); + }; + }; + //adds game to list if not there already - if (!Array.prototype.slice.call(grid.children).some((x) => x.timestamp == filterjson[i].timestamp)) { + if (!Array.prototype.slice.call(grid.children).some((x) => x.timestamp == game.timestamp)) { for (var slotnum = 0; true; slotnum++) { //this is really a while loop but shh don't tell anyone if (slotnum >= grid.children.length) { for (var i = 0; i < 3; i ++) { @@ -82,18 +90,11 @@ $(document).ready(function (){ } } if (grid.children[slotnum].className == "emptyslot") { - insertGame(slotnum, filterjson[i]); + insertGame(slotnum, game); break; }; }; } - - //updates game in list - for (var slotnum = 0; slotnum < grid.children.length; slotnum++) { - if (grid.children[slotnum].timestamp == filterjson[i].timestamp) { - insertGame(slotnum, filterjson[i]); - }; - }; }; //remove last rows if not needed