annotate mamba/habitats/userlevelmenu.py @ 431:15771cc126fc

Always return network levels in a consistent order.
author Simon Cross <hodgestar@gmail.com>
date Sat, 17 Sep 2011 19:12:59 +0200
parents 001c3797a63b
children f4f883418ac2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Level menu."""
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
399
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
3 import urllib2
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
4
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
5 from mamba.habitats.levelmenu import LevelMenu
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6 from mamba.level import Level
345
04bb1ffcd054 Activated ctpug level server.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
7 from mamba.constants import LEVEL_SERVER
402
001c3797a63b Editor now uses templates and the user level directory.
Jeremy Thurgood <firxen@gmail.com>
parents: 399
diff changeset
8 from mamba.data import get_level_list, load_file
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
11 class UserLevelApi(object):
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
13 def __init__(self, ctype, url=LEVEL_SERVER, timeout=5):
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
14 assert ctype in ("curated", "uncurated")
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
15 assert url.endswith("/")
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
16 self.ctype = ctype
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
17 self.level_namespace = ctype
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
18 self.url = url
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
19 self.timeout = timeout
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
20 self.cache = {}
335
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
21
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
22 def _url_data(self, route):
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
23 url = "%s%s/%s" % (self.url, self.ctype, route)
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
24 return urllib2.urlopen(url, timeout=self.timeout).read()
335
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
25
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
26 def _populate_level(self, name):
335
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
27 try:
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
28 source = self._url_data("level/%s" % name)
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
29 level = Level(name, self.level_namespace, source)
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
30 except:
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
31 print "Failed to download online level %r" % name
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
32 return
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
33 self.cache[name] = level
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
34
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
35 def _populate_cache(self):
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
36 try:
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
37 data = self._url_data("index")
335
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
38 except:
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
39 print "Failed to download online level index."
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
40 return
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
41 levels = [x.strip() for x in data.splitlines()]
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
42
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
43 for name in levels:
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
44 self._populate_level(name)
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46 def list_levels(self):
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
47 if not self.cache:
335
b0ac80c7db04 Cache online levels.
Simon Cross <hodgestar@gmail.com>
parents: 334
diff changeset
48 self._populate_cache()
431
15771cc126fc Always return network levels in a consistent order.
Simon Cross <hodgestar@gmail.com>
parents: 402
diff changeset
49 return sorted(self.cache.keys())
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50
366
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
51 def print_levels(self):
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
52 title = "%s levels:" % self.ctype.title()
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
53 print title
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
54 print "=" * len(title)
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
55 for name in self.list_levels():
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
56 print name
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
57 print "=" * len(title)
d759f49c477d Option for printing list of uncurated levels.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
58
334
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
59 def get_level(self, name):
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
60 if name not in self.cache:
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
61 self._populate_level(name)
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
62 return self.cache[name]
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
63
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
64
399
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
65 class NetworkLevelMenu(LevelMenu):
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
66
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
67 API = UserLevelApi("curated")
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
68
399
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
69 @property
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
70 def level_namespace(self):
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
71 self.API.level_namespace
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
72
362
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
73 def list_levels(self):
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
74 return self.API.list_levels()
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
75
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
76 def get_level(self, name):
cc8be536a7fc Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents: 361
diff changeset
77 return self.API.get_level(name)
399
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
78
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
79
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
80 class UserLevelMenu(LevelMenu):
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
81
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
82 level_namespace = "user"
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
83
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
84 def list_levels(self):
402
001c3797a63b Editor now uses templates and the user level directory.
Jeremy Thurgood <firxen@gmail.com>
parents: 399
diff changeset
85 return get_level_list('user_levels', is_user_dir=True)
399
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
86
12d67f69e6b2 Support for user levels.
Jeremy Thurgood <firxen@gmail.com>
parents: 366
diff changeset
87 def get_level(self, name):
402
001c3797a63b Editor now uses templates and the user level directory.
Jeremy Thurgood <firxen@gmail.com>
parents: 399
diff changeset
88 source = load_file('user_levels/%s.txt' % (name,), is_user_dir=True)
001c3797a63b Editor now uses templates and the user level directory.
Jeremy Thurgood <firxen@gmail.com>
parents: 399
diff changeset
89 return Level(name, self.level_namespace, source.read())