129 lines
3.2 KiB
C#
129 lines
3.2 KiB
C#
|
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;
|
||
|
currentLobbyList = await SteamMatchmaking.LobbyList.WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString()).RequestAsync();
|
||
|
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++)
|
||
|
{
|
||
|
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;
|
||
|
}
|
||
|
}
|