Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions addons/nohub.gd/lobby.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends RefCounted
class_name NohubLobby

var id: String = ""
var is_visible: bool = true
var is_locked: bool = false
var data: Dictionary = {}

func _to_string() -> String:
return "NohubLobby(id=%s, is_visible=%s, is_locked=%s, data=%s)" % [id, is_visible, is_locked, data]
1 change: 1 addition & 0 deletions addons/nohub.gd/lobby.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://darwb07a50hht
2 changes: 2 additions & 0 deletions addons/nohub.gd/nohub.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tool
extends EditorPlugin
1 change: 1 addition & 0 deletions addons/nohub.gd/nohub.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://c4r2pqwp1rtml
140 changes: 140 additions & 0 deletions addons/nohub.gd/nohub_client.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
extends RefCounted
class_name NohubClient


var _connection: StreamPeerTCP
var _reactor: TrimsockTCPClientReactor


func _init(connection: StreamPeerTCP):
_connection = connection
_connection.set_no_delay(true)

_reactor = TrimsockTCPClientReactor.new(connection)

func poll() -> void:
_reactor.poll()

func set_game(id: String) -> NohubResult:
var request := TrimsockCommand.request("session/set-game")\
.with_params([id])
return await _bool_request(request)

func create_lobby(address: String, data: Dictionary) -> NohubResult.Lobby:
var request := TrimsockCommand.request("lobby/create")\
.with_params([address])
for key in data:
request.with_kv_pairs([TrimsockCommand.pair_of(key, data[key])])

var xchg := _reactor.submit_request(request)
var response := await xchg.read()

if response.is_success():
return NohubResult.Lobby.of_value(_command_to_lobby(response))
else:
return _command_to_error(response)

func get_lobby(id: String, properties: Array[String] = []) -> NohubResult.Lobby:
var request := TrimsockCommand.request("lobby/get")\
.with_params([id] + properties)
var xchg := _reactor.submit_request(request)
var response := await xchg.read()

if response.is_success():
return NohubResult.Lobby.of_value(_command_to_lobby(response))
else:
return _command_to_error(response)

func list_lobbies(fields: Array[String] = []) -> NohubResult.LobbyList:
var result := [] as Array[NohubLobby]
var request := TrimsockCommand.request("lobby/list")\
.with_params(fields)

var xchg := _reactor.submit_request(request)
while xchg.is_open():
var cmd := await xchg.read()

if cmd.is_error():
return _command_to_error(cmd)
if not cmd.is_stream_chunk():
continue

result.append(_command_to_lobby(cmd))

return NohubResult.LobbyList.of_value(result)

func delete_lobby(lobby_id: String) -> NohubResult:
var request := TrimsockCommand.request("lobby/delete")\
.with_params([lobby_id])
return await _bool_request(request)

func join_lobby(lobby_id: String) -> NohubResult.Address:
var request := TrimsockCommand.request("lobby/join")\
.with_params([lobby_id])

var xchg := _reactor.submit_request(request)
var response := await xchg.read()

if response.is_success():
return NohubResult.Address.of_value(response.params[0])
else:
return _command_to_error(response)

func lock_lobby(lobby_id: String) -> NohubResult:
var request := TrimsockCommand.request("lobby/lock")\
.with_params([lobby_id])
return await _bool_request(request)

func unlock_lobby(lobby_id: String) -> NohubResult:
var request := TrimsockCommand.request("lobby/unlock")\
.with_params([lobby_id])
return await _bool_request(request)

func hide_lobby(lobby_id: String) -> NohubResult:
var request := TrimsockCommand.request("lobby/hide")\
.with_params([lobby_id])
return await _bool_request(request)

func publish_lobby(lobby_id: String) -> NohubResult:
var request := TrimsockCommand.request("lobby/publish")\
.with_params([lobby_id])
return await _bool_request(request)

func set_lobby_data(lobby_id: String, data: Dictionary) -> NohubResult:
var request := TrimsockCommand.request("lobby/set-data")\
.with_params([lobby_id])\
.with_kv_map(data)
return await _bool_request(request)

func whereami() -> String:
var request := TrimsockCommand.request("whereami")
var xchg := _reactor.submit_request(request)
var response := await xchg.read()

if response.is_success():
return response.text
else:
return ""

func _bool_request(request: TrimsockCommand) -> NohubResult:
var xchg := _reactor.submit_request(request)
var response := await xchg.read()
if response.is_success():
return NohubResult.of_success()
else:
return _command_to_error(response)

func _command_to_lobby(command: TrimsockCommand) -> NohubLobby:
var lobby := NohubLobby.new()
lobby.id = command.params[0]
lobby.is_locked = command.params.find("locked", 1) >= 0
lobby.is_visible = command.params.find("hidden", 1) < 0
lobby.data = command.kv_map

return lobby

func _command_to_error(command: TrimsockCommand) -> NohubResult:
if command.is_error() and command.params.size() >= 2:
return NohubResult.of_error(command.params[0], command.params[1])
else:
return NohubResult.of_error(command.name, "")
1 change: 1 addition & 0 deletions addons/nohub.gd/nohub_client.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://b5po2uj4gudcc
7 changes: 7 additions & 0 deletions addons/nohub.gd/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="nohub.gd"
description="A Godot client for nohub, the open source lobby service"
author="Tamás Gálffy"
version="0.9.0"
script="nohub.gd"
84 changes: 84 additions & 0 deletions addons/nohub.gd/result.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
extends RefCounted
class_name NohubResult

class ErrorData:
var name: String
var message: String

func _init(p_name: String, p_message: String):
name = p_name
message = p_message

func _to_string() -> String:
return "%s: %s" % [name, message]

class Lobby extends NohubResult:
static func of_value(value: NohubLobby) -> Lobby:
var result := Lobby.new()
result._is_success = true
result._value = value
return result

func value() -> NohubLobby:
if _is_success:
return _value as NohubLobby
else:
return null

class LobbyList extends NohubResult:
static func of_value(value: Array[NohubLobby]) -> LobbyList:
var result := LobbyList.new()
result._is_success = true
result._value = value
return result

func value() -> Array[NohubLobby]:
if _is_success:
return _value as Array[NohubLobby]
else:
return []

class Address extends NohubResult:
static func of_value(value: String) -> Address:
var result := Address.new()
result._is_success = true
result._value = value
return result

func value() -> String:
if _is_success:
return _value as String
else:
return ""

var _is_success: bool
var _value: Variant
var _error: ErrorData


static func of_error(error: String, message: String) -> NohubResult:
var result := NohubResult.new()
result._is_success = false
result._error = ErrorData.new(error, message)
return result

static func of_success() -> NohubResult:
var result := NohubResult.new()
result._is_success = true
return result


func is_success() -> bool:
return _is_success

func error() -> ErrorData:
if _is_success:
return null
else:
return _error

func _to_string() -> String:
if _is_success:
return str(_value)
else:
return str(_error)
1 change: 1 addition & 0 deletions addons/nohub.gd/result.gd.uid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uid://cv4r0rosao3v2
Loading
Loading