Try adding is lobby public

If lobby is public it will allow it to be recived as public lobby for joining it via some kind of automatic system...
This commit is contained in:
pietru 2024-05-31 09:54:56 +02:00
parent 7561a873cc
commit 2098118844
4 changed files with 44 additions and 12 deletions

View file

@ -1,6 +1,6 @@
extends Node extends Node
enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL} enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL, GET_PUBLIC}
@export var autojoin := true @export var autojoin := true
@export var lobby := "" # Will create a new lobby if empty. @export var lobby := "" # Will create a new lobby if empty.
@ -20,6 +20,8 @@ signal answer_received(id, answer)
signal candidate_received(id, mid, index, sdp) signal candidate_received(id, mid, index, sdp)
signal lobby_sealed() signal lobby_sealed()
signal public_lobbies_recived(id, list)
func connect_to_url(url): func connect_to_url(url):
close() close()
@ -88,13 +90,15 @@ func _parse_msg():
if not candidate[1].is_valid_int(): if not candidate[1].is_valid_int():
return false return false
candidate_received.emit(src_id, candidate[0], candidate[1].to_int(), candidate[2]) candidate_received.emit(src_id, candidate[0], candidate[1].to_int(), candidate[2])
elif type == Message.GET_PUBLIC:
public_lobbies_recived.emit(src_id, JSON.parse_string(msg.data))
else: else:
return false return false
return true # Parsed return true # Parsed
func join_lobby(lobby: String, single_host:=false): func join_lobby(lobby: String, single_host:=false, public:=false):
var data = {"lobby":lobby,"single_host":single_host} var data = {"lobby":lobby,"single_host":single_host,"public":public}
return _send_msg(Message.JOIN, 0 if mesh else 1, JSON.stringify(data)) return _send_msg(Message.JOIN, 0 if mesh else 1, JSON.stringify(data))
@ -114,6 +118,10 @@ func send_answer(id, answer) -> int:
return _send_msg(Message.ANSWER, id, answer) return _send_msg(Message.ANSWER, id, answer)
func ask_for_public_lobbies(id) -> int:
return _send_msg(Message.GET_PUBLIC, id, "")
func _send_msg(type: int, id: int, data:="") -> int: func _send_msg(type: int, id: int, data:="") -> int:
return ws.send_text(JSON.stringify({ return ws.send_text(JSON.stringify({
"type": type, "type": type,

View file

@ -1,6 +1,6 @@
extends Node extends Node
enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL} enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL, GET_PUBLIC}
const TIMEOUT = 1000 # Unresponsive clients times out after 1 sec const TIMEOUT = 1000 # Unresponsive clients times out after 1 sec
const SEAL_TIME = 10000 # A sealed room will be closed after this time const SEAL_TIME = 10000 # A sealed room will be closed after this time
@ -45,11 +45,13 @@ class Lobby extends RefCounted:
var time = 0 var time = 0
var mesh := true var mesh := true
var single_host := false var single_host := false
var public := false
func _init(host_id: int, use_mesh: bool, is_single_host: bool): func _init(host_id: int, use_mesh: bool, is_single_host: bool, is_public:bool):
host = host_id host = host_id
mesh = use_mesh mesh = use_mesh
single_host = is_single_host single_host = is_single_host
public=is_public
func join(peer: Peer) -> bool: func join(peer: Peer) -> bool:
if sealed: return false if sealed: return false
@ -168,11 +170,11 @@ func poll():
peers.erase(id) peers.erase(id)
func _join_lobby(peer: Peer, lobby: String, mesh: bool, single_host:bool) -> bool: func _join_lobby(peer: Peer, lobby: String, mesh: bool, single_host:bool, public:bool) -> bool:
if lobby == "": if lobby == "":
for _i in range(0, 32): for _i in range(0, 32):
lobby += char(_alfnum[rand.randi_range(0, ALFNUM.length()-1)]) lobby += char(_alfnum[rand.randi_range(0, ALFNUM.length()-1)])
lobbies[lobby] = Lobby.new(peer.id, mesh, single_host) lobbies[lobby] = Lobby.new(peer.id, mesh, single_host, public)
elif not lobbies.has(lobby): elif not lobbies.has(lobby):
return false return false
lobbies[lobby].join(peer) lobbies[lobby].join(peer)
@ -202,7 +204,7 @@ func _parse_msg(peer: Peer) -> bool:
if peer.lobby: # Peer must not have joined a lobby already! if peer.lobby: # Peer must not have joined a lobby already!
return false return false
var dt = JSON.parse_string(msg.data) var dt = JSON.parse_string(msg.data)
return _join_lobby(peer, dt.lobby, msg.id == 0, dt.single_host) return _join_lobby(peer, dt.lobby, msg.id == 0, dt.single_host, dt.public)
if not lobbies.has(peer.lobby): # Lobby not found? if not lobbies.has(peer.lobby): # Lobby not found?
return false return false
@ -213,6 +215,14 @@ func _parse_msg(peer: Peer) -> bool:
# Client is sealing the room # Client is sealing the room
return lobby.seal(peer.id) return lobby.seal(peer.id)
if msg.type == Message.GET_PUBLIC:
var pb = []
for lb in lobbies.keys():
if lobbies[lb].public:
pb.append(lb)
peers[peer.id].send(msg.type, 0, JSON.stringify(pb))
return true
var dest_id: int = msg.id var dest_id: int = msg.id
if dest_id == MultiplayerPeer.TARGET_PEER_SERVER: if dest_id == MultiplayerPeer.TARGET_PEER_SERVER:
dest_id = lobby.host dest_id = lobby.host

View file

@ -35,6 +35,7 @@ const CMD = {
ANSWER: 5, // eslint-disable-line sort-keys ANSWER: 5, // eslint-disable-line sort-keys
CANDIDATE: 6, // eslint-disable-line sort-keys CANDIDATE: 6, // eslint-disable-line sort-keys
SEAL: 7, // eslint-disable-line sort-keys SEAL: 7, // eslint-disable-line sort-keys
GET_PUBLIC: 8, // eslint-disable-line sort-keys
}; };
function randomInt(low, high) { function randomInt(low, high) {
@ -85,11 +86,12 @@ class Peer {
} }
class Lobby { class Lobby {
constructor(name, host, mesh, single_host) { constructor(name, host, mesh, single_host, public) {
this.name = name; this.name = name;
this.host = host; this.host = host;
this.mesh = mesh; this.mesh = mesh;
this.single_host = single_host; this.single_host = single_host;
this.public = public;
this.peers = []; this.peers = [];
this.sealed = false; this.sealed = false;
this.closeTimer = -1; this.closeTimer = -1;
@ -158,7 +160,7 @@ class Lobby {
const lobbies = new Map(); const lobbies = new Map();
let peersCount = 0; let peersCount = 0;
function joinLobby(peer, pLobby, mesh, single_host) { function joinLobby(peer, pLobby, mesh, single_host, public) {
let lobbyName = pLobby; let lobbyName = pLobby;
if (lobbyName === '') { if (lobbyName === '') {
if (lobbies.size >= MAX_LOBBIES) { if (lobbies.size >= MAX_LOBBIES) {
@ -169,7 +171,7 @@ function joinLobby(peer, pLobby, mesh, single_host) {
throw new ProtoError(4000, STR_ALREADY_IN_LOBBY); throw new ProtoError(4000, STR_ALREADY_IN_LOBBY);
} }
lobbyName = randomSecret(); lobbyName = randomSecret();
lobbies.set(lobbyName, new Lobby(lobbyName, peer.id, mesh, single_host)); lobbies.set(lobbyName, new Lobby(lobbyName, peer.id, mesh, single_host, public));
console.log(`Peer ${peer.id} created lobby ${lobbyName}`); console.log(`Peer ${peer.id} created lobby ${lobbyName}`);
console.log(`Open lobbies: ${lobbies.size}`); console.log(`Open lobbies: ${lobbies.size}`);
} }
@ -206,7 +208,7 @@ function parseMsg(peer, msg) {
// Lobby joining. // Lobby joining.
if (type === CMD.JOIN) { if (type === CMD.JOIN) {
const dt = JSON.parse(data); const dt = JSON.parse(data);
joinLobby(peer, dt['lobby'], id === 0, dt['single_host']); joinLobby(peer, dt['lobby'], id === 0, dt['single_host'], dt['public']);
return; return;
} }
@ -223,6 +225,18 @@ function parseMsg(peer, msg) {
lobby.seal(peer); lobby.seal(peer);
return; return;
} }
if (type === CMD.GET_PUBLIC) {
let pb = []
for (lb in lobbies.keys()){
if (lobbies[lb].public){
pb.append(lb)
}
}
const data = JSON.stringify(pb)
peer.ws.send(ProtoMessage(type, 0, data));
return;
}
// Message relaying format: // Message relaying format:
// //