예제 #1
0
def check():
    app.logger.debug("Got a JSON request")
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    rslt = {"re": 0}

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        rslt["re"] = 1
        if len(matches) >= flask.session["target_count"]:
            rslt["re"] = 5
    elif text in matches:
        #flask.flash("You already found {}".format(text))
        rslt["re"] = 2
    elif not matched:
        #flask.flash("{} isn't in the list of words".format(text))
        rslt["re"] = 3
    elif not in_jumble:
        #flask.flash('"{}" can\'t be made from the letters {}'.format(text, jumble))
        rslt["re"] = 4
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    return flask.jsonify(result=rslt)
예제 #2
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])
    app.logger.debug(matches)

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not ((text + " ") in matches):
        # Cool, they found a new word
        matches.append(text + " ")
        flask.session["matches"] = matches
    rslt = {"in_matches": text in matches, "txt_input": matches, "matched": matched, "in_jumble": in_jumble,
            "complete": len(matches) >= flask.session["target_count"]}
    return flask.jsonify(result=rslt)
예제 #3
0
def example():
    """
    Example ajax request handler
    """

    app.logger.debug("Got a JSON request")
    jumble = flask.session["jumble"]
    text = request.args.get("text", type=str)

    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    matchd = False
    a = matched and (text not in matchedwords)

    if a:
        matchd = text not in matchedwords
        if in_jumble:
            matchedwords.append(text)

    count = 0
    for i in range(len(matchedwords)):
        if matchedwords[i] == text:
            count += 1

    print(matchd)
    print(matchedwords)

    rslt = {"key": matched and in_jumble and matchd, "already": (count > 1)}

    return flask.jsonify(result=rslt)
예제 #4
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """

    # The data we need, from the frontend seperated into a list by spaces
    textList = request.args.get("text", type=str).strip().split()

    #get current guess
    text = textList[-1]

    #Get the current jumble from the Jinja in the frontend
    jumble = flask.session["jumble"]

    #Get all of the previous matches excluding our current one
    matches = textList[:-1]

    #setup dictionary data to send to frontend
    rslt = {
        "in_jumble": LetterBag(jumble).contains(text),
        "matched": WORDS.has(text),
        "already_found": text in matches,
        "jumble": jumble,
        "guess": text,
        "success": len(matches) >= flask.session["target_count"]
    }

    # Return JSON to frontend
    return flask.jsonify(result=rslt)
예제 #5
0
def example():

    """
    Example ajax request handler
    """

    app.logger.debug("Got a JSON request")
    jumble = flask.session["jumble"]
    text = request.args.get("text",type=str )


    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    matchd = False
    a = matched and (text not in matchedwords)

    if a:
        matchd = {"foundalready": text not in matchedwords}


    
    print(text in matchedwords)
    rslt = {"key": matched and in_jumble and matchd,
            "already":   text in matchedwords}
    if a:
        matchedwords.append(text)

    print(text in matchedwords)

    return flask.jsonify(result=rslt)
예제 #6
0
def test_contains_basic_examples():
    """
    Examples from the docstring of LetterBag.contains,
    with and without auto-conversion to LetterBag
    """
    # Passing other as LetterBag
    assert LetterBag("abbc").contains(LetterBag("abc"))
    assert LetterBag("abbc").contains(LetterBag("abbc"))
    assert not LetterBag("abc").contains(LetterBag("abbc"))
    # Passing other as string
    assert LetterBag("abbc").contains("abc")
    assert LetterBag("abbc").contains("abbc")
    assert not LetterBag("abc").contains("abbc")
예제 #7
0
def success():
    return flask.render_template('success.html')

    #######################
    # Form handler.
    #   You'll need to change this to a
    #   a JSON request handler
    #######################
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    return_message = ""
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        return_message = "new_word"
    elif text in matches:
        return_message = "already_found"
    elif not matched:
        return_message = "not_found"
    elif not in_jumble:
        return_message = "not_in"
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # Choose page:  Solved enough, or keep going?
    return_success = len(matches) >= flask.session["target_count"]

    rlst = {
        "input": text,
        "jumble": jumble,
        "matches": matches,
        "matched": matched,
        "return_message": return_message,
        "return_success": return_success
    }
    return flask.jsonify(resutl=rslt)
예제 #8
0
def check():
    """
    User has entered a word in form ('#attempt')
    that should be formed from the jumble and on the
    vocab list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # JQuery initializes the text variable to whatever is entered by the user
    text = request.args.get("text", type=str)

    # The data we need from the session cookie
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Does it contain the letters of the word user entered? Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    message = ""
    if matched and in_jumble and not (text in matches):  # New word found
        matches.append(text)
        flask.session["matches"] = matches
        message = 'You found "{}"!'.format(text)
    elif text in matches:
        message = "You already found {}".format(text)
    elif not matched:
        message = '"{}" isn\'t in the list of words'.format(text)
    elif not in_jumble:
        message = '"{}" can\'t be made from the letters {}'.format(
            text, jumble)
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # Communicates with JQuery so that it can change to success.html upon matching # of required words
    finished = False
    if len(matches) >= flask.session["target_count"]:
        finished = True

    # rslt is a dict that will be turned into a JSON response using flask.jsonify()
    rslt = {
        "entry": text,
        "message": message,
        "matched": matched,
        "finished": finished,
        "found": matches
    }

    return flask.jsonify(
        result=rslt)  # Returns JSON response to browser with current result
예제 #9
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # New default values
    new_match = False
    old_match = False
    not_matched = False
    not_word = False

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        new_match = True
    elif text in matches:
        old_match = True
    elif not matched:
        not_matched = True
    elif not in_jumble:
        not_word = True
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError
    finished = len(matches) >= flask.session["target_count"]

    rslt = {
        "new_match": new_match,
        "old_match": old_match,
        "not_word": not_word,
        "not_matched": not_matched,
        "jumble": jumble,
        "finished": finished
    }
    return flask.jsonify(result=rslt)
예제 #10
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list
    app.logger.debug(text)

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # goalRem = flask.session["target_count"] - len(matches) #remaining number of words left to complete the goal

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches  #update flasks matches

    app.logger.debug("Got a JSON request")
    rslt = {
        "in_jumble": LetterBag(jumble).contains(text),
        "match": WORDS.has(text),
        "wordFound": text in matches,
        "jumble": jumble,
        "text": text,
        "success": len(matches) >= flask.session["target_count"]
    }
    return flask.jsonify(result=rslt)
예제 #11
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list
    in_matches = (text + " ") in matches
    # matches = request.args.get("results", type=str)
    app.logger.debug(matches)

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    # Respond appropriately
    if matched and in_jumble and not ((text + " ") in matches):
        # Cool, they found a new word
        matches.append(text + " ")
        flask.session["matches"] = matches
        """
    elif text in matches:
        flask.flash("You already found {}".format(text))
    elif not matched:
        flask.flash("{} isn't in the list of words".format(text))
    elif not in_jumble:
        flask.flash(
            '"{}" can\'t be made from the letters {}'.format(text, jumble))
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    if len(matches) >= flask.session["target_count"]:
       return flask.redirect(flask.url_for("success"))
    else:
    """
    rslt = {
        "in_matches": in_matches,
        "results": matches,
        "matched": matched,
        "in_jumble": in_jumble,
        "done": len(matches) >= flask.session["target_count"]
    }
    return flask.jsonify(result=rslt)
예제 #12
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches")  # Default to empty list
    count = flask.session["count"]

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        count += 1
        flask.session["matches"] = matches
        flask.session["count"] = count
        rslt = {"found_em": True}
    elif text in matches:
        flask.flash("You already found {}".format(text))
        rslt = {"found_em": False}
    elif not matched:

        flask.flash("{} isn't in the list of words".format(text))
        rslt = {"found_em": False}
    elif not in_jumble:
        flask.flash('"{}" can\'t be made from the letters {}'.format(
            text, jumble))
        rslt = {"found_em": False}
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # Choose page:  Solved enough, or keep going?
    #rslt = {"found_em": len(matches) >= flask.session["target_count"]}
    if flask.session["count"] >= flask.session["target_count"]:
        rslt = {"found_em": 'success.html'}

    return flask.jsonify(result=rslt)
예제 #13
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    rslt = {
        'matched': False,
        'in_jumble': False,
        'found_already': False,
        'found_all': False,
        'jumble': jumble,
        'text': '',
        'matches': matches
    }

    rslt['found_already'] = text in matches
    rslt['text'] = text

    if (matched) and (in_jumble) and (text not in matches):
        matches.append(text)
        flask.session["matches"] = matches

    num_matches = len(matches)
    #print(f'matches = {matches} \n')

    rslt['matched'] = WORDS.has(text)

    rslt['in_jumble'] = in_jumble

    rslt['found_all'] = num_matches >= flask.session['target_count']

    #print(rslt)
    return flask.jsonify(result=rslt)
예제 #14
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    code = -1
    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        code = 1
        matches.append(text)
        flask.session["matches"] = matches
    # if the word has already been found
    elif text in matches:
        code = 2
    # if the word is not in the list of words
    elif not matched:
        code = 3
    # if the word is not a valid anagram of the letters
    elif not in_jumble:
        code = 4
    # if there are enough matches to satisfy the condition
    if len(matches) >= flask.session["target_count"]:
        code = 0
    if (code == -1):
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # create a dictionary of the code that has been created from logic above
    # and the current matches
    rslt = {"code": code, "matches": matches}
    # jsonify the dictionary
    return flask.jsonify(result=rslt)
예제 #15
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches")  # Default to empty list
    app.logger.debug(text)

    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    # print(matches)

    rslt = {
        'matched': False,
        'you_already_found': False,
        'in_jumble': False,
        'success': False,
        'jumble': jumble,
        'text': '',
        'matches': matches
    }

    rslt['you_already_found'] = text in matches

    print('text in matches', text in matches)
    if matched and in_jumble and (not (text in matches)):
        matches.append(text)
        flask.session["matches"] = matches

    rslt['success'] = len(matches) >= flask.session["target_count"]
    rslt['matched'] = WORDS.has(text)
    rslt['in_jumble'] = in_jumble
    rslt['text'] = text
    rslt['matches'] = matches

    print(rslt)
    return flask.jsonify(result=rslt)
예제 #16
0
def check():
    data = request.args.get("data", type=str).strip()
    total_count = request.args.get("len", type=int)
    jumble = flask.session["jumble"]

    all_items = data.split(" ")
    matches = all_items[:len(all_items)-1]
    text = all_items[len(all_items)-1]

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    rslt = {"is_in_jumble": in_jumble, "is_matched": matched, "is_repeat": text in matches, 
            "is_complete": False}
    rslt["is_complete"] = in_jumble and matched and not rslt["is_repeat"] and len(all_items) >= flask.session["target_count"] 

    return flask.jsonify(result=rslt)
예제 #17
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("txt", type=str)
    text = text.lower()  # make not case sensitive
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        if len(matches) >= flask.session["target_count"]:
            return flask.jsonify(m=4)
        else:
            return flask.jsonify(m=1)
    elif text in matches:
        return flask.jsonify(m=2)

    elif not matched:
        return flask.jsonify(m=0)
    elif not in_jumble:
        return flask.jsonify(m=3)
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # Choose page:  Solved enough, or keep going?
    if len(matches) >= flask.session["target_count"]:
        return flask.jsonify(m=4)
예제 #18
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
    elif text in matches:
        # Return in JSON
        return flask.jsonify(result=("You already found {}".format(text)))
    elif not matched:
        return flask.jsonify(
            result=("{} isn't in the list of words".format(text)))
    elif not in_jumble:
        return flask.jsonify(result=(
            '"{}" can\'t be made from the letters {}'.format(text, jumble)))
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    # Choose page:  Solved enough, or keep going?
    if len(matches) >= flask.session["target_count"]:
        #Using flask.jsonify rather than flask.redirect
        return flask.jsonify(URL=(flask.url_for("success")))
    else:
        return flask.jsonify(match=matches)
예제 #19
0
def check():
    """
    keyup event has occurred in the ('attempt')
    entry form. Checks variables and returns them
    to vocab.html
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]

    # Variables passed to vocab.html
    in_jumble = LetterBag(jumble).contains(text)  # In the jumble?
    matched = WORDS.has(text)  # Does it match?
    valid_len = len(text) <= len(jumble)  # Length
    rslt = {"matched": matched, "in_jumble": in_jumble, "valid_len": valid_len}
    # return results
    return flask.jsonify(result=rslt)
예제 #20
0
def check():
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    prev_match = text in matches
    target = flask.session["target_count"]
    rslt = {
        'matched': matched,
        'in_jumble': in_jumble,
        'prev_match': prev_match,
        "target": target
    }
    if in_jumble and matched and text not in matches:
        matches.append(text)
        flask.session["matches"] = matches

    return flask.jsonify(result=rslt)
예제 #21
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")
    text=request.args.get("text",type=str)
    # The data we need, from form and from cookie
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list
    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    condition=""
    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
    elif text in matches:
        condition="You already found {}".format(text)#record warning notes into condition
    elif not matched:
        condition="{} isn't in the list of words".format(text)#also
    elif not in_jumble:
        condition='"{}" can\'t be made from the letters {}'.format(text, jumble)#also        
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError
    # Choose page:  Solved enough, or keep going?
    if len(matches) >= flask.session["target_count"]:
    #save the result of whether solved into solve
      solve=True
    else:
      solve=False
    #rslt save all information needed in JSON,including text,codition,solve,matched and found(this is because avoid use string on .html)
    rslt={"text":text,"found":"You Found:","condition":condition,"solve":solve,"matched":matched}
    return flask.jsonify(result=rslt)#and call .jsonify, transmiss into vocab.html
예제 #22
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    # text = flask.request.form["attempt"]
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)
    success = False

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        flask.session["target_count"] -= 1
        success = True

    rslt = {
        "found": success,
        "cont": len(matches) != NUM,
        "remain": NUM - len(matches),
        "matched": matches,
        "repeated": text in matches,
        "invalid": not matched,
        "notjumble": not in_jumble
    }
    # AJAX handler
    return flask.jsonify(result=rslt)
예제 #23
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = flask.request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        status = "SUCCESS"
    elif text in matches:
        status = "ALREADY_FOUND"
    elif not matched:
        status = "NOT_MATCHED"
    elif not in_jumble:
        status = "BAD_INPUT"
    else:
        app.logger.debug("This case shouldn't happen!")
        assert False  # Raises AssertionError

    if len(matches) >= flask.session["target_count"]:
        status = "FINISHED"

    rslt = {"status": status, "matches": matches}
    return flask.jsonify(result=rslt)
예제 #24
0
def check():
    """
    User has submitted the form with a word ('attempt')
    that should be formed from the jumble and on the
    vocabulary list.  We respond depending on whether
    the word is on the vocab list (therefore correctly spelled),
    made only from the jumble letters, and not a word they
    already found.
    """
    app.logger.debug("Entering check")

    # The data we need, from form and from cookie
    text = request.args.get("text", type=str)
    jumble = flask.session["jumble"]
    matches = flask.session.get("matches", [])  # Default to empty list

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    # Respond appropriately
    if matched and in_jumble and not (text in matches):
        # Cool, they found a new word
        matches.append(text)
        flask.session["matches"] = matches
        found_all = False
        if len(matches) >= flask.session["target_count"]:
            found_all = True
        remaining = flask.session["target_count"] - len(matches)
        return flask.jsonify(found_all=found_all, new_match=True, status=1, remaining=remaining)
        # if target number reached, can return extra flag
    elif text in matches:
        return flask.jsonify(feedback=f"You already found {text}", status=0)
    elif not matched:
        return flask.jsonify(feedback=f"{text} isn't in the list of words", status=0)
    elif not in_jumble:
        return flask.jsonify(feedback=f"'{text}' can\'t be made from the letters {jumble}", status=0)
    # otherwise, this is an error
    app.logger.debug("This case shouldn't happen!")
    # return a JSON error instead of asserting false
    return flask.jsonify(feedback="something went wrong")
예제 #25
0
def example():
    """
    Example ajax request handler
    """
    app.logger.debug("Got a JSON request")
    jumble = flask.session["jumble"]
    text = request.args.get("text", type=str)

    # Is it good?
    in_jumble = LetterBag(jumble).contains(text)
    matched = WORDS.has(text)

    rslt = {
        "key": matched and in_jumble and not (text in MATCHES),
        "alreadyfound": text in MATCHES
    }

    if rslt.get("key"):
        MATCHES.append(text)

    return flask.jsonify(result=rslt)
예제 #26
0
def checkword():
    # Check if the word can be made using the letters and return result
    word = flask.request.args.get("word", type=str)
    rslt = {"uses_letters": LetterBag(letters_to_use).contains(word)}
    return flask.jsonify(result=rslt)
예제 #27
0
def test_empty():
    """
    Empty string <=> empty LetterBag
    """
    assert str(LetterBag("")) == ""
예제 #28
0
def test_complex_merge():
    bag1 = LetterBag("xxyyzzz")
    bag2 = LetterBag("xyyyzzzz")
    bag1.merge(bag2)
    assert bag1.as_string() == "xxyyyzzzz"
예제 #29
0
def test_simple_merge():
    bag_abbc = LetterBag("abbc")
    bag_abccd = LetterBag("abccd")
    bag_abbc.merge(bag_abccd)
    assert bag_abbc.as_string() == "abbccd"
예제 #30
0
def test_simple_str():
    """
    A simple LetterBag with multiples of a couple letters.
    """
    assert str(LetterBag("xaxyzyb")) == "abxxyyz"