def _create_match():
    data = request.get_json()

    if 'access_token' not in data or not is_valid_token(data['access_token']):
        abort(400)

    username = get_username_from_token(data['access_token'])

    deck_id = None
    if 'deck_id' not in data or not data['deck_id']:
        return jsonify({
            'success': False,
            'errors': {
                'message': 'no deck_id provided'
            }
        })
    else:
        deck_id = get_deck_id(username, data['deck_id'])

    if not deck_id:
        return jsonify({
            'success': False,
            'errors': {
                'message': 'invalid deck_id'
            }
        })

    # todo: setup player's game data: hand, drawpile, discardpile, etc.
    deck = Deck(username, deck_id)
    draw_pile = CardPile(deck)
    draw_pile.shuffle()
    hand = CardPile()
    discard_pile = CardPile()

    for i in range(0, 5):
        hand.push(draw_pile.pop())

    next = 0
    try:
        current = int(matchDatabase.get('current').value)
    except Exception, e:
        matchDatabase.set('current', str(0))
        current = int(matchDatabase.get('current').value)
def _get_match_info(match_id):
    try:
        token = request.args.get('access_token')
        username = get_username_from_token(token)
        if is_valid_token(token):
            try:
                result = matchDatabase.get(get_id_from_match(match_id)).value
                for k in result['data']['data'][username]:
                    result['data']['data'][k] = result['data']['data'][
                        username][k]
                return jsonify({'success': True, 'results': {'match': result}})
            except Exception, e:
                return jsonify({
                    'success': False,
                    'errors': {
                        'message': str(e)
                    }
                })
    except Exception, e:
        return jsonify({'success': False, 'errors': {'message': str(e)}})
def _get_deck_info(player_id, deck_id):
    token = request.args.get('access_token')
    if token is not None:
        if is_valid_token(token):
            try:
                pass
                # deck = deckDatabase.get(deck_id).value
            except Exception, e:
                return jsonify({
                    'success': False,
                    'errors': {
                        'message': str(e)
                    }
                })
        else:
            return jsonify({
                'success': False,
                'errors': {
                    'message': 'invalid access token'
                }
            })
def _users_me():
    token = request.args.get('access_token')

    if token is not None:
        if is_valid_token(token):
            username = get_username_from_token(token)
            try:
                result = userDatabase.get(username)
                return jsonify({
                    'success': True,
                    'results': {
                        'data': result.value
                    }
                })
            except Exception, e:
                return jsonify({
                    'success': False,
                    'errors': {
                        'message': 'database error: ' + str(e)
                    }
                })
def _take_turn(match_id):
    try:
        url = GAMEHOST_ENDPOINT + match_id + '/turn'
        data = request.get_json()

        if len(data) < 2:
            return jsonify({
                'success': False,
                'errors': {
                    'message': 'missing access token'
                }
            })

        access_token = data['access_token']

        if not is_valid_token(access_token):
            return jsonify({
                'success': False,
                'errors': {
                    'message': 'invalid accesss token'
                }
            })

        match = matchDatabase.get(get_id_from_match(match_id)).value
        if not is_valid_match(match):
            return jsonify({
                'success': False,
                'errors': {
                    'message':
                    'cannot take turn. match ' + match_id +
                    ' has been cancelled.'
                }
            })

        resp = MY_POST(url, data)

        return resp
    except Exception, e:
        return jsonify({'success': False, 'errors': {'message': str(e)}})
def _cancel_match(match_id):
    data = request.get_json()
    if len(data) < 1:
        return jsonify({
            'success': False,
            'errors': {
                'message': 'missing access_token'
            }
        })

    access_token = data['access_token']

    if not is_valid_token(access_token):
        return jsonify({
            'success': False,
            'errors': {
                'message': 'invalid token'
            }
        })

    match = matchDatabase.get(get_id_from_match(match_id)).value

    if not is_valid_match(match):
        return jsonify({
            'success': False,
            'errors': {
                'message':
                'cannot take turn. match ' + match_id + ' has status ' +
                match['status']
            }
        })

    try:
        match = matchDatabase.get(get_id_from_match(match_id)).value
        match['status'] = MATCH_CANCELLED

    except Exception, e:
        return jsonify({'success': False, 'errors': {'message': str(e)}})
def _list_matches():
    try:
        token = request.args.get('access_token')
        if is_valid_token(token):
            results = []
            username = get_username_from_token(token)

            i = 0
            try:
                current = int(matchDatabase.get('current').value)
            except Exception, e:
                current = 0

            while i <= current:
                try:
                    match = matchDatabase.get(str(i)).value
                    participants = match['participants']
                    for p in participants:
                        if p['player']['participantId'] == username:
                            results.append(match)
                except Exception, e:
                    pass
                i += 1

            try:
                json_result = jsonify({
                    'success': True,
                    'results': {
                        'matches': results
                    }
                })
            except Exception, e:
                return jsonify({
                    'success': False,
                    'errors': {
                        'message': str(e)
                    }
                })
def _list_decks(player_id):
    token = request.args.get('access_token')
    if token is not None:
        if is_valid_token(token):
            try:
                username = get_username_from_token(token)
                decks = deckDatabase.get(username).value
                i = 0
                return jsonify({'success': True, 'results': decks})
            except Exception, e:
                return jsonify({
                    'success': False,
                    'errors': {
                        'message': str(e)
                    }
                })
        else:
            return jsonify({
                'success': False,
                'errors': {
                    'message': 'invalid access token'
                }
            })