-
Notifications
You must be signed in to change notification settings - Fork 0
/
score_writer.py
27 lines (24 loc) · 1019 Bytes
/
score_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import csv
import os
class ScoreWriter():
def __init__(self,score_file_path):
self.score_file_path = score_file_path
self.score = 0
if not os.path.isfile(self.score_file_path):
self.update_score_file(0)
def add_and_update_scenario_score(self,delta_score: int):
if os.path.isfile(self.score_file_path):
with open(self.score_file_path, 'r', newline='') as scorefile:
reader = csv.reader(scorefile)
data = list(reader)
assert(len(data) == 2)
self.score = int(data[1][0])
self.score += delta_score
self.update_score_file(self.score)
else:
self.update_score_file(0)
def update_score_file(self,score: int):
with open(self.score_file_path, 'w', newline='') as scorefile:
writer = csv.DictWriter(scorefile, fieldnames=['score'])
writer.writeheader()
writer.writerow({'score': score})