From de54590ac96e4797cb03a6e7184f8809c6bbaf19 Mon Sep 17 00:00:00 2001 From: Jacob Wilkins Date: Sun, 21 Mar 2021 23:54:25 +0000 Subject: [PATCH] Working online test --- Board.gd | 3 +-- FileParser.gd | 2 +- Game.gd | 2 +- Globals.gd | 3 +++ Lobby.gd | 16 +++++++++++----- MainGame.gd | 19 +++++++++++++------ Piece.gd | 13 +++++++++---- Piece.tscn | 1 + Root.gd | 15 ++++++++++++++- TopMenu.gd | 3 ++- TopMenu.tscn | 18 +++++++++++++++++- UI.gd | 4 ++-- UI.tscn | 12 ++++++++---- games4.pgn | 16 ++-------------- project.godot | 4 ++++ 15 files changed, 89 insertions(+), 42 deletions(-) create mode 100644 Globals.gd diff --git a/Board.gd b/Board.gd index 998efb9..54f8bc1 100644 --- a/Board.gd +++ b/Board.gd @@ -21,7 +21,6 @@ func add_piece(colour, type, pos, has_moved:bool = false): null.get_node("Crash") var piece = PIECE.instance() - add_child(piece) piece.init(colour, type) # Find used names @@ -39,7 +38,7 @@ func add_piece(colour, type, pos, has_moved:bool = false): i += 1 trial_name = piece.get_ID()+str(i) piece.ID = trial_name - + add_child(piece) update_piece(piece, pos) piece.moved = has_moved diff --git a/FileParser.gd b/FileParser.gd index 6ec91c2..81cfe21 100644 --- a/FileParser.gd +++ b/FileParser.gd @@ -105,7 +105,7 @@ func parse_SAN(SAN: String) -> Game: else: parsed_turns.push_back(currTurn) - if not has_turn(turns[-1], "Black"): # Catch leftover + if len(turns)>0 and not has_turn(turns[-1], "Black"): # Catch leftover parsed_turns.push_back(turns[-1]) turns.clear() # Clean up diff --git a/Game.gd b/Game.gd index 31cb584..f992703 100644 --- a/Game.gd +++ b/Game.gd @@ -7,7 +7,7 @@ var turns := [Turn.new()] func _to_string() -> String: var string = "" for elem in data: - string += '["{elem}" "{data}"]\n'.format({"elem":elem, "data":data[elem]}) + string += '[{elem} "{data}"]\n'.format({"elem":elem, "data":data[elem]}) string += "\n" var capture: String var promote: String diff --git a/Globals.gd b/Globals.gd new file mode 100644 index 0000000..dd20066 --- /dev/null +++ b/Globals.gd @@ -0,0 +1,3 @@ +extends Node + +var online = false diff --git a/Lobby.gd b/Lobby.gd index 534e80e..047e2f0 100644 --- a/Lobby.gd +++ b/Lobby.gd @@ -1,6 +1,9 @@ extends Node signal sync_games(id) +signal server_connected(serverIP, port) +signal server_started(port) +signal server_disconnected() const PLAYERS = 2 const HOST_ID = 1 @@ -14,7 +17,6 @@ func _ready() -> void: get_tree().connect("network_peer_disconnected", self, "_close_connection") get_tree().connect("connection_failed", self, "_connection_failed") get_tree().connect("server_disconnected", self, "_server_disconnected") - self._close_connection() func host(port: int, pwIn: String): _close_connection() # In case of restart @@ -23,6 +25,8 @@ func host(port: int, pwIn: String): get_tree().set_network_peer(peer) my_id = HOST_ID self.pw = pwIn + Globals.online = true + emit_signal("server_started", port, self.pw) func client(ip: String, port: int, pwIn: String): _close_connection() # In case of restart @@ -33,18 +37,20 @@ func client(ip: String, port: int, pwIn: String): var peer = NetworkedMultiplayerENet.new() peer.create_client(self.targetIP, self.targetPort) get_tree().set_network_peer(peer) + emit_signal("server_connected", self.targetIP, self.targetPort) + Globals.online = true func _check_pw(id, pwIn): if pwIn != pw: rpc_id(id, "error", "Cannot join", "Passwords don't match") - get_tree().network_peer.disconnect_peer(id) + rpc_id(id, "_close_connection") + get_tree().network_peer.disconnect_peer(id) remote func _close_connection(): if get_tree().network_peer: get_tree().network_peer.close_connection() - var peer = NetworkedMultiplayerENet.new() - peer.create_server(10000, 1) - get_tree().set_network_peer(peer) + Globals.online = false + emit_signal("server_disconnected") func _player_connected(id: int): emit_signal("sync_games", id) diff --git a/MainGame.gd b/MainGame.gd index 5631715..f51815a 100644 --- a/MainGame.gd +++ b/MainGame.gd @@ -31,7 +31,10 @@ func prev_turn(): load_turn(currTurn) func load_turn(turn: int, loadGame: int = -1): - rpc("_load_turn", turn, loadGame) + if Globals.online: + rpc("_load_turn", turn, loadGame) + else: + _load_turn(turn, loadGame) remotesync func _load_turn(turn: int, loadGame: int = -1): if loadGame < 0: @@ -53,6 +56,7 @@ remotesync func _load_turn(turn: int, loadGame: int = -1): func _on_Board_piece_moved(turn) -> void: self.currPlayer = 1 - self.currPlayer var branching = currTurn < currGame.nTurns()-1 + print(branching) if branching: var newBranch = branch(currGame, currTurn) new_game(newBranch) @@ -66,6 +70,8 @@ func _on_Board_piece_moved(turn) -> void: func new_game(game: Game): self.games.push_back(game) + if Globals.online: + sync_games() emit_signal("new_game", game) load_turn(0, len(games)-1) @@ -78,14 +84,14 @@ func branch(toCopy: Game = null, turn: int = -1) -> Game: var copy = Game.new() copy.data = toCopy.data copy.turns = toCopy.turns.slice(0, turn) - var basename = toCopy.path.split(':')[0] + var basename = toCopy.data.get("DisplayName").split(':')[0] var num = 0 for game in self.games: - var split = game.path.split(':') + var split = game.data.get("DisplayName").split(':') if split[0] == basename and len(split) > 1: num = max(num, split[1].to_int()) - copy.path = basename + ":" + str(num+1) + copy.data["DisplayName"] = basename + ":" + str(num+1) return copy master func sync_games(id: int = 0): @@ -99,11 +105,12 @@ master func sync_games(id: int = 0): else: rpc_id(id, "set_games", encode, self.currGameIndex, self.currTurn) -remote func set_games(gameBytes: String, gameIdx: int, turn: int): +puppet func set_games(gameBytes: String, gameIdx: int, turn: int): var FP = FileParser.new() self.games = FP.parse_matches(gameBytes) emit_signal("reload_games") - load_turn(turn, gameIdx) + if self.games: + load_turn(turn, gameIdx) func _error(title, message): get_tree().get_root().get_node("Root").error(title, message) diff --git a/Piece.gd b/Piece.gd index 222a2db..dba1851 100644 --- a/Piece.gd +++ b/Piece.gd @@ -25,8 +25,9 @@ var ID : String func _process(delta: float) -> void: if $Draggable.dragging: rpc_unreliable("set_pos", get_global_mouse_position()) - -remotesync func set_pos(newPos: Vector2): + set_pos(get_global_mouse_position()) + +remote func set_pos(newPos: Vector2): self.position = newPos func init(colour: int, type: int) -> void: @@ -88,8 +89,12 @@ func can_move(newLoc, capture: bool, boardIn) -> bool: func _on_Draggable_stopdrag() -> void: var mousePos = get_global_mouse_position() var newPos = board.world_to_map(mousePos) - board.rpc("move_piece", self.gridPos, newPos) - rpc("move_to_pos") + if Globals.online: + board.rpc("move_piece", self.gridPos, newPos) + rpc("move_to_pos") + else: + board.move_piece(self.gridPos, newPos) + self.move_to_pos() func move_to_pos(): self.position = board.map_to_world(self.gridPos) + board.TILE_OFFSET diff --git a/Piece.tscn b/Piece.tscn index 05e5861..13079a7 100644 --- a/Piece.tscn +++ b/Piece.tscn @@ -6,6 +6,7 @@ [node name="Piece" type="Sprite"] scale = Vector2( 0.3, 0.3 ) +z_index = 3 texture = ExtResource( 1 ) vframes = 2 hframes = 6 diff --git a/Root.gd b/Root.gd index 0c8c0a5..00ddcdf 100644 --- a/Root.gd +++ b/Root.gd @@ -3,7 +3,7 @@ extends Node onready var UIMain = $VBoxContainer/HBoxContainer/UIPanel onready var MainGame = $VBoxContainer/HBoxContainer/ViewportContainer/MainGame onready var GamesList = $GamesDialog/ScrollContainer/VBoxContainer - +onready var ConnectionStatus = $VBoxContainer/TopMenu/HBoxContainer/ConnectionStatus func new_game(game: Game): MainGame.new_game(game) @@ -41,3 +41,16 @@ func add_button(game: Game): button.text = "%s v. %s" % [game.data.get("White", "Unknown"), game.data.get("Black", "Unknown")] GamesList.add_child(button) GamesList.move_child(button, GamesList.get_child_count()-1) + + +func _on_Lobby_server_connected(serverIP, port) -> void: + ConnectionStatus.text = "Connected to %s:%d" % [serverIP, port] + +func _on_Lobby_server_disconnected() -> void: + ConnectionStatus.text = "Offline" + +func _on_Lobby_server_started(port, pw) -> void: + ConnectionStatus.text = "Hosting on port %d" % port + if pw: + ConnectionStatus.text += " -- Password: %s" % pw + diff --git a/TopMenu.gd b/TopMenu.gd index 5eb3f93..e529027 100644 --- a/TopMenu.gd +++ b/TopMenu.gd @@ -22,6 +22,7 @@ signal close_game signal host(port, pw) signal connect(ip, port, pw) +signal disconnect() var file_loc: String @@ -102,7 +103,7 @@ func _on_NetworkMenu_id_pressed(id: int) -> void: self.port = "" self.pw = "" NETWORK.DISCONNECT: - get_tree().network_peer = null + emit_signal("disconnect") func _on_FileDialog_file_selected(path: String) -> void: self.file_loc = path diff --git a/TopMenu.tscn b/TopMenu.tscn index d4c50a1..98d569d 100644 --- a/TopMenu.tscn +++ b/TopMenu.tscn @@ -40,6 +40,22 @@ text = "Network" items = [ "Connect to...", null, 0, false, false, 1, 0, null, "", false, "Disconnect", null, 0, false, false, 2, 0, null, "", false, "Host", null, 0, false, false, 0, 0, null, "", false ] switch_on_hover = true +[node name="VSeparator" type="VSeparator" parent="HBoxContainer"] +margin_left = 162.0 +margin_right = 166.0 +margin_bottom = 140.0 + +[node name="ConnectionStatus" type="Label" parent="HBoxContainer"] +margin_left = 170.0 +margin_top = 63.0 +margin_right = 213.0 +margin_bottom = 77.0 +text = "Offline" +align = 2 +__meta__ = { +"_edit_use_anchors_": false +} + [node name="FileDialog" type="FileDialog" parent="."] margin_right = 416.0 margin_bottom = 184.0 @@ -47,6 +63,7 @@ rect_min_size = Vector2( 400, 140 ) filters = PoolStringArray( "*.pgn" ) [node name="Network" type="ConfirmationDialog" parent="."] +visible = true margin_left = 7.0 margin_top = 7.0 margin_right = 1017.0 @@ -144,7 +161,6 @@ __meta__ = { margin_left = 188.0 margin_right = 246.0 margin_bottom = 24.0 -secret = true __meta__ = { "_edit_use_anchors_": false } diff --git a/UI.gd b/UI.gd index e89c0a9..6923b31 100644 --- a/UI.gd +++ b/UI.gd @@ -62,8 +62,8 @@ func close_game() -> void: # Delete from stored games game.games.remove(idx) - self.lastGame = 0 - self.lastTurn = 0 + self.lastGame = -1 + self.lastTurn = -1 reload_games() diff --git a/UI.tscn b/UI.tscn index 945dd22..17bdc95 100644 --- a/UI.tscn +++ b/UI.tscn @@ -28,9 +28,10 @@ __meta__ = { } [node name="TopMenu" parent="VBoxContainer" instance=ExtResource( 4 )] +margin_bottom = 154.0 [node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] -margin_top = 32.0 +margin_top = 152.0 margin_right = 1024.0 margin_bottom = 600.0 mouse_filter = 2 @@ -42,7 +43,7 @@ __meta__ = { [node name="ViewportContainer" type="ViewportContainer" parent="VBoxContainer/HBoxContainer"] margin_right = 720.0 -margin_bottom = 568.0 +margin_bottom = 448.0 mouse_filter = 2 size_flags_horizontal = 3 size_flags_vertical = 3 @@ -53,7 +54,7 @@ __meta__ = { } [node name="MainGame" parent="VBoxContainer/HBoxContainer/ViewportContainer" instance=ExtResource( 1 )] -size = Vector2( 720, 568 ) +size = Vector2( 720, 448 ) handle_input_locally = false render_target_update_mode = 3 @@ -63,7 +64,7 @@ anchor_right = 0.0 anchor_bottom = 0.0 margin_left = 724.0 margin_right = 1024.0 -margin_bottom = 568.0 +margin_bottom = 448.0 size_flags_horizontal = 1 [node name="FileParser" parent="." instance=ExtResource( 5 )] @@ -116,4 +117,7 @@ script = ExtResource( 6 ) [connection signal="turn_updated" from="VBoxContainer/HBoxContainer/ViewportContainer/MainGame" to="VBoxContainer/HBoxContainer/UIPanel" method="update_turn"] [connection signal="panic_game" from="VBoxContainer/HBoxContainer/UIPanel" to="." method="new_game"] [connection signal="read" from="FileParser" to="." method="_on_FileParser_read"] +[connection signal="server_connected" from="Lobby" to="." method="_on_Lobby_server_connected"] +[connection signal="server_disconnected" from="Lobby" to="." method="_on_Lobby_server_disconnected"] +[connection signal="server_started" from="Lobby" to="." method="_on_Lobby_server_started"] [connection signal="sync_games" from="Lobby" to="VBoxContainer/HBoxContainer/ViewportContainer/MainGame" method="sync_games"] diff --git a/games4.pgn b/games4.pgn index e31eeef..ff25758 100644 --- a/games4.pgn +++ b/games4.pgn @@ -1,15 +1,3 @@ -["Event" "Witney U170"] -["Site" "Cokethorpe School"] -["Date" "2015.10.18"] -["Round" "5"] -["White" "Ian Bush"] -["Black" "Shayarna Sivarajasingam"] -["Result" "1-0"] -["WhiteElo" "139"] -["BlackElo" "143"] -["ECO" "C10n"] -["WhiteBCF" "139"] -["BlackBCF" "142"] -["FlipB" "1"] +["DisplayName" "New game"] -1. Pe2e4 Pe7e6 2. Pd2d4 Pd7d5 3. Nb1c3 Pd5xe4 4. Nc3xe4 Nb8d7 5. Ng1f3 Ng8f6 6. Bf1d3 Nf6xe4 7. Bd3xe4 Nd7f6 8. Be4d3 Bf8e7 9. O-O O-O 10. Bc1g5 Ph7h6 11. Bg5h4 Pc7c5 12. Rf1e1 Qd8b6 13. Bd3c4 Qb6xb2 14. Pd4d5 Qb2b4 15. Pd5d6 Rf8d8 16. Pd6xe7 Rd8xd1 17. Ra1xd1 Pg7g5 18. Rd1d8 Kg8g7 19. Bh4g3 Pg5g4 20. Bg3e5 Pg4xf3 21. Re1d1 Qb4xc4 22. Be5xf6 Kg7xf6 23. Pe7e8=Q Qc4g4 24. Qe8g8 Qg4xg8 25. Rd8xg8 Pf3xg2 26. Rd1d8 Pa7a5 27. Rd8xc8 Ra8a6 28. Rc8a8 Ra6b6 29. Ra8xa5 Rb6b2 30. Pc2c4 Rb2c2 31. Ra5xc5 Rc2xa2 32. Rc5b5 Ra2c2 33. Pc4c5 Ph6h5 34. Rg8b8 Ph5h4 35. Kg1xg2 Rc2c3 36. Ph2h3 Pe6e5 37. Rb8xb7 Pe5e4 38. Rb7b6 Kf6e5 39. Pc5c6 Ke5f4 40. Rb5b3 Rc3c2 41. Pc6c7 Pf7f5 42. Rb6b7 Kf4g5 43. Rb3a3 Pf5f4 44. Ra3a5 Kg5g6 45. Rb7b6 Kg6g7 46. Ra5a7 Pf4f3 47. Kg2g1 Rc2c1 48. Kg1h2 Kg7f8 49. Rb6b8 Kf8f7 1-0 \ No newline at end of file +1. Pf2f4 0-0 diff --git a/project.godot b/project.godot index fde190e..518e8d1 100644 --- a/project.godot +++ b/project.godot @@ -48,6 +48,10 @@ config/name="ChessRender" run/main_scene="res://UI.tscn" config/icon="res://icon.png" +[autoload] + +Globals="*res://Globals.gd" + [rendering] environment/default_environment="res://default_env.tres"