Mercurial > mamba
annotate mamba/habitats/userlevelmenu.py @ 362:cc8be536a7fc
Add ability to play uncurated levels via the command line.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 17 Sep 2011 00:03:46 +0200 |
parents | d4062344d8f4 |
children | d759f49c477d |
rev | line source |
---|---|
334 | 1 """Level menu.""" |
2 | |
3 from mamba.habitats.levelmenu import LevelMenu | |
4 from mamba.level import Level | |
345
04bb1ffcd054
Activated ctpug level server.
Simon Cross <hodgestar@gmail.com>
parents:
340
diff
changeset
|
5 from mamba.constants import LEVEL_SERVER |
334 | 6 |
7 import urllib2 | |
8 | |
9 | |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
10 class UserLevelApi(object): |
334 | 11 |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
12 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
|
13 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
|
14 assert url.endswith("/") |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
15 self.ctype = ctype |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
16 self.level_namespace = ctype |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
17 self.url = url |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
18 self.timeout = timeout |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
19 self.cache = {} |
335 | 20 |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
21 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
|
22 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
|
23 return urllib2.urlopen(url, timeout=self.timeout).read() |
335 | 24 |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
25 def _populate_level(self, name): |
335 | 26 try: |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
27 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
|
28 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
|
29 except: |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
30 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
|
31 return |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
32 self.cache[name] = level |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
33 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
34 def _populate_cache(self): |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
35 try: |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
36 data = self._url_data("index") |
335 | 37 except: |
38 print "Failed to download online level index." | |
39 return | |
40 levels = [x.strip() for x in data.splitlines()] | |
41 | |
42 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
|
43 self._populate_level(name) |
334 | 44 |
45 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
|
46 if not self.cache: |
335 | 47 self._populate_cache() |
362
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
48 return self.cache.keys() |
334 | 49 |
50 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
|
51 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
|
52 self._populate_level(name) |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
53 return self.cache[name] |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
54 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
55 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
56 class UserLevelMenu(LevelMenu): |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
57 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
58 API = UserLevelApi("curated") |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
59 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
60 def list_levels(self): |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
61 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
|
62 |
cc8be536a7fc
Add ability to play uncurated levels via the command line.
Simon Cross <hodgestar@gmail.com>
parents:
361
diff
changeset
|
63 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
|
64 return self.API.get_level(name) |