Compare commits

..

No commits in common. "main" and "v1.1.0" have entirely different histories.
main ... v1.1.0

8 changed files with 26 additions and 151 deletions

View file

@ -2,10 +2,6 @@
Simple choice game. Simple choice game.
## Demo
https://ab.21x9.org
## Installation ## Installation
0. Install `python3` (and `pip` if needed) 0. Install `python3` (and `pip` if needed)
@ -32,35 +28,3 @@ Or just run `python3 ./game.py`
## Use ## Use
Open your browser and navigate to `http://localhost:5000` or whatever you configured as `bind` and `port` in your `config.ini`. Open your browser and navigate to `http://localhost:5000` or whatever you configured as `bind` and `port` in your `config.ini`.
## Reverse Proxy
To run this game in an production environment it's strongly recommended to set up an reverse proxy upfront.
### nginx
Make sure you understand the config and replace the `location` and/or `proxy_pass` value if necessary.
```nginx
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 32M;
proxy_redirect off;
proxy_intercept_errors off;
proxy_read_timeout 86400s;
proxy_ignore_client_abort on;
proxy_connect_timeout 120s;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_headers_hash_max_size 512;
proxy_buffering on;
proxy_cache_bypass $http_pragma $http_authorization $cookie_nocache;
}
```

2
ab.txt
View file

@ -1035,7 +1035,7 @@ Multiplayer;Einzelspieler
Pizza Hawaii;keine Pizza Pizza Hawaii;keine Pizza
Pacman;Tetris Pacman;Tetris
Tischfußball;Tischtennis Tischfußball;Tischtennis
PC;Spielkonsolen Computer;Spielkonsolen
Live-Action-Rollenspiele;Online-Spiele Live-Action-Rollenspiele;Online-Spiele
Wrestling;Mix Martial Arts Wrestling;Mix Martial Arts
Bowling;Tennis Bowling;Tennis

View file

@ -18,7 +18,7 @@ more = Weiter »»»
title = A oder B title = A oder B
mail_link = Einreichen! mail_link = Einreichen!
help_link = Hilfe? help_link = Hilfe?
help = <strong>Hilfe</strong><p>Wähle die Antworten durch Klicken/Tippen oder mit der Tastatur durch Drücken der Tasten "a" bzw. "b" aus (beantwortet die jeweils nächste unbeantwortete Frage), eine Frage kann mit "o" übersprungen werden. Sobald alle Antworten ausgewählt wurden, werden nach einer kurzen Wartezeit automatisch neue Fragen geladen.</p><p>Möchtest Du nicht alle Fragen beantworten, klicke einfach auf die Schaltfläche "Weiter" unten rechts oder drücke "F5" (beides klappt auch, während der Countdown läuft) bzw. "s" für Skip (mit kurzer Wartezeit).</p><p>Klicke/tippe außerhalb dieses Fensters oder bewege die Maus, um fortzufahren.</p><p>Diese Webseite zeichnet keine Daten auf und verwendet keine Cookies.</p> help = <strong>Hilfe</strong><p>Wähle die Antworten durch Klicken/Tippen oder mit der Tastatur durch Drücken der Tasten "a" bzw. "b" aus (beantwortet die jeweils nächste unbeantwortete Frage). Sobald alle Antworten ausgewählt wurden, werden nach einer kurzen Wartezeit automatisch neue Fragen geladen.</p><p>Möchtest Du nicht alle Fragen beantworten, klicke einfach auf die Schaltfläche "Weiter" unten rechts oder drücke "F5" (beides klappt auch, während der Countdown läuft).</p><p>Klicke/tippe außerhalb dieses Fensters oder bewege die Maus, um fortzufahren.</p><p>Diese Webseite zeichnet keine Daten auf und verwendet keine Cookies.</p>
separator = oder separator = oder
questions_prefix = Es gibt derzeit questions_prefix = Es gibt derzeit
questions_suffix = Fragen. questions_suffix = Fragen.

90
game.py
View file

@ -1,16 +1,12 @@
#! /bin/python3 #! /bin/python3
""" from flask import Flask, render_template, request
Simple A/B choice game
"""
from configparser import ConfigParser from configparser import ConfigParser
from datetime import datetime from datetime import datetime
from email.message import EmailMessage from email.message import EmailMessage
import smtplib import smtplib
import random import random
import pytz import pytz
from flask import Flask, render_template, request
app = Flask(__name__, app = Flask(__name__,
static_url_path='', static_url_path='',
@ -49,14 +45,10 @@ conf = {
'animations': config.get('main', 'animations') 'animations': config.get('main', 'animations')
} }
with open("ab.txt", "r", encoding="utf-8") as f: with open("ab.txt", "r") as f:
num_lines = sum(1 for _ in f) num_lines = sum(1 for _ in f)
def get_epoch(): def getEpoch():
"""
Get current time as epoch timestamp
"""
now = datetime.now(tz=tz) now = datetime.now(tz=tz)
epoch = now.timestamp() epoch = now.timestamp()
epoch = int(epoch) epoch = int(epoch)
@ -64,47 +56,24 @@ def get_epoch():
return epoch return epoch
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(): def page_not_found(e):
""" epoch = getEpoch()
404 Error Page
"""
epoch = get_epoch()
return render_template('404.html', config=conf, i18n=i18n, epoch=epoch), 404 return render_template('404.html', config=conf, i18n=i18n, epoch=epoch), 404
@app.errorhandler(500) @app.errorhandler(500)
def internal_server_error(): def internal_server_error(e):
""" epoch = getEpoch()
500 Error Page return render_template('500.html', config=conf, i18n=i18n, epoch=epoch), 500
"""
epoch = get_epoch()
return render_template(
'500.html',
config=conf,
i18n=i18n,
epoch=epoch
), 500
if conf['mailform'] == "true": if conf['mailform'] == "true":
@app.route("/form") @app.route("/form")
def mailform(): def mailform():
""" epoch = getEpoch()
Mail form return render_template('mailform.html', config=conf, i18n=i18n, epoch=epoch, num_lines=num_lines)
"""
epoch = get_epoch()
return render_template(
'mailform.html',
config=conf,
i18n=i18n,
epoch=epoch,
num_lines=num_lines
)
@app.route("/send", methods=['POST', 'GET']) @app.route("/send", methods=['POST', 'GET'])
def sendmail(): def sendmail():
""" epoch = getEpoch()
Send form data via E-Mail
"""
epoch = get_epoch()
if request.method == 'POST': if request.method == 'POST':
mailcontent = request.form['questions'] mailcontent = request.form['questions']
@ -121,48 +90,29 @@ if conf['mailform'] == "true":
smtp_server.send_message(message) smtp_server.send_message(message)
smtp_server.quit() smtp_server.quit()
return render_template( return render_template('thanks.html', config=conf, i18n=i18n, epoch=epoch, questions=mailcontent)
'thanks.html',
config=conf,
i18n=i18n,
epoch=epoch,
questions=mailcontent
)
@app.route("/") @app.route("/")
def hello(): def hello():
"""
Default/Main page
"""
ablines = [] ablines = []
epoch = get_epoch() epoch = getEpoch()
lines = get_content() lines = getContent()
while len(lines) < 2: while len(lines) < 2:
print('Error reading content') print('Error reading content')
print(lines) print(lines)
lines = get_content() lines = getContent()
for line in lines: for line in lines:
abq = line.split(conf['separator_char']) ab = line.split(conf['separator_char'])
ablines.append( ablines.append(
{'A': str(abq[0]), 'B': str(abq[1])} {'A': str(ab[0]), 'B': str(ab[1])}
) )
return render_template( return render_template('index.html', content=ablines, config=conf, i18n=i18n, num_lines=num_lines, epoch=epoch)
'index.html',
content=ablines,
config=conf,
i18n=i18n,
num_lines=num_lines,
epoch=epoch
)
def get_content(): def getContent():
""" lines = [a.strip() for a in open("ab.txt", "r").readlines()]
Read content from file
"""
lines = [a.strip() for a in open("ab.txt", "r", encoding="utf-8").readlines()]
result = random.sample(lines, 5) result = random.sample(lines, 5)
return result return result

View file

@ -30,7 +30,7 @@ a #help {
a:hover #help { a:hover #help {
position: absolute; position: absolute;
top: 105px; top: 125px;
left: 50%; left: 50%;
height: auto; height: auto;
width: 50%; width: 50%;
@ -46,7 +46,6 @@ a:hover #help {
display: inline-block; display: inline-block;
text-align: center; text-align: center;
transform: translate(-50%); transform: translate(-50%);
z-index: 10;
} }
textarea { textarea {
@ -189,9 +188,6 @@ input[type="radio"] {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
color: var(--fg-sep); color: var(--fg-sep);
border-radius: 10px;
z-index: 5;
cursor: pointer;
} }
#footer { #footer {
@ -245,33 +241,6 @@ input[type="radio"] {
} }
@media only screen and (max-width:640px) { @media only screen and (max-width:640px) {
#header {
padding-top: 15px;
padding-bottom: 15px;
}
a:hover #help {
top: 5px;
width: 100%;
padding: 5px;
}
.a {
padding: 15px;
font-size: small;
text-indent: 0;
}
.b {
padding: 15px;
font-size: small;
text-indent: 0;
}
.o {
padding: 15px;
}
#content { #content {
min-width: 95%; min-width: 95%;
transform: none; transform: none;
@ -283,8 +252,7 @@ input[type="radio"] {
#next { #next {
min-width: 100%; min-width: 100%;
margin-top: 10px; margin-top: 20px;
height: 50px;
} }
#questions { #questions {

View file

@ -9,7 +9,7 @@ function countdown() {
document.getElementById("next").innerHTML = timeleft; document.getElementById("next").innerHTML = timeleft;
} }
timeleft -= 1; timeleft -= 1;
}, 500); }, 750);
} }
function autoReload() { function autoReload() {

View file

@ -53,12 +53,6 @@ window.addEventListener("keydown", function (event) {
case "b": case "b":
check("b"); check("b");
break; break;
case "o":
check("o");
break;
case "s":
countdown();
break;
default: default:
checkCount(); checkCount();
return; return;

View file

@ -10,8 +10,7 @@
<div class="ab"> <div class="ab">
<input type="radio" id="a{{ id.value }}" class="a" name="group-{{ id.value }}"> <input type="radio" id="a{{ id.value }}" class="a" name="group-{{ id.value }}">
<label for="a{{ id.value }}" class="a slide{{ id.value }}" title="{{ question['A'] }}"> A)&nbsp;{{ question['A'] }}</label> <label for="a{{ id.value }}" class="a slide{{ id.value }}" title="{{ question['A'] }}"> A)&nbsp;{{ question['A'] }}</label>
<input type="radio" id="o{{ id.value }}" class="separator" name="group-{{ id.value }}"> <div class="separator">{{ i18n.separator }}</div>
<label for="o{{ id.value }}" class="separator">{{ i18n.separator }}</label>
<input type="radio" id="b{{ id.value }}" class="b" name="group-{{ id.value }}"> <input type="radio" id="b{{ id.value }}" class="b" name="group-{{ id.value }}">
<label for="b{{ id.value }}" class="b slide{{ id.value }}" title="{{ question['B'] }}">B)&nbsp;{{ question['B'] }}</label> <label for="b{{ id.value }}" class="b slide{{ id.value }}" title="{{ question['B'] }}">B)&nbsp;{{ question['B'] }}</label>
</div> </div>