import placelist,secrets
import colorsys,orgparse,os,re,time
from datetime import datetime,timedelta

thisyear = datetime.now().strftime("%Y")
    
year = 1993

gamestartyear = 2017

concernedfiles = []

while year < int(thisyear) + 1:
    month = 0
    while month < 13:
        if month < 10:
            strmonth = "0" + str(month)
        else:
            strmonth = str(month)
        recdir = str(year) + "/" + strmonth + "/"
        fullpath = secrets.orgpath + recdir
        if os.path.exists(fullpath):
            for file in sorted(os.listdir(fullpath)):
                filename = fullpath + str(file)
                if filename.endswith(".org"):
                    concernedfiles.append(filename)
        month = month + 1
    year = year + 1

places = []
placenames = []

games = []
gamenames = []
holding = []

for file in concernedfiles:
    filedate = file[-14:-4]
    dateobj = datetime.strptime(filedate,"%Y-%m-%d")
    parsefile = orgparse.load(file)
    try:
        for node in parsefile.children:
            if node.heading == "places":
                for action in node.children:
                    if action.heading == "visited":
                        for place in action.children:
                            placename = re.sub(" <.*>","",re.sub("<.*> ","",place.heading))
                            if placename not in placenames:
                                status = "new"
                                placenames.append(placename)
                            else:
                                status = "existing"
                            if "<" in place.heading:
                                dates = re.sub(" [A-Z][a-z][a-z]","",re.sub("--"," to ",re.sub(">","",re.sub("<","",(re.findall("\<.*\>",place.heading)[0])))))
                            else:
                                dates = filedate
                            if status == "new":
                                try:
                                    lookup = placename + ", " + placelist.disambig[placename]
                                except:
                                    lookup = placename
                                thedict = {"name":placename,"dates":[dates],"lookup":lookup}
                                places.append(thedict)
                            else:
                                twodict = {"name":placename,"dates":[dates]}
                                for origplace in places:
                                    if twodict["name"] == origplace["name"]:
                                        origplace["dates"].extend(twodict["dates"])
            if node.heading == "games":
                for action in node.children:
                    for game in action.children:
                        name = re.sub(" \(.*\)","",game.heading)
                        if game.heading not in gamenames:
                            status = "new"
                            gamenames.append(game.heading)
                        else:
                            status = "existing"
                        console = (re.findall("\(.*\)",game.heading)[0])[1:-1]
                        if status == "new":
                            thedict = {"id":game.heading,"name":name,"initialdate":dateobj,"console":console,game.parent.heading:dateobj,"recent":game.body,"lastupdate":dateobj}
                            if game.parent.heading != "acquired":
                                thedict.update({"firstplayed":dateobj})
                            games.append(thedict)
                        else:
                            twodict = {"id":game.heading,game.parent.heading:dateobj,"lastupdate":dateobj}
                            if len(game.body) > 1:
                                twodict.update({"recent":game.body})
                            for origgame in games:
                                if twodict["id"] == origgame["id"]:
                                    try:
                                        if origgame["firstplayed"]:
                                            playedbefore = True
                                    except:
                                        playedbefore = False
                            if playedbefore == False:
                                twodict.update({"firstplayed":dateobj})
                            holding.append(twodict)
                        for gameupdate in holding:
                            for origgame in games:
                                if gameupdate["id"] == origgame["id"]:
                                    gamemerge = {**origgame, **gameupdate}
                                    games.remove(origgame)
                                    games.insert(0,gamemerge)
                                    holding.remove(gameupdate)
    except:
        pass

for place in places:
    place["dates"] = list(dict.fromkeys(place["dates"]))

theplaces = sorted(places,key=lambda d: d["name"])

for place in theplaces:
    place["lat"] = False
    place["long"] = False
    for checkplace in placelist.places:
        if place["lookup"] == checkplace["lookup"]:
            place["lat"] = checkplace["lat"]
            place["long"] = checkplace["long"]
    if place["lat"] == False:
        print("Warning: " + place["name"] + " should be added to list")
    
alllats = []

alllongs = []

for place in theplaces:
    if place["lat"] != False:
        alllats.append(place["lat"])
    if place["long"] != False:
        alllongs.append(place["long"])
        
avglat = (max(alllats) + min(alllats))/2

avglong = (max(alllongs) + min(alllongs))/2

writefile = open("build/places/index.html","w")

writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <base target=\"_top\">\n    <meta charset=\"utf-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n    <title>Map</title>\n    <link rel=\"stylesheet\" href=\"/main.css\">\n    <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n    <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n    <style>\n     #map {\n       width: 90vw;\n       height: 90vh;\n       margin: auto;\n     }\n    </style>\n  </head>\n  <body>\n    <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n    <script>\n     const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n     const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n	 attribution: '&copy; <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n     }).addTo(map);\n")

for place in theplaces:
    if place["lat"] != False:
        saniname = re.sub(" ","",(re.sub("’","",(re.sub("-","",place["name"])))))
        writefile.write("     const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>")
        for date in place["dates"]:
            writefile.write("<li><code>" + date + "</code></li>")
        writefile.write("</ul>');\n")
        
writefile.write("    </script>\n   <ul id=\"foot\">\n	<li><a href=\"/\">home</a></li>\n      </ul>\n  <a href=\"/\"><img src=\"/a.png\" style=\"position:fixed;bottom:2px;right:2px;\" title=\"home\"></a>\n  </body>\n</html>")

games = sorted(games,key=lambda d: d["lastupdate"])

consoles = [{"code":"ps5","name":"PlayStation 5","shortname":"PS5","img":"PS5"},
            {"code":"ps2","name":"PlayStation 2","shortname":"PS2","img":"PS2"},
            {"code":"ps3","name":"PlayStation 3","shortname":"PS3","img":"PS3"},
            {"code":"xbox 360","name":"Xbox 360","shortname":"Xbox360","img":"360"},
            {"code":"nintendo ds","name":"Nintendo DS","shortname":"DS","img":"NDS"},
            {"code":"nintendo 3ds","name":"Nintendo 3DS","shortname":"3DS","img":"3DS"},
            {"code":"nintendo switch","name":"Nintendo Switch","shortname":"Switch","img":"Switch"},
            {"code":"pc","name":"PC","shortname":"PC","img":"PC"}]

endlessgames = ["the sims 4 (pc)",
                "american truck simulator (pc)",
                "civilization iv (pc)",
                "final fantasy theatrhythm curtain call (nintendo 3ds)",
                "final fantasy xiv (pc)",
                "medieval ii: total war (pc)",
                "tabletop simulator (pc)",
                "train simulator 2020 (pc)"]

consolelists = []
for console in consoles:
    consolegames = []
    for game in games:
        if game["console"] == console["code"]:
            consolegames.append(game)
    consolelists.append({"code":console["code"],"name":console["name"],"games":consolegames})

def completed(selection):
    completed = 0
    for game in selection:
        try:
            if game["completed"]:
                completed += 1
        except:
            pass
    return completed

def beaten(selection):
    beaten = 0
    for game in selection:
        try:
            if game["completed"]:
                pass
        except:
            try:
                if game["beaten"]:
                    beaten += 1
            except:
                pass
    return beaten

def endless(selection):
    endless = 0
    for game in selection:
        try:
            if game["completed"]:
                pass
        except:
            try:
                if game["beaten"]:
                    pass
            except:
                try:
                    if game["gameplay"]:
                        if game["id"] in endlessgames:
                            endless += 1
                except:
                    pass
    return(endless)

def unfinished(selection):
    unfinished = 0
    for game in selection:
        try:
            if game["completed"]:
                pass
        except:
            try:
                if game["beaten"]:
                    pass
            except:
                try:
                    if game["gameplay"]:
                        if game["id"] not in endlessgames:
                            unfinished += 1
                except:
                    pass
    return(unfinished)

def total(selection):
    return len(selection)

def unplayed(selection):
    unplayed = total(selection) - (completed(selection) + beaten(selection) + unfinished(selection) + endless(selection))
    return(unplayed)

gamehue = 250

gamedescription = "I nicked this design and method of game tracking from <a href=\"https://backloggery.com\" target=\"_blank\">Backloggery</a>. I don’t have full data on when I obtained/started playing some of my games, so some of these statistics are inaccurate.</p><p>I classify games by the console I play them on, not the one they were released for, so there are PS4 games under PS5, GBA games under DS, etc.</p><p>“Complete” means getting all the achievements if it’s a game/platform that has them; otherwise it means getting in-game 100% completion.</p><p>Achievements and playtime are automatically tracked <a href=\"https://www.exophase.com/user/nawwarnugans/\">on exophase</a>."

def headerwrite(output,section):
    header = open(output, "a")
    rgb = colorsys.hls_to_rgb((gamehue)/360,0.3,0.45)
    hexstring = ""
    for element in rgb:
        hexstring += str(hex(int(element * 255)))[2:]
    header.write("<!DOCTYPE html>\n<html lang=\"en\" style=\"--active-base: hsl(" + str(gamehue) + ", 45%, 30%); --active-secondary: hsl(" + str(gamehue) + ", 15%, 30%); --active-accent: hsl(" + str(gamehue) + ", 90%, 70%);\">\n  <head>\n    <meta charset=\"utf-8\">\n    <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n    <meta name=\"theme-color\" content=\"#" + hexstring + "\">\n    <title>Static backlog</title>\n    <link rel=\"alternate\" type=\"application/rss+xml\" title=\"Static backlog\" href=\"/trackers/games/" + "feed.xml\">\n    <link href=\"/trackers/games/backloggery.css\" rel=\"stylesheet\">\n  </head>\n  <body>\n    <div id=\"app\">\n      <div class=\"profile\">\n        <aside>\n          <div>\n            <div id=\"about_note\">\n              <h2>About</h2>\n              <div class=\"markdown\"><p>" + gamedescription + "</p></div>\n            </div>\n          </div>\n        </aside>\n        <main>\n          <section>\n            <div class=\"tabs\">\n              <a href=\"")
    if section == "backlog":
        header.write("\" class=\"router-link-exact-active router-link-active\" aria-current=\"page")
    else:
        header.write("/trackers/games/backlog")
    header.write("\">Backlog</a>\n              <a ")
    if section == "library":
        header.write("class=\"router-link-exact-active router-link-active\" aria-current=\"page\" href=\"")
    else:
        if section == "sublibrary":
            header.write("class=\"router-link-active\" aria-current=\"page\" ")
        header.write("href=\"/trackers/games/library")
    header.write("\">Library</a>\n              <a href=\"")
    if section == "history":
        header.write("\" class=\"router-link-exact-active router-link-active\" aria-current=\"page")
    else:
        header.write("/trackers/games/history")
    header.write("\">History</a>\n            </div>\n")
    header.close()

def footerwrite(output):
    footer = open(output, "a")
    footer.write("          </section>\n        </main>\n      </div>\n    </div>\n    <a href=\"/\"><img src=\"/a.png\" style=\"position:fixed;bottom:2px;right:2px;\" title=\"home\"></a>\n</body>\n</html>\n")
    footer.close()

def playcard(game,decay=False):
    cardstring = ""
    if decay == True:
        cardstring += "                  <details class=\"game-item decay\">\n"
    else:
        cardstring += "                  <details class=\"game-item\">\n"
    cardstring += "                    <summary>\n                      <div class=\"platform\">\n                        <div>\n"
    for console in consoles:
        if game["console"] == console["code"]:
            cardstring += "                          <img src=\"/trackers/games/" + console["img"] + ".png\" title=\"" + console["name"] + "\" alt=\"" + console["shortname"] + "\" class=\"bw\">\n"
    cardstring += "                        </div>\n                      </div>\n                      <div class=\"status\">\n"
    try:
        if game["completed"]:
            cardstring += "                        <img src=\"/trackers/games/C.png\" alt=\"C\" title=\"Completed\">\n"
    except:
        try:
            if game["beaten"]:
                cardstring += "                        <img src=\"/trackers/games/B.png\" alt=\"B\" title=\"Beaten\">\n"
        except:
            try:
                if game["gameplay"]:
                    if game["id"] in endlessgames:
                        cardstring += "                        <img src=\"/trackers/games/E.png\" alt=\"E\" title=\"Endless\">\n"
                    else:
                        cardstring += "                        <img src=\"/trackers/games/UF.png\" alt=\"UF\" title=\"Unfinished\">\n"
            except:
                cardstring += "                        <img src=\"/trackers/games/UP.png\" alt=\"UP\" title=\"Unplayed\">\n"
    cardstring += "                      </div>\n                      <div class=\"text\">\n                        <div class=\"title\">" + game["name"] + "</div>\n"
    try:
        if len(game["recent"]) > 1:
            cardstring += "                        <div class=\"markdown\">" + game["recent"] + "</div>\n"
    except:
        pass
    try:
        if game["gameplay"]:
            try:
                if game["completed"]:
                    if game["gameplay"] > game["completed"]:
                        if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\"/trackers/games/nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
                    else:
                        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
            except:
                try:
                    if game["beaten"]:
                        if game["gameplay"] > game["beaten"]:
                            if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                                cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                            else:
                                cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\"/trackers/games/nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                except:
                    if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                        if game["id"] in endlessgames:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
                        else:
                            cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\"/trackers/games/paused.png\" alt=\"⏸\" title=\"Paused\">\n                      </div>\n"
                    else:
                        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <img src=\"/trackers/games/nowplaying.png\" alt=\"⯈\" title=\"Now Playing\">\n                      </div>\n"
    except:
        cardstring += "                      </div>\n                      <div class=\"priority\">\n                        <span title=\"Normal\"/>\n                      </div>\n"
    cardstring += "                    </summary>\n                    <div class=\"game-info game-guest\">\n                      <div class=\"data\">\n                        <div class=\"box_2\">\n"
    try:
        if len(game["recent"]) > 1:
            cardstring += "                          <div>\n                            <label>Notes</label>\n                            <div class=\"markdown\">" + game["recent"] + "</div>\n                          </div>\n"
    except:
        pass
    cardstring += "                          <div class=\"flex\">\n                            <div>\n                              <label>Status</label>\n                              <p>"
    try:
        if game["completed"]:
            cardstring += "Completed"
    except:
        try:
            if game["beaten"]:
                cardstring += "Beaten"
        except:
            try:
                if game["gameplay"]:
                    if game["id"] in endlessgames:
                        cardstring += "Endless"
                    else:
                        cardstring += "Unfinished"
            except:
                cardstring += "Unplayed"
    cardstring += "</p>\n                            </div>\n                            <div>\n                              <label>Currently</label>\n                              <p>"
    try:
        if game["gameplay"]:
            try:
                if game["completed"]:
                    if game["gameplay"] > game["completed"]:
                        if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                            cardstring += "Not playing"
                        else:
                            cardstring += "Playing"
                    else:
                        cardstring += "Not playing"
            except:
                try:
                    if game["beaten"]:
                        if game["gameplay"] > game["beaten"]:
                            if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                                cardstring += "Not playing"
                            else:
                                cardstring += "Playing"
                        else:
                            cardstring += "Not playing"
                except:
                    if game["gameplay"] < (datetime.now() - timedelta(days=180)):
                        if game["id"] in endlessgames:
                            cardstring += "Not playing"
                        else:
                            cardstring += "Paused"
                    else:
                        cardstring += "Playing"
    except:
        cardstring += "Not playing"
    cardstring += "</p>\n                            </div>\n                          </div>\n                        </div>\n                        <div class=\"box_1 m-r0\">\n                          <div>\n                            <label>Platform</label>\n                            <p>"
    for console in consoles:
        if game["console"] == console["code"]:
            cardstring += console["name"]
    cardstring += "</p>\n                          </div>\n                          <div>\n                            <label>Last Updated</label>\n                            <p>" + game["lastupdate"].strftime("%Y-%m-%d") + "</p>\n                          </div>\n                        </div>\n                      </div>\n                      <div class=\"history\">\n                        <div class=\"box_1\">\n                          <h2>Milestones</h2>\n                          <div class=\"list\">\n                            <div>\n                              <div>" + game["initialdate"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Added\"></div>Added</div>\n                            </div>\n"
    try:
        if game["firstplayed"]:
            cardstring += "                            <div>\n                              <div>" + game["firstplayed"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot "
            if game["id"] in endlessgames:
                cardstring += "Endless"
            else:
                cardstring += "Started"
            cardstring += "\"></div>Started</div>\n                            </div>\n"
    except:
        pass
    try:
        if game["beaten"]:
            cardstring += "                            <div>\n                              <div>" + game["beaten"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Beaten\"></div>Beaten</div>\n                            </div>\n"
    except:
        pass
    try:
        if game["completed"]:
            cardstring += "                            <div>\n                              <div>" + game["completed"].strftime("%Y-%m-%d") + "</div>\n                              <div>\n                                <div class=\"status-dot Completed\"></div>Completed</div>\n                            </div>\n"
    except:
        pass
    cardstring += "                          </div>\n                        </div>\n                      </div>\n                    </div>\n                  </details>\n"
    return cardstring

def backlog():
    # delete existing file
    if not os.path.isdir("build/games/backlog"):
        os.mkdir("build/games/backlog")
    if os.path.exists("build/games/backlog/index.html"):
        os.remove("build/games/backlog/index.html")
    # write header
    headerwrite("build/games/backlog/index.html","backlog")
    output = "build/games/backlog/index.html"
    filewrite = open(output, "a")
    filewrite.write("            <section>\n              <div>\n                <h1>Now Playing</h1>\n                <div class=\"now-playing\">\n")
    playingnow = []
    def nowplaying():
        for console in consolelists:
            if len(console["games"]) > 0:
                thegame = (console["games"])[-1]
                try:
                    if thegame["gameplay"] > (datetime.now() - timedelta(days=180)):
                        try:
                            if thegame["completed"]:
                                if thegame["completed"] >= thegame["gameplay"]:
                                    playing = False
                                else:
                                    playing = True
                        except:
                            try:
                                if thegame["beaten"]:
                                    if thegame["beaten"] >= thegame["gameplay"]:
                                        playing = False
                                    else:
                                        playing = True
                            except:
                                playing = True
                    else:
                        playing = False
                except:
                    playing = False
                if playing == True:
                    playingnow.append(thegame)

    nowplaying()

    playingnow = sorted(playingnow,key=lambda d: d["lastupdate"],reverse=True)

    for game in playingnow:
        if playingnow.index(game) == 0:
            filewrite.write(playcard(game))
        else:
            filewrite.write(playcard(game,True))
    
    filewrite.write("                </div>\n")

    # BACKLOG BREAKDOWN

    filewrite.write("                <h1>Backlog Breakdown</h1>\n                <div class=\"backlog-breakdown\">\n                  <div class=\"backlog-charts\">\n                    <div class=\"mem-sum\">\n                      <div class=\"donut\">\n                        <svg viewBox=\"0 0 42 42\">\n                          <a href=\"/trackers/games/library\"><text x=\"21\" y=\"17.5\" style=\"font-size: 3.5px; opacity: 0.75; text-anchor: middle;\">Total Games</text>\n                          <text x=\"21\" y=\"27\" style=\"font-size: 10px; text-anchor: middle;\">" + str(len(games)) + "</text>\n                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"4\" stroke=\"#000000c0\" fill=\"transparent\">\n                          </circle>\n")

    backlog = unfinished(games) + unplayed(games)
    takeup = 100
    if unplayed(games) > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(200, 30%, 30%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(takeup) + " 100\">\n                          </circle>\n")
        takeup = takeup - round(((unplayed(games)/total(games))*100),1)
    if unfinished(games) > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(0, 38%, 35%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(takeup) + " 100\">\n                          </circle>\n")
        takeup = takeup - round(((unfinished(games)/total(games))*100),1)
    if beaten(games) > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(0, 0%, 82%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(takeup) + " 100\">\n                          </circle>\n")
        takeup = takeup - round(((beaten(games)/total(games))*100),1)
    if completed(games) > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(48, 75%, 70%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(takeup) + " 100\">\n                          </circle>\n")
        takeup = takeup - round(((completed(games)/total(games))*100),1)
    if endless(games) > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(275, 39%, 32%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(takeup) + " 100\">\n                          </circle>\n")

    yeargames = 0
    yearplaying = 0
    for game in games:
        if game["initialdate"].year == int(thisyear):
            try:
                if game["gameplay"]:
                    if game["id"] not in endlessgames:
                        yeargames += 1
            except:
                yeargames += 1
    for game in games:
        try:
            if game["beaten"].year == int(thisyear):
                yeargames -= 1
        except:
            try:
                if game["completed"].year == int(thisyear):
                    yeargames -= 1
            except:
                pass

    filewrite.write("                        </a></svg>\n                      </div>\n                      <div class=\"donut\">\n                        <svg viewBox=\"0 0 42 42\">\n                          <a href=\"")
    filewrite.write("/trackers/games/history")
    filewrite.write("\"><text x=\"21\" y=\"17.5\" style=\"font-size: 3.5px; opacity: 0.75; text-anchor: middle;\">" + thisyear + " Backlog</text>\n                          <text x=\"21\" y=\"27\" style=\"font-size: 10px; text-anchor: middle;\">")

    if yeargames > 0:
        filewrite.write("⬆")
    elif yeargames < 0:
        filewrite.write("⬇")

    filewrite.write(str(abs(yeargames)) + "</text>\n                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"4\" stroke=\"#000000c0\" fill=\"transparent\">\n                          </circle>\n")

    yearbacklog = []
    for game in games:
        try:
            if game["completed"].year == int(thisyear):
                yearbacklog.append(game)
        except:
            try:
                if game["beaten"].year == int(thisyear):
                    yearbacklog.append(game)
            except:
                if game["initialdate"].year == int(thisyear):
                    yearbacklog.append(game)

    yearcompleted = 0
    yearbeaten = 0
    yearendless = 0
    yearunfinished = 0
    yeartotal = len(yearbacklog)
    if yeartotal > 0:
        for game in yearbacklog:
            try:
                if game["completed"]:
                    yearcompleted += 1
            except:
                try:
                    if game["beaten"]:
                        yearbeaten += 1
                except:
                    try:
                        if game["gameplay"]:
                            if game["id"] in endlessgames:
                                yearendless += 1
                            else:
                                yearunfinished += 1
                    except:
                        pass
    yearunplayed = yeartotal - (yearcompleted + yearbeaten + yearunfinished + yearendless)
    yeartakeup = 100
    if yearunplayed > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(200, 30%, 30%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(yeartakeup) + " 100\">\n                          </circle>\n")
        yeartakeup = yeartakeup - round(((yearunplayed/yeartotal)*100),1)
    if yearunfinished > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(0, 38%, 35%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(yeartakeup) + " 100\">\n                          </circle>\n")
        yeartakeup = yeartakeup - round(((yearunfinished/yeartotal)*100),1)
    if yearbeaten > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(0, 0%, 82%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(yeartakeup) + " 100\">\n                          </circle>\n")
        yeartakeup = yeartakeup - round(((yearbeaten/yeartotal)*100),1)
    if yearcompleted > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(48, 75%, 70%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(yeartakeup) + " 100\">\n                          </circle>\n")
        yeartakeup = yeartakeup - round(((yearcompleted/yeartotal)*100),1)
    if yearendless > 0:
        filewrite.write("                          <circle id=\"circle\" r=\"15.91549430918954\" cy=\"21\" cx=\"21\" stroke-width=\"3\" stroke=\"hsla(275, 39%, 32%, 0.9)\" fill=\"transparent\" stroke-dasharray=\"" + str(yeartakeup) + " 100\">\n                          </circle>\n")
    
    filewrite.write("                        </a></svg>\n                      </div>\n                    </div>\n                    <div class=\"status-tally\">\n                      <div class=\"backlog-tally\">\n                        <div style=\"width: " + str(round(((backlog/total(games))*100),1)) + "%;\">")
    if ((backlog/total(games))*100) > 50:
        filewrite.write("\n                          <span>Total Backlog · " + str(backlog) + " · " + str(round(((backlog/total(games))*100),1)) + "%</span>")
    filewrite.write("\n                        </div>\n                        <div style=\"width: " + str(100 - round(((backlog/total(games))*100),1)) + "%;\">")
    if ((backlog/total(games))*100) <= 50:
        filewrite.write("\n                          <span>Active Backlog · " + str(backlog) + " · " + str(round(((backlog/total(games))*100),1)) + "%</span>")
    filewrite.write("\n                        </div>\n                      </div>\n")

    compare = []
    compare.append(unplayed(games))
    compare.append(unfinished(games))
    compare.append(beaten(games))
    compare.append(completed(games))
    compare.append(endless(games))
    maxvalue = max(compare)

    if unplayed(games) / maxvalue > 0.5:
        filewrite.write("                      <a href=\"/trackers/games/library/all-unplayed\">\n                        <div>" + str(unplayed(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/UP.png\">\n                        </div>\n                        <div>\n                          <div class=\"Unplayed\" style=\"width: calc(100% * (" + str(unplayed(games)) + " / " + str(maxvalue) + "));\">" + str(round(((unplayed(games)/total(games))*100),1)) + "% Unplayed</div>\n                        </div>\n                      </a>\n")
    else:
        filewrite.write("                      <a href=\"/trackers/games/library/all-unplayed\">\n                        <div>" + str(unplayed(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/UP.png\">\n                        </div>\n                        <div>\n                          <div class=\"Unplayed\" style=\"width: calc(100% * (" + str(unplayed(games)) + " / " + str(maxvalue) + "));\">\n                          </div>\n                          <div class=\"borderless\">" + str(round(((unplayed(games)/total(games))*100),1)) + "% Unplayed</div>\n                        </div>\n                      </a>\n")
    
    if unfinished(games) / maxvalue > 0.5:
        filewrite.write("                      <a href=\"/trackers/games/library/all-unfinished\">\n                        <div>" + str(unfinished(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/UF.png\">\n                        </div>\n                        <div>\n                          <div class=\"Unfinished\" style=\"width: calc(100% * (" + str(unfinished(games)) + " / " + str(maxvalue) + "));\">" + str(round(((unfinished(games)/total(games))*100),1)) + "% Unfinished</div>\n                        </div>\n                      </a>\n")
    else:
        filewrite.write("                      <a href=\"/trackers/games/library/all-unfinished\">\n                        <div>" + str(unfinished(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/UF.png\">\n                        </div>\n                        <div>\n                          <div class=\"Unfinished\" style=\"width: calc(100% * (" + str(unfinished(games)) + " / " + str(maxvalue) + "));\">\n                          </div>\n                          <div class=\"borderless\">" + str(round(((unfinished(games)/total(games))*100),1)) + "% Unfinished</div>\n                        </div>\n                      </a>\n")

    if beaten(games) / maxvalue > 0.5:
        filewrite.write("                      <a href=\"/trackers/games/library/all-beaten\">\n                        <div>" + str(beaten(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/B.png\">\n                        </div>\n                        <div>\n                          <div class=\"Beaten\" style=\"width: calc(100% * (" + str(beaten(games)) + " / " + str(maxvalue) + "));\">" + str(round(((beaten(games)/total(games))*100),1)) + "% Beaten</div>\n                        </div>\n                      </a>\n")
    else:
        filewrite.write("                      <a href=\"/trackers/games/library/all-beaten\">\n                        <div>" + str(beaten(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/B.png\">\n                        </div>\n                        <div>\n                          <div class=\"Beaten\" style=\"width: calc(100% * (" + str(beaten(games)) + " / " + str(maxvalue) + "));\">\n                          </div>\n                          <div class=\"borderless\">" + str(round(((beaten(games)/total(games))*100),1)) + "% Beaten</div>\n                        </div>\n                      </a>\n")
    
    if completed(games) / maxvalue > 0.5:
        filewrite.write("                      <a href=\"/trackers/games/library/all-completed\">\n                        <div>" + str(completed(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/C.png\">\n                        </div>\n                        <div>\n                          <div class=\"Completed\" style=\"width: calc(100% * (" + str(completed(games)) + " / " + str(maxvalue) + "));\">" + str(round(((completed(games)/total(games))*100),1)) + "% Completed</div>\n                        </div>\n                      </a>\n")
    else:
        filewrite.write("                      <a href=\"/trackers/games/library/all-completed\">\n                        <div>" + str(completed(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/C.png\">\n                        </div>\n                        <div>\n                          <div class=\"Completed\" style=\"width: calc(100% * (" + str(completed(games)) + " / " + str(maxvalue) + "));\">\n                          </div>\n                          <div class=\"borderless\">" + str(round(((completed(games)/total(games))*100),1)) + "% Completed</div>\n                        </div>\n                      </a>\n")

    if endless(games) / maxvalue > 0.5:
        filewrite.write("                      <a href=\"/trackers/games/library/all-endless\">\n                        <div>" + str(endless(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/E.png\">\n                        </div>\n                        <div>\n                          <div class=\"Endless\" style=\"width: calc(100% * (" + str(endless(games)) + " / " + str(maxvalue) + "));\">" + str(round(((endless(games)/total(games))*100),1)) + "% Endless</div>\n                        </div>\n                      </a>\n")
    else:
        filewrite.write("                      <a href=\"/trackers/games/library/all-endless\">\n                        <div>" + str(endless(games)) + "</div>\n                        <div>\n                          <img src=\"/trackers/games/E.png\">\n                        </div>\n                        <div>\n                          <div class=\"Endless\" style=\"width: calc(100% * (" + str(endless(games)) + " / " + str(maxvalue) + "));\">\n                          </div>\n                          <div class=\"borderless\">" + str(round(((endless(games)/total(games))*100),1)) + "% Endless</div>\n                        </div>\n                      </a>\n")
    
    filewrite.write("                    </div>\n                  </div>\n                </div>\n")

    # PLATFORM SUMMARY

    filewrite.write("                <h1>Platform Summary</h1>\n                <div class=\"platform-summary\">\n")

    listofconsoles = sorted(consoles,key=lambda d: d["name"])

    for console in listofconsoles:
        consolegames = []
        for game in games:
            if game["console"] == console["code"]:
                consolegames.append(game)
        ccompleted = 0
        cbeaten = 0
        cendless = 0
        cunfinished = 0
        ctotal = len(consolegames)
        if ctotal > 0:
            filewrite.write("                  <div class=\"platform-card\">\n                    <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-all\" class=\"title\">" + console["name"] + "</a>\n                    <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-all\"class=\"abbr\">" + console["shortname"] + "</a>\n                    <div class=\"bars\">\n")
            for game in consolegames:
                try:
                    if game["completed"]:
                        ccompleted += 1
                except:
                    try:
                        if game["beaten"]:
                            cbeaten += 1
                    except:
                        try:
                            if game["gameplay"]:
                                if game["id"] in endlessgames:
                                    cendless += 1
                                else:
                                    cunfinished += 1
                        except:
                            pass
            cunplayed = ctotal - (ccompleted + cbeaten + cunfinished + cendless)
            if cunplayed > 0:
                filewrite.write("                      <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-unplayed\" class=\"unplayed\" title=\"Unplayed\" style=\"flex: " + str(cunplayed) + " 1 0%;\">" + str(cunplayed) + "</a>\n")
            if cunfinished > 0:
                filewrite.write("                      <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-unfinished\" class=\"unfinished\" title=\"Unfinished\" style=\"flex: " + str(cunfinished) + " 1 0%;\">" + str(cunfinished) + "</a>\n")
            if cbeaten > 0:
                filewrite.write("                      <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-beaten\"class=\"beaten\" title=\"Beaten\" style=\"flex: " + str(cbeaten) + " 1 0%;\">" + str(cbeaten) + "</a>\n")
            if ccompleted > 0:
                filewrite.write("                      <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-completed\" class=\"completed\" title=\"Completed\" style=\"flex: " + str(ccompleted) + " 1 0%;\">" + str(ccompleted) + "</a>\n")
            if cendless > 0:
                filewrite.write("                      <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-endless\" class=\"endless\" title=\"Endless\" style=\"flex: " + str(cendless) + " 1 0%;\">" + str(cendless) + "</a>\n")
            filewrite.write("                    </div>\n                    <a href=\"/trackers/games/library/" + console["shortname"].lower() + "-all\" class=\"total\">" + str(ctotal) + "\n                      <span>Total</span>\n                    </a>\n                  </div>\n")
    filewrite.write("                </div>\n              </div>\n            </div>\n")
    filewrite.close()
    footerwrite("build/games/backlog/index.html")

if __name__ == "__main__":
    backlog()

def library():
    # delete existing file
    if not os.path.isdir("build/games/library"):
        os.mkdir("build/games/library")
    if os.path.exists("build/games/library/index.html"):
        os.remove("build/games/library/index.html")
    # write header
    headerwrite("build/games/library/index.html","library")
    output = "build/games/library/index.html"
    filewrite = open(output, "a")
    filewrite.write("            <section id=\"library-top\" class=\"library\">\n              <div>\n                <div class=\"unibar\">\n                  <div>\n                    <span>Total Found</span> " + str(total(games)) + "\n                  </div>\n")
    if unplayed(games) > 0:
        filewrite.write("                  <div class=\"unplayed\" style=\"flex: " + str(unplayed(games)) + " 1 0%;\"><a href=\"/trackers/games/library/all-unplayed\">" + str(unplayed(games)) + "</a></div>\n")
    if unfinished(games) > 0:
        filewrite.write("                  <div class=\"unfinished\" style=\"flex: " + str(unfinished(games)) + " 1 0%;\"><a href=\"/trackers/games/library/all-unfinished\">" + str(unfinished(games)) + "</a></div>\n")
    if beaten(games) > 0:
        filewrite.write("                  <div class=\"beaten\" style=\"flex: " + str(beaten(games)) + " 1 0%;\"><a href=\"/trackers/games/library/all-beaten\">" + str(beaten(games)) + "</a></div>\n")
    if completed(games) > 0:
        filewrite.write("                  <div class=\"completed\" style=\"flex: " + str(completed(games)) + " 1 0%;\"><a href=\"/trackers/games/library/all-completed\">" + str(completed(games)) + "</a></div>\n")
    if endless(games) > 0:
        filewrite.write("                  <div class=\"endless\" style=\"flex: " + str(endless(games)) + " 1 0%;\"><a href=\"/trackers/games/library/all-endless\">" + str(endless(games)) + "</a></div>\n")
    filewrite.write("                </div>\n                <div class=\"library-list sorted\">\n")

    newgames = sorted(games,key=lambda d: d["id"])

    for game in newgames:
        filewrite.write(playcard(game))

    filewrite.close()
    footerwrite("build/games/library/index.html")

if __name__ == "__main__":
    library()

def sublibrary():
    statuses = ["all","completed","beaten","unfinished","endless","unplayed"]
    consoleextra = [{"shortname":"all"}]
    consolesplus = consoles + consoleextra
    for console in consolesplus:
        gameslist = []
        for game in games:
            if console["shortname"] == "all":
                gameslist.append(game)
            elif game["console"] == console["code"]:
                gameslist.append(game)
        for status in statuses:
            filterlist = []
            if status == "all":
                for game in gameslist:
                    filterlist.append(game)
            elif status == "completed":
                for game in gameslist:
                    try:
                        if game["completed"]:
                            filterlist.append(game)
                    except:
                        pass
            elif status == "beaten":
                for game in gameslist:
                    try:
                        if game["completed"]:
                            pass
                    except:
                        try:
                            if game["beaten"]:
                                filterlist.append(game)
                        except:
                            pass
            elif status == "unfinished":
                for game in gameslist:
                    try:
                        if game["completed"]:
                            pass
                    except:
                        try:
                            if game["beaten"]:
                                pass
                        except:
                            try:
                                if game["gameplay"]:
                                    if game["id"] not in endlessgames:
                                        filterlist.append(game)
                            except:
                                pass
            elif status == "endless":
                for game in gameslist:
                    try:
                        if game["completed"]:
                            pass
                    except:
                        try:
                            if game["beaten"]:
                                pass
                        except:
                            try:
                                if game["gameplay"]:
                                    if game["id"] in endlessgames:
                                        filterlist.append(game)
                            except:
                                pass
            elif status == "unplayed":
                for game in gameslist:
                    try:
                        if game["completed"]:
                            pass
                    except:
                        try:
                            if game["beaten"]:
                                pass
                        except:
                            try:
                                if game["gameplay"]:
                                    pass
                            except:
                                filterlist.append(game)
            if not os.path.isdir("build/games/library/" + console["shortname"].lower() + "-" + status):
                if len(filterlist) > 0:
                    os.mkdir("build/games/library/" + console["shortname"].lower() + "-" + status)
            if os.path.exists("build/games/library/" + console["shortname"].lower() + "-" + status + "/index.html"):
                os.remove("build/games/library/" + console["shortname"].lower() + "-" + status + "/index.html")
            # write header
            if len(filterlist) > 0:
                headerwrite(("build/games/library/" + console["shortname"].lower() + "-" + status + "/index.html"),"sublibrary")
                output = "build/games/library/" + console["shortname"].lower() + "-" + status + "/index.html"
                filewrite = open(output, "a")
                filewrite.write("            <section id=\"library-top\" class=\"library\">\n              <div>\n             <div class=\"filters\">\n  <div>Filtered by\n")
                if console["shortname"] != "all":
                    filewrite.write("    <span class=\"item\"><a href=\"")
                    if status == "all":
                        filewrite.write("/trackers/games/library")
                    else:
                        filewrite.write("/trackers/games/library/all-" + status)
                    filewrite.write("\">1 Platform<span class=\"filter_clear\">x</span></a></span>\n")
                if status != "all":
                    filewrite.write("    <span class=\"item\"><a href=\"")
                    if console["shortname"] == "all":
                        filewrite.write("/trackers/games/library")
                    else:
                        filewrite.write("/trackers/games/library/" + console["shortname"].lower() + "-all")
                    filewrite.write("\">1 Status<span class=\"filter_clear\">x</span></a></span>\n")
                filewrite.write("  </div>\n</div>                <div class=\"unibar\">\n                  <div>\n                    <span>Total Found</span> " + str(total(filterlist)) + "\n                  </div>\n")
                if unplayed(filterlist) > 0:
                    filewrite.write("                  <div class=\"unplayed\" style=\"flex: " + str(unplayed(filterlist)) + " 1 0%;\"><a href=\"/trackers/games/library/" + console["shortname"].lower() + "-unplayed\">" + str(unplayed(filterlist)) + "</a></div>\n")
                if unfinished(filterlist) > 0:
                    filewrite.write("                  <div class=\"unfinished\" style=\"flex: " + str(unfinished(filterlist)) + " 1 0%;\"><a href=\"/trackers/games/library/" + console["shortname"].lower() + "-unfinished\">" + str(unfinished(filterlist)) + "</a></div>\n")
                if beaten(filterlist) > 0:
                    filewrite.write("                  <div class=\"beaten\" style=\"flex: " + str(beaten(filterlist)) + " 1 0%;\"><a href=\"/trackers/games/library/" + console["shortname"].lower() + "-beaten\">" + str(beaten(filterlist)) + "</a></div>\n")
                if completed(filterlist) > 0:
                    filewrite.write("                  <div class=\"completed\" style=\"flex: " + str(completed(filterlist)) + " 1 0%;\"><a href=\"/trackers/games/library/" + console["shortname"].lower() + "-completed\">" + str(completed(filterlist)) + "</a></div>\n")
                if endless(filterlist) > 0:
                    filewrite.write("                  <div class=\"endless\" style=\"flex: " + str(endless(filterlist)) + " 1 0%;\"><a href=\"/trackers/games/library/" + console["shortname"].lower() + "-endless\">" + str(endless(filterlist)) + "</a></div>\n")
                filewrite.write("                </div>\n                <div class=\"library-list sorted\">\n")

                newgames = sorted(filterlist,key=lambda d: d["id"])

                for game in newgames:
                    filewrite.write(playcard(game))

                filewrite.close()
                footerwrite("build/games/library/" + console["shortname"].lower() + "-" + status + "/index.html")

if __name__ == "__main__":
    sublibrary()

def history():
    # delete existing files
    if not os.path.isdir("build/games/history"):
        os.mkdir("build/games/history")
    if os.path.exists("build/games/history/index.html"):
        os.remove("build/games/history/index.html")
    if os.path.exists("build/games/feed.xml"):
        os.remove("build/games/feed.xml")
    # write header
    headerwrite("build/games/history/index.html","history")
    output = "build/games/history/index.html"
    filewrite = open(output, "a")

    filewrite.write("            <section class=\"history\">\n              <div class=\"list\">\n")

    feedwrite = open("build/games/feed.xml", "a")
    feedwrite.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n  <channel>\n    <atom:link href=\"https://tre.praze.net/trackers/games/feed.xml\" rel=\"self\" type=\"application/rss+xml\" />\n    <title>Static backlog</title>\n    <link>https://tre.praze.net/trackers/games/backlog</link>\n    <description>Feed for gaming updates</description>\n    <language>en-gb</language>")

    theyear = int(thisyear)
    while theyear >= gamestartyear:
        yearlist = []
        enddate = datetime.strptime((str(theyear) + "-12-31"),"%Y-%m-%d")
        startdate = datetime.strptime((str(theyear) + "-01-01"),"%Y-%m-%d")
        eachdate = enddate
        while eachdate >= startdate:
            for game in games:
                if game["initialdate"] == eachdate:
                    yearlist.append({"date":eachdate,"name":game["name"],"console":game["console"],"action":"Added"})
                try:
                    if game["firstplayed"] == eachdate:
                        yearlist.append({"date":eachdate,"name":game["name"],"console":game["console"],"action":"Started"})
                except:
                    pass
                try:
                    if game["beaten"] == eachdate:
                        yearlist.append({"date":eachdate,"name":game["name"],"console":game["console"],"action":"Beat"})
                except:
                    pass
                try:
                    if game["completed"] == eachdate:
                        yearlist.append({"date":eachdate,"name":game["name"],"console":game["console"],"action":"Completed"})
                except:
                    pass
            eachdate -= timedelta(days=1)
        checkdate = enddate
        for event in yearlist:
            filewrite.write("                <div>\n")
            if checkdate != event["date"]:
                filewrite.write("                  <h2>" + event["date"].strftime("%Y-%m-%d") + "</h2>\n")
            filewrite.write("                  <div>\n                    <div> " + event["action"] + "\n                      <div class=\"status-dot ")
            if event["action"] == "Started":
                fullpath = str(event["name"] + " (" + event["console"] + ")")
                if fullpath in endlessgames:
                    filewrite.write("Endless")
                else:
                    filewrite.write("Started")
            else:
                filewrite.write(event["action"])
            filewrite.write("\"></div>\n                    </div>\n                    <div> " + event["name"] + "\n                      <span>(" + event["console"] + ")</span>\n                    </div>\n                  </div>\n                </div>\n")
            feedwrite.write("    <item>\n      <title>" + event["action"] + " " + event["name"] + " (" + event["console"] + ")</title>\n      <pubDate>" + event["date"].strftime("%a, %-d %b %Y") + " 00:00:00 GMT</pubDate>\n      <link>https://tre.praze.net/trackers/games/history</link>\n      <guid isPermaLink=\"false\">" + event["action"] + "-" + event["name"].replace(" ","-") + "-" + event["console"] + "</guid>\n      <description>" + event["action"] + " " + event["name"] + " (" + event["console"] + ")</description>\n    </item>\n")
            checkdate = event["date"]
        theyear -= 1

    feedwrite.write("  </channel>\n</rss>")
    feedwrite.close()

    filewrite.write("              </div>\n")

    theyear = int(thisyear)
    filewrite.write("              <div class=\"side box_1\">\n")
    while theyear > 2016:
        yeargames = 0
        yearplaying = 0
        for game in games:
            if game["initialdate"].year == theyear:
                try:
                    if game["gameplay"]:
                        if game["id"] not in endlessgames:
                            yeargames += 1
                except:
                    yeargames += 1
        for game in games:
            try:
                if game["beaten"].year == theyear:
                    yeargames -= 1
            except:
                try:
                    if game["completed"].year == theyear:
                        yeargames -= 1
                except:
                    pass

        yearbacklog = []
        for game in games:
            try:
                if game["completed"].year == theyear:
                    yearbacklog.append(game)
            except:
                try:
                    if game["beaten"].year == theyear:
                        yearbacklog.append(game)
                except:
                    if game["initialdate"].year == theyear:
                        yearbacklog.append(game)

        yearcompleted = 0
        yearbeaten = 0
        yearstarted = 0
        yearadded = 0
        yeartotal = len(yearbacklog)
        if yeartotal > 0:
            for game in yearbacklog:
                try:
                    if game["initialdate"].year == theyear:
                        yearadded += 1
                except:
                    pass
                try:
                    if game["firstplayed"].year == theyear:
                        yearstarted += 1
                except:
                    pass
                try:
                    if game["completed"].year == theyear:
                        yearcompleted += 1
                except:
                    pass
                try:
                    if game["beaten"].year == theyear:
                        yearbeaten += 1
                except:
                    pass

        compare = []
        compare.append(yearadded)
        compare.append(yearstarted)
        compare.append(yearbeaten)
        compare.append(yearcompleted)
        maxvalue = max(compare)
        
        filewrite.write("                <div class=\"year-tally")
        if theyear == int(thisyear):
            filewrite.write(" active")
        filewrite.write("\">\n                  <h2> " + str(theyear) + " Summary\n                    <span>Backlog ")
        if yeargames > 0:
            filewrite.write("▲ " + str(yeargames))
        elif yeargames == 0:
            filewrite.write(" 0")
        elif yeargames < 0:
            filewrite.write("▼ " + str(yeargames * -1))
        filewrite.write("</span></h2>\n                  <div>\n                    <div>Added</div>\n                    <div>" + str(yearadded) + "</div>\n                    <div>\n                      <div class=\"unplayed\" style=\"width: calc(95% * (" + str(yearadded) + " / " + str(maxvalue) + "));\">\n                      </div>\n                    </div>\n                  </div>\n                  <div>\n                    <div>Started</div>\n                    <div>" + str(yearstarted) + "</div>\n                    <div>\n                      <div class=\"unfinished\" style=\"width: calc(95% * (" + str(yearstarted) + " / " + str(maxvalue) + "));\">\n                      </div>\n                    </div>\n                  </div>\n                  <div>\n                    <div>Beat</div>\n                    <div>" + str(yearbeaten) + "</div>\n                    <div>\n                      <div class=\"beaten\" style=\"width: calc(95% * (" + str(yearbeaten) + " / " + str(maxvalue) + "));\">\n                      </div>\n                    </div>\n                  </div>\n                  <div>\n                    <div>Completed</div>\n                    <div>" + str(yearcompleted) + "</div>\n                    <div>\n                      <div class=\"completed\" style=\"width: calc(95% * (" + str(yearcompleted) + " / " + str(maxvalue) + "));\">\n                      </div>\n                    </div>\n                  </div>\n                </div>\n")
        theyear -= 1

    filewrite.write("              </div>\n            </section>\n")

    filewrite.close()
    footerwrite("build/games/history/index.html")

if __name__ == "__main__":
    history()