def tile(self): z = 11 x = randint(496,523) y = randint(772,797) try: z,x,y = map(int,[z,x,y]) tid = "t:%s:%d+%d+%d"%(self.ts.shpfile,z,x,y) tile = Tile.get_by_key_name(tid) if tile: self._tile = tile return tile else: shpfile = memcache.get(self.ts.shpfile.encode('utf8')) if not shpfile: shpfile = Shapefile.get_by_key_name(self.ts.shpfile) memcache.set(self.ts.shpfile.encode('utf8'),shpfile) typ,dat = shpfile.raw_tile(x,y,z) tile = Tile(tid) tile.typ = typ tile.dat = str(dat) tile.put() self._tile = tile return tile except: raise return False return False
def post(self): tile = Tile() tile.x = int(self.request.get("x")) tile.y = int(self.request.get("y")) tile.height = int(self.request.get("height")) tile.type = int(self.request.get("type")) tile.put()
def get(self): map = memcache.get("map") HeightMapData = FetchImage("http://humanitymmo.appspot.com/static/earthmini.png") w,h = HeightMapData[:2] # should get the tiles, within proximity of units. # foreach unit, get tiles within range of the unit # in this instance limit it to 1 unit only unit = Unit.get_by_id( int(self.request.get("id")) ) json = {"tiles":[],"enemyunits":[]} fov = 2 # fov is how many tiles a unit can see around it xleft = max(unit.x - fov, 0) xright = min(unit.x + fov + 1, w) ytop = max(unit.y - fov, 0) ybottom = min(unit.y + fov + 1, h) # get heightmap based tiles for x in range(xleft, xright): for y in range(ytop, ybottom): # is the tile in the datastore curTile = Tile.gql("where x = :1 and y = :2", x,y) if curTile.count() > 0: if curTile.type.canTravel: curAlt = 80 else: curAlt = 81 json["tiles"].append( {"x":curTile.x,"y":curTile.y,"alt":curAlt} ) else alt = GetHeightAt(x,y,HeightMapData) json["tiles"].append( {"x":x, "y":y, "alt":alt } ) # write to datastore t = Tile() t.x = x t.y = y t.height = alt if alt < 81: # get tile type where name = water, then reference it else: # get tile type for land t.put() self.response.out.write(demjson.encode(json))
def tile(self): if self._tile and not self._get_raw: return self._tile if not self.ts: return False get = self.request.REQUEST.get x = get('x') y = get('y') z = get('z') b = True if get('b',-1) == '0': b = False if not (x and y and z): return False try: z,x,y = map(int,[z,x,y]) if self._get_raw: shpfile = Shapefile.get_by_key_name(self.ts.shpfile) png = shpfile.raw_png(x,y,z,border=b) return png if b: tid = "t:%s:%d+%d+%d"%(self.ts.shpfile,z,x,y) else: tid = "u:%s:%d+%d+%d"%(self.ts.shpfile,z,x,y) tile = Tile.get_by_key_name(tid) if tile: self._tile = tile return tile else: logger.info("Creating tile x%d,y%d,z%d of %s"%(x,y,z,tid)) shpfile = memcache.get(self.ts.shpfile.encode('utf8')) if not shpfile: shpfile = Shapefile.get_by_key_name(self.ts.shpfile) memcache.set(self.ts.shpfile.encode('utf8'),shpfile) typ,dat = shpfile.raw_tile(x,y,z,border=b) tile = Tile(tid) tile.typ = typ tile.dat = str(dat) tile.put() self._tile = tile return tile except: logger.error("Exception occured while Getting or Creating Tile (x%d,y%d,z%d)"%(x,y,z)) return False return False
def post(self): """ Saves the image sent via JSON in the blobstore. If an entry for (x, y) is not in the Tile table of the datastore, create it with the blob key for the image. Otherwise update the entry with the new blob key and delete the existing associated blob. Send messages to any open channel that a change has been made to the tile. Delete any channels that may have been open for more than two hours. """ #Get JSON data x = int(self.request.get('x')) y = int(self.request.get('y')) data = self.request.get('data') #base64 decode image data = base64.b64decode(data) #Write image to blobstore file_name = files.blobstore.create(mime_type='image/png') with files.open(file_name, 'a') as f: f.write(data) files.finalize(file_name) #Get blob key for newly created blob in blobstore blob_key = files.blobstore.get_blob_key(file_name) # Check if tile is already in database query = Tile.gql("WHERE x = :1 AND y = :2", x, y) myTile = query.get() if myTile is None: #Create new tile entry in database with key to new blob myTile = Tile(x=x, y=y, blob_key=blob_key, rand_num=random.random()) myTile.put() else: #Update the blob key in the entry for this tile old_key = myTile.blob_key myTile.blob_key = blob_key myTile.put() #Delete the blob previously associated with the tile google.appengine.ext.blobstore.delete(old_key) #For live updates: #Send a message to the channels indicating this tile has changed. channels = UpdateChannel.gql("").fetch(100) message = json.dumps({"x": x, "y": y, "Type": "Tile"}) for ch in channels: ch_id = ch.channel_id #if a channel is two hours old, delete it. d = parse_datetime(ch_id.split(",")[0]) if d < datetime.now() + timedelta(hours=-2): ch.key.delete() elif ch_id != self.session.get("channel_id"): channel.send_message(ch.channel_id, message) #No response data to send. self.response.set_status(200)