Exemplo n.º 1
0
async def hint_bird(request: Request):
    logger.info("endpoint: hint bird")

    session_id = get_session_id(request)
    database.zincrby(f"daily.web:{date()}", 1, "hint")

    currentBird = database.hget(f"web.session:{session_id}",
                                "bird").decode("utf-8")
    if currentBird != "":  # check if there is bird
        return {"hint": currentBird[0]}

    logger.info("bird is blank")
    raise HTTPException(status_code=404, detail="Bird is blank")
Exemplo n.º 2
0
async def skip_bird(request: Request):
    logger.info("endpoint: skip bird")

    session_id = get_session_id(request)
    user_id = int(database.hget(f"web.session:{session_id}", "user_id"))
    database.zincrby(f"daily.web:{date()}", 1, "skip")

    currentBird = database.hget(f"web.session:{session_id}",
                                "bird").decode("utf-8")
    if currentBird != "":  # check if there is bird
        database.hset(f"web.session:{session_id}", "bird", "")
        database.hset(f"web.session:{session_id}", "answered", "1")
        if user_id != 0:
            streak_increment(user_id, None)  # reset streak
        scibird = await get_sciname(currentBird)
        url = get_wiki_url(currentBird)  # sends wiki page
    else:
        logger.info("bird is blank")
        raise HTTPException(status_code=404, detail="Bird is blank")
    return {"answer": currentBird, "sciname": scibird, "wiki": url}
Exemplo n.º 3
0
def increment_bird_frequency(bird, user_id):
    bird_setup(user_id, bird)
    database.zincrby("frequency.bird:global", 1, string.capwords(bird))
Exemplo n.º 4
0
async def check_bird(request: Request, guess: str):
    logger.info("endpoint: check bird")

    session_id = get_session_id(request)
    user_id = int(database.hget(f"web.session:{session_id}", "user_id"))

    currentBird = database.hget(f"web.session:{session_id}",
                                "bird").decode("utf-8")
    if currentBird == "":  # no bird
        logger.info("bird is blank")
        raise HTTPException(status_code=404, detail="Bird is blank")
    if guess == "":
        logger.info("empty guess")
        raise HTTPException(status_code=422, detail="empty guess")

    # if there is a bird, it checks answer
    sciBird = (await get_sciname(currentBird)).lower().replace("-", " ")
    guess = guess.lower().replace("-", " ")
    currentBird = currentBird.lower().replace("-", " ")
    alpha_code = alpha_codes.get(string.capwords(currentBird), "")
    logger.info("currentBird: " + currentBird)
    logger.info("args: " + guess)

    database.zincrby(f"daily.web:{date()}", 1, "check")
    if user_id != 0:
        bird_setup(user_id, currentBird)

    accepted_answers = [currentBird, sciBird]
    if currentBird == "screech owl":
        accepted_answers += screech_owls
        accepted_answers += sci_screech_owls

    if (better_spellcheck(guess, accepted_answers,
                          birdListMaster + sciListMaster)
            or guess.upper() == alpha_code):
        logger.info("correct")

        database.hset(f"web.session:{session_id}", "bird", "")
        database.hset(f"web.session:{session_id}", "answered", "1")

        tempScore = int(database.hget(f"web.session:{session_id}",
                                      "tempScore"))
        if user_id != 0:
            database.zincrby(f"daily.webscore:{date()}", 1, user_id)
            score_increment(user_id, 1)
            streak_increment(user_id, 1)
        # elif tempScore >= 10:
        #     logger.info("trial maxed")
        #     raise HTTPException(status_code=403, detail="Sign in to continue")
        else:
            database.hset(f"web.session:{session_id}", "tempScore",
                          str(tempScore + 1))

        url = get_wiki_url(currentBird)
        return {
            "guess": guess,
            "answer": currentBird,
            "sciname": sciBird,
            "status": "correct",
            "wiki": url,
        }

    logger.info("incorrect")
    database.hset(f"web.session:{session_id}", "bird", "")
    database.hset(f"web.session:{session_id}", "answered", "1")
    database.zincrby("incorrect:global", 1, currentBird)

    if user_id != 0:
        incorrect_increment(user_id, currentBird, 1)
        streak_increment(user_id, None)  # reset streak

    url = get_wiki_url(currentBird)
    return {
        "guess": guess,
        "answer": currentBird,
        "sciname": sciBird,
        "status": "incorrect",
        "wiki": url,
    }