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

205 lines
5.3 KiB
C#

using System.Collections;
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;
public bool censorOffensiveLobbyNames = true;
private Coroutine loadLobbyListCoroutine;
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;
}
if (loadLobbyListCoroutine != null)
{
StopCoroutine(loadLobbyListCoroutine);
}
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;
loadLobbyListCoroutine = StartCoroutine(loadLobbyListAndFilter(currentLobbyList));
}
else
{
Debug.Log("Lobby list is null after request.");
serverListBlankText.text = "No available servers to join.";
}
}
private IEnumerator loadLobbyListAndFilter(Lobby[] lobbyList)
{
string[] offensiveWords = new string[21]
{
"nigger", "faggot", "n1g", "nigers", "cunt", "pussies", "pussy", "minors", "chink", "buttrape",
"molest", "rape", "coon", "negro", "beastiality", "cocks", "cumshot", "ejaculate", "pedophile", "furfag",
"necrophilia"
};
for (int i = 0; i < currentLobbyList.Length; i++)
{
Friend[] array = SteamFriends.GetBlocked().ToArray();
if (array != null)
{
for (int j = 0; j < array.Length; j++)
{
Debug.Log($"blocked user: {array[j].Name}; id: {array[j].Id}");
if (currentLobbyList[i].IsOwnedBy(array[j].Id))
{
Debug.Log("Hiding lobby by blocked user: " + array[j].Name);
}
}
}
else
{
Debug.Log("Blocked users list is null");
}
string lobbyName = currentLobbyList[i].GetData("name");
if (lobbyName.Length == 0)
{
continue;
}
string lobbyNameNoCapitals = lobbyName.ToLower();
if (censorOffensiveLobbyNames)
{
bool nameIsOffensive = false;
for (int b = 0; b < offensiveWords.Length; b++)
{
if (lobbyNameNoCapitals.Contains(offensiveWords[b]))
{
nameIsOffensive = true;
break;
}
if (b % 5 == 0)
{
yield return null;
}
}
if (nameIsOffensive)
{
continue;
}
}
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 = lobbyName.Substring(0, Mathf.Min(lobbyName.Length, 40));
componentInChildren.playerCount.text = $"{currentLobbyList[i].MemberCount} / 4";
componentInChildren.lobbyId = currentLobbyList[i].Id;
componentInChildren.thisLobby = currentLobbyList[i];
}
}
private void Update()
{
refreshServerListTimer += Time.deltaTime;
}
}