summaryrefslogtreecommitdiff
path: root/Game/Lobby
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-11-27 14:36:20 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-11-27 14:36:20 +0100
commita854a1862a30632e49520f6e1e11333d5c8ff241 (patch)
tree2b10c4d19bb66b270a7726d660ed85d7fcf9482e /Game/Lobby
parentcb9bb9d1c757b53707c741a3c8c218b513ffb5ec (diff)
next commit
Diffstat (limited to 'Game/Lobby')
-rw-r--r--Game/Lobby/Server/index.php56
-rw-r--r--Game/Lobby/game_lobby.gd89
2 files changed, 125 insertions, 20 deletions
diff --git a/Game/Lobby/Server/index.php b/Game/Lobby/Server/index.php
index 16be3ef..ae734a6 100644
--- a/Game/Lobby/Server/index.php
+++ b/Game/Lobby/Server/index.php
@@ -2,9 +2,11 @@
/*
* Routes:
- * /host ?host&port=$port&name=$name
- * /get-list ?get-list
- * /join ?join=$id
+ * /host ?port=$port&name=$name
+ * /get-list ?
+ * /join ?id=$id
+ * /close ?id=$id
+ * /keep-alive ?id=$id
*/
$dbname = $_ENV['DB_NAME'] ?? 'db';
@@ -24,7 +26,8 @@ if (php_sapi_name() == 'cli') {
port integer,
creation_time integer,
ping_time integer,
- name text
+ name text,
+ secret text
);
SQL)
->execute();
@@ -33,14 +36,19 @@ if (php_sapi_name() == 'cli') {
return;
}
-$inputPost = json_decode(file_get_contents('php://input'), true);
-if ($inputPost) {
- $_POST = $inputPost + $_POST;
+$url = parse_url("$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
+$method = $_SERVER['REQUEST_METHOD'];
+
+if ($method == 'POST') {
+ $inputPost = json_decode(file_get_contents('php://input'), true);
+ if ($inputPost) {
+ $_POST = $inputPost + $_POST;
+ }
}
$response = [];
-if (isset($_POST['host'])) {
+if ($method == 'POST' && $url['path'] == '/host') {
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);
if ($ip === false) {
@@ -54,9 +62,10 @@ if (isset($_POST['host'])) {
$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) values (:id, :ip, :port, :timestamp, :timestamp, :name)
+ 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,
@@ -64,13 +73,16 @@ if (isset($_POST['host'])) {
'port' => $port,
'timestamp' => time(),
'name' => $name,
+ 'secret' => $secret,
]);
-
if ($success) {
$response = [
'success' => $success,
- 'data' => $id,
+ 'data' => [
+ 'id' => $id,
+ 'secret' => $secret,
+ ],
];
} else {
$response = [
@@ -81,7 +93,7 @@ if (isset($_POST['host'])) {
}
}
-else if (isset($_GET['get-list'])) {
+else if ($method == 'GET' && $url['path'] == '/get-list') {
$statement = $db->prepare(<<<SQL
select id, name from games
where ping_time > :comparison_time
@@ -105,8 +117,8 @@ else if (isset($_GET['get-list'])) {
];
}
-else if (isset($_GET['join'])) {
- $id = $_GET['join'];
+else if ($method == 'GET' && $url['path'] == '/join') {
+ $id = $_GET['id'];
$statement = $db->prepare(<<<SQL
select ip, port from games
@@ -136,14 +148,16 @@ else if (isset($_GET['join'])) {
}
}
-else if (isset($_POST['close'])) {
- $id = $_POST['close'];
+else if ($method == 'POST' && $url['path'] == '/close') {
+ $id = $_POST['id'];
+ $secret = $_POST['secret'];
$success = $db->prepare(<<<SQL
- delete from games where id = :id
+ delete from games where id = :id and secret = :secret
SQL)
->execute([
'id' => $id,
+ 'secret' => $secret,
]);
$response = [
@@ -151,14 +165,16 @@ else if (isset($_POST['close'])) {
];
}
-else if (isset($_POST['keep-alive'])) {
- $id = $_POST['keep-alive'];
+else if ($method == 'POST' && $url['path'] == '/keep-alive') {
+ $id = $_POST['id'];
+ $secret = $_POST['secret'];
$success = $db->prepare(<<<SQL
- update games set ping_time = :ping_time where id = :id
+ update games set ping_time = :ping_time where id = :id and secret = :secret
SQL)
->execute([
'id' => $id,
+ 'secret' => $secret,
'ping_time' => time(),
]);
diff --git a/Game/Lobby/game_lobby.gd b/Game/Lobby/game_lobby.gd
new file mode 100644
index 0000000..6bfa8be
--- /dev/null
+++ b/Game/Lobby/game_lobby.gd
@@ -0,0 +1,89 @@
+extends Node
+
+
+class HTTPRequestConfig extends Resource:
+ var headers: PackedStringArray
+ var method: HTTPClient.Method
+ var data: Dictionary
+ var node: HTTPRequest
+
+ @warning_ignore("shadowed_variable")
+ func _init(
+ headers := PackedStringArray(),
+ method := HTTPClient.METHOD_GET,
+ data := {},
+ node := HTTPRequest.new()
+ ) -> void:
+ self.headers = headers
+ self.method = method
+ self.data = data
+ self.node = node
+
+
+class HTTPResponse extends Resource:
+ var result_code: HTTPRequest.Result
+ var response_code: int
+ var headers: PackedStringArray
+ var body: PackedByteArray
+
+ func parse_json() -> Dictionary:
+ return JSON.parse_string(body.get_string_from_utf8())
+
+
+var server_base_url: String = Client.get_config().get_value("general", "game_lobby_server_base_url")
+var current_game_id: String
+var current_secret: String
+
+
+func request(
+ url: String,
+ config: HTTPRequestConfig = HTTPRequestConfig.new()
+) -> HTTPResponse:
+ get_tree().root.add_child(config.node)
+
+ if config.method == HTTPClient.METHOD_POST:
+ config.set("id", current_game_id)
+ config.set("secret", current_secret)
+
+ var response = HTTPResponse.new()
+ config.node.request_completed.connect(
+ func (
+ result_code: int,
+ response_code: int,
+ headers: PackedStringArray,
+ body: PackedByteArray
+ ):
+ response.result_code = result_code
+ response.response_code = response_code
+ response.headers = headers
+ response.body = body
+
+ get_tree().root.remove_child(config.node)
+ )
+ config.node.request(
+ "%s%s" % [server_base_url, url],
+ config.headers,
+ config.method,
+ JSON.stringify(config.data)
+ )
+
+ await config.node.request_completed
+ return response
+
+
+func request_get(url: String, headers := PackedStringArray()):
+ var config := HTTPRequestConfig.new(headers)
+
+ return await request(url, config)
+
+
+func request_post(url: String, data: Dictionary = {}, headers := PackedStringArray()):
+ var config := HTTPRequestConfig.new(headers, HTTPClient.METHOD_POST, data)
+
+ return await request(url, config)
+
+
+func check_available() -> bool:
+ var response = await GameLobby.request_get("")
+
+ return response.result_code == HTTPRequest.RESULT_SUCCESS