diff options
Diffstat (limited to 'Game/Lobby')
-rw-r--r-- | Game/Lobby/Server/.gitignore | 1 | ||||
-rw-r--r-- | Game/Lobby/Server/db | bin | 16384 -> 0 bytes | |||
-rw-r--r-- | Game/Lobby/Server/index.php | 58 |
3 files changed, 50 insertions, 9 deletions
diff --git a/Game/Lobby/Server/.gitignore b/Game/Lobby/Server/.gitignore new file mode 100644 index 0000000..c2de89b --- /dev/null +++ b/Game/Lobby/Server/.gitignore @@ -0,0 +1 @@ +/db diff --git a/Game/Lobby/Server/db b/Game/Lobby/Server/db Binary files differdeleted file mode 100644 index 9e551a0..0000000 --- a/Game/Lobby/Server/db +++ /dev/null diff --git a/Game/Lobby/Server/index.php b/Game/Lobby/Server/index.php index de7d5c0..16be3ef 100644 --- a/Game/Lobby/Server/index.php +++ b/Game/Lobby/Server/index.php @@ -7,8 +7,11 @@ * /join ?join=$id */ +$dbname = $_ENV['DB_NAME'] ?? 'db'; +$timeoutMax = intval($_ENV['TIMEOUT_MAX'] ?? (1 * 60)); + global $db; -$db = new PDO('sqlite:./db', options: [ +$db = new PDO('sqlite:./' . $dbname, options: [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); @@ -19,7 +22,8 @@ if (php_sapi_name() == 'cli') { id text primary key, ip text unique, port integer, - timestamp integer, + creation_time integer, + ping_time integer, name text ); SQL) @@ -29,6 +33,11 @@ if (php_sapi_name() == 'cli') { return; } +$inputPost = json_decode(file_get_contents('php://input'), true); +if ($inputPost) { + $_POST = $inputPost + $_POST; +} + $response = []; if (isset($_POST['host'])) { @@ -44,29 +53,42 @@ if (isset($_POST['host'])) { else { $port = intval($_POST['port']); $name = $_POST['name']; + $id = md5($name . time()); $success = $db->prepare(<<<SQL - insert into games (id, ip, port, timestamp, name) values (:id, :ip, :port, :timestamp, :name) + insert or replace into games (id, ip, port, creation_time, ping_time, name) values (:id, :ip, :port, :timestamp, :timestamp, :name) SQL) ->execute([ - 'id' => md5($name . time()), + 'id' => $id, 'ip' => $ip, 'port' => $port, 'timestamp' => time(), 'name' => $name, ]); - $response = [ - 'success' => $success, - ]; + + if ($success) { + $response = [ + 'success' => $success, + 'data' => $id, + ]; + } else { + $response = [ + 'success' => $success, + 'error' => $db->errorInfo(), + ]; + } } } else if (isset($_GET['get-list'])) { $statement = $db->prepare(<<<SQL select id, name from games + where ping_time > :comparison_time SQL); - $statement->execute(); + $statement->execute([ + 'comparison_time' => time() - $timeoutMax, + ]); $results = $statement->fetchAll(); $list = []; @@ -90,7 +112,9 @@ else if (isset($_GET['join'])) { select ip, port from games where id = :id SQL); - $statement->execute(['id' => $id]); + $statement->execute([ + 'id' => $id, + ]); $result = $statement->fetch(); if ($result === false) { @@ -127,6 +151,22 @@ else if (isset($_POST['close'])) { ]; } +else if (isset($_POST['keep-alive'])) { + $id = $_POST['keep-alive']; + + $success = $db->prepare(<<<SQL + update games set ping_time = :ping_time where id = :id + SQL) + ->execute([ + 'id' => $id, + 'ping_time' => time(), + ]); + + $response = [ + 'success' => $success, + ]; +} + else { $response = [ 'success' => false, |