-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_db.py
56 lines (49 loc) · 1.07 KB
/
game_db.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
#!/usr/bin/env python
#~ import cPickle as serialize
import pickle as serialize
#~ import json as serialize
class PlayerData():
name = None
skill = 0.0
mu = 30.0
sigma = 3.0
ngames = 0
class GameData():
id = 0 # number
date = None # time.asctime
map = None
players={} # {name:rank}
class GameDB():
games = {}
players={}
latest = 0
#
# global methods to allow easy serialisation for gamedb
# it's as clumsy, as it looks.
#
def load():
db = GameDB()
try:
f = open("games/saved_latest", "rb")
db.latest = serialize.load(f)
f.close()
f = open("games/saved_players", "rb")
db.players = serialize.load(f)
f.close()
f = open("games/saved_games", "rb")
db.games = serialize.load(f)
f.close()
except:
# better empty, than corrupted
db = GameDB()
return db
def save(db):
f = open("games/saved_latest", "wb")
serialize.dump(db.latest,f)
f.close()
f = open("games/saved_players", "wb")
serialize.dump(db.players,f)
f.close()
f = open("games/saved_games", "wb")
serialize.dump(db.games,f)
f.close()