225 lines
9.7 KiB
Plaintext
225 lines
9.7 KiB
Plaintext
|
|
extends Control
|
||
|
|
|
||
|
|
@onready var list : ItemList = $/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer/VBoxLobbylist/ItemList
|
||
|
|
@onready var lobby_members_list : ItemList = $/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer/VBoxCurrentLobby/LobbyMemberList
|
||
|
|
@onready var profileSprite : Sprite2D = $/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer4/VBoxContainer/ProfileSprite
|
||
|
|
@onready var current_lobby_label : Label = $/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer/VBoxCurrentLobby/Label
|
||
|
|
const PACKET_READ_LIMIT: int = 32
|
||
|
|
const app_id: int = 480
|
||
|
|
var is_initiallized: bool = false #Steam initialized successfully
|
||
|
|
|
||
|
|
var lobby_index_list: Dictionary = {} # index -> lobbyid
|
||
|
|
var member_index_list: Dictionary = {} # index -> steam_id
|
||
|
|
var lobby_data
|
||
|
|
var lobby_id: int = 0
|
||
|
|
var lobby_members: Array = []
|
||
|
|
var lobby_members_max: int = 5
|
||
|
|
var steam_id: int = 0
|
||
|
|
var steam_username: String = ""
|
||
|
|
|
||
|
|
func initialize() -> void:
|
||
|
|
var initialize_response: Dictionary = Steam.steamInitEx(false, Steamworks.app_id)
|
||
|
|
if initialize_response["status"] != OK:
|
||
|
|
match initialize_response["status"]:
|
||
|
|
1:
|
||
|
|
push_error("[Steam] Initialization failed: Error")
|
||
|
|
2:
|
||
|
|
push_error("[Steam] Initialization failed: Can't connect to steam")
|
||
|
|
3:
|
||
|
|
push_error("[Steam] Initialization failed: Steam client out of date")
|
||
|
|
return
|
||
|
|
print("[Steam] Initialized successfully")
|
||
|
|
steam_id = Steam.getSteamID()
|
||
|
|
steam_username = Steam.getPersonaName()
|
||
|
|
#Load user avatar
|
||
|
|
Steam.getPlayerAvatar(Steam.AVATAR_SMALL,Steam.getSteamID())
|
||
|
|
Steam.avatar_loaded.connect(steam_avatar_loaded)
|
||
|
|
|
||
|
|
Steam.join_requested.connect(on_lobby_join_requested)
|
||
|
|
Steam.lobby_chat_update.connect(on_lobby_chat_update)
|
||
|
|
Steam.lobby_created.connect(on_lobby_created)
|
||
|
|
# Steam.lobby_data_update.connect(on_lobby_data_update)
|
||
|
|
#Steam.lobby_invite.connect(_on_lobby_invite)
|
||
|
|
Steam.lobby_joined.connect(on_lobby_joined)
|
||
|
|
Steam.lobby_match_list.connect(on_lobby_match_list)
|
||
|
|
Steam.lobby_message.connect(on_lobby_message)
|
||
|
|
Steam.persona_state_change.connect(on_persona_change)
|
||
|
|
# Check for command line arguments
|
||
|
|
check_command_line()
|
||
|
|
#Load lobbylist
|
||
|
|
render_global_lobbies()
|
||
|
|
#render_friend_lobbies()
|
||
|
|
is_initiallized = true
|
||
|
|
|
||
|
|
func _notification(what: int) -> void:
|
||
|
|
if what == NOTIFICATION_WM_CLOSE_REQUEST:
|
||
|
|
# do save stuff here
|
||
|
|
if lobby_id != 0:
|
||
|
|
Steam.leaveLobby(lobby_id)
|
||
|
|
print("[Steam] Left lobby " + str(lobby_id))
|
||
|
|
print("Exiting")
|
||
|
|
get_tree().quit()
|
||
|
|
func check_command_line() -> void:
|
||
|
|
var args: Array = OS.get_cmdline_args()
|
||
|
|
# There are arguments to process
|
||
|
|
if args.size() <= 0:
|
||
|
|
return
|
||
|
|
# A Steam connection argument exists
|
||
|
|
if args[0] != "+connect_lobby":
|
||
|
|
return
|
||
|
|
# Lobby invite exists so try to connect to it
|
||
|
|
if int(args[1]) <= 0:
|
||
|
|
return
|
||
|
|
print("[Steam] Command line lobby ID: %s" % args[1])
|
||
|
|
join_lobby(int(args[1]))
|
||
|
|
|
||
|
|
func steam_avatar_loaded(user_id: int, avatar_size: int, avatar_buffer: PackedByteArray):
|
||
|
|
var avatar_image: Image = Image.create_from_data(avatar_size, avatar_size, false, Image.FORMAT_RGBA8, avatar_buffer)
|
||
|
|
var avatar_texture: ImageTexture = ImageTexture.create_from_image(avatar_image)
|
||
|
|
if user_id == steam_id:
|
||
|
|
profileSprite.set_texture(avatar_texture)
|
||
|
|
var index = member_index_list.find_key(user_id)
|
||
|
|
if index != null:
|
||
|
|
$/root/Main/Mainmenu/Menu/MarginContainer/VBoxContainer/HBoxContainer/VBoxCurrentLobby/LobbyMemberList.set_item_icon(index,avatar_texture)
|
||
|
|
|
||
|
|
func render_global_lobbies():
|
||
|
|
Steam.addRequestLobbyListDistanceFilter(Steam.LOBBY_DISTANCE_FILTER_WORLDWIDE)
|
||
|
|
Steam.requestLobbyList()
|
||
|
|
|
||
|
|
func on_lobby_match_list(lobbies: Array):
|
||
|
|
list.clear()
|
||
|
|
lobby_index_list = {}
|
||
|
|
await get_tree().create_timer(.05).timeout
|
||
|
|
for lobby in lobbies:
|
||
|
|
var lobby_name = Steam.getLobbyData(lobby,"name")
|
||
|
|
var lobby_mode = Steam.getLobbyData(lobby,"mode")
|
||
|
|
#TODO: Remove when own steam page is aquired
|
||
|
|
if lobby_name == "" or lobby_mode != "Spacebots":
|
||
|
|
continue
|
||
|
|
if Steam.getNumLobbyMembers(lobby) == 0 or lobby == lobby_id:
|
||
|
|
continue
|
||
|
|
var index = list.add_item("%s [%s/%s]" % [lobby_name, Steam.getNumLobbyMembers(lobby), Steam.getLobbyMemberLimit(lobby)] , null, false)
|
||
|
|
list.set_item_tooltip(index, str(lobby))
|
||
|
|
lobby_index_list.set(index,lobby)
|
||
|
|
|
||
|
|
func create_lobby() -> void:
|
||
|
|
# Make sure a lobby is not already set
|
||
|
|
if lobby_id == 0:
|
||
|
|
print("[Steam] Creating Lobby")
|
||
|
|
Steam.createLobby(Steam.LOBBY_TYPE_PUBLIC, lobby_members_max)
|
||
|
|
func on_lobby_created(connect_: int, this_lobby_id: int) -> void:
|
||
|
|
if connect_ != 1:
|
||
|
|
push_error("[Steam] Error creating lobby:" + str(connect_))
|
||
|
|
return
|
||
|
|
# Set the lobby ID
|
||
|
|
lobby_id = this_lobby_id
|
||
|
|
print("[Steam] Created steam lobby: %s" % lobby_id)
|
||
|
|
|
||
|
|
Steam.setLobbyJoinable(lobby_id, true)
|
||
|
|
Steam.setLobbyData(lobby_id, "name", steam_username + "'s lobby")
|
||
|
|
Steam.setLobbyData(lobby_id, "mode", "Spacebots")
|
||
|
|
print("[Steam] Lobbydata set successfully")
|
||
|
|
lobby_members_list.add_item(Steam.getPlayerNickname(steam_id), profileSprite.get_texture(), true)
|
||
|
|
#Allow P2P connections to fallback to being relayed through Steam if needed
|
||
|
|
#var set_relay: bool = Steam.allowP2PPacketRelay(true)
|
||
|
|
#print("Allowing Steam to be relay backup: %s" % set_relay)
|
||
|
|
|
||
|
|
func join_lobby(this_lobby_id: int) -> void:
|
||
|
|
print("[Steam] Attempting to join lobby %s" % this_lobby_id)
|
||
|
|
|
||
|
|
|
||
|
|
# Make the lobby join request to Steam
|
||
|
|
Steam.joinLobby(this_lobby_id)
|
||
|
|
|
||
|
|
func on_lobby_joined(this_lobby_id: int, _permissions: int, _locked: bool, response: int) -> void:
|
||
|
|
# If joining was successful
|
||
|
|
if response != Steam.CHAT_ROOM_ENTER_RESPONSE_SUCCESS:
|
||
|
|
# Get the failure reason
|
||
|
|
var fail_reason: String
|
||
|
|
match response:
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_DOESNT_EXIST: fail_reason = "This lobby no longer exists."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_NOT_ALLOWED: fail_reason = "You don't have permission to join this lobby."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_FULL: fail_reason = "The lobby is now full."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_ERROR: fail_reason = "Uh... something unexpected happened!"
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_BANNED: fail_reason = "You are banned from this lobby."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_LIMITED: fail_reason = "You cannot join due to having a limited account."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_CLAN_DISABLED: fail_reason = "This lobby is locked or disabled."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_COMMUNITY_BAN: fail_reason = "This lobby is community locked."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_MEMBER_BLOCKED_YOU: fail_reason = "A user in the lobby has blocked you from joining."
|
||
|
|
Steam.CHAT_ROOM_ENTER_RESPONSE_YOU_BLOCKED_MEMBER: fail_reason = "A user you have blocked is in the lobby."
|
||
|
|
print("[Steam] Failed to join lobby : %s" % fail_reason)
|
||
|
|
return
|
||
|
|
lobby_id = this_lobby_id
|
||
|
|
# Clear any previous lobby members lists, if you were in a previous lobby
|
||
|
|
get_lobby_members()
|
||
|
|
# TODO: Add failed to connect dialog
|
||
|
|
|
||
|
|
func on_lobby_join_requested(this_lobby_id: int, friend_id: int) -> void:
|
||
|
|
# Get the lobby owner's name
|
||
|
|
if this_lobby_id == lobby_id:
|
||
|
|
print("[Steam] Lobby join canceled: Already in that lobby")
|
||
|
|
return
|
||
|
|
if lobby_id != 0:
|
||
|
|
print("[Steam] Leaving lobby " + str(lobby_id))
|
||
|
|
leave_lobby()
|
||
|
|
var owner_name: String = Steam.getFriendPersonaName(friend_id)
|
||
|
|
print("[Steam] Joining %s's lobby..." % owner_name)
|
||
|
|
|
||
|
|
# Attempt to join the lobby
|
||
|
|
join_lobby(this_lobby_id)
|
||
|
|
|
||
|
|
func get_lobby_members() -> void:
|
||
|
|
# Clear your previous lobby list
|
||
|
|
lobby_members.clear()
|
||
|
|
lobby_members_list.clear()
|
||
|
|
# Get the number of members from this lobby from Steam
|
||
|
|
var num_of_members: int = Steam.getNumLobbyMembers(lobby_id)
|
||
|
|
|
||
|
|
# Get the data of these players from Steam
|
||
|
|
for this_member in range(0, num_of_members):
|
||
|
|
var member_steam_id: int = Steam.getLobbyMemberByIndex(lobby_id, this_member)
|
||
|
|
var member_steam_name: String = Steam.getFriendPersonaName(member_steam_id)
|
||
|
|
# Add them to the list
|
||
|
|
lobby_members.append({"steam_id":member_steam_id, "steam_name":member_steam_name})
|
||
|
|
Steam.getPlayerAvatar(Steam.AVATAR_SMALL,member_steam_id)
|
||
|
|
if member_steam_id == Steam.getLobbyOwner(lobby_id):
|
||
|
|
member_steam_name += " (Host)"
|
||
|
|
var index: int = lobby_members_list.add_item(member_steam_name, null, true)
|
||
|
|
if member_steam_id != Steam.getLobbyOwner(lobby_id):
|
||
|
|
lobby_members_list.set_item_tooltip_enabled(index, false)
|
||
|
|
else:
|
||
|
|
lobby_members_list.set_item_tooltip(index,str(lobby_id))
|
||
|
|
if Steam.getFriendRelationship(member_steam_id) == Steam.FRIEND_FLAG_IMMEDIATE:
|
||
|
|
list.set_item_custom_fg_color(index, Color(0.9,1,0.9,1))
|
||
|
|
member_index_list.set(index, member_steam_id)
|
||
|
|
|
||
|
|
func on_persona_change(this_steam_id: int, flag: int) -> void:
|
||
|
|
# Make sure you're in a lobby and this user is valid or Steam might spam your console log
|
||
|
|
if lobby_id > 0:
|
||
|
|
var hex : String = "0x%04X" % flag
|
||
|
|
var name_ : String = Steam.getFriendPersonaName(this_steam_id)
|
||
|
|
print("[Steam] %s (%s) had information change, updating member list: %s" % [name_, this_steam_id, hex])
|
||
|
|
# Update the player list
|
||
|
|
get_lobby_members()
|
||
|
|
|
||
|
|
func on_lobby_message(lobby_id_msg: int, user: int, buffer: String, _chat_type: Steam.ChatEntryType):
|
||
|
|
print("[Chat] " + Steam.getFriendPersonaName(user) + ": " + buffer)
|
||
|
|
if(user != Steam.getLobbyOwner(lobby_id)):
|
||
|
|
print("[Steam] Message not by Lobby owner: " + str(lobby_id_msg) + " " + str(Steam.getLobbyOwner(lobby_id)))
|
||
|
|
return
|
||
|
|
if(buffer.begins_with("NorayID:")):
|
||
|
|
buffer = buffer.trim_prefix("NorayID:")
|
||
|
|
print(buffer)
|
||
|
|
$/root/Main.on_Connect(buffer)
|
||
|
|
|
||
|
|
func on_lobby_chat_update(_lobby_id_msg_update: int, _changed_id: int, _changer_id: int, chat_state: Steam.ChatMemberStateChange):
|
||
|
|
print("lobby chat update: " + str(chat_state))
|
||
|
|
if Steam.CHAT_MEMBER_STATE_CHANGE_ENTERED and Steam.getLobbyOwner(lobby_id) == steam_id:
|
||
|
|
var result = Steam.sendLobbyChatMsg(lobby_id, "NorayID:" + str(Noray.oid))
|
||
|
|
print("lobby chat update2: " + str(chat_state))
|
||
|
|
if result == false:
|
||
|
|
print("Chat message couldn't be send")
|
||
|
|
func leave_lobby():
|
||
|
|
Steam.leaveLobby(lobby_id)
|
||
|
|
lobby_id = 0
|