This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest.py
55 lines (42 loc) · 1.6 KB
/
quest.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
import os.path
import cPickle
import logging
logger = logging.getLogger(__name__)
class Quest:
def __init__(self):
self.users = {}
def create_user(self, username):
# Remove operator signs
if '@' in username and username.index('@') == 0:
username = username[1:]
if username not in self.users:
self.users[username] = QuestUser(username)
def hibernate_user(self, username):
if username in self.users:
self.users[username].hibernate()
del self.users[username]
class QuestUser:
def __init__(self, username):
self.version = 1
self.username = username
if os.path.exists("archive/" + self.username + ".user"):
logger.info("User '%s' found in archive." % self.username)
self.load()
else:
logger.info("User '%s' not found in archive." % self.username)
self.save()
def hibernate(self):
logger.info("User '%s' goes into hibernation." % (self.username))
self.save()
def load(self):
with open('archive/' + self.username + '.user') as f:
tmp_dict = cPickle.load(f)
loaded_version = tmp_dict['version']
logger.info(" - Loaded userfile with data format version %i." %
loaded_version)
self.__dict__.update(tmp_dict)
def save(self):
with open('archive/' + self.username + '.user', 'wb') as f:
cPickle.dump(self.__dict__, f, 2)
logger.info("Saved user file for '%s', data format version %i." %
(self.username, self.version))