LethalCompany/Lethal Company/ExportedProject/Assets/Scripts/Assembly-CSharp/SteamLobbyManager.cs
2023-12-22 19:30:32 -05:00

159 lines
4.2 KiB
C#

using System.Linq;
using Steamworks;
using Steamworks.Data;
using Steamworks.ServerList;
using TMPro;
using UnityEngine;
public class SteamLobbyManager : MonoBehaviour
{
private Internet Request;
private Lobby[] currentLobbyList;
public TextMeshProUGUI serverListBlankText;
public Transform levelListContainer;
public GameObject LobbySlotPrefab;
private float lobbySlotPositionOffset;
public int sortByDistanceSetting = 2;
private float refreshServerListTimer;
private void Start()
{
}
public void ChangeDistanceSort(int newValue)
{
sortByDistanceSetting = newValue;
}
private void DebugLogServerList()
{
if (currentLobbyList != null)
{
for (int i = 0; i < currentLobbyList.Length; i++)
{
Debug.Log($"Lobby #{i} id: {currentLobbyList[i].Id}; members: {currentLobbyList[i].MemberCount}");
uint ip = 0u;
ushort port = 0;
SteamId serverId = default(SteamId);
Debug.Log($"Is lobby #{i} valid?: {currentLobbyList[i].GetGameServer(ref ip, ref port, ref serverId)}");
}
}
else
{
Debug.Log("Server list null");
}
}
public void RefreshServerListButton()
{
if (!(refreshServerListTimer < 1f))
{
LoadServerList();
}
}
public async void LoadServerList()
{
if (GameNetworkManager.Instance.waitingForLobbyDataRefresh)
{
return;
}
refreshServerListTimer = 0f;
serverListBlankText.text = "Loading server list...";
currentLobbyList = null;
LobbySlot[] array = Object.FindObjectsOfType<LobbySlot>();
for (int i = 0; i < array.Length; i++)
{
Object.Destroy(array[i].gameObject);
}
switch (sortByDistanceSetting)
{
case 0:
SteamMatchmaking.LobbyList.FilterDistanceClose();
break;
case 1:
SteamMatchmaking.LobbyList.FilterDistanceFar();
break;
case 2:
SteamMatchmaking.LobbyList.FilterDistanceWorldwide();
break;
}
currentLobbyList = null;
Debug.Log("Requested server list");
GameNetworkManager.Instance.waitingForLobbyDataRefresh = true;
switch (sortByDistanceSetting)
{
case 0:
currentLobbyList = await SteamMatchmaking.LobbyList.FilterDistanceClose().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString())
.RequestAsync();
break;
case 1:
currentLobbyList = await SteamMatchmaking.LobbyList.FilterDistanceFar().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString())
.RequestAsync();
break;
default:
currentLobbyList = await SteamMatchmaking.LobbyList.FilterDistanceWorldwide().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString())
.RequestAsync();
break;
}
GameNetworkManager.Instance.waitingForLobbyDataRefresh = false;
if (currentLobbyList != null)
{
Debug.Log("Got lobby list!");
DebugLogServerList();
if (currentLobbyList.Length == 0)
{
serverListBlankText.text = "No available servers to join.";
}
else
{
serverListBlankText.text = "";
}
lobbySlotPositionOffset = 0f;
for (int j = 0; j < currentLobbyList.Length; j++)
{
Friend[] array2 = SteamFriends.GetBlocked().ToArray();
if (array2 != null)
{
for (int k = 0; k < array2.Length; k++)
{
Debug.Log($"blocked user: {array2[k].Name}; id: {array2[k].Id}");
if (currentLobbyList[j].IsOwnedBy(array2[k].Id))
{
Debug.Log("Hiding lobby by blocked user: " + array2[k].Name);
}
}
}
else
{
Debug.Log("Blocked users list is null");
}
GameObject obj = Object.Instantiate(LobbySlotPrefab, levelListContainer);
obj.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, 0f + lobbySlotPositionOffset);
lobbySlotPositionOffset -= 42f;
LobbySlot componentInChildren = obj.GetComponentInChildren<LobbySlot>();
componentInChildren.LobbyName.text = currentLobbyList[j].GetData("name");
componentInChildren.playerCount.text = $"{currentLobbyList[j].MemberCount} / 4";
componentInChildren.lobbyId = currentLobbyList[j].Id;
componentInChildren.thisLobby = currentLobbyList[j];
}
}
else
{
Debug.Log("Lobby list is null after request.");
serverListBlankText.text = "No available servers to join.";
}
}
private void Update()
{
refreshServerListTimer += Time.deltaTime;
}
}