Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore(lobbies): upgrade multiplayer manager sdk addon
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Sep 10, 2024
1 parent f4f19b5 commit 84bb55b
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions modules/lobbies/sdk_addons/godot/multiplayer_manager.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extends Node
class_name BackendMultiplayerManager
class_name RivetMultiplayerManager

enum Transport { ENET, WEB_SOCKET }

Expand Down Expand Up @@ -79,7 +79,7 @@ var _player_token = null
func setup_multiplayer():
# Validate only initialized once
if _multiplayer_setup:
BackendLogger.warning("setup_multiplayer already called")
RivetLogger.warning("setup_multiplayer already called")
return
_multiplayer_setup = true

Expand Down Expand Up @@ -111,29 +111,29 @@ func setup_multiplayer():
if OS.has_environment("LOBBY_ID"):
_lobby_id = OS.get_environment("LOBBY_ID")
else:
BackendLogger.warning("Missing lobby ID")
RivetLogger.warning("Missing lobby ID")

if OS.has_environment("LOBBY_TOKEN"):
_lobby_token = OS.get_environment("LOBBY_TOKEN")

# Start server
if transport == Transport.ENET:
BackendLogger.log("Starting ENet server: %s:%s" % [_server_hostname, _server_port])
RivetLogger.log("Starting ENet server: %s:%s" % [_server_hostname, _server_port])

peer = ENetMultiplayerPeer.new()
peer.set_bind_ip(_server_hostname)
peer.create_server(_server_port)
# TODO: crash if create server fails
multiplayer.set_multiplayer_peer(peer)
elif transport == Transport.WEB_SOCKET:
BackendLogger.log("Starting WebSocket server: %s:%s" % [_server_hostname, _server_port])
RivetLogger.log("Starting WebSocket server: %s:%s" % [_server_hostname, _server_port])

peer = WebSocketMultiplayerPeer.new()
peer.create_server(_server_port, _server_hostname)
# TODO: crash if create server fails
multiplayer.set_multiplayer_peer(peer)
else:
BackendLogger.error("Unsupported transport: %s" % transport)
RivetLogger.error("Unsupported transport: %s" % transport)
OS.crash("Unsupported transport")

# Notify lobby ready
Expand All @@ -142,24 +142,24 @@ func setup_multiplayer():
}
if _lobby_token != null:
request["lobbyToken"] = _lobby_token
var response = await Backend.lobbies.set_lobby_ready(request).async()
var response = await Rivet.lobbies.set_lobby_ready(request).async()
if response.is_ok():
BackendLogger.log("Lobby ready")
RivetLogger.log("Lobby ready")
else:
BackendLogger.warning("Lobby ready failed failed: %s" % response.body)
RivetLogger.warning("Lobby ready failed failed: %s" % response.body)
OS.crash("Lobby ready failed")
return

## Connect to a lobby returned from the backend.
func connect_to_lobby(lobby, player):
if !_multiplayer_setup:
BackendLogger.error("setup_multiplayer needs to be called in _ready")
RivetLogger.error("setup_multiplayer needs to be called in _ready")
return
if is_server:
BackendLogger.warning("Cannot called set_player_token on server")
RivetLogger.warning("Cannot called set_player_token on server")
return

BackendLogger.log("Connecting to lobby: %s %s" % [lobby, player])
RivetLogger.log("Connecting to lobby: %s %s" % [lobby, player])

# Save token
_player_token = player.token
Expand All @@ -179,12 +179,12 @@ func connect_to_lobby(lobby, player):
hostname = backend_port.hostname
port = backend_port.port
else:
BackendLogger.error("Unsupported lobby backend: %s" % lobby.backend)
RivetLogger.error("Unsupported lobby backend: %s" % lobby.backend)
return

# Start server
if transport == Transport.ENET:
BackendLogger.log("Connecting to ENet server: %s:%s" % [hostname, port])
RivetLogger.log("Connecting to ENet server: %s:%s" % [hostname, port])

peer = ENetMultiplayerPeer.new()
peer.create_client(hostname, port)
Expand All @@ -197,44 +197,44 @@ func connect_to_lobby(lobby, player):
ws_protocol = "ws"
var url = "%s://%s:%s" % [ws_protocol, hostname, port]

BackendLogger.log("Connecting to WebSocket server: %s" % url)
RivetLogger.log("Connecting to WebSocket server: %s" % url)

peer = WebSocketMultiplayerPeer.new()
peer.create_client(url)
multiplayer.set_multiplayer_peer(peer)
else:
BackendLogger.error("Unsupported transport: %s" % transport)
RivetLogger.error("Unsupported transport: %s" % transport)

# MARK: Peers
func _on_peer_connected(id):
BackendLogger.log('Peer connected: %s' % id)
RivetLogger.log('Peer connected: %s' % id)

if is_server:
client_connected.emit(id)
else:
server_connected.emit()

func _on_peer_disconnected(id):
BackendLogger.log('Peer disconnected: %s' % id)
RivetLogger.log('Peer disconnected: %s' % id)

if is_server:
# Remove player from lobby
var player_token = _player_tokens.get(id)
if player_token != null:
_player_tokens.erase(id)
BackendLogger.log("Removing player %s" % player_token)
RivetLogger.log("Removing player %s" % player_token)

var request = {
"lobbyId": _lobby_id,
"playerTokens": [player_token],
}
if _lobby_token != null:
request["lobbyToken"] = _lobby_token
var response = await Backend.lobbies.set_player_disconnected(request).async()
var response = await Rivet.lobbies.set_player_disconnected(request).async()
if response.is_error():
BackendLogger.warning("Player disconnect failed for %id: %s" % [id, response.body])
RivetLogger.warning("Player disconnect failed for %id: %s" % [id, response.body])
else:
BackendLogger.warning("Player disconnected without player token: %s" % id)
RivetLogger.warning("Player disconnected without player token: %s" % id)
return

# Signal
Expand All @@ -243,13 +243,13 @@ func _on_peer_disconnected(id):
server_disconnected.emit()

func _on_connected_to_server():
BackendLogger.log('Connected to server')
RivetLogger.log('Connected to server')

func _on_connection_failed():
BackendLogger.log('Connection failed')
RivetLogger.log('Connection failed')

func _on_server_disconnected():
BackendLogger.log('Server disconnected')
RivetLogger.log('Server disconnected')

# MARK: Authentication
func _auth_callback(id: int, buf: PackedByteArray):
Expand All @@ -259,12 +259,12 @@ func _auth_callback(id: int, buf: PackedByteArray):
json.parse(buf.get_string_from_utf8())
var data = json.get_data()

BackendLogger.log("Player authenticating %s: %s" % [id, data])
RivetLogger.log("Player authenticating %s: %s" % [id, data])

# Check token
var player_token = data["player_token"]
if player_token == null:
BackendLogger.warning("Player token not provided in auth for %s" % id)
RivetLogger.warning("Player token not provided in auth for %s" % id)
_scene_multiplayer.disconnect_peer(id)
return

Expand All @@ -278,13 +278,13 @@ func _auth_callback(id: int, buf: PackedByteArray):
}
if _lobby_token != null:
request["lobbyToken"] = _lobby_token
var response = await Backend.lobbies.set_player_connected(request).async()
var response = await Rivet.lobbies.set_player_connected(request).async()
if response.is_ok():
BackendLogger.log("Player authenticated for %s" % id)
RivetLogger.log("Player authenticated for %s" % id)
_scene_multiplayer.complete_auth(id)
else:
# Player will be cleaned up on disconnect handler
BackendLogger.warning("Player authentiation failed for %s: %s" % [id, response.body])
RivetLogger.warning("Player authentiation failed for %s: %s" % [id, response.body])
_scene_multiplayer.disconnect_peer(id)
else:
# Server does not need to auth with client
Expand All @@ -294,7 +294,7 @@ func _on_peer_authenticating(id):
#if is_server:
#return

BackendLogger.log("Authenticating with server")
RivetLogger.log("Authenticating with server")
var body = JSON.stringify({ "player_token": _player_token })
(multiplayer as SceneMultiplayer).send_auth(id, body.to_utf8_buffer())

Expand All @@ -303,6 +303,6 @@ func _on_peer_authentication_failed(id):
#if !is_server:
#return

BackendLogger.warning("Client authentication failed %s" % id)
RivetLogger.warning("Client authentication failed %s" % id)
multiplayer.set_multiplayer_peer(null)

0 comments on commit 84bb55b

Please sign in to comment.