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.
## Demo
https://ab.21x9.org
## Installation
0. Install `python3` (and `pip` if needed)
@ -32,35 +28,3 @@ Or just run `python3 ./game.py`
## Use
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
Pacman;Tetris
Tischfußball;Tischtennis
PC;Spielkonsolen
Computer;Spielkonsolen
Live-Action-Rollenspiele;Online-Spiele
Wrestling;Mix Martial Arts
Bowling;Tennis

View file

@ -18,7 +18,7 @@ more = Weiter »»»
title = A oder B
mail_link = Einreichen!
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
questions_prefix = Es gibt derzeit
questions_suffix = Fragen.

90
game.py
View file

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

View file

@ -30,7 +30,7 @@ a #help {
a:hover #help {
position: absolute;
top: 105px;
top: 125px;
left: 50%;
height: auto;
width: 50%;
@ -46,7 +46,6 @@ a:hover #help {
display: inline-block;
text-align: center;
transform: translate(-50%);
z-index: 10;
}
textarea {
@ -189,9 +188,6 @@ input[type="radio"] {
justify-content: center;
align-items: center;
color: var(--fg-sep);
border-radius: 10px;
z-index: 5;
cursor: pointer;
}
#footer {
@ -245,33 +241,6 @@ input[type="radio"] {
}
@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 {
min-width: 95%;
transform: none;
@ -283,8 +252,7 @@ input[type="radio"] {
#next {
min-width: 100%;
margin-top: 10px;
height: 50px;
margin-top: 20px;
}
#questions {

View file

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

View file

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

View file

@ -10,8 +10,7 @@
<div class="ab">
<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>
<input type="radio" id="o{{ id.value }}" class="separator" name="group-{{ id.value }}">
<label for="o{{ id.value }}" class="separator">{{ i18n.separator }}</label>
<div class="separator">{{ i18n.separator }}</div>
<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>
</div>