def getNewSummoner():
    error = 0

    # get the currently highest summonerId from the database
    summonerIDPointer = db.selectFetchOne("SELECT summonerId FROM summoner ORDER BY summonerId DESC")
    if summonerIDPointer is None:
        summonerIDPointer = 0
    else:
        summonerIDPointer = summonerIDPointer[0] + 1
    print(summonerIDPointer)

    while True:
        # check the next 40 ids
        summonerIDs = []
        for i in range(summonerIDPointer, summonerIDPointer + 39):
            summonerIDs.append(i)
        response = api.getSummonerByIDs(summonerIDs, "euw")
        if type(response) is dict:
            error = 0
            db.insertSummoner(response)
        else:
            error += 1

        # if there are more than 400 empty ids it's fair to assume this was the last one
        if error >= 20:
            print("50 Errors in a row... fairly sure that's all for now.")
            break

        summonerIDPointer += 40
    # check match
    response = api.getMatch(matchId, "euw", True)
    # if this Id belongs to a match
    if type(response) is dict:
        # insert it into the database
        t = threading.Thread(target=insertIntoDatabase, args=(response,))
        t.daemon = False
        t.start()
        # if the match was played in the last 2 hours wait a bit so we don't
        # overlook games which are currently still running
        if (response["matchCreation"] + 7200000) > int(time.time()*1000):
            print("We're getting kinda close to currently played games. Sleeping for 5 minutes")
            time.sleep(300)
    return True

def insertIntoDatabase(data):
    db.insertMatch(data)

# get the last match we collected
result = db.selectFetchOne("SELECT matchId FROM lol.match ORDER BY idmatch DESC")
# if no matches are collected yet use this Id as entry point
if result is None:
    matchId = 1792756886
else:
    matchId = result[0] + 1


while True:
    insertMatch(matchId)
    matchId += 1