LethalCompany/Lethal Company/ExportedProject/Assets/Scripts/Assembly-CSharp/SteamLobbyManager.cs

229 lines
6.3 KiB
C#
Raw Normal View History

2023-12-23 00:55:14 +00:00
using System.Collections;
2023-12-23 00:30:32 +00:00
using System.Linq;
2023-12-22 22:51:17 +00:00
using Steamworks;
using Steamworks.Data;
using Steamworks.ServerList;
using TMPro;
using UnityEngine;
2023-12-23 02:13:19 +00:00
using UnityEngine.UI;
2023-12-22 22:51:17 +00:00
public class SteamLobbyManager : MonoBehaviour
{
private Internet Request;
private Lobby[] currentLobbyList;
public TextMeshProUGUI serverListBlankText;
public Transform levelListContainer;
public GameObject LobbySlotPrefab;
2023-12-23 02:13:19 +00:00
public GameObject LobbySlotPrefabChallenge;
2023-12-22 22:51:17 +00:00
private float lobbySlotPositionOffset;
public int sortByDistanceSetting = 2;
private float refreshServerListTimer;
2023-12-23 00:55:14 +00:00
public bool censorOffensiveLobbyNames = true;
private Coroutine loadLobbyListCoroutine;
2023-12-23 02:13:19 +00:00
public UnityEngine.UI.Image sortWithChallengeMoonsCheckbox;
private bool sortWithChallengeMoons = true;
public GameObject sortWithChallengeMoonsBoxContainer;
public TMP_InputField serverTagInputField;
public void ToggleSortWithChallengeMoons()
2023-12-22 22:51:17 +00:00
{
2023-12-23 02:13:19 +00:00
sortWithChallengeMoons = !sortWithChallengeMoons;
sortWithChallengeMoonsCheckbox.enabled = sortWithChallengeMoons;
2023-12-22 22:51:17 +00:00
}
public void ChangeDistanceSort(int newValue)
{
sortByDistanceSetting = newValue;
}
2023-12-23 02:13:19 +00:00
private void OnEnable()
{
if (Object.FindObjectOfType<MenuManager>().hasChallengeBeenCompleted)
{
sortWithChallengeMoonsBoxContainer.SetActive(value: false);
}
serverTagInputField.text = string.Empty;
}
2023-12-22 22:51:17 +00:00
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()
{
2023-12-23 02:13:19 +00:00
if (!(refreshServerListTimer < 0.5f))
2023-12-22 22:51:17 +00:00
{
LoadServerList();
}
}
public async void LoadServerList()
{
if (GameNetworkManager.Instance.waitingForLobbyDataRefresh)
{
return;
}
2023-12-23 00:55:14 +00:00
if (loadLobbyListCoroutine != null)
{
StopCoroutine(loadLobbyListCoroutine);
}
2023-12-22 22:51:17 +00:00
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;
2023-12-23 02:13:19 +00:00
LobbyQuery lobbyQuery = sortByDistanceSetting switch
2023-12-23 00:30:32 +00:00
{
2023-12-23 02:13:19 +00:00
0 => SteamMatchmaking.LobbyList.FilterDistanceClose().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString()),
1 => SteamMatchmaking.LobbyList.FilterDistanceFar().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString()),
_ => SteamMatchmaking.LobbyList.FilterDistanceWorldwide().WithSlotsAvailable(1).WithKeyValue("vers", GameNetworkManager.Instance.gameVersionNum.ToString()),
};
if (!sortWithChallengeMoons || Object.FindObjectOfType<MenuManager>().hasChallengeBeenCompleted)
{
lobbyQuery = lobbyQuery.WithKeyValue("chal", "f");
2023-12-23 00:30:32 +00:00
}
2023-12-23 02:17:37 +00:00
if (serverTagInputField.text != string.Empty)
{
lobbyQuery = lobbyQuery.WithKeyValue("tag", serverTagInputField.text.Substring(0, Mathf.Min(19, serverTagInputField.text.Length)));
}
currentLobbyList = await lobbyQuery.RequestAsync();
2023-12-22 22:51:17 +00:00
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;
2023-12-23 00:55:14 +00:00
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)
{
2023-12-23 02:13:19 +00:00
string[] offensiveWords = new string[23]
2023-12-23 00:55:14 +00:00
{
"nigger", "faggot", "n1g", "nigers", "cunt", "pussies", "pussy", "minors", "chink", "buttrape",
"molest", "rape", "coon", "negro", "beastiality", "cocks", "cumshot", "ejaculate", "pedophile", "furfag",
2023-12-23 02:13:19 +00:00
"necrophilia", "yiff", "sex"
2023-12-23 00:55:14 +00:00
};
for (int i = 0; i < currentLobbyList.Length; i++)
{
Friend[] array = SteamFriends.GetBlocked().ToArray();
if (array != null)
2023-12-22 22:51:17 +00:00
{
2023-12-23 00:55:14 +00:00
for (int j = 0; j < array.Length; j++)
2023-12-23 00:30:32 +00:00
{
2023-12-23 00:55:14 +00:00
Debug.Log($"blocked user: {array[j].Name}; id: {array[j].Id}");
if (currentLobbyList[i].IsOwnedBy(array[j].Id))
2023-12-23 00:30:32 +00:00
{
2023-12-23 00:55:14 +00:00
Debug.Log("Hiding lobby by blocked user: " + array[j].Name);
2023-12-23 00:30:32 +00:00
}
}
2023-12-23 00:55:14 +00:00
}
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)
2023-12-23 00:30:32 +00:00
{
2023-12-23 00:55:14 +00:00
continue;
2023-12-23 00:30:32 +00:00
}
2023-12-22 22:51:17 +00:00
}
2023-12-23 02:13:19 +00:00
GameObject original = ((!(lobbyList[i].GetData("chal") == "t")) ? LobbySlotPrefab : LobbySlotPrefabChallenge);
GameObject obj = Object.Instantiate(original, levelListContainer);
2023-12-23 00:55:14 +00:00
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];
2023-12-22 22:51:17 +00:00
}
}
private void Update()
{
refreshServerListTimer += Time.deltaTime;
}
}