-
Notifications
You must be signed in to change notification settings - Fork 3
/
savegamedata.py
39 lines (31 loc) · 975 Bytes
/
savegamedata.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
import random
import string
import json
def generateFilename(size: int) -> str:
game_data_savename = ''.join([random.choice(string.ascii_uppercase
+ string.ascii_lowercase + string.digits) for _ in range(size)])
return game_data_savename
def getUniqueSavefile() -> str:
gamefile = generateFilename(16)
filename = "saved_games/" + gamefile + ".json"
result = False
try:
f = open(filename, 'r')
f.close()
except:
result = True
if result:
result_file = filename
else:
result_file = getUniqueSavefile()
return result_file
def saveGame(data: dict) -> None:
filename = getUniqueSavefile()
savefile = open(filename, 'w')
json.dump(data, savefile, indent=4)
savefile.close()
def saveGameToID(data: dict, gameid: str) -> None:
filename = "saved_games/" + gameid + ".json"
savefile = open(filename, 'w')
json.dump(data, savefile, indent=4)
savefile.close()