commit 64b309b085195b23262f0e689cd7fbbb78db16ed Author: Nikolaj Date: Fri Dec 24 18:14:59 2021 +0100 The whole-ass thing diff --git a/current_games/3KJ5SVOn b/current_games/3KJ5SVOn new file mode 100644 index 0000000..5ae016e --- /dev/null +++ b/current_games/3KJ5SVOn @@ -0,0 +1,5 @@ +points:7 +i:10 +game_length:10 +images:WCQU,WD2r,WDcX,WDm+,WDwV,WDHM,WDSo,WE0V,WEb2,WElr +fonts:Akzidenz-Grotesk,Helvetica,Futura,Helvetica,Futura,Calibri,Akzidenz-Grotesk,Calibri,Helvetica,Roboto \ No newline at end of file diff --git a/current_games/3KJ63z2M b/current_games/3KJ63z2M new file mode 100644 index 0000000..97f4a1c --- /dev/null +++ b/current_games/3KJ63z2M @@ -0,0 +1,5 @@ +points:10 +i:10 +game_length:10 +images:7g1k,7geK,7gps,7gzz,7gKk,7gTP,7h0A,7h9Y,7hkN,7hzd +fonts:Roboto,Roboto,Calibri,Helvetica,Roboto,Akzidenz-Grotesk,Futura,Calibri,Arial,Arial \ No newline at end of file diff --git a/current_games/3KJ69bto b/current_games/3KJ69bto new file mode 100644 index 0000000..2e334ce --- /dev/null +++ b/current_games/3KJ69bto @@ -0,0 +1,5 @@ +points:4 +i:6 +game_length:10 +images:cUCv,cUOb,cUYI,cV6T,cVgK,cVqA,cVBc,cVLa,cVUo,cW1_ +fonts:Futura,Roboto,Arial,Akzidenz-Grotesk,Futura,Calibri,Helvetica,Akzidenz-Grotesk,Arial,Futura \ No newline at end of file diff --git a/font-list.txt b/font-list.txt new file mode 100644 index 0000000..db90c6c --- /dev/null +++ b/font-list.txt @@ -0,0 +1,29 @@ +Akzidenz Grotesk +Arial +#Baskerville +#Bembo +#Bodoni +#Cambria +Calibri +#Clarendon +#DaxPro +#Didot +#Franklin Gothic +#Frutiger +Futura +#Garamond +#Georgia +#Gill Sans +#Gotham +Helvetica +#Minion +#Mrs Eaves +#Myriad +#News Gothic +Roboto +#Roboto Slab +#Rockwell +#Sabon +#Segoe UI +#Times New Roman +#Verdana \ No newline at end of file diff --git a/font_game/__init__.py b/font_game/__init__.py new file mode 100644 index 0000000..1bbbdde --- /dev/null +++ b/font_game/__init__.py @@ -0,0 +1,5 @@ +from .util import (base64, FONT_SIZE, FONT_COLOR, IMAGE_BACKGROUND, IMAGE_SIZE, + MARGINS, FONTS) +from . import images +from . import web +from . import game diff --git a/font_game/__pycache__/__init__.cpython-310.pyc b/font_game/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..bbf7c2d Binary files /dev/null and b/font_game/__pycache__/__init__.cpython-310.pyc differ diff --git a/font_game/__pycache__/game.cpython-310.pyc b/font_game/__pycache__/game.cpython-310.pyc new file mode 100644 index 0000000..67ec4ae Binary files /dev/null and b/font_game/__pycache__/game.cpython-310.pyc differ diff --git a/font_game/__pycache__/images.cpython-310.pyc b/font_game/__pycache__/images.cpython-310.pyc new file mode 100644 index 0000000..c483537 Binary files /dev/null and b/font_game/__pycache__/images.cpython-310.pyc differ diff --git a/font_game/__pycache__/util.cpython-310.pyc b/font_game/__pycache__/util.cpython-310.pyc new file mode 100644 index 0000000..471abad Binary files /dev/null and b/font_game/__pycache__/util.cpython-310.pyc differ diff --git a/font_game/__pycache__/web.cpython-310.pyc b/font_game/__pycache__/web.cpython-310.pyc new file mode 100644 index 0000000..09acad7 Binary files /dev/null and b/font_game/__pycache__/web.cpython-310.pyc differ diff --git a/font_game/game.py b/font_game/game.py new file mode 100644 index 0000000..6baf3a3 --- /dev/null +++ b/font_game/game.py @@ -0,0 +1,47 @@ +import random +import time +import os + +from font_game.images import gen_image +from font_game import base64, FONTS + +def purge_games(): + for root, _, files in os.walk("current_games"): + for file in files: + os.remove(os.path.join(root, file)) + +def gen_id(): + return base64(int(time.time()*10000)) + +def pick_fonts(game_length: int): + weights = {key: 100 for key in FONTS} + font_list = [] + for _ in range(game_length): + picked = random.choice([k for k in weights for _ in range(weights[k])]) + font_list.append(picked) + weights[picked] = weights[picked] // 2 + + return [(i, FONTS[i]) for i in font_list] + +def start_game(game_length: int = 10) -> str: + picked_fonts = pick_fonts(game_length) + images = [] + for font in picked_fonts: + images.append(gen_image(font[1])) + + game_id = gen_id() + + + game_dict = { + "points": "0", + "i": "0", + "game_length": str(game_length), + "images": ','.join(images), + "fonts": ','.join(i[0] for i in picked_fonts) + } + game = "\n".join([f"{key}:{value}" for key, value in game_dict.items()]) + + with open(f"current_games/{game_id}", "w", encoding="utf-8") as file: + file.write(game) + + return game_id diff --git a/font_game/images.py b/font_game/images.py new file mode 100644 index 0000000..8b2332f --- /dev/null +++ b/font_game/images.py @@ -0,0 +1,45 @@ +import os +import time +from PIL import Image, ImageFont, ImageDraw +import lorem + +from font_game import base64, IMAGE_SIZE, IMAGE_BACKGROUND, MARGINS, FONT_COLOR + +def gen_image(font: ImageFont.ImageFont) -> str: + img = Image.new("RGB", IMAGE_SIZE, IMAGE_BACKGROUND) + drawer = ImageDraw.Draw(img) + text_width = IMAGE_SIZE[0] - 2 * MARGINS + text = lorem.sentence() + text_list = [] + line = "" + for word in text.split(" "): + if drawer.textsize(line + word, font)[0] > text_width: + text_list.append(line) + line = "" + else: + line += word + " " + + text_list.append(line) + + drawer.text( + (MARGINS, MARGINS), + '\n'.join(text_list), + FONT_COLOR, + font, + spacing=30 + ) + + filename = base64(int(time.time() * 10000) % 0xffffff) + + path = f"static/images/{filename}.png" + + new_size = (IMAGE_SIZE[0] // 2, IMAGE_SIZE[1] // 2) + img = img.resize(new_size, resample=Image.ANTIALIAS) + + img.save(path) + return filename + +def purge_images(): + for root, _, files in os.walk("static/images"): + for file in files: + os.remove(os.path.join(root, file)) diff --git a/font_game/util.py b/font_game/util.py new file mode 100644 index 0000000..34ee715 --- /dev/null +++ b/font_game/util.py @@ -0,0 +1,49 @@ +import string +import pathlib +from PIL import ImageFont + +BASE64_DIGITS = string.digits + string.ascii_letters + "+_" + +FONT_SIZE = 120 +FONT_COLOR = (0,0,0) +IMAGE_BACKGROUND = (255,255,255) +IMAGE_SIZE = (1800, 1200) + +MARGINS = 60 + + +def base64(num: int) -> str: + temp = "" + while num > 0: + temp = BASE64_DIGITS[num % 64] + temp + num = num//64 + + return temp + + +def make_fonts(): + with open("font-list.txt", "r", encoding="utf-8") as file_pointer: + fonts = file_pointer.read().split("\n") + + image_fonts = {} + for font_name in fonts: + if font_name[0] == "#": + continue + font_name = font_name.replace(" ","-") + if pathlib.Path(f"./fonts/{font_name}.TTF").is_file(): + file_type = "TTF" + elif pathlib.Path(f"./fonts/{font_name}.OTF").is_file(): + file_type = "OTF" + else: + print(f"Could not locate font \033[0;31m{font_name}\033[0m") + continue + + font_path = f"./fonts/{font_name}.{file_type}" + new_font = ImageFont.truetype(font_path, FONT_SIZE) + image_fonts[font_name] = new_font + print(f"Successfully loaded font \033[0;32m{font_name}\033[0m") + + return image_fonts + + +FONTS = make_fonts() diff --git a/font_game/web.py b/font_game/web.py new file mode 100644 index 0000000..3f6ac98 --- /dev/null +++ b/font_game/web.py @@ -0,0 +1,89 @@ +import pathlib +import flask +from font_game.images import purge_images +from font_game.game import start_game, purge_games +from font_game import FONTS + +app = flask.Flask( + __name__, + static_folder="../static", + template_folder="../templates" +) + +@app.before_first_request +def activate(): + purge_images() + purge_games() + +@app.route("/fontgame", methods=["GET", "POST"]) +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(): + 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) + + args = flask.request.args + if 'id' in args: + if not pathlib.Path(f"current_games/{args['id']}").is_file(): + 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')] + ) + + if int(game['i']) == int(game['game_length']): + return flask.render_template("final.html", points=game['points'], + game_length=game['game_length']) + + images = game['images'].split(",") + image = images[int(game['i'])] + + url = f"/static/images/{image}.png" + parameters = { + "url": url, + "i": str(int(game['i']) + 1), + "round_n": game['i'], + "game_length": game['game_length'], + "points":game['points'], + "fonts":FONTS, + "id":args['id'] + } + 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") + +@app.route("/startgame", methods=["POST"]) +def start(): + if 'game_length' in flask.request.form: + game_length = flask.request.form['game_length'] + else: + game_length = 10 + return flask.redirect(f"/fontgame?n={game_length}") diff --git a/fonts/Akzidenz-Grotesk.TTF b/fonts/Akzidenz-Grotesk.TTF new file mode 100644 index 0000000..f4b14d2 Binary files /dev/null and b/fonts/Akzidenz-Grotesk.TTF differ diff --git a/fonts/Arial.TTF b/fonts/Arial.TTF new file mode 100644 index 0000000..7ff88f2 Binary files /dev/null and b/fonts/Arial.TTF differ diff --git a/fonts/Baskerville.TTF b/fonts/Baskerville.TTF new file mode 100644 index 0000000..3fadf70 Binary files /dev/null and b/fonts/Baskerville.TTF differ diff --git a/fonts/Bodoni.OTF b/fonts/Bodoni.OTF new file mode 100644 index 0000000..a3d5eba Binary files /dev/null and b/fonts/Bodoni.OTF differ diff --git a/fonts/Calibri.TTF b/fonts/Calibri.TTF new file mode 100644 index 0000000..072384a Binary files /dev/null and b/fonts/Calibri.TTF differ diff --git a/fonts/Futura.TTF b/fonts/Futura.TTF new file mode 100644 index 0000000..8abcf15 Binary files /dev/null and b/fonts/Futura.TTF differ diff --git a/fonts/Garamond.TTF b/fonts/Garamond.TTF new file mode 100644 index 0000000..d00050e Binary files /dev/null and b/fonts/Garamond.TTF differ diff --git a/fonts/Georgia.TTF b/fonts/Georgia.TTF new file mode 100644 index 0000000..c24d59b Binary files /dev/null and b/fonts/Georgia.TTF differ diff --git a/fonts/Helvetica.TTF b/fonts/Helvetica.TTF new file mode 100644 index 0000000..718f22d Binary files /dev/null and b/fonts/Helvetica.TTF differ diff --git a/fonts/Roboto-Slab.TTF b/fonts/Roboto-Slab.TTF new file mode 100644 index 0000000..f3b141c Binary files /dev/null and b/fonts/Roboto-Slab.TTF differ diff --git a/fonts/Roboto.TTF b/fonts/Roboto.TTF new file mode 100644 index 0000000..5e84294 Binary files /dev/null and b/fonts/Roboto.TTF differ diff --git a/fonts/Segoe-UI.TTF b/fonts/Segoe-UI.TTF new file mode 100644 index 0000000..7db735b Binary files /dev/null and b/fonts/Segoe-UI.TTF differ diff --git a/fonts/Times-New-Roman.TTF b/fonts/Times-New-Roman.TTF new file mode 100644 index 0000000..55f734a Binary files /dev/null and b/fonts/Times-New-Roman.TTF differ diff --git a/fonts/Verdana.TTF b/fonts/Verdana.TTF new file mode 100644 index 0000000..754a9b7 Binary files /dev/null and b/fonts/Verdana.TTF differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..c2e3e25 --- /dev/null +++ b/main.py @@ -0,0 +1,4 @@ +from font_game.web import app + +if __name__ == "__main__": + app.run() diff --git a/static/css/stylesheet.css b/static/css/stylesheet.css new file mode 100644 index 0000000..46eebf0 --- /dev/null +++ b/static/css/stylesheet.css @@ -0,0 +1,34 @@ +.mainpage, .stats { + display: flex; +} + +.mainpage img { + width: 600pt; +} + +.rounds, .points { + font-size: 15pt; + font-weight: bold; + margin: 10pt; +} + +.finalpage, .menupage { + display: flex; + flex-direction: column; + justify-content: center; + align-content: center; + align-items: center; + justify-items: center; +} + +.finalpage .finalscore { + margin-bottom: 30pt; + margin-top: 60pt; + font-weight: bold; + font-size: 20pt; +} + +.menupage form .number { + width: 50pt; + text-align: right; +} diff --git a/static/images/7g1k.png b/static/images/7g1k.png new file mode 100644 index 0000000..aaf3d77 Binary files /dev/null and b/static/images/7g1k.png differ diff --git a/static/images/7gKk.png b/static/images/7gKk.png new file mode 100644 index 0000000..5ae4db3 Binary files /dev/null and b/static/images/7gKk.png differ diff --git a/static/images/7gTP.png b/static/images/7gTP.png new file mode 100644 index 0000000..0ebabc2 Binary files /dev/null and b/static/images/7gTP.png differ diff --git a/static/images/7geK.png b/static/images/7geK.png new file mode 100644 index 0000000..b5a3861 Binary files /dev/null and b/static/images/7geK.png differ diff --git a/static/images/7gps.png b/static/images/7gps.png new file mode 100644 index 0000000..68de016 Binary files /dev/null and b/static/images/7gps.png differ diff --git a/static/images/7gzz.png b/static/images/7gzz.png new file mode 100644 index 0000000..3b9a0e9 Binary files /dev/null and b/static/images/7gzz.png differ diff --git a/static/images/7h0A.png b/static/images/7h0A.png new file mode 100644 index 0000000..5d8efed Binary files /dev/null and b/static/images/7h0A.png differ diff --git a/static/images/7h9Y.png b/static/images/7h9Y.png new file mode 100644 index 0000000..653f6f6 Binary files /dev/null and b/static/images/7h9Y.png differ diff --git a/static/images/7hkN.png b/static/images/7hkN.png new file mode 100644 index 0000000..beba40e Binary files /dev/null and b/static/images/7hkN.png differ diff --git a/static/images/7hzd.png b/static/images/7hzd.png new file mode 100644 index 0000000..1e19032 Binary files /dev/null and b/static/images/7hzd.png differ diff --git a/static/images/WCQU.png b/static/images/WCQU.png new file mode 100644 index 0000000..4ccd759 Binary files /dev/null and b/static/images/WCQU.png differ diff --git a/static/images/WD2r.png b/static/images/WD2r.png new file mode 100644 index 0000000..d879388 Binary files /dev/null and b/static/images/WD2r.png differ diff --git a/static/images/WDHM.png b/static/images/WDHM.png new file mode 100644 index 0000000..626b0f6 Binary files /dev/null and b/static/images/WDHM.png differ diff --git a/static/images/WDSo.png b/static/images/WDSo.png new file mode 100644 index 0000000..f3ced22 Binary files /dev/null and b/static/images/WDSo.png differ diff --git a/static/images/WDcX.png b/static/images/WDcX.png new file mode 100644 index 0000000..168b52d Binary files /dev/null and b/static/images/WDcX.png differ diff --git a/static/images/WDm+.png b/static/images/WDm+.png new file mode 100644 index 0000000..ca8e2a1 Binary files /dev/null and b/static/images/WDm+.png differ diff --git a/static/images/WDwV.png b/static/images/WDwV.png new file mode 100644 index 0000000..4d1a957 Binary files /dev/null and b/static/images/WDwV.png differ diff --git a/static/images/WE0V.png b/static/images/WE0V.png new file mode 100644 index 0000000..c9474a2 Binary files /dev/null and b/static/images/WE0V.png differ diff --git a/static/images/WEb2.png b/static/images/WEb2.png new file mode 100644 index 0000000..c842277 Binary files /dev/null and b/static/images/WEb2.png differ diff --git a/static/images/WElr.png b/static/images/WElr.png new file mode 100644 index 0000000..2700074 Binary files /dev/null and b/static/images/WElr.png differ diff --git a/static/images/cUCv.png b/static/images/cUCv.png new file mode 100644 index 0000000..e877348 Binary files /dev/null and b/static/images/cUCv.png differ diff --git a/static/images/cUOb.png b/static/images/cUOb.png new file mode 100644 index 0000000..7368a64 Binary files /dev/null and b/static/images/cUOb.png differ diff --git a/static/images/cUYI.png b/static/images/cUYI.png new file mode 100644 index 0000000..7145aca Binary files /dev/null and b/static/images/cUYI.png differ diff --git a/static/images/cV6T.png b/static/images/cV6T.png new file mode 100644 index 0000000..5fb1ab8 Binary files /dev/null and b/static/images/cV6T.png differ diff --git a/static/images/cVBc.png b/static/images/cVBc.png new file mode 100644 index 0000000..9159a03 Binary files /dev/null and b/static/images/cVBc.png differ diff --git a/static/images/cVLa.png b/static/images/cVLa.png new file mode 100644 index 0000000..c9c2ed4 Binary files /dev/null and b/static/images/cVLa.png differ diff --git a/static/images/cVUo.png b/static/images/cVUo.png new file mode 100644 index 0000000..dc4ecfe Binary files /dev/null and b/static/images/cVUo.png differ diff --git a/static/images/cVgK.png b/static/images/cVgK.png new file mode 100644 index 0000000..8aa5459 Binary files /dev/null and b/static/images/cVgK.png differ diff --git a/static/images/cVqA.png b/static/images/cVqA.png new file mode 100644 index 0000000..dca9203 Binary files /dev/null and b/static/images/cVqA.png differ diff --git a/static/images/cW1_.png b/static/images/cW1_.png new file mode 100644 index 0000000..f64ef04 Binary files /dev/null and b/static/images/cW1_.png differ diff --git a/templates/final.html b/templates/final.html new file mode 100644 index 0000000..f334b90 --- /dev/null +++ b/templates/final.html @@ -0,0 +1,15 @@ + + + + + + +
+ Final Score: + {{ points }}/{{ game_length }} +
+
+ +
+ + diff --git a/templates/fontgame.html b/templates/fontgame.html new file mode 100644 index 0000000..3ecf75b --- /dev/null +++ b/templates/fontgame.html @@ -0,0 +1,28 @@ + + + + + + + +
+
+
+ Round {{ i }} of {{ game_length }} +
+
+ {{ points }}/{{ round_n }} points. +
+
+
+ + + +
+
+ + diff --git a/templates/menu.html b/templates/menu.html new file mode 100644 index 0000000..e3270f8 --- /dev/null +++ b/templates/menu.html @@ -0,0 +1,14 @@ + + + + + + +

Font Game

+
+ + + +
+ +