Exemplo n.º 1
0
    def create_session(cls, slug, mode=None):
        """
        Returns application settings for local.
        """
        game = get_game_by_slug(slug)
        if not game:
            response.status_int = 404
            return {'ok': False, 'msg': 'Game does not exist: %s' % slug}

        if 'canvas' == mode:
            prefix = 'play/%s/' % slug
        else:
            prefix = ''
        mapping_table = 'mapping_table.json'
        if game:
            mapping_table = str(game.mapping_table)

        user = get_current_user()
        game_session_list = GameSessionList.get_instance()
        game_session_id = game_session_list.create_session(user, game)

        StoreList.reset()
        DataShareList.reset()
        GameNotificationKeysList.reset()

        return {
            'ok': True,
            'mappingTable':
            {
                'mappingTableURL': prefix + mapping_table,
                'mappingTablePrefix': prefix + 'staticmax/',
                'assetPrefix': 'missing/'
            },
            'gameSessionId': game_session_id
        }
Exemplo n.º 2
0
    def validate_yaml(self):
        result = {}

        try:
            badges = GameBadges(self)
        except BadgesUnsupportedException:
            pass
        except ApiException as e:
            result['Badges'] = [('badges.yaml', {
                'errors': ['%s' % e]
            })]
        else:
            issues = badges.validate()
            if issues:
                result['Badges'] = issues

        try:
            leaderboards = LeaderboardsList.load(self)
        except LeaderboardsUnsupported:
            pass
        except LeaderboardError as e:
            result['Leaderboards'] = [('leaderboards.yaml', {
                'errors': ['incorrect format: %s' % e]
            })]
        except KeyError as e:
            result['Leaderboards'] = [('leaderboards.yaml', {
                'errors': ['key %s could not be found.' % e]
            })]
        except ValidationException as e:
            result['Leaderboards'] = e.issues
        else:
            issues = leaderboards.issues
            if issues:
                result['Leaderboards'] = leaderboards.issues

        try:
            store = StoreList.load(self)
        except StoreUnsupported:
            pass
        except StoreError as e:
            result['Store'] = [('store.yaml', {
                'errors': ['incorrect format: %s' % e]
            })]
        except ValidationException as e:
            result['Store'] = e.issues
        else:
            issues = store.issues
            if issues:
                result['Store'] = store.issues

        try:
            for v in result.itervalues():
                for item in v:
                    if item[1]['errors']:
                        return (result, True)
        except (KeyError, IndexError):
            LOG.error('badly formatted result structure when checking YAML issues')
            return (result, True)
        return (result, False)
Exemplo n.º 3
0
    def create_session(cls, slug, mode=None):
        """
        Returns application settings for local.
        """
        game = get_game_by_slug(slug)
        if not game:
            response.status_int = 404
            return {'ok': False, 'msg': 'Game does not exist: %s' % slug}

        if 'canvas' == mode:
            prefix = 'play/%s/' % slug
        else:
            prefix = ''
        mapping_table = 'mapping_table.json'
        if game:
            mapping_table = str(game.mapping_table)

        user = get_current_user()

        game_session_list = GameSessionList.get_instance()

        if asbool(request.params.get('closeExistingSessions', False)):
            game_session_list.remove_game_sessions(user, game)

        game_session = game_session_list.create_session(user, game)

        # Reset API's (so YAML files are reloaded on a page refresh)
        StoreList.reset()
        DataShareList.reset()
        GameNotificationKeysList.reset()

        return {
            'ok': True,
            'mappingTable':
            {
                'mappingTableURL': prefix + mapping_table,
                'mappingTablePrefix': prefix + 'staticmax/',
                'assetPrefix': 'missing/'
            },
            'gameSessionId': game_session.gamesession_id
        }
Exemplo n.º 4
0
    def create_session(cls, slug, mode=None):
        """
        Returns application settings for local.
        """
        game = get_game_by_slug(slug)
        if not game:
            response.status_int = 404
            return {'ok': False, 'msg': 'Game does not exist: %s' % slug}

        if 'canvas' == mode:
            prefix = 'play/%s/' % slug
        else:
            prefix = ''
        mapping_table = 'mapping_table.json'
        if game:
            mapping_table = str(game.mapping_table)

        user = get_current_user()

        game_session_list = GameSessionList.get_instance()

        if (asbool(request.params.get('closeExistingSessions', False))):
            game_session_list.remove_game_sessions(user, game)

        game_session = game_session_list.create_session(user, game)

        # Reset API's (so YAML files are reloaded on a page refresh)
        StoreList.reset()
        DataShareList.reset()
        GameNotificationKeysList.reset()

        return {
            'ok': True,
            'mappingTable': {
                'mappingTableURL': prefix + mapping_table,
                'mappingTablePrefix': prefix + 'staticmax/',
                'assetPrefix': 'missing/'
            },
            'gameSessionId': game_session.gamesession_id
        }
Exemplo n.º 5
0
    def consume_user_items(cls, params=None):
        session = cls._get_gamesession(params)

        try:
            def get_param(param):
                value = params[param]
                if value is None:
                    raise KeyError(param)
                return value

            consume_item = get_param('key')
            consume_amount = get_param('consume')
            token = get_param('token')
            gamesession_id = get_param('gameSessionId')
        except KeyError as e:
            response.status_int = 400
            return {'ok': False, 'msg': 'Missing parameter %s' % str(e)}

        try:
            game = session.game
            user = session.user

            store = StoreList.get(game)

            transactions = UserTransactionsList.get(user)

            # check if the transaction has already been attempted
            consume_transaction = transactions.get_consume_transaction(gamesession_id, token)

            new_consume_transaction = ConsumeTransaction(user, game, consume_item,
                                                         consume_amount, gamesession_id, token)
            if consume_transaction is None:
                consume_transaction = new_consume_transaction
            elif not consume_transaction.check_match(new_consume_transaction):
                response.status_int = 400
                return {'ok': False, 'msg': 'Reused session token'}

            if not consume_transaction.consumed:
                consume_transaction.consume()

            store_user = store.get_store_user(user)
            return {'ok': True, 'data': {'consumed': consume_transaction.consumed,
                                         'userItems': store_user.get_items()}}

        except StoreUnsupported:
            return {'ok': True, 'data': {'compareAndSet': False, 'userItems': {}}}
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 6
0
    def read_meta(cls, slug):
        game = get_game_by_slug(slug)
        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            return {'ok': True, 'data': {'items': store.read_meta(), 'resources': store.read_resources()}}

        except StoreUnsupported:
            return {'ok': True, 'data': {'items': {}, 'resources': {}}}
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 7
0
    def remove_all(cls, slug):
        user = get_current_user()
        game = get_game_by_slug(slug)

        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            store.get_store_user(user).remove_items()
            return {'ok': True}

        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 8
0
    def remove_all(cls, slug):
        user = get_current_user()
        game = get_game_by_slug(slug)

        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            store.get_store_user(user).remove_items()
            return {'ok': True}

        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 9
0
    def read_user_items(cls, slug):
        user = get_current_user()
        game = get_game_by_slug(slug)
        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            store_user = store.get_store_user(user)
            return {'ok': True, 'data': {'userItems': store_user.get_items()}}

        except StoreUnsupported:
            return {'ok': True, 'data': {'userItems': {}}}
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 10
0
    def read_user_items(cls, slug):
        user = get_current_user()
        game = get_game_by_slug(slug)
        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            store_user = store.get_store_user(user)
            return {'ok': True, 'data': {'userItems': store_user.get_items()}}

        except StoreUnsupported:
            return {'ok': True, 'data': {'userItems': {}}}
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 11
0
    def read_meta(cls, slug):
        game = get_game_by_slug(slug)
        if game is None:
            response.status_int = 404
            return {'ok': False, 'msg': 'No game with slug %s' % slug}

        try:
            store = StoreList.get(game)
            return {
                'ok': True,
                'data': {
                    'items': store.read_meta(),
                    'resources': store.read_resources()
                }
            }

        except StoreUnsupported:
            return {'ok': True, 'data': {'items': {}, 'resources': {}}}
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}
Exemplo n.º 12
0
    def validate_yaml(self):
        result = {}

        try:
            badges = GameBadges(self)
        except BadgesUnsupportedException:
            pass
        except ApiException as e:
            result['Badges'] = [('badges.yaml', {'errors': ['%s' % e]})]
        else:
            issues = badges.validate()
            if issues:
                result['Badges'] = issues

        try:
            notification_keys = GameNotificationKeysList.get(self)
        except GameNotificationsUnsupportedException:
            pass
        except ApiException as e:
            result['Notifications'] = [('notifications.yaml', {
                'errors': ['%s' % e]
            })]
        else:
            issues = notification_keys.validate()
            if issues:
                result['Notifications'] = issues

        try:
            leaderboards = LeaderboardsList.load(self)
        except LeaderboardsUnsupported:
            pass
        except LeaderboardError as e:
            result['Leaderboards'] = [('leaderboards.yaml', {
                'errors': ['incorrect format: %s' % e]
            })]
        except KeyError as e:
            result['Leaderboards'] = [('leaderboards.yaml', {
                'errors': ['key %s could not be found.' % e]
            })]
        except ValidationException as e:
            result['Leaderboards'] = e.issues
        else:
            issues = leaderboards.issues
            if issues:
                result['Leaderboards'] = leaderboards.issues

        try:
            store = StoreList.load(self)
        except StoreUnsupported:
            pass
        except StoreError as e:
            result['Store'] = [('store.yaml', {
                'errors': ['incorrect format: %s' % e]
            })]
        except ValidationException as e:
            result['Store'] = e.issues
        else:
            issues = store.issues
            if issues:
                result['Store'] = store.issues

        try:
            for v in result.itervalues():
                for item in v:
                    if item[1]['errors']:
                        return (result, True)
        except (KeyError, IndexError):
            LOG.error(
                'badly formatted result structure when checking YAML issues')
            return (result, True)
        return (result, False)
Exemplo n.º 13
0
    def consume_user_items(cls, params=None):
        session = cls._get_gamesession(params)

        try:

            def get_param(param):
                value = params[param]
                if value is None:
                    raise KeyError(param)
                return value

            consume_item = get_param('key')
            consume_amount = get_param('consume')
            token = get_param('token')
            gamesession_id = get_param('gameSessionId')
        except KeyError as e:
            response.status_int = 400
            return {'ok': False, 'msg': 'Missing parameter %s' % str(e)}

        try:
            game = session.game
            user = session.user

            store = StoreList.get(game)

            transactions = UserTransactionsList.get(user)

            # check if the transaction has already been attempted
            consume_transaction = transactions.get_consume_transaction(
                gamesession_id, token)

            new_consume_transaction = ConsumeTransaction(
                user, game, consume_item, consume_amount, gamesession_id,
                token)
            if consume_transaction is None:
                consume_transaction = new_consume_transaction
            elif not consume_transaction.check_match(new_consume_transaction):
                response.status_int = 400
                return {'ok': False, 'msg': 'Reused session token'}

            if not consume_transaction.consumed:
                consume_transaction.consume()

            store_user = store.get_store_user(user)
            return {
                'ok': True,
                'data': {
                    'consumed': consume_transaction.consumed,
                    'userItems': store_user.get_items()
                }
            }

        except StoreUnsupported:
            return {
                'ok': True,
                'data': {
                    'compareAndSet': False,
                    'userItems': {}
                }
            }
        except ValidationException as e:
            response.status_int = 400
            return {'ok': False, 'msg': str(e)}
        except StoreError as e:
            response.status_int = e.response_code
            return {'ok': False, 'msg': str(e)}