forked from alexfrostdesu/RoyalBattleofRogalique
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.py
75 lines (63 loc) · 2.23 KB
/
save.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
70
71
72
73
74
75
import pickle, redis, os
REDIS_URL = os.environ["REDIS_URL"]
class RedisConnection(object):
def __init__(self, connection_string):
self.connection_string = connection_string
self.connection = redis.from_url(self.connection_string)
def set_value(self, key, value):
"""Sets the value of key to value"""
self.connection.set(key, value)
def get_value(self, key):
"""Gets value of a key"""
return self.connection.get(key)
def delete(self, key):
"""Deletes value by key"""
self.connection.delete(key)
def save_game(self, key, game):
"""Serializes game to binary and saves"""
game_serialized = pickle.dumps(game)
self.set_value(key, game_serialized)
def get_game(self, key):
"""Returns deserialized game object"""
serizalized = self.get_value(key)
return pickle.loads(serizalized)
def get_all_games(self):
"""Returns dictionary with payer_id's as keys, and games as values"""
result = {}
keys = self.connection.keys('*')
for key in keys:
result[int(key)] = self.get_game(key)
return result
def delete_all_saves(self):
"""
Deletes all the entries in redis
Requires python process stop, otherwise will be overritten by new entries
"""
keys = self.connection.keys('*')
for key in keys:
self.delete(key)
def short_info(self):
"""
Prints all saved character classes, their lvl and user_id
"""
keys = self.connection.keys('*')
res = ""
for key in keys:
game = self.get_game(key)
player = game.get_playerchar()
lvl = player.get_lvl()
cla = player.get_class()
res += f"player {int(key)} of class {cla}:lvl {lvl}\n"
print(res)
def full_info(self):
"""
Prints all the saved character's stats
"""
keys = self.connection.keys('*')
res = ""
from events import StatusMessage
for key in keys:
game = self.get_game(key)
player = game.get_playerchar()
res += StatusMessage(player).stats_message() + '\n'
print(res)