From a43499efa6c228cea3633f9661f3ff09127bd82d Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 27 Oct 2024 15:52:54 +0100 Subject: next commit --- Matchmaking/Server/db | Bin 0 -> 16384 bytes Matchmaking/Server/index.php | 139 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 Matchmaking/Server/db create mode 100644 Matchmaking/Server/index.php diff --git a/Matchmaking/Server/db b/Matchmaking/Server/db new file mode 100644 index 0000000..9e551a0 Binary files /dev/null and b/Matchmaking/Server/db differ diff --git a/Matchmaking/Server/index.php b/Matchmaking/Server/index.php new file mode 100644 index 0000000..de7d5c0 --- /dev/null +++ b/Matchmaking/Server/index.php @@ -0,0 +1,139 @@ + PDO::FETCH_ASSOC, +]); + +if (php_sapi_name() == 'cli') { + if ($argv[$argc - 1] == 'init-db') { + $db->query(<<execute(); + } + + return; +} + +$response = []; + +if (isset($_POST['host'])) { + $ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP); + + if ($ip === false) { + $response = [ + 'success' => false, + 'error' => 'Invalid IP.', + ]; + } + + else { + $port = intval($_POST['port']); + $name = $_POST['name']; + + $success = $db->prepare(<<execute([ + 'id' => md5($name . time()), + 'ip' => $ip, + 'port' => $port, + 'timestamp' => time(), + 'name' => $name, + ]); + + $response = [ + 'success' => $success, + ]; + } +} + +else if (isset($_GET['get-list'])) { + $statement = $db->prepare(<<execute(); + $results = $statement->fetchAll(); + + $list = []; + foreach ($results as $row) { + $list[] = [ + 'id' => $row['id'], + 'name' => $row['name'], + ]; + } + + $response = [ + 'success' => true, + 'data' => $list, + ]; +} + +else if (isset($_GET['join'])) { + $id = $_GET['join']; + + $statement = $db->prepare(<<execute(['id' => $id]); + $result = $statement->fetch(); + + if ($result === false) { + $response = [ + 'success' => false, + 'error' => 'Game not found.' + ]; + http_response_code(404); + } + + else { + $response = [ + 'success' => true, + 'data' => [ + 'ip' => $result['ip'], + 'port' => $result['port'], + ], + ]; + } +} + +else if (isset($_POST['close'])) { + $id = $_POST['close']; + + $success = $db->prepare(<<execute([ + 'id' => $id, + ]); + + $response = [ + 'success' => $success, + ]; +} + +else { + $response = [ + 'success' => false, + 'error' => 'Route not found.' + ]; + http_response_code(404); +} + +header('Content-Type: application/json'); +echo json_encode($response); -- cgit v1.2.3