From 959a4b1ad6174ebc63b704f42d6689cc7c5a1859 Mon Sep 17 00:00:00 2001 From: kacperks Date: Thu, 6 Jul 2023 12:40:33 +0200 Subject: [PATCH] Simple server list added --- LICENSE | 4 +-- README.md | 4 +-- app.py | 0 model.txt | 6 ----- requirements.txt | 7 +++++ server-list.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 app.py delete mode 100644 model.txt create mode 100644 requirements.txt create mode 100644 server-list.py diff --git a/LICENSE b/LICENSE index 2071b23..52e8194 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,4 @@ -MIT License - -Copyright (c) +Copyright (c) 2023 Cube Software(kacperks,Looki2000,mrkubax10) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 1428d49..a1ceee3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# server-list +# Polygun online -Database of online servers for the PolyGun game \ No newline at end of file +Database of online servers, users and mods for the PolyGun game \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..e69de29 diff --git a/model.txt b/model.txt deleted file mode 100644 index 75b8851..0000000 --- a/model.txt +++ /dev/null @@ -1,6 +0,0 @@ -Server: - IP - PORT ( if not 1337) - Name - Description - Icon(optional) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..37cc6d3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +uuid +configparser +pyjwt +flask +flask_marshmallow +flask_sqlalchemy +functools \ No newline at end of file diff --git a/server-list.py b/server-list.py new file mode 100644 index 0000000..ec174d4 --- /dev/null +++ b/server-list.py @@ -0,0 +1,70 @@ +from flask import Flask, request, jsonify +from flask_sqlalchemy import SQLAlchemy +from datetime import datetime, timedelta +import threading + +app = Flask(__name__) +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///servers.db' +db = SQLAlchemy(app) + +class Server(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(50), nullable=False) + description = db.Column(db.String(100)) + ip = db.Column(db.String(15), nullable=False) + port = db.Column(db.Integer, nullable=False) + last_updated = db.Column(db.DateTime, nullable=False) + + def __init__(self, name, description, ip, port): + self.name = name + self.description = description + self.ip = ip + self.port = port + self.last_updated = datetime.utcnow() + + def update_last_updated(self): + self.last_updated = datetime.utcnow() + +@app.route('/servers', methods=['POST']) +def add_server(): + data = request.get_json() + server = Server(data['name'], data['description'], data['ip'], data['port']) + db.session.add(server) + db.session.commit() + return jsonify({'message': 'Server added successfully'}), 201 + +@app.route('/servers', methods=['GET']) +def get_servers(): + servers = Server.query.all() + server_list = [] + for server in servers: + server_data = { + 'name': server.name, + 'description': server.description, + 'ip': server.ip, + 'port': server.port + } + server_list.append(server_data) + return jsonify(server_list) + +def remove_inactive_servers(): + inactive_threshold = datetime.utcnow() - timedelta(minutes=5) + inactive_servers = Server.query.filter(Server.last_updated < inactive_threshold).all() + for server in inactive_servers: + db.session.delete(server) + db.session.commit() + +def check_inactive_servers(): + while True: + remove_inactive_servers() + # Sleep for 5 minutes before checking again + time.sleep(300) + +if __name__ == '__main__': + db.create_all() + + # Start a separate thread for checking inactive servers + checker_thread = threading.Thread(target=check_inactive_servers) + checker_thread.start() + + app.run() \ No newline at end of file