2025-09-06 02:57:46 +03:00
|
|
|
from flask import Flask, jsonify, request
|
|
|
|
|
import mysql.connector
|
|
|
|
|
import uuid
|
|
|
|
|
import base64
|
|
|
|
|
import json
|
2025-09-06 15:52:22 +03:00
|
|
|
import keyfile
|
2025-09-06 02:57:46 +03:00
|
|
|
|
2025-09-06 15:52:22 +03:00
|
|
|
|
|
|
|
|
API_KEY_NS = keyfile.API_KEY
|
2025-09-06 02:57:46 +03:00
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
# Database connection
|
|
|
|
|
|
2025-09-06 15:55:28 +03:00
|
|
|
db_config = keyfile.requestDBCreds
|
2025-09-06 02:57:46 +03:00
|
|
|
|
|
|
|
|
def get_db_connection():
|
|
|
|
|
try:
|
|
|
|
|
connection = mysql.connector.connect(**db_config)
|
|
|
|
|
return connection
|
|
|
|
|
except mysql.connector.Error as err:
|
|
|
|
|
print(f"Error: {err}")
|
|
|
|
|
return None
|
|
|
|
|
# NEEDS FIXING
|
|
|
|
|
@app.route('/api/tiropitAddUser', methods=['POST'])
|
|
|
|
|
def tiropit_add_user():
|
|
|
|
|
data = request.form
|
|
|
|
|
picture = request.files.get('ProfilePicture')
|
|
|
|
|
user_id = str(uuid.uuid4())
|
|
|
|
|
user_name = data.get('UserName')
|
|
|
|
|
steam_name = data.get('SteamName')
|
|
|
|
|
steam_id = data.get('SteamID') # treat this as string
|
|
|
|
|
level = int(data.get('Level', 0))
|
|
|
|
|
awards = data.get('Awards', '[]')
|
|
|
|
|
user_games = data.get('UserGames', '[]')
|
|
|
|
|
warn_level = int(data.get('WarnLevel', 1))
|
|
|
|
|
banned = data.get('Banned')
|
|
|
|
|
admin_level = int(data.get('AdminLevel', 1))
|
|
|
|
|
user_discord = data.get('UserDiscord')
|
|
|
|
|
user_PIN = data.get('UserPIN')
|
|
|
|
|
friend_code = data.get('FirendCode')
|
|
|
|
|
|
|
|
|
|
if not picture:
|
|
|
|
|
return jsonify({"error": "No profile picture provided"}), 400
|
|
|
|
|
if not user_name or not steam_name or not steam_id:
|
|
|
|
|
return jsonify({"error": "UserName, SteamName, and SteamID are required"}), 400
|
|
|
|
|
if not friend_code or len(friend_code) != 5:
|
|
|
|
|
return jsonify({"error": "FirendCode must be 5 characters long"}), 400
|
|
|
|
|
if not user_PIN or len(user_PIN) != 4:
|
|
|
|
|
return jsonify({"error": "UserPIN must be 4 characters long"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn = get_db_connection()
|
|
|
|
|
Cursor = conn.cursor()
|
|
|
|
|
try:
|
|
|
|
|
Cursor.execute("SELECT 1 FROM users WHERE FriendCode = %s", (friend_code,))
|
|
|
|
|
if Cursor.fetchone():
|
|
|
|
|
return jsonify({"error": "FriendCode already exists"}), 409
|
|
|
|
|
|
|
|
|
|
profile_pic = picture.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cursor.execute("""
|
|
|
|
|
INSERT INTO users (UserID, UserName, SteamName, Level, Awards, ProfilePicture, UserGames, WarnLevel, Banned, AdminLevel, SteamID, UserDiscord, UserPIN, FriendCode)
|
|
|
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
|
|
|
|
""", (
|
|
|
|
|
user_id, user_name, steam_name, level, awards, profile_pic,
|
|
|
|
|
user_games, warn_level, banned, admin_level, steam_id, user_discord, user_PIN, friend_code
|
|
|
|
|
))
|
|
|
|
|
conn.commit()
|
|
|
|
|
except mysql.connector.Error as e:
|
|
|
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
finally:
|
|
|
|
|
Cursor.close()
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
return jsonify({"message": "User added successfully", "UserID": user_id}), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|