Skip to content

Commit

Permalink
Working online test
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Mar 21, 2021
1 parent da4a003 commit de54590
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 42 deletions.
3 changes: 1 addition & 2 deletions Board.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion FileParser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Game.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Globals.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends Node

var online = false
16 changes: 11 additions & 5 deletions Lobby.gd
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions MainGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions Piece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Piece.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion Root.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

3 changes: 2 additions & 1 deletion TopMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ signal close_game

signal host(port, pw)
signal connect(ip, port, pw)
signal disconnect()

var file_loc: String

Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion TopMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,30 @@ 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
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
Expand Down Expand Up @@ -144,7 +161,6 @@ __meta__ = {
margin_left = 188.0
margin_right = 246.0
margin_bottom = 24.0
secret = true
__meta__ = {
"_edit_use_anchors_": false
}
Expand Down
4 changes: 2 additions & 2 deletions UI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 8 additions & 4 deletions UI.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 )]
Expand Down Expand Up @@ -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"]
16 changes: 2 additions & 14 deletions games4.pgn
Original file line number Diff line number Diff line change
@@ -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
1. Pf2f4 0-0
4 changes: 4 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit de54590

Please sign in to comment.