some things

This commit is contained in:
Nikolaj
2021-12-29 12:24:07 +01:00
parent b36be893a4
commit 0ef63ca80b
9 changed files with 46 additions and 38 deletions

View File

@ -6,7 +6,7 @@ Arial
#Cambria
Calibri
#Clarendon
#DaxPro
#Dax Pro
#Didot
#Franklin Gothic
#Frutiger
@ -24,6 +24,6 @@ Roboto
#Roboto Slab
#Rockwell
#Sabon
#Segoe UI
Segoe UI
#Times New Roman
#Verdana

View File

@ -1,5 +1,5 @@
from .util import (base64, FONT_SIZE, FONT_COLOR, IMAGE_BACKGROUND, IMAGE_SIZE,
MARGINS, FONTS)
MARGINS, FONTS, read_file, write_file)
from . import images
from . import web
from . import game

View File

@ -1,9 +1,10 @@
import random
import time
import os
import pathlib
from font_game.images import gen_image
from font_game import base64, FONTS
from font_game import base64, FONTS, read_file, write_file
def purge_games():
for root, _, files in os.walk("current_games"):
@ -45,3 +46,16 @@ def start_game(game_length: int = 10) -> str:
file.write(game)
return game_id
def test_game(game_id: str):
return pathlib.Path(f"current_games/{game_id}").is_file()
def guess_font(game_id: str, font_guess: str):
game_dict = read_file(f"current_games/{game_id}")
i = int(game_dict['i'])
font = game_dict['fonts'].split(',')[i]
if font_guess == font:
game_dict['points'] = str(int(game_dict['points']) + 1)
game_dict['i'] = str(i+1)
write_file(f"current_games/{game_id}", game_dict)

View File

@ -11,6 +11,21 @@ IMAGE_SIZE = (1800, 1200)
MARGINS = 60
def read_file(path: str) -> dict:
with open(path, "r", encoding="utf-8") as file:
game_dict = dict(
[tuple(line.split(":")) for line in file.read().split('\n')]
)
return game_dict
def write_file(path: str, game: dict):
game_string = "\n".join(
[f"{key}:{value}" for key, value in game.items()]
)
with open(path, "w", encoding="utf-8") as file:
file.write(game_string)
def base64(num: int) -> str:
temp = ""

View File

@ -1,8 +1,8 @@
import pathlib
import os
import flask
from font_game.images import purge_images
from font_game.game import start_game, purge_games
from font_game import FONTS
from font_game.game import start_game, purge_games, guess_font, test_game
from font_game import FONTS, read_file
app = flask.Flask(
__name__,
@ -20,36 +20,21 @@ def font_game():
if flask.request.method == "POST":
game_id = flask.request.form['id']
font_guess = flask.request.form['font']
if not pathlib.Path(f"current_games/{game_id}").is_file():
if not test_game(game_id):
return flask.redirect("/")
with open(f"current_games/{game_id}", "r+", encoding="utf-8") as file:
game_dict = dict(
[tuple(line.split(":")) for line in file.read().split('\n')]
)
i = int(game_dict['i'])
font = game_dict['fonts'].split(',')[i]
if font_guess == font:
game_dict['points'] = str(int(game_dict['points']) + 1)
game_dict['i'] = str(i+1)
game = "\n".join(
[f"{key}:{value}" for key, value in game_dict.items()]
)
file.seek(0)
file.write(game)
guess_font(game_id, font_guess)
args = flask.request.args
if 'id' in args:
if not pathlib.Path(f"current_games/{args['id']}").is_file():
game_id = args['id']
if not test_game(game_id):
return flask.redirect("/")
with open(f"current_games/{args['id']}", "r", encoding="utf-8") as file:
game = dict(
[tuple(line.split(":")) for line in file.read().split('\n')]
)
game = read_file(f"current_games/{game_id}")
if int(game['i']) == int(game['game_length']):
os.remove(f"current_games/{game_id}")
return flask.render_template("final.html", points=game['points'],
game_length=game['game_length'])
@ -68,14 +53,6 @@ def font_game():
}
return flask.render_template("fontgame.html", **parameters)
if 'n' in flask.request.args:
game_length = int(flask.request.args['n'])
else:
game_length = 10
game_id = start_game(game_length)
return flask.redirect(f"/fontgame?id={game_id}")
@app.route("/")
def root():
return flask.render_template("menu.html")
@ -83,7 +60,9 @@ def root():
@app.route("/startgame", methods=["POST"])
def start():
if 'game_length' in flask.request.form:
game_length = flask.request.form['game_length']
game_length = int(flask.request.form['game_length'])
else:
game_length = 10
return flask.redirect(f"/fontgame?n={game_length}")
game_id = start_game(game_length)
return flask.redirect(f"/fontgame?id={game_id}")