summaryrefslogtreecommitdiff
path: root/Matchmaking
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-10-27 15:52:54 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-10-27 15:52:54 +0100
commita43499efa6c228cea3633f9661f3ff09127bd82d (patch)
tree6952ed33fe789e2ec3748fa189ea2ddb08efc1aa /Matchmaking
parent15a1d2a66e48324e1d560dc13b776627820f3999 (diff)
next commit
Diffstat (limited to 'Matchmaking')
-rw-r--r--Matchmaking/Server/dbbin0 -> 16384 bytes
-rw-r--r--Matchmaking/Server/index.php139
2 files changed, 139 insertions, 0 deletions
diff --git a/Matchmaking/Server/db b/Matchmaking/Server/db
new file mode 100644
index 0000000..9e551a0
--- /dev/null
+++ b/Matchmaking/Server/db
Binary files 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 @@
+<?php
+
+/*
+ * Routes:
+ * /host ?host&port=$port&name=$name
+ * /get-list ?get-list
+ * /join ?join=$id
+ */
+
+global $db;
+$db = new PDO('sqlite:./db', options: [
+ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+]);
+
+if (php_sapi_name() == 'cli') {
+ if ($argv[$argc - 1] == 'init-db') {
+ $db->query(<<<SQL
+ create table if not exists games (
+ id text primary key,
+ ip text unique,
+ port integer,
+ timestamp integer,
+ name text
+ );
+ SQL)
+ ->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(<<<SQL
+ insert into games (id, ip, port, timestamp, name) values (:id, :ip, :port, :timestamp, :name)
+ SQL)
+ ->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(<<<SQL
+ select id, name from games
+ SQL);
+ $statement->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(<<<SQL
+ select ip, port from games
+ where id = :id
+ SQL);
+ $statement->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(<<<SQL
+ delete from games where id = :id
+ SQL)
+ ->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);