Skip to content

Commit

Permalink
Merge branch 'main' into 09-06-captcha-module
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCxFF committed Sep 17, 2024
2 parents d9aba12 + 47deee2 commit 677ac6c
Show file tree
Hide file tree
Showing 39 changed files with 397 additions and 289 deletions.
4 changes: 0 additions & 4 deletions modules/achievements/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions modules/achievements/module.json

This file was deleted.

3 changes: 2 additions & 1 deletion modules/auth_email/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"auth",
"user"
"user",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
3 changes: 2 additions & 1 deletion modules/auth_email_link/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"auth",
"user"
"user",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
3 changes: 0 additions & 3 deletions modules/eos/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions modules/eos/module.json

This file was deleted.

6 changes: 0 additions & 6 deletions modules/idem/README.md

This file was deleted.

16 changes: 0 additions & 16 deletions modules/idem/module.json

This file was deleted.

3 changes: 2 additions & 1 deletion modules/identities/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tags": [
"core",
"user",
"auth"
"auth",
"internal"
],
"authors": [
"rivet-gg",
Expand Down
2 changes: 1 addition & 1 deletion modules/lobbies/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"list_regions": {
"name": "List Regions",
"description": "List available regions.",
"public": false
"public": true
},
"fetch_lobby_manager_state": {
"name": "Fetch Lobby Manager State",
Expand Down
8 changes: 6 additions & 2 deletions modules/lobbies/scripts/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface LobbyListEntry {
id: string;
version: string;
tags: Record<string, string>;
createdAt: string;
players: number;
maxPlayers: number;
maxPlayersDirect: number;
}

export async function run(
Expand Down Expand Up @@ -46,11 +50,11 @@ export async function run(
id: lobby.id,
version: lobby.version,
tags: lobby.tags,
createdAt: lobby.createdAt,
createdAt: new Date(lobby.createdAt).toISOString(),
players: lobby.players,
maxPlayers: lobby.maxPlayers,
maxPlayersDirect: lobby.maxPlayersDirect,
}));
} satisfies LobbyListEntry));

return { lobbies: lobbyList };
}
69 changes: 36 additions & 33 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,55 +111,58 @@ 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)
OS.crash("Unsupported transport")
RivetLogger.error("Unsupported transport: %s" % transport)
return

# Notify lobby ready
var request = {
"lobbyId": _lobby_id,
}
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.error("Lobby ready failed failed: %s" % response.body)

# Crash the server so Rivet stops waiting for the server to start
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 +182,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 +200,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 +246,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 +262,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 +281,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 +297,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 +306,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)

2 changes: 1 addition & 1 deletion modules/lobbies/tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test("lobby tags", async (ctx: TestContext) => {
{
version: VERSION,
region: REGION,
tags: { gameMode: "a", region: "atl" },
tags: { gameMode: "a" },
players: [{}],
maxPlayers: 8,
maxPlayersDirect: 8,
Expand Down
4 changes: 0 additions & 4 deletions modules/matchmaker/README.md

This file was deleted.

14 changes: 0 additions & 14 deletions modules/matchmaker/module.json

This file was deleted.

3 changes: 2 additions & 1 deletion modules/rivet/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"icon": "r",
"tags": [
"core",
"infra"
"infra",
"internal"
],
"authors": [
"NathanFlurry",
Expand Down
4 changes: 0 additions & 4 deletions modules/steam/README.md

This file was deleted.

Loading

0 comments on commit 677ac6c

Please sign in to comment.