source: nagslang/world.py@ 261:db7c8e74efb4

Last change on this file since 261:db7c8e74efb4 was 261:db7c8e74efb4, checked in by Stefano Rivera <stefano@…>, 8 years ago

(really rubbish) bullets

File size: 1.7 KB
Line 
1# The world object
2#
3# This is a global object for tracking state across scenes and all that
4
5import os
6import sys
7
8from nagslang.yamlish import dump, load
9
10
11class World(object):
12
13 def __init__(self):
14 self.reset()
15
16 def reset(self):
17 self.attacks = 0
18 self.transformations = 0
19 self.kills = 0
20 self.rooms = 0
21
22 def _save_location(self):
23 app = 'nagslang'
24 if sys.platform.startswith('win'):
25 if 'APPDATA' in os.environ:
26 return os.path.join(os.environ['APPDATA'], app)
27 return os.path.join(os.path.expanduser('~'), '.' + app)
28 elif 'XDG_DATA_HOME' in os.environ:
29 return os.path.join(os.environ['XDG_DATA_HOME'], app)
30 return os.path.join(os.path.expanduser('~'), '.local', 'share', app)
31
32 def save(self):
33 data = {}
34 for attr in dir(self):
35 if attr.startswith('_'):
36 continue
37 value = getattr(self, attr)
38 if hasattr(value, '__name__'):
39 continue
40 data[attr] = value
41
42 fn = self._save_location()
43 if not os.path.isdir(os.path.dirname(fn)):
44 os.makedirs(os.path.dirname(fn))
45 with open(fn, 'w') as f:
46 dump(data, f)
47
48 def load(self):
49 fn = self._save_location()
50 if not os.path.exists(fn):
51 return False
52 with open(fn) as f:
53 data = load(f)
54 for k, v in data.iteritems():
55 setattr(self, k, v)
56 return True
57
58 def get_formatted_stats(self):
59 return "\n".join([
60 "Times transformed: %d" % self.transformations,
61 "Enemies killed: %d" % self.kills,
62 "Rooms entered: %d" % self.rooms
63 ])
Note: See TracBrowser for help on using the repository browser.