diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-11-26 17:13:40 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-11-26 17:13:40 +0100 |
commit | cb9bb9d1c757b53707c741a3c8c218b513ffb5ec (patch) | |
tree | 1e3741a70672504d71685db98b22f7fe61e0e6cb /Game | |
parent | 72863ffa31e27f4c6be862d80472cfb897fccaea (diff) |
next commit
Diffstat (limited to 'Game')
-rw-r--r-- | Game/Client.gd | 63 | ||||
-rw-r--r-- | Game/Client.tscn | 9 | ||||
-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 | ||||
-rw-r--r-- | Game/Network.gd | 29 |
6 files changed, 120 insertions, 40 deletions
diff --git a/Game/Client.gd b/Game/Client.gd index 13fae56..fd24a07 100644 --- a/Game/Client.gd +++ b/Game/Client.gd @@ -9,6 +9,12 @@ signal placed_tower(tower: Tower) signal multi_select_finished(nodes) +var previous_scene: String + +var game_lobby_url := "http://localhost:8910" +@onready var game_lobby_server: HTTPRequest = %GameLobbyServer +var current_game_id := "" + var state: State : set(value): state = value @@ -23,6 +29,12 @@ var player: Player: return Network.get_player(multiplayer.get_unique_id()) +func _notification(what: int) -> void: + if what == NOTIFICATION_WM_CLOSE_REQUEST: + if multiplayer.is_server(): + await Network.close_game() # TODO: doesn't work? + + func initialize_stage(stage: Stage): current_stage = stage @@ -39,14 +51,6 @@ func place_tower(tower: Tower): tower.owner_id = network_id tower.name = "Tower@" + str(network_id) + "@" + str(Time.get_ticks_usec()) - #tower.attack_range = [ - #current_stage.map.tile_set.tile_size.x * 2, - #current_stage.map.tile_set.tile_size.x * 3, - #current_stage.map.tile_set.tile_size.x * 4, - #].pick_random() - #tower.attack_power = [1, 2, 3, 4].pick_random() - #tower.attack_speed = [1, 2, 3, 4].pick_random() - current_stage.place_tower.rpc(inst_to_dict(tower.to_network_data())) @@ -83,7 +87,7 @@ func update_tower(path: NodePath, data: Tower.NetworkData): func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null): - if not current_stage.can_spawn_unit(): + if not current_stage.can_spawn_unit(unit): return var network_id = multiplayer.get_unique_id() @@ -92,13 +96,10 @@ func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null): unit.global_position = spawn.spawn_position unit.target = spawn.next_node - #unit.hp = randi_range(5, 10) #20000b - #unit.base_speed = randi_range(50, 150) if overwrite_target: unit.target = overwrite_target - #var data := UnitCreationData.fromUnit(unit) current_stage.spawn_unit.rpc(inst_to_dict(unit.to_network_data())) @@ -124,3 +125,41 @@ func array_intersect(first, second): return true return false + + +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()) + +func request( + url: String, + custom_headers: PackedStringArray = PackedStringArray(), + method: HTTPClient.Method = HTTPClient.METHOD_GET, + request_data: String = "" +) -> HTTPResponse: + var response = HTTPResponse.new() + + %GameLobbyServer.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 + ) + %GameLobbyServer.request(url, custom_headers, method, request_data) + + await %GameLobbyServer.request_completed + return response + +func check_game_lobby_available() -> bool: + var response = await Client.request("%s" % [Client.game_lobby_url], [], HTTPClient.METHOD_GET) + return response.result_code == HTTPRequest.RESULT_SUCCESS diff --git a/Game/Client.tscn b/Game/Client.tscn new file mode 100644 index 0000000..ba9a1a8 --- /dev/null +++ b/Game/Client.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3 uid="uid://uf2qebewyohd"] + +[ext_resource type="Script" path="res://Game/Client.gd" id="1_kj0qy"] + +[node name="Client" type="Node"] +script = ExtResource("1_kj0qy") + +[node name="GameLobbyServer" type="HTTPRequest" parent="."] +unique_name_in_owner = true 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, diff --git a/Game/Network.gd b/Game/Network.gd index 5dcb074..23542e2 100644 --- a/Game/Network.gd +++ b/Game/Network.gd @@ -32,6 +32,14 @@ func join_game(ip, port): Client.player.id = multiplayer.get_unique_id() +func close_game(): + multiplayer.multiplayer_peer.close() + multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new() + reset_players() + + await Client.request(Client.game_lobby_url, [], HTTPClient.METHOD_POST, JSON.stringify({"close": Client.current_game_id})) + Client.current_game_id = "" + func _on_connected_to_server(): print(multiplayer.get_unique_id(), ": connected to server") @@ -39,8 +47,7 @@ func _on_connected_to_server(): func _on_disconnected_from_server(): print("disconnected from server") - multiplayer.multiplayer_peer = OfflineMultiplayerPeer.new() - reset_players() + close_game() func _on_peer_connected(id: int): print(multiplayer.get_unique_id(), ": peer connected: ", id) @@ -52,16 +59,12 @@ func _on_peer_connected(id: int): elif is_running: get_tree().change_scene_to_file("res://UI/Start.tscn") ) - - # TODO: add existing towers to new peers func _on_peer_disconnected(id: int): print(multiplayer.get_unique_id(), ": peer disconnected: ", id) remove_player(id) - # TODO: move towers owned by peer to host - if id == 1: # if host disconnected go back to Start get_tree().change_scene_to_file("res://UI/Start.tscn") @@ -139,10 +142,10 @@ func update_player(id: int, remote_data: Dictionary): @rpc("any_peer") -#func destroy_tower(remote_tower: Dictionary): func destroy_tower(remote_data: Dictionary): var data: Tower.NetworkData = dict_to_inst(remote_data) var remote_tower = Tower.from_network_data(data) + var player = get_player(remote_tower.owner_id) var tower = player.towers.get(remote_tower.global_position) @@ -172,15 +175,3 @@ func update_unit(remote_unit_node_path: NodePath, remote_data: Dictionary): if unit: unit.update_with_network_data(data) - #if "hp" in data: - #unit.hp = data.hp - #if "position" in data: - #unit.position = data.position - #if "sprite" in data: - #unit.get_node("Sprite2D").self_modulate = data.sprite.self_modulate - #if "current_path" in data: - #unit.current_path = data.current_path - #unit.line.points = PackedVector2Array(data.current_path) - #if "current_path_idx" in data: - #unit.current_path_idx = data.current_path_idx - |