diff options
Diffstat (limited to 'Game/Lobby/Server/index.php')
-rw-r--r-- | Game/Lobby/Server/index.php | 186 |
1 files changed, 93 insertions, 93 deletions
diff --git a/Game/Lobby/Server/index.php b/Game/Lobby/Server/index.php index 1086d24..7d12195 100644 --- a/Game/Lobby/Server/index.php +++ b/Game/Lobby/Server/index.php @@ -19,18 +19,18 @@ $db = new PDO('sqlite:./' . $dbname, options: [ 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, - creation_time integer, - ping_time integer, - name text, - secret text - ); - SQL) - ->execute(); + $db->query(<<<SQL + create table if not exists games ( + id text primary key, + ip text unique, + port integer, + creation_time integer, + ping_time integer, + name text, + secret text + ); + SQL) + ->execute(); } return; @@ -42,7 +42,7 @@ $method = $_SERVER['REQUEST_METHOD']; if ($method == 'POST') { $inputPost = json_decode(file_get_contents('php://input'), true); if ($inputPost) { - $_POST = $inputPost + $_POST; + $_POST = $inputPost + $_POST; } } @@ -50,73 +50,73 @@ $response = []; if ($method == 'POST' && $url['path'] == '/host') { $ip = filter_var( - $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'], - FILTER_VALIDATE_IP + $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'], + FILTER_VALIDATE_IP ); if ($ip === false) { - $response = [ - 'success' => false, - 'error' => 'Invalid IP.', - ]; + $response = [ + 'success' => false, + 'error' => 'Invalid IP.', + ]; } else { - $port = intval($_POST['port']); - $name = $_POST['name']; - $id = md5($name . time()); - $secret = md5($id . random_bytes($port)); - - $success = $db->prepare(<<<SQL - insert or replace into games (id, ip, port, creation_time, ping_time, name, secret) values (:id, :ip, :port, :timestamp, :timestamp, :name, :secret) - SQL) - ->execute([ - 'id' => $id, - 'ip' => $ip, - 'port' => $port, - 'timestamp' => time(), - 'name' => $name, - 'secret' => $secret, - ]); - - if ($success) { - $response = [ - 'success' => $success, - 'data' => [ - 'id' => $id, - 'secret' => $secret, - ], - ]; - } else { - $response = [ - 'success' => $success, - 'error' => $db->errorInfo(), - ]; - } + $port = intval($_POST['port']); + $name = $_POST['name']; + $id = md5($name . time()); + $secret = md5($id . random_bytes($port)); + + $success = $db->prepare(<<<SQL + insert or replace into games (id, ip, port, creation_time, ping_time, name, secret) values (:id, :ip, :port, :timestamp, :timestamp, :name, :secret) + SQL) + ->execute([ + 'id' => $id, + 'ip' => $ip, + 'port' => $port, + 'timestamp' => time(), + 'name' => $name, + 'secret' => $secret, + ]); + + if ($success) { + $response = [ + 'success' => $success, + 'data' => [ + 'id' => $id, + 'secret' => $secret, + ], + ]; + } else { + $response = [ + 'success' => $success, + 'error' => $db->errorInfo(), + ]; + } } } else if ($method == 'GET' && $url['path'] == '/get-list') { $statement = $db->prepare(<<<SQL - select id, name from games - where ping_time > :comparison_time - SQL); + select id, name from games + where ping_time > :comparison_time + SQL); $statement->execute([ - 'comparison_time' => time() - $timeoutMax, + 'comparison_time' => time() - $timeoutMax, ]); $results = $statement->fetchAll(); $list = []; foreach ($results as $row) { - $list[] = [ - 'id' => $row['id'], - 'name' => $row['name'], - ]; + $list[] = [ + 'id' => $row['id'], + 'name' => $row['name'], + ]; } $response = [ - 'success' => true, - 'data' => $list, + 'success' => true, + 'data' => $list, ]; } @@ -124,30 +124,30 @@ else if ($method == 'GET' && $url['path'] == '/join') { $id = $_GET['id']; $statement = $db->prepare(<<<SQL - select ip, port from games - where id = :id - SQL); + select ip, port from games + where id = :id + SQL); $statement->execute([ - 'id' => $id, + 'id' => $id, ]); $result = $statement->fetch(); if ($result === false) { - $response = [ - 'success' => false, - 'error' => 'Game not found.' - ]; - http_response_code(404); + $response = [ + 'success' => false, + 'error' => 'Game not found.' + ]; + http_response_code(404); } else { - $response = [ - 'success' => true, - 'data' => [ - 'ip' => $result['ip'], - 'port' => $result['port'], - ], - ]; + $response = [ + 'success' => true, + 'data' => [ + 'ip' => $result['ip'], + 'port' => $result['port'], + ], + ]; } } @@ -156,15 +156,15 @@ else if ($method == 'POST' && $url['path'] == '/close') { $secret = $_POST['secret']; $success = $db->prepare(<<<SQL - delete from games where id = :id and secret = :secret - SQL) - ->execute([ - 'id' => $id, - 'secret' => $secret, - ]); + delete from games where id = :id and secret = :secret + SQL) + ->execute([ + 'id' => $id, + 'secret' => $secret, + ]); $response = [ - 'success' => $success, + 'success' => $success, ]; } @@ -173,23 +173,23 @@ else if ($method == 'POST' && $url['path'] == '/keep-alive') { $secret = $_POST['secret']; $success = $db->prepare(<<<SQL - update games set ping_time = :ping_time where id = :id and secret = :secret - SQL) - ->execute([ - 'id' => $id, - 'secret' => $secret, - 'ping_time' => time(), - ]); + update games set ping_time = :ping_time where id = :id and secret = :secret + SQL) + ->execute([ + 'id' => $id, + 'secret' => $secret, + 'ping_time' => time(), + ]); $response = [ - 'success' => $success, + 'success' => $success, ]; } else { $response = [ - 'success' => false, - 'error' => 'Route not found.' + 'success' => false, + 'error' => 'Route not found.' ]; http_response_code(404); } |