1. Benoit Boissinot
  2. lemur-web

Commits

Benoit Boissinot  committed 0bd31e4

random tiles (not exactly in a nice way)

  • Participants
  • Parent commits 7e6209b
  • Branches default

Comments (0)

Files changed (2)

File index.yaml

View file
 # manually, move them above the marker line.  The index.yaml file is
 # automatically uploaded to the admin console when you next deploy
 # your application using appcfg.py.
+
+- kind: Tile
+  properties:
+  - name: __key__
+    direction: desc

File main.py

View file
-from google.appengine.ext import webapp
+from google.appengine.ext import db, webapp
 from google.appengine.ext.webapp.util import run_wsgi_app
 from django.utils import simplejson as json
-import zlib
-
-from google.appengine.ext import db
+import zlib, random
 
 maxrange = 2**27
 tilelength = 32*32
     q = db.Query(Tile)
     return q.filter('x =', x).filter('y =', y).get()
 
+def get_random_tile():
+    q = db.Query(Tile, keys_only=True)
+    q.order('-__key__')
+    rows = q.fetch(300)
+    if not rows:
+        return 0, 0, None
+    t = Tile.get_by_id(random.choice(rows).id())
+    return t.x, t.y, zlib.decompress(t.content)
+
+def get_tile_content(x, y):
+    t = get_tile(x, y)
+    if (x, y) == (0, 0) and (t is None or t.content != welcome_tile):
+        content = welcome_tile
+        tile = Tile(x=x, y=y, content=content)
+        tile.put()
+    elif t is None:
+        content = empty_tile
+    else:
+        content = t.content
+
+    return zlib.decompress(content)
+
 class Tiles(webapp.RequestHandler):
     def get(self, *args):
+        content = None
         if not args:
             x, y = 0, 0
-        else:
+        elif len(args) == 1 and args[0] == 'random':
+            x, y, content = get_random_tile()
+        elif len(args) == 2:
             x = long(args[0]) % maxrange
             y = long(args[1]) % maxrange
+        else:
+            raise Exception('Not parsed')
 
-        t = get_tile(x, y)
-        if (x, y) == (0, 0) and (t is None or t.content != welcome_tile):
-            content = welcome_tile
-            tile = Tile(x=x, y=y, content=content)
-            tile.put()
-        elif t is None:
-            content = empty_tile
-        else:
-            content = t.content
-
-        content = zlib.decompress(content)
-
-        tile = {
-            'coord': (x, y),
-            'content': content, #list(split32(content)),
-        }
+        if content is None:
+            content = get_tile_content(x, y)
 
         self.response.headers['Content-Type'] = 'text/plain; charset="UTF-8"'
         self.response.headers['X-Wall-Coord'] = '%dx%d' % (x, y)
 
 application = webapp.WSGIApplication(
                                      [(r'/tiles/(\d+)x(\d+)', Tiles),
+                                      (r'/tiles/(random/?)', Tiles),
                                       (r'/tiles/?', Tiles)],
                                      debug=True)