sevivon

[DORMANT] multiplayer dreidel game for mobile devices w/ godot
git clone git://git.figbert.com/sevivon.git
Log | Files | Refs | README | LICENSE

commit 12dc2a58b0974291c95e7d7f6cf3d948cd7cb053
parent fc7eacc09893e21a33fc29e303ad9a96d406ffdc
Author: FIGBERT <figbert@figbert.com>
Date:   Mon,  1 Feb 2021 00:21:11 -0800

Split server and client logic in main.gd

Diffstat:
Msrc/scripts/main.gd | 49+++++++++++++++++++++++--------------------------
1 file changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/scripts/main.gd b/src/scripts/main.gd @@ -7,43 +7,40 @@ const MAX_PLAYERS := 2 func _ready() -> void: - _initalize_instance() + if "--server" in OS.get_cmdline_args() or OS.has_feature("Server"): + _initialize_server() + else: + _initialize_client() -func _initalize_instance() -> void: - var is_server := "--server" in OS.get_cmdline_args() or OS.has_feature("Server") - +## Server Logic +func _initialize_server() -> void: var peer := NetworkedMultiplayerENet.new() - if is_server: - peer.create_server(SERVER_PORT, MAX_PLAYERS) - else: - peer.create_client(SERVER_IP, SERVER_PORT) + peer.create_server(SERVER_PORT, MAX_PLAYERS) get_tree().network_peer = peer - - if not is_server: - get_tree().connect("connected_to_server", self, "_connected_successfully") - get_tree().connect("connection_failed", self, "_connection_failed") - get_tree().connect("network_peer_connected", self, "_peer_joined") - get_tree().connect("network_peer_disconnected", self, "_peer_left") + get_tree().connect("network_peer_connected", self, "_client_joined_server") + get_tree().connect("network_peer_disconnected", self, "_client_left_server") + +func _client_joined_server(id: int) -> void: + print("%s joined successfully" % id) -func _peer_joined(id: int) -> void: - if get_tree().is_network_server(): - print("%s joined successfully" % id) - elif id != 1: - $Label.text += "%s has joined the lobby\n" % id +func _client_left_server(id: int) -> void: + print("%s disconnected from the server" % id) -func _peer_left(id: int) -> void: - if get_tree().is_network_server(): - print("%s disconnected from the server" % id) - elif id != 1: - $Label.text += "%s has left the lobby\n" % id +## Client Logic +func _initialize_client() -> void: + var peer := NetworkedMultiplayerENet.new() + peer.create_client(SERVER_IP, SERVER_PORT) + get_tree().network_peer = peer + get_tree().connect("connected_to_server", self, "_client_connected_successfully") + get_tree().connect("connection_failed", self, "_client_connection_failed") -func _connected_successfully() -> void: +func _client_connected_successfully() -> void: $Label.text += "Connection to server established.\n" -func _connection_failed() -> void: +func _client_connection_failed() -> void: $Label.text += "Could not connect to server.\n"