build words once on runtime

This commit is contained in:
Daniel Tsvetkov 2022-01-18 22:42:38 +01:00
parent 390ea9e702
commit 545efc2d7e

63
app.py
View File

@ -9,7 +9,8 @@ from play import build_non_letter_hint, build_letter_hint
app = Flask(__name__) app = Flask(__name__)
app.config['SECRET_KEY'] = sensitive.SECRET_KEY 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 = '|' GUESS_SPLITTER = '|'
STATE_PLAYING = 'PLAYING' STATE_PLAYING = 'PLAYING'
STATE_WIN = 'WIN' STATE_WIN = 'WIN'
@ -17,12 +18,6 @@ STATE_LOSE = 'LOSE'
RANDOM_GAME_ID = -1 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): def init_game(game_id, word):
session['game_id'] = game_id session['game_id'] = game_id
session['word'] = word session['word'] = word
@ -30,29 +25,6 @@ def init_game(game_id, word):
session['state'] = STATE_PLAYING 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/<int:game_id>')
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(): def resolve_guesses():
word = session['word'] word = session['word']
guesses = session.get('guesses', '').split(GUESS_SPLITTER) guesses = session.get('guesses', '').split(GUESS_SPLITTER)
@ -83,6 +55,35 @@ def build_game_state():
state=session['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/<int:game_id>')
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') @app.route('/random')
def random_game(): def random_game():
if 'word' not in session or 'state' not in session: if 'word' not in session or 'state' not in session:
@ -107,7 +108,7 @@ def make_guess():
flash("Make a 5 letter guess.") flash("Make a 5 letter guess.")
return build_game_state() return build_game_state()
guess = guess.lower() guess = guess.lower()
if guess not in cat_words(): if guess not in WORDS:
flash('Not in word list: {}'.format(guess)) flash('Not in word list: {}'.format(guess))
return build_game_state() return build_game_state()
word = session['word'] word = session['word']