diff options
Diffstat (limited to 'Game/Lobby/Server/index.php')
-rw-r--r-- | Game/Lobby/Server/index.php | 58 |
1 files changed, 49 insertions, 9 deletions
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, |