From 77b504fe764641ffb4d7bba5105046a17b5f7dfb Mon Sep 17 00:00:00 2001 From: 2000-Trek Date: Wed, 31 May 2023 20:33:45 +0200 Subject: [PATCH] initial --- main.py | 146 +++++++++++++++++++++++++++++++++++++++++ mate.db | Bin 0 -> 24576 bytes templates/addtag.html | 26 ++++++++ templates/adduser.html | 7 ++ templates/change.html | 26 ++++++++ 5 files changed, 205 insertions(+) create mode 100644 main.py create mode 100644 mate.db create mode 100644 templates/addtag.html create mode 100644 templates/adduser.html create mode 100644 templates/change.html diff --git a/main.py b/main.py new file mode 100644 index 0000000..537fa7a --- /dev/null +++ b/main.py @@ -0,0 +1,146 @@ +import queue +from flask import Flask, render_template, request, make_response +from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/ +import sqlite3 +import time +import atexit +import sys + +db_path = 'mate.db' +conn = sqlite3.connect(db_path) +c = conn.cursor() + +app = Flask(__name__) +socketio = SocketIO(app) +app.debug = True + +status = True +users = queue.Queue() +finished = None + +def exit_handler(): + conn.close() + sys.exit("Program sucsesfully exited") + +#website +@app.route("/") +def index(): + return 'user and tag list

The creator of this website accepts no liability for any linguistic or technical errors!

' + +@app.route("/list") +def list(): + c.execute("SELECT * FROM users") + users = c.fetchall() + text = "" + for i in users: + text = text + f'

{i[1]}: {i[2]}

Change balance:



' + return '
Search for User:


' + text + +@app.route("/list/user", methods=['GET']) +def user(): + username = request.args.get("user") + c.execute("SELECT * FROM users WHERE username = '%s'" % username) + user = c.fetchall() + if user != []: + user = user[0] + return f'

{user[1]}: {user[2]}

Change balance:

' + else: + return "User does not exists" + +@app.route("/adduser") +def new_user(): + return render_template("adduser.html") + +@app.route("/adduser/user", methods=['GET']) +def adduser(): + user = request.args.get("username") + c.execute(f"SELECT * FROM users WHERE username='{str(user)}'") + if c.fetchall() == []: + c.execute("INSERT or IGNORE INTO users (username, balance) VALUES ('%s', 0)" % user) + conn.commit() + return "alert('Added userd')" + else: + return "alert('User already exists')" + + +@app.route("/change", methods=['GET']) +def change(): + user_id = int(request.args.get("id")) + try: + change = int(request.args.get("change")) + except ValueError: + return '

Pleas enter a number!

tags and user list' + if change < 0: + text = "removed from" + elif change > 0: + text = "added to" + elif change == 0: + return "

Nothing was done!

" + c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}") + c.execute(f"SELECT * FROM users WHERE id={user_id}") + user = c.fetchall()[0][1] + return f'

{change} was {text} {user}' + +@app.route("/addtag", methods=['GET']) +def get_addtag_request(): + global users + try: + user_id = int(request.args.get("id")) + except: #except im Normalen Code! + return "Wrong user id!" + users.put(user_id) + return render_template("addtag.html", user=user_id) + +@socketio.on('addtag') +def request_addtag(data): + global finished + if len(users.queue) > 0: + user = users.queue[len(users.queue) - 1] + if user == data["data"]: + socketio.emit("wait", "wait") + i = 0 + while finished != data["data"]: + time.sleep(1) + i += 1 + if i > 20: + socketio.emit("error", "timeout") + nothing = users.get() + pass + c.execute(f"SELECT * FROM users WHERE id ={user}") + username = c.fetchall()[0][1] + c.execute(f"SELECT * FROM tags WHERE userid={user}") + tag_id = c.fetchall()[0][0] + socketio.emit("finished", f"Added {tag_id} to {username}") + else: + socketio.emit("busy", "busy") + else: + socketio.emit("error", "error") + +#api +@app.route("/api/tag_id", methods=['GET']) +def get_id(): + global users + global finished + tag_id = int(request.args.get("id")) + c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}") + tag_list = c.fetchall() + if tag_list != []: + tag = tag_list[0] + if users.qsize() == 0: + c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}") + conn.commit() + return make_response("True") + + elif users.qsize() > 0: + user = users.get() + c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") + conn.commit() + finished = user + return make_response("True") + + return make_response("False") + +def main(): + atexit.register(exit_handler) + socketio.run(app) +main() diff --git a/mate.db b/mate.db new file mode 100644 index 0000000000000000000000000000000000000000..d141ef3aaf4e9adeea8397b1df5f5ffc110b52b1 GIT binary patch literal 24576 zcmeI(!E4h{9KiASY}c-ZCL*g5#PNs+HxOZXoQKu%Hi|XVEP^|g%w^Kib+}|q@G!dn z$CLkvSHYVI{slYxUb4=ubhm+{d=p;s@_Wt8@AtVql5W23^s+?l9vtq+nR+hn3Ms{k zQbNe85TasQ*|a;R)z16oly7p>x&CbCc((Vw;hu<+GcTN<&NpYiaqN7#{tiYEKmY** z5I_I{1Q0*~fhiMkPvnbRRFYYseVvrf@Lr|rd7eBiW$|`DIZBhmG(VJ9L*0tBidru_ zTKRd*R}1x;-`nwP?I6HKdlSAiv>o^3!3Q%k;l4Lv z`*kbaQm^#Zf}MM@zW8LO;w?9&=nZy~&*`VWS-*DUqwFBR?q>1Fw7X=Fm7ywEy=GIM zKF+hZG23RY=59kp>ZVs@KQZ}Bg_(NRHbUKA4~o2OZ8kR*>NOqeU{!C9Op2t<4Oa>& zo@8n<=)+0y#1KFL0R#|0009ILKmY**5I_Kd8xv@lnWTFr+%xyjjZMl#2q1s}0tg_0 z00IagfB*srAn?Bg9{hXotSpP>Y?h?idCY#xZ@=+pLjVB;5I_I{1Q0*~0R#|00D&nK zu + + + + \ No newline at end of file diff --git a/templates/adduser.html b/templates/adduser.html new file mode 100644 index 0000000..feb180b --- /dev/null +++ b/templates/adduser.html @@ -0,0 +1,7 @@ + + +

+ Username: +

+

+ \ No newline at end of file diff --git a/templates/change.html b/templates/change.html new file mode 100644 index 0000000..cb7aad4 --- /dev/null +++ b/templates/change.html @@ -0,0 +1,26 @@ + + + + + \ No newline at end of file