diff options
| author | Anson Bridges <bridges.anson@gmail.com> | 2023-11-18 06:18:23 -0500 |
|---|---|---|
| committer | Anson Bridges <bridges.anson@gmail.com> | 2023-11-18 06:18:23 -0500 |
| commit | c560de266caad6db97e58757ca43e8558c103c01 (patch) | |
| tree | 852deb7079d616042a255c906a9196b135ca0bba /dashboard_website/db.py | |
| parent | 939f7a093dadfa89ac1bbe43b0c9fded404c1ad5 (diff) | |
csv import, manual visiting
Diffstat (limited to 'dashboard_website/db.py')
| -rw-r--r-- | dashboard_website/db.py | 68 |
1 files changed, 48 insertions, 20 deletions
diff --git a/dashboard_website/db.py b/dashboard_website/db.py index 6a638e7..166ac7a 100644 --- a/dashboard_website/db.py +++ b/dashboard_website/db.py @@ -48,6 +48,7 @@ def updateRoutes_background(): # run in thread due to long runtime def updateRoutes(): # if necessary global currently_updating if not currently_updating: + save() currently_updating = True t = Thread(target=updateRoutes_background) t.start() @@ -155,18 +156,20 @@ def deleteClue(clue_name): def visitClue(clue_name): + global clues_last_changed for clue in clues: if clue.name == clue_name: if clue.status == "VISITED": return 3 # already visited clue.visit() clues_last_changed = time.time() - # updateRoutes() + updateRoutes() return 0 # OK return 2 # no clue found def visitClueTeam(team_name, clue_name): + global clues_last_changed b = None for bike in bikes: if bike.name == team_name: @@ -185,6 +188,7 @@ def visitClueTeam(team_name, clue_name): return 5 # clue not found # if visited clue is the expected one (current target) + # no need to recalculate if clue_name == b.target_name: if c.distanceTo(b) < CLUE_MIN_DISTANCE: return 3 # too far away @@ -195,6 +199,7 @@ def visitClueTeam(team_name, clue_name): # otherwise c.visit() clues_last_changed = time.time() + updateRoutes() # must be updated due to unexpected visitation def load(filename=None): @@ -204,8 +209,9 @@ def load(filename=None): :return: None """ # if there is no filename, wipe all clues - if filename is None: + if filename == None: clues.clear() + filename = "savefile.csv" # otherwise, load from file with open(filename, newline='') as f: @@ -215,13 +221,46 @@ def load(filename=None): next(csvreader) for row in csvreader: - name = row[0] - latitude = row[1] - longitude = row[2] - info = row[3] - status = row[4] + try: + name = row[0] + latitude = row[1] + longitude = row[2] + info = row[3] + status = row[4] + + clues.append(Clue(latitude, longitude, name, info, status)) + except: + return 1 + clues_last_changed = time.time() + updateRoutes() + return 0 + +def loadDirty(filename=None): + global clues_last_changed + if filename == None: + return + + # otherwise, load from file + with open(filename, newline='') as f: + csvreader = csv.reader(f, delimiter=',', quotechar='"') - clues.append(Clue(latitude, longitude, name, info, status)) + # skip header row + next(csvreader) + for row in csvreader: + try: + name = row[0] + info = row[2] + latlong = row[3].split(",") + if len(latlong) != 2: continue + latitude = float(latlong[0]) + longitude = float(latlong[1]) + + clues.append(Clue(latitude, longitude, name, info, "UNVISITED")) + except: + return 1 + clues_last_changed = time.time() + updateRoutes() + return 0 def save(): @@ -237,15 +276,4 @@ def save(): csvwriter.writerow([clue.name, clue.latitude, clue.longitude, clue.info, clue.status]) -# junk for testing -with open("all_clues.csv", newline='') as f: - csvreader = csv.reader(f, delimiter=',', quotechar='"') - i = 1 - for row in csvreader: - coords = row[1].split(",") - coords[0] = float(coords[0]); - coords[1] = float(coords[1]); - - newClue = Clue(coords[0], coords[1], f"Clue #{i}", row[0], "UNVISITED" if i < 100 else "VISITED") - clues.append(newClue) - i += 1 +load()
\ No newline at end of file |
