From 545efc2d7e6f43d63960866c307ba3070974e92b Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Tue, 18 Jan 2022 22:42:38 +0100 Subject: [PATCH] build words once on runtime --- app.py | 63 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index 50b297a..cc155e0 100644 --- a/app.py +++ b/app.py @@ -9,7 +9,8 @@ from play import build_non_letter_hint, build_letter_hint app = Flask(__name__) app.config['SECRET_KEY'] = sensitive.SECRET_KEY -NUM_GAMES = len(list(cat_words())) - 1 +WORDS = list(cat_words()) +NUM_GAMES = len(WORDS) - 1 GUESS_SPLITTER = '|' STATE_PLAYING = 'PLAYING' STATE_WIN = 'WIN' @@ -17,12 +18,6 @@ STATE_LOSE = 'LOSE' RANDOM_GAME_ID = -1 -@app.route('/') -def home(): - num_games = NUM_GAMES # len(list(cat_words())) - 1 - return render_template('home.html', num_games=num_games) - - def init_game(game_id, word): session['game_id'] = game_id session['word'] = word @@ -30,29 +25,6 @@ def init_game(game_id, word): session['state'] = STATE_PLAYING -@app.route('/init/random') -def init_random_game(): - word = random.choice([w for w in cat_words()]) - init_game(RANDOM_GAME_ID, word) - return redirect(url_for('random_game')) - - -@app.route('/init/deterministic', methods=['post']) -def init_deterministic_game(): - game_id = request.form.get('game_id') - return redirect(url_for('init_deterministic_game_id', game_id=game_id)) - - -@app.route('/init/deterministic/') -def init_deterministic_game_id(game_id): - if not 0 <= int(game_id) <= NUM_GAMES: - flash("game_id must be between 0 and {}".format(NUM_GAMES)) - return redirect(url_for('home')) - word = [w for w in cat_words()][int(game_id)] - init_game(game_id, word) - return redirect(url_for('deterministic_game_id', game_id=game_id)) - - def resolve_guesses(): word = session['word'] guesses = session.get('guesses', '').split(GUESS_SPLITTER) @@ -83,6 +55,35 @@ def build_game_state(): state=session['state']) +@app.route('/') +def home(): + num_games = NUM_GAMES + return render_template('home.html', num_games=num_games) + + +@app.route('/init/random') +def init_random_game(): + word = random.choice(WORDS) + init_game(RANDOM_GAME_ID, word) + return redirect(url_for('random_game')) + + +@app.route('/init/deterministic', methods=['post']) +def init_deterministic_game(): + game_id = request.form.get('game_id') + return redirect(url_for('init_deterministic_game_id', game_id=game_id)) + + +@app.route('/init/deterministic/') +def init_deterministic_game_id(game_id): + if not 0 <= int(game_id) <= NUM_GAMES: + flash("game_id must be between 0 and {}".format(NUM_GAMES)) + return redirect(url_for('home')) + word = WORDS[int(game_id)] + init_game(game_id, word) + return redirect(url_for('deterministic_game_id', game_id=game_id)) + + @app.route('/random') def random_game(): if 'word' not in session or 'state' not in session: @@ -107,7 +108,7 @@ def make_guess(): flash("Make a 5 letter guess.") return build_game_state() guess = guess.lower() - if guess not in cat_words(): + if guess not in WORDS: flash('Not in word list: {}'.format(guess)) return build_game_state() word = session['word']