def get_game(): """get a all games or filter with query param filter. filters supported: active: games which have not winner. Returns: dict: list of games. """ cursor = int(request.args.get('cursor', 1)) filter_selected = request.args.get('filter') if filter_selected == 'active': game_query = Game.all().filter('winner', None) else: filter_selected = None game_query = Game.all() games = game_query.fetch(offset=PAGE_LIMIT * (cursor - 1), limit=PAGE_LIMIT) if not games: error_response = { 'errors': { 'msg': 'No games found.' }, } return (jsonify(error_response), 404) links = {'self': request.url} if cursor > 1: links['previous'] = '{base}?cursor={cursor}'.format( base=request.base_url, cursor=cursor - 1) if filter_selected is not None: links['previous'] = '{previous}&filter={filter}'.format( previous=links['previous'], filter=filter_selected) if len(games) == PAGE_LIMIT: links['next'] = '{base}?cursor={cursor}'.format(base=request.base_url, cursor=cursor + 1) if filter_selected is not None: links['next'] = '{next}&filter={filter}'.format( next=links['next'], filter=filter_selected) response = { 'data': [game.encode() for game in games], 'links': links, 'meta': { 'cursor': cursor + 1 } } return (jsonify(response), 200)
def get_messages(game_id): """returns all the messages which belong to a given game. Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) if game.game_messages is None: response = {'data': []} return (jsonify(response), 200) messages = game.game_messages.order('date') response = {'data': [message.encode() for message in messages]} return (jsonify(response), 200)
def get_snapshots(game_id): """returns all the snapshot of the game. Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) if game.treasures is None: error_response = { 'errors': { 'msg': 'The game not have any treasure.' }, } return (jsonify(error_response), 404) snapshots = [] for treasures in game.treasures: for snapshot in treasures.snapshots: snapshots.append(snapshot.encode()) response = {'data': snapshots} return (jsonify(response), 200)
def delete(game_id): """delete the game with the given id_game. Args: game_id (str): the game identifier. Returns: dict: contains errors if needed. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) for treasure in game.treasures: for snapshot in treasure.snapshots: snapshot.delete() treasure.delete() for player in game.players: user = User.get_by_key_name(player) user.games.remove(game.game_id) user.put() game.zone.delete() game.delete() return (jsonify({}), 200)
def get_game_treasures(user_email, game_id): """returns all the treasures of the user in the given game. Args: user_email (str): the game identifier. game_id (str): the game identifier. Returns: dict: the endpoint response. """ user = User.get_by_key_name(user_email) if user is None: error_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(error_response), 404) game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) if game.game_id not in user.games: error_response = { 'errors': { 'msg': 'User not is in game' }, } return (jsonify(error_response), 404) user_snapshots = user.user_snapshots treasures_id = [treasure.treasure_id for treasure in game.treasures] snapshots = [] for snapshot in user_snapshots: if snapshot.treasure.treasure_id in treasures_id: snapshots.append(snapshot.encode()) response = { 'data': { 'treasures': [treasure.encode() for treasure in game.treasures], 'snapshots': snapshots } } return (jsonify(response), 200)
def remove_player(game_id, user_email): """remove a player into a game. Args: game_id (str): the game identifier. user_email (str): the user identifier. Returns: dict: the endpoint response. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) user = User.get_by_key_name(user_email) if user is None: error_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(error_response), 404) if user.email not in game.players: error_response = { 'errors': { 'msg': 'User not is in game' }, } return (jsonify(error_response), 404) game.players.remove(user.email) game.put() user.games.remove(game.game_id) user.put() for snapchot in user.user_snapshots: snapchot.delete() response = {'data': game.encode()} return (jsonify(response), 200)
def get_map_image(game_id): """Get map image from the given game. Args: game_id (str): the game identifier. Returns: dict: the endpoint respondes. """ game = Game.get_by_key_name(game_id) if not game: error_response = { 'errors': { 'msg': 'User is in game' }, } return (jsonify(error_response), 404) return Response(game.map_blob, mimetype='image/jpg')
def upload_map_image(game_id): """Upload map image to the given game. Args: game_id (str): the game identifier. Returns: dict: the endpoint respondes. """ game = Game.get_by_key_name(game_id) if not game: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) game.set_map_blob(request.files.get('file')) return (jsonify(game.encode()), 200)
def get(game_id): """get the game with the given id_game. Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) response = {'data': game.encode()} return (jsonify(response), 200)
def search_game(game_id): """Search a game which starts with the given game_id. Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ cursor = int(request.args.get('cursor', 1)) game_query = (Game.all().filter('game_id >=', game_id).filter( 'game_id <', game_id + u'\uFFFD').fetch(offset=PAGE_LIMIT * (cursor - 1), limit=PAGE_LIMIT)) if not game_query: error_response = { 'errors': { 'msg': 'No games found.' }, } return (jsonify(error_response), 404) links = {'self': request.url} if cursor > 1: links['previous'] = '{base}?cursor={cursor}'.format( base=request.base_url, cursor=cursor - 1) if len(game_query) == PAGE_LIMIT: links['next'] = '{base}?cursor={cursor}'.format(base=request.base_url, cursor=cursor + 1) response = { 'data': [game.encode() for game in game_query], 'links': links, 'meta': { 'cursor': cursor + 1 } } return (jsonify(response), 200)
def get_games(user_email): """returns all the games which user is playing. Args: user_email (str): the user identifier. Returns: dict: the endpoint response. """ user = User.get_by_key_name(user_email) if user is None: error_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(error_response), 404) games = [Game.get_by_key_name(game_id) for game_id in user.games] response = {'data': [game.encode() for game in games]} return (jsonify(response), 200)
def get_players(game_id): """returns all the players which belong to the given game. Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) players = [User.get_by_key_name(email) for email in game.players] response = {'data': [player.encode() for player in players]} return (jsonify(response), 200)
def add_player(game_id): """Enroll a player into a game. json expected: { "email": "string" } Args: game_id (str): the game identifier. Returns: dict: the endpoint response. """ payload = request.get_json() try: jsonschema.validate(payload, game_schemas.add_user_schema) except jsonschema.ValidationError as e: error_response = { 'errors': { 'msg': str(e) }, } return (jsonify(error_response), 400) game = Game.get_by_key_name(game_id) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) user = User.get_by_key_name(payload['email']) if user is None: error_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(error_response), 404) if user.email in game.players: error_response = { 'errors': { 'msg': 'User is in game' }, } return (jsonify(error_response), 404) game.players.append(user.email) game.put() user.games.append(game.game_id) user.put() response = {'data': game.encode()} tweets.publish('{email} se ha unido al juego {game}'.format( email=user.email, game=game.game_id)) return (jsonify(response), 200)
def create(): """create a game. json expected: { "title": string, "description": string, "owner": string, "zone": geoJSON } Returns: dict: the created game. """ payload = request.get_json() try: jsonschema.validate(payload, game_schemas.create_game_schema) except jsonschema.ValidationError as e: error_response = { 'errors': { 'msg': str(e) }, } return (jsonify(error_response), 400) user = User.get_by_key_name(payload['owner']) if user is None: errors_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(errors_response), 400) geojson_transform = decode_geojson(payload['zone']) zone_id = str(uuid.uuid4()) zone = Zone(key_name=zone_id, zone_id=zone_id, payload=json.dumps(geojson_transform['zone'])) zone.put() game_id = str(uuid.uuid4()) new_game = Game(key_name=game_id, game_id=game_id, title=payload['title'], description=payload['description'], zone=zone, owner=user) new_game.put() user.games.append(new_game.game_id) user.put() for treasure_json in geojson_transform['treasures']: treasure_id = str(uuid.uuid4()) treasure = Treasure(key_name=treasure_id, treasure_id=treasure_id, payload=json.dumps(treasure_json), game=new_game) treasure.put() response = {'data': new_game.encode()} tweets.publish( 'Nuevo juego creado {id}, unete ya y que empiecen los juegos del hambre' .format(id=game_id)) return (jsonify(response), 200)
def update(): """update the game with the given game_id. json expected: { "game_id": "string", "title": "string", "description": "string", "winner": "string", "close": "bool" } Returns: dict: the updated game. """ payload = request.get_json() try: jsonschema.validate(payload, game_schemas.update_game_schema) except jsonschema.ValidationError as e: error_response = { 'errors': { 'msg': str(e) }, } return (jsonify(error_response), 400) game = Game.get_by_key_name(payload.get('game_id')) if game is None: error_response = { 'errors': { 'msg': 'Game not found.' }, } return (jsonify(error_response), 404) title = payload.get('title') description = payload.get('description') winner_mail = payload.get('winner') if winner_mail is not None: winner = User.get_by_key_name(winner_mail) if winner is not None: game.winner = winner tweets.publish( 'Ehnorabuena {email} por ganar el juego {game}'.format( email=winner_mail, game=game.game_id)) else: error_response = { 'errors': { 'msg': 'User not found.' }, } return (jsonify(error_response), 404) if title is not None: game.title = title if description is not None: game.description = description if payload.get('closed'): tweets.publish( 'El juego {game} se ha cerrado'.format(game=game.game_id)) game.closed = True game.put() response = {'data': game.encode()} return (jsonify(response), 200)