add functions.py and prediction checks

This commit is contained in:
fram3d 2023-10-03 23:45:48 +02:00
parent ff088a6a24
commit ba69c2a97f
Signed by: fram3d
GPG Key ID: 938920E709EEA32A
2 changed files with 34 additions and 6 deletions

16
freeriders/functions.py Normal file
View File

@ -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

View File

@ -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.'