diff --git a/server.py b/server.py index abdf91d..310f334 100755 --- a/server.py +++ b/server.py @@ -397,17 +397,24 @@ def mapnames(): '''Index Location''' # TODO list by user - player = flask.request.headers.get("X-Forwarded-Preferred-Username") + player = flask.request.headers.get("X-Forwarded-Preferred-Username") or "anonymous" maps_query = db.session.query(Map).order_by(asc(Map.mapname)) # limit leaderboard to game # - game = flask.request.args.get("game") - if game == "tm2020": - maps_query = maps_query.filter(Map.game=="tm2020") - elif game=="tmnf": - maps_query = maps_query.filter(Map.game!="tm2020") - else: - pass + settings = db.session.query(UserSettings).filter(UserSettings.user==player).first() + if settings: + if not settings.show_tm_2020 and not settings.show_tmnf: + maps_query = maps_query.filter(Map.game=="tm2020") + latest_season = tm2020parser.get_latest_season_from_maps(maps_query.all()) + # handle no replays # + if latest_season: + maps_query = maps_query.filter(Map.map_uid.like("{}%".format(latest_season))) + elif settings.show_tm_2020 and not settings.show_tmnf: + maps_query = maps_query.filter(Map.game=="tm2020") + elif not settings.show_tm_2020 and settings.show_tmnf: + maps_query = maps_query.filter(Map.game=="tmnf") + else: + pass maps = maps_query.all() diff --git a/static/user_settings.js b/static/user_settings.js index 4525294..5a283d9 100644 --- a/static/user_settings.js +++ b/static/user_settings.js @@ -32,14 +32,16 @@ function submit(e){ } const s = e.target - console.log(s) const json_data = JSON.stringify({ payload : [ { key : s.id, value : s.checked } ] }) - console.log(json_data) fetch("/update-user-settings", { method: "POST", credentials: "include", headers: {'Content-Type': 'application/json'}, body: json_data } - ).then(response => {}) + ).then(response => { + if(s.id.startsWith("show")){ + window.location.reload() + } + }) } diff --git a/tm2020parser.py b/tm2020parser.py index 0715da1..2622917 100644 --- a/tm2020parser.py +++ b/tm2020parser.py @@ -5,6 +5,41 @@ import hashlib import pygbx import xmltodict +def get_latest_season_from_maps(maps): + '''Determine the latest season in DB''' + + order = ["Winter", "Spring", "Summer", "Fall"] + + max_year = 0 + max_season = "Winter" + + if not maps: + return + + for m in maps: + splitted = m.map_uid.split(" ") + + # not a campain map # + if len(splitted) < 3: + return + + season = splitted[0] + year = splitted[1] + + # also not a campaing map # + if not season in order: + return + try: + year = int(year) + except ValueError: + return + + if year > max_year or year == max_year and order.index(season) >= order.index(max_season): + max_year = year + max_season = season + + return "{} {}".format(max_season, max_year) + class GhostWrapper(): def __init__(self, fullpath, uploader):