def on_fetch_location(self, response): """ Callback invoked when we get location data. Lazily check to see if we already have an entry in MongoDB about this location or not. If not, create one. """ user_id = self.get_secure_cookie("user_id") user = db.get_user(user_id) location = json.loads(response.body) location_data = db.get_or_create_location_by_id(location["id"]) # Lazy get this location_data["checkins"] = location["checkins"] db.save_location(location_data) inventory = db.get_inventory_for_user(user) # power is a function of checkins * something self.render("templates/location.html", datetime=datetime, location=location, data=location_data, current_user=user, inventory=inventory)
def post(self, location_id): """ This handler is a bit more complex - handles the actions that can be taken at this location. For instance: - extort - take control - leave guards - battle """ user_id = self.get_secure_cookie("user_id") user = db.get_user(user_id) location = db.get_or_create_location_by_id(location_id) action = self.get_argument("action") if action == "take-control": # TODO: Do an additional check to make sure this isn't owned # by someone else and there are no guards here. location["owner"] = { "name" : user["name"], "user_id" : user["id"] } db.save_location(location) elif action == "extort" and location["owner"]["user_id"] == user["id"]: inventory = db.get_inventory_for_user(user) extortion_value = 5 * location["checkins"] inventory["money"] += extortion_value db.save_inventory(inventory) location["last_extort_time"] = datetime.datetime.now() db.save_location(location) self.redirect("/location/%s" % location_id)
def post(self, location_id): """ This handler is a bit more complex - handles the actions that can be taken at this location. For instance: - extort - take control - leave guards - battle """ user_id = self.get_secure_cookie("user_id") user = db.get_user(user_id) location = db.get_or_create_location_by_id(location_id) action = self.get_argument("action") if action == "take-control": # TODO: Do an additional check to make sure this isn't owned # by someone else and there are no guards here. location["owner"] = {"name": user["name"], "user_id": user["id"]} db.save_location(location) elif action == "extort" and location["owner"]["user_id"] == user["id"]: inventory = db.get_inventory_for_user(user) extortion_value = 5 * location["checkins"] inventory["money"] += extortion_value db.save_inventory(inventory) location["last_extort_time"] = datetime.datetime.now() db.save_location(location) self.redirect("/location/%s" % location_id)