-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerStatistics.py
47 lines (38 loc) · 1.41 KB
/
PlayerStatistics.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import os
class PlayerStatistics:
def __init__(self):
self.data = {}
self.save_file = 'stats.json'
def update_player(self, name, ratio):
# if player hasn't played yet -> register in dictionary
if not self.data.__contains__(name):
self.data[name] = 0
self.save_to_file()
# if score of current game is higher than in dictionary -> highscore
if ratio > self.data[name]:
self.data[name] = ratio
self.save_to_file()
def is_new_highscore(self, name, ratio):
try:
return ratio > self.data[name]
except KeyError:
# if player has not been found -> first score => highscore
return True
def save_to_file(self):
# saves data from dictionary into the file
with open(self.save_file, 'w') as outfile:
json.dump(self.data, outfile)
def load_from_file(self):
if not os.path.exists(self.save_file):
# creates new file if it doesn't exist
open(self.save_file, 'w').close()
self.save_to_file()
# loads data from file into the dictionary
with open(self.save_file) as file:
self.data = json.load(file)
def get_highscore(self, name):
try:
return self.data[name]
except KeyError:
return None