-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
69 lines (61 loc) · 1.51 KB
/
score.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Functions for score management
def sort(lst):
if len(lst) < 1:
return lst
# Clean list from invalid lines
for l in range(len(lst)):
try:
if not (':' in lst[l]):
del lst[l]
if lst[l] == '\n':
del lst[l]
except:
pass
for j in range(len(lst)):
for k in range(len(lst)-1):
item1 = lst[k]
item1t = float((item1.split(':')[1]))
if k != len(lst)-1:
item2 = lst[k+1]
item2t = float((item2.split(':')[1]))
if item1t < item2t:
# Swap items
lst[k] = str(item2)
lst[k+1] = str(item1)
# Remove duplicates
names = []
h = 0
while h < len(lst):
s = lst[h].split(":")
if s[0] in names:
del lst[h]
else:
names.append(s[0])
h += 1
return lst
def top(f, n, s):
try:
file = open(f, 'r')
except:
file = open(f, 'x')
scores = []
else:
scores = file.readlines()
finally:
if n != "" and n != None:
scores.append(f"{n}:{s}")
scores = sort(scores)
file = open(f, 'w')
for i in scores:
file.write(str(i).strip('\n') + '\n')
file.flush()
file.close()
def get(f):
try:
fobj = open(f, 'r')
except:
return [""]
else:
lst = fobj.readlines()
fobj.close()
return lst