def favoriteBlunder():
    logger.info("API Handler blunder/favorite")

    if session.isAnonymous():
        return {
            'status': 'error',
            'message': 'Favorites allowed only for authorized user'
        }

    try:
        blunder_id = request.json['blunder_id']
    except Exception:
        return {'status': 'error', 'message': 'Blunder id required'}

    if not postgre.blunder.favoriteBlunder(session.userID(), blunder_id):
        return {'status': 'error', 'message': "Can't favorite blunder"}

    result = postgre.blunder.getBlunderInfoById(session.userID(), blunder_id)
    if result is None:
        return {
            'status': 'error',
            'message': 'Invalid blunder id',
        }

    return {'status': 'ok', 'data': result}
Exemple #2
0
def voteBlunderComment():
    logger.info("API Handler comment/vote")

    if session.isAnonymous():
        return {
            'status': 'error',
            'message': 'Voting allowed only for authorized user'
        }

    try:
        blunder_id = request.json['blunder_id']
        comment_id = request.json['comment_id']
        vote = request.json['vote']
    except Exception:
        return {
            'status': 'error',
            'message': 'Blunder id, comment id and vote required'
        }

    if postgre.blunder.blunderCommentAuthor(comment_id) == session.userID():
        return {'status': 'error', 'message': "Can't vote for own comments"}

    if not postgre.blunder.voteBlunderComment(session.userID(), comment_id,
                                              vote):
        return {'status': 'error', 'message': "Can't vote comment"}

    result = postgre.blunder.getBlunderInfoById(session.userID(), blunder_id)
    if result is None:
        return {
            'status': 'error',
            'message': 'Invalid blunder id',
        }

    return {'status': 'ok', 'data': result}
Exemple #3
0
def getPackInfo():
    logger.info("API Handler pack/info")

    try:
        pass
    except Exception:
        return {
            'status': 'error',
            'message': 'Unknown'
        }

    if(session.isAnonymous()):
        return {
            'status': 'error',
            'message': 'Working with packs in anonymous mode is not supported'
        }

    packs, unlocked = postgre.pack.getPacks(session.userID())
    packs = [ postgre.pack.idToHashId(pack_id) for pack_id in packs]

    return {
        'status':'ok',
        'data': {
            'packs': packs,
            'unlocked': unlocked
        }
    }
def validateExploreBlunder(blunder_id, user_line, spent_time):  #pylint: disable=unused-argument
    if session.isAnonymous():
        return {'status': 'ok'}

    # In explore mode, just remove blunder from task list
    # It is also ok, that there is no blunder to delete
    postgre.blunder.closeBlunderTask(session.userID(), blunder_id,
                                     const.tasks.EXPLORE)

    return {'status': 'ok'}
Exemple #5
0
def analyzeBlunder():
    logger.info("API Handler blunder/analyze")

    try:
        blunder_id = request.json['blunder_id']
        line = request.json['line']
    except Exception:
        return {
            'status': 'error',
            'message': 'blunder_id and line is required'
        }

    if (session.isAnonymous()):
        return {
            'status': 'error',
            'message': 'Analyzing in anonymous mode is not supported'
        }

    blunder = postgre.blunder.getBlunderById(blunder_id)
    if blunder is None:
        return {'status': 'error', 'message': "Invalid blunder id"}

    blunder_fen = blunder['fen_before']
    blunder_move = blunder['blunder_move']
    forced_line = blunder['forced_line']

    if chess.mismatchCheck(blunder_move, forced_line, line):
        return {
            'status': 'error',
            'message': "Remote database has been changed"
        }

    data = chess.boardsToAnalyze(blunder_fen, blunder_move, forced_line, line)

    data = searchForPreanalyzed(blunder_id, data)

    # No need to start engine process if all needed data known
    if not isAllCalculated(data):
        data = calcualteWithEngine(blunder_id, blunder_fen, data)

    return {
        'status': 'ok',
        'data': {
            'variations': [{
                'line': element['engine']['line'],
                'score': element['engine']['score'],
                'status': element['status']
            } for element in data]
        }
    }
Exemple #6
0
def getPack():
    logger.info("API Handler pack/get")

    try:
        hash_id = request.json['pack_id']
    except Exception:
        return {'status': 'error', 'message': 'Pack id required'}

    pack_id = postgre.pack.hashIdToId(hash_id)
    if (pack_id is None):
        return {
            'status': 'error',
            'message': 'Pack with given hash id not found'
        }

    if (session.isAnonymous()):
        return {
            'status': 'error',
            'message': 'Working with packs in anonymous mode is not supported'
        }

    pack_info = postgre.pack.getPackInfo(pack_id)
    if pack_info is None:
        return {'status': 'error', 'message': 'This pack not exist'}
    caption = pack_info['caption']
    body = pack_info['body']

    blunder_ids = postgre.pack.getAssignedBlunders(session.userID(), pack_id)
    if blunder_ids is None:
        return {'status': 'error', 'message': 'This pack not assigned to user'}

    blunders = [{
        'get':
        utils.jsonifyBlunder(postgre.blunder.getBlunderById(blunder_id)),
        'info':
        postgre.blunder.getBlunderInfoById(session.userID(), blunder_id)
    } for blunder_id in blunder_ids]

    return {
        'status': 'ok',
        'data': {
            'caption': caption,
            'body': body,
            'pack_id': postgre.pack.idToHashId(pack_id),
            'blunders': blunders
        }
    }
Exemple #7
0
def commentBlunder():
    logger.info("API Handler comment/send")

    if session.isAnonymous():
        return {
            'status': 'error',
            'message': 'Commenting allowed only for authorized user'
        }

    try:
        blunder_id = request.json['blunder_id']
        comment_id = request.json['comment_id']
        user_input = request.json['user_input']
    except Exception:
        return {
            'status': 'error',
            'message': 'Blunder id, input and comment_id required'
        }

    # Frontend uses 0 to say comment is the root comment,
    # but backend uses None for that
    if comment_id == 0:
        comment_id = None

    if len(user_input) > const.comment.MAX_SIZE:
        return {
            'status': 'error',
            'message':
            'Input length can\'t be greater than %d' % MAX_MESSAGE_SIZE
        }

    if not postgre.blunder.commentBlunder(session.userID(), blunder_id,
                                          comment_id, user_input):
        return {'status': 'error', 'message': "Can't comment blunder"}

    result = postgre.blunder.getBlunderInfoById(session.userID(), blunder_id)
    if result is None:
        return {
            'status': 'error',
            'message': 'Invalid blunder id',
        }

    return {'status': 'ok', 'data': result}
Exemple #8
0
def getNewPack():
    logger.info("API Handler pack/new")

    try:
        pack_type_name = request.json['type_name']
        pack_type_args_user = request.json[
            'args'] if 'args' in request.json else {}
    except Exception:
        return {
            'status': 'error',
            'message': 'Type name required for pack type'
        }

    if (session.isAnonymous()):
        return {
            'status': 'error',
            'message': 'Working with packs in anonymous mode is not supported'
        }

    return packSelector(pack_type_name, pack_type_args_user)
def validatePackBlunder(blunder_id, user_line, spent_time):
    # In pack mode, anonymous user can't validate, this is error
    if session.isAnonymous():
        return {
            'status': 'error',
            'message': "Working with packs in anonymous mode is not supported"
        }

    result = validate(blunder_id, user_line, spent_time, const.tasks.PACK)

    # This is optional field, client should not rely on. Gets updated info of the position
    # so user can update it without need of sending separate info request
    result['data'].update({
        'info':
        postgre.blunder.getBlunderInfoById(session.userID(), blunder_id)
    })

    # Remove user asociated packs which are complatelly solved
    postgre.pack.gcHistoryPacks(session.userID())

    return result
Exemple #10
0
def removePack():
    logger.info("API Handler pack/remove")

    try:
        hash_id = request.json['pack_id']
    except Exception:
        return {'status': 'error', 'message': 'Pack id required'}

    pack_id = postgre.pack.hashIdToId(hash_id)
    if (pack_id is None):
        return {
            'status': 'error',
            'message': 'Pack with given hash id not found'
        }

    if (session.isAnonymous()):
        return {
            'status': 'error',
            'message': 'Working with packs in anonymous mode is not supported'
        }

    postgre.pack.removePack(session.userID(), pack_id, False)

    return {'status': 'ok'}
def validateRatedBlunder(blunder_id, user_line, spent_time):
    # In rated mode, anonymous users have nothing to validate, this is correct situation
    if session.isAnonymous():
        return {'status': 'ok'}

    return validate(blunder_id, user_line, spent_time, const.tasks.RATED)