From ba69c2a97fa8a96ee567c23b2a819a0c6ce19c41 Mon Sep 17 00:00:00 2001 From: fram3d Date: Tue, 3 Oct 2023 23:45:48 +0200 Subject: [PATCH] add functions.py and prediction checks --- freeriders/functions.py | 16 ++++++++++++++++ freeriders/routes.py | 24 ++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 freeriders/functions.py diff --git a/freeriders/functions.py b/freeriders/functions.py new file mode 100644 index 0000000..a7af5fc --- /dev/null +++ b/freeriders/functions.py @@ -0,0 +1,16 @@ +import time + +def formatprefix2(number): + return "{:02d}".format(number) + +def formatprefix10(number): + return "{:010d}".format(number) + +def predict(timestamp): + base_ticket = 4339636 + base_timestamp = 1694003498 + step = 8 + + ticket = base_ticket + (int(time.time()) - base_timestamp) * step + return ticket + diff --git a/freeriders/routes.py b/freeriders/routes.py index 85b0038..adb4ac9 100644 --- a/freeriders/routes.py +++ b/freeriders/routes.py @@ -1,18 +1,27 @@ from flask import render_template, request, redirect from freeriders import app, db +from freeriders.functions import formatprefix2,formatprefix10, predict from freeriders.models import Tickets from datetime import datetime import time +PREDICTTIMERANGE = 60 * 80 + @app.route('/sms', methods=['GET']) def sms(): if request.method == 'GET': + timenow = time.time() + try: - lastticket = Tickets.query.order_by(Tickets.timestamp.desc()).first().ticket - lastticket = "{:010d}".format(lastticket) + lastticket = Tickets.query.order_by(Tickets.timestamp.desc()).first() + + if lastticket.timestamp < timenow - (90 * 60): + lastticket = formatprefix10(predict(timenow)) + else: + lastticket = formatprefix10(lastticket.ticket) date = datetime.now() - datenow = f'{"{:02d}".format(date.day)}.{"{:02d}".format(date.month)}.{date.year}' - timenow = f'{"{:02d}".format(date.hour)}:{"{:02d}".format(date.minute)}:{date.second}' + datenow = f'{formatprefix2(date.day)}.{formatprefix2(date.month)}.{date.year}' + timenow = f'{formatprefix2(date.hour)}:{formatprefix2(date.minute)}:formatprefix2{date.second}' return render_template('sms.html', ticket = lastticket, date = datenow, time = timenow) except: return 'Error retriving last ticket' @@ -27,8 +36,11 @@ def submit(): timenow = int(time.time()) ticket_input = request.form['ticket'] - if ticket_input.isdigit() and len(ticket_input) == 10: - ticket = Tickets(ticket = int(ticket_input), timestamp = timenow) + if ticket_input.isdigit() and len(ticket_input) == 10 : + if ticket_input < predict(timenow + PREDICTTIMERANGE ) and ticket_input > predict(timenow - PREDICTTIMERANGE ) : + ticket = Tickets(ticket = int(ticket_input), timestamp = timenow) + else: + return 'Ticket number is in unexpected range.' else: return 'Ticket format is wrong. Only 10 digits allowed.'