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)}
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)}
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)}
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)}
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)}
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)}