636 lines
15 KiB
C#
636 lines
15 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using FishNet.Managing;
|
|
using Heathen.SteamworksIntegration;
|
|
using Michsky.UI.Heat;
|
|
using Obvious.Soap;
|
|
using Steamworks;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class UI_MultiplayerPanel : MonoBehaviour
|
|
{
|
|
public enum State
|
|
{
|
|
LobbySearch = 0,
|
|
LobbyCreate = 1,
|
|
JoiningLobby = 2,
|
|
InLobby = 3,
|
|
LeavingLobby = 4,
|
|
EnteringLocation = 5
|
|
}
|
|
|
|
private State state;
|
|
|
|
private LobbyManager lobbyManager;
|
|
|
|
[SerializeField]
|
|
private LocalizationSettings localizationSettings;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventNoParam OnLobbyJoinFail;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventNoParam OnLobbyErrorIncorrectName;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventNoParam OnLobbyErrorDuplicateName;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventNoParam OnLobbyErrorIncorrectPassword;
|
|
|
|
[SerializeField]
|
|
private Image playerAvatar;
|
|
|
|
[SerializeField]
|
|
private ButtonManager playerName;
|
|
|
|
[SerializeField]
|
|
private PanelManager panelContent;
|
|
|
|
[SerializeField]
|
|
private PanelManager panelContentInLobby;
|
|
|
|
[SerializeField]
|
|
private GameObject hotkeysForPanelContent;
|
|
|
|
[SerializeField]
|
|
private GameObject hotkeysForPanelContentInLobby;
|
|
|
|
[SerializeField]
|
|
private GameObject loadingPleaseWait;
|
|
|
|
[SerializeField]
|
|
private GameObject lobbyList;
|
|
|
|
[SerializeField]
|
|
private UI_LobbyListItem lobbyListItemTemplate;
|
|
|
|
[SerializeField]
|
|
private GameObject noLobbiesFound;
|
|
|
|
[SerializeField]
|
|
private TMP_InputField inputFieldLobbyName;
|
|
|
|
[SerializeField]
|
|
private TMP_InputField inputFieldLobbyPassword;
|
|
|
|
[SerializeField]
|
|
private SwitchManager switchPrivate;
|
|
|
|
[SerializeField]
|
|
private Michsky.UI.Heat.Dropdown locationDropdown;
|
|
|
|
[SerializeField]
|
|
private TMP_Text lobbyErrorIncorrectName;
|
|
|
|
[SerializeField]
|
|
private TMP_Text lobbyErrorDuplicateName;
|
|
|
|
[SerializeField]
|
|
private TMP_Text lobbyErrorIncorrectPassword;
|
|
|
|
[SerializeField]
|
|
private Vector2Int lobbyNameLength = new Vector2Int(6, 30);
|
|
|
|
[SerializeField]
|
|
private TMP_Text lobbyName;
|
|
|
|
[SerializeField]
|
|
private GameObject lobbyPlayerList;
|
|
|
|
[SerializeField]
|
|
private UI_LobbyPlayerListItem lobbyPlayerListItemTemplate;
|
|
|
|
[SerializeField]
|
|
private ButtonManager leaveLobbyButton;
|
|
|
|
[SerializeField]
|
|
private ButtonManager readyButton;
|
|
|
|
[SerializeField]
|
|
private ButtonManager leaveServerButton;
|
|
|
|
private int maxLobbiesSearchCount = 32;
|
|
|
|
private List<LobbyData> foundLobbies = new List<LobbyData>();
|
|
|
|
private int lobbyListSorting;
|
|
|
|
private string lobbyCode;
|
|
|
|
private LobbyManager LobbyManager
|
|
{
|
|
get
|
|
{
|
|
lobbyManager = lobbyManager ?? (lobbyManager = Object.FindFirstObjectByType<LobbyManager>());
|
|
return lobbyManager;
|
|
}
|
|
}
|
|
|
|
private NetworkManager NetworkManager => NetworkManager.Instances[0];
|
|
|
|
public void PanelContentPanelChanged(int index)
|
|
{
|
|
if (state == State.LobbySearch || state == State.LobbyCreate)
|
|
{
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void InputFieldLobbyNameValueChanged()
|
|
{
|
|
if (state == State.LobbyCreate)
|
|
{
|
|
HideCreateLobbyErrors();
|
|
}
|
|
if (inputFieldLobbyName.text.Length > lobbyNameLength.y)
|
|
{
|
|
inputFieldLobbyName.text = inputFieldLobbyName.text.Substring(0, lobbyNameLength.y);
|
|
}
|
|
}
|
|
|
|
public void InputFieldLobbyPasswordValueChanged()
|
|
{
|
|
if (state == State.LobbyCreate)
|
|
{
|
|
HideCreateLobbyErrors();
|
|
}
|
|
if (inputFieldLobbyPassword.text.Length > lobbyNameLength.y)
|
|
{
|
|
inputFieldLobbyPassword.text = inputFieldLobbyPassword.text.Substring(0, lobbyNameLength.y);
|
|
}
|
|
}
|
|
|
|
public void CreateLobbyClick()
|
|
{
|
|
if (state != State.LobbyCreate || ShowCreateLobbyErrors())
|
|
{
|
|
return;
|
|
}
|
|
LobbyManager.createArguments.name = inputFieldLobbyName.text;
|
|
LobbyManager.createArguments.type = ELobbyType.k_ELobbyTypePublic;
|
|
LobbyManager.createArguments.slots = 10;
|
|
LobbyManager.createArguments.metadata.Clear();
|
|
if ((bool)LocalizationManager.instance && (bool)LocalizationManager.instance.currentLanguageAsset)
|
|
{
|
|
LobbyManager.createArguments.metadata.Add(new MetadataTemplate
|
|
{
|
|
key = "Country",
|
|
value = localizationSettings.languages.FindIndex((LocalizationSettings.Language element) => element.languageID == LocalizationManager.instance.currentLanguageAsset.languageID).ToString()
|
|
});
|
|
}
|
|
if (switchPrivate.isOn)
|
|
{
|
|
LobbyManager.createArguments.metadata.Add(new MetadataTemplate
|
|
{
|
|
key = "Password",
|
|
value = inputFieldLobbyPassword.text
|
|
});
|
|
}
|
|
ChapterManager chapterManager = Object.FindFirstObjectByType<ChapterManager>(FindObjectsInactive.Include);
|
|
if (chapterManager != null)
|
|
{
|
|
chapterManager.chapters.Where((ChapterManager.ChapterItem element) => element.defaultState == ChapterManager.ChapterState.Unlocked);
|
|
ChapterManager.ChapterItem chapterItem = chapterManager.chapters[locationDropdown.index];
|
|
LobbyManager.createArguments.metadata.Add(new MetadataTemplate
|
|
{
|
|
key = "Map",
|
|
value = chapterItem.chapterID
|
|
});
|
|
}
|
|
LobbyManager.Create(LobbyCreationResult);
|
|
state = State.JoiningLobby;
|
|
Refresh();
|
|
}
|
|
|
|
public void SwitchPrivateValueChanged(bool value)
|
|
{
|
|
if (state == State.LobbyCreate)
|
|
{
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void LobbyListRefreshClick()
|
|
{
|
|
if (state == State.LobbySearch && SteamSettings.Initialized)
|
|
{
|
|
LobbyManager.Search(maxLobbiesSearchCount);
|
|
}
|
|
}
|
|
|
|
public void LobbyListQuickJoinClick()
|
|
{
|
|
if (state != State.LobbySearch)
|
|
{
|
|
return;
|
|
}
|
|
foreach (LobbyData foundLobby in foundLobbies)
|
|
{
|
|
if (foundLobby.MemberCount < foundLobby.MaxMembers && !foundLobby.GetMetadata().TryGetValue("Password", out var _))
|
|
{
|
|
LobbyManager.Join(foundLobby);
|
|
state = State.JoiningLobby;
|
|
Refresh();
|
|
return;
|
|
}
|
|
}
|
|
OnLobbyJoinFail.Raise();
|
|
}
|
|
|
|
public void LobbyListSortOnValueChanged(int value)
|
|
{
|
|
if (state == State.LobbySearch)
|
|
{
|
|
lobbyListSorting = value;
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void LobbyListCodeOnSubmit(string value)
|
|
{
|
|
if (state == State.LobbySearch)
|
|
{
|
|
lobbyCode = value;
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
private void UI_LobbyListItem_OnJoinLobbyClick(LobbyData lobbyData)
|
|
{
|
|
if (state == State.LobbySearch)
|
|
{
|
|
if (lobbyData.MemberCount >= lobbyData.MaxMembers)
|
|
{
|
|
Debug.LogWarning("Lobby '" + lobbyData.Name + "' is full.");
|
|
return;
|
|
}
|
|
if (lobbyData.GetMetadata().TryGetValue("Password", out var value) && !string.Equals(lobbyCode, value))
|
|
{
|
|
Debug.LogWarning("Incorrect password for lobby.");
|
|
return;
|
|
}
|
|
LobbyManager.Join(lobbyData);
|
|
state = State.JoiningLobby;
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void OnLeaveLobbyClick()
|
|
{
|
|
if (state == State.InLobby && LobbyManager.HasLobby)
|
|
{
|
|
LobbyManager.Leave();
|
|
}
|
|
}
|
|
|
|
private void LobbyCreationResult(EResult resultCode, LobbyData lobby, bool ioError)
|
|
{
|
|
if (resultCode == EResult.k_EResultOK && !ioError)
|
|
{
|
|
state = State.InLobby;
|
|
}
|
|
else
|
|
{
|
|
OnLobbyJoinFail.Raise();
|
|
state = State.LobbyCreate;
|
|
}
|
|
Refresh();
|
|
}
|
|
|
|
private void LobbyFoundResult(LobbyData[] lobbies)
|
|
{
|
|
foundLobbies = lobbies.ToList();
|
|
Refresh();
|
|
}
|
|
|
|
private void LobbyEnterSuccess(LobbyData lobbyData)
|
|
{
|
|
state = State.InLobby;
|
|
Refresh();
|
|
}
|
|
|
|
private void LobbyEnterFailed(EChatRoomEnterResponse response)
|
|
{
|
|
OnLobbyJoinFail.Raise();
|
|
state = State.LobbySearch;
|
|
Refresh();
|
|
}
|
|
|
|
private void OtherUserJoinLobby(UserData userData)
|
|
{
|
|
if (state == State.InLobby)
|
|
{
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
private void OtherUserLeaveLobby(UserLobbyLeaveData userLobbyLeaveData)
|
|
{
|
|
if (state == State.InLobby)
|
|
{
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
private void OnLobbyLeave()
|
|
{
|
|
state = State.LobbySearch;
|
|
Refresh();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
LobbyManager.evtFound.AddListener(LobbyFoundResult);
|
|
LobbyManager.evtEnterSuccess.AddListener(LobbyEnterSuccess);
|
|
LobbyManager.evtEnterFailed.AddListener(LobbyEnterFailed);
|
|
LobbyManager.evtUserJoined.AddListener(OtherUserJoinLobby);
|
|
LobbyManager.evtUserLeft.AddListener(OtherUserLeaveLobby);
|
|
LobbyManager.evtLeave.AddListener(OnLobbyLeave);
|
|
UI_LobbyListItem.OnJoinLobbyClick += UI_LobbyListItem_OnJoinLobbyClick;
|
|
StartCoroutine(SearchLobbiesCoroutine());
|
|
Refresh();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
LobbyManager.evtFound.RemoveListener(LobbyFoundResult);
|
|
LobbyManager.evtEnterSuccess.RemoveListener(LobbyEnterSuccess);
|
|
LobbyManager.evtEnterFailed.RemoveListener(LobbyEnterFailed);
|
|
LobbyManager.evtUserJoined.RemoveListener(OtherUserJoinLobby);
|
|
LobbyManager.evtUserLeft.RemoveListener(OtherUserLeaveLobby);
|
|
LobbyManager.evtLeave.RemoveListener(OnLobbyLeave);
|
|
UI_LobbyListItem.OnJoinLobbyClick -= UI_LobbyListItem_OnJoinLobbyClick;
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
DetermineState();
|
|
RefreshPlayerName();
|
|
RefreshPanelContent();
|
|
RefreshLoadingPleaseWait();
|
|
RefreshCreateLobbyPanel();
|
|
RefreshInLobbyPanel();
|
|
RefreshJoinPanel();
|
|
}
|
|
|
|
private void DetermineState()
|
|
{
|
|
if (state != State.JoiningLobby && state != State.LeavingLobby)
|
|
{
|
|
_ = state;
|
|
_ = 5;
|
|
}
|
|
if ((state == State.LobbySearch || state == State.LobbyCreate) && LobbyManager.HasLobby)
|
|
{
|
|
state = State.InLobby;
|
|
}
|
|
if (state == State.InLobby && !LobbyManager.HasLobby)
|
|
{
|
|
state = State.LobbySearch;
|
|
}
|
|
if (state == State.LobbySearch || state == State.LobbyCreate)
|
|
{
|
|
if (panelContent.currentPanelIndex == 0)
|
|
{
|
|
state = State.LobbySearch;
|
|
}
|
|
else
|
|
{
|
|
state = State.LobbyCreate;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RefreshPlayerName()
|
|
{
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
playerName.SetText(UserData.Me.Name);
|
|
UserData.Me.LoadAvatar(delegate(Texture2D avatarTex)
|
|
{
|
|
if (avatarTex != null)
|
|
{
|
|
playerAvatar.sprite = Sprite.Create(avatarTex, new Rect(0f, 0f, avatarTex.width, avatarTex.height), new Vector2(0.5f, 0.5f));
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
playerName.SetText("Unknown Player");
|
|
}
|
|
}
|
|
|
|
private void RefreshPanelContent()
|
|
{
|
|
if (state == State.LobbySearch || state == State.LobbyCreate)
|
|
{
|
|
panelContent.gameObject.SetActive(value: true);
|
|
hotkeysForPanelContent.gameObject.SetActive(value: true);
|
|
panelContentInLobby.gameObject.SetActive(value: false);
|
|
hotkeysForPanelContentInLobby.gameObject.SetActive(value: false);
|
|
}
|
|
else if (state == State.InLobby)
|
|
{
|
|
panelContent.gameObject.SetActive(value: false);
|
|
hotkeysForPanelContent.gameObject.SetActive(value: false);
|
|
panelContentInLobby.gameObject.SetActive(value: true);
|
|
hotkeysForPanelContentInLobby.gameObject.SetActive(value: true);
|
|
}
|
|
else
|
|
{
|
|
panelContent.gameObject.SetActive(value: false);
|
|
hotkeysForPanelContent.gameObject.SetActive(value: false);
|
|
panelContentInLobby.gameObject.SetActive(value: false);
|
|
hotkeysForPanelContentInLobby.gameObject.SetActive(value: false);
|
|
}
|
|
}
|
|
|
|
private void RefreshLoadingPleaseWait()
|
|
{
|
|
loadingPleaseWait.SetActive(value: false);
|
|
if (state == State.JoiningLobby || state == State.EnteringLocation || state == State.LeavingLobby)
|
|
{
|
|
loadingPleaseWait.SetActive(value: true);
|
|
}
|
|
}
|
|
|
|
private void HideCreateLobbyErrors()
|
|
{
|
|
lobbyErrorIncorrectName.gameObject.SetActive(value: false);
|
|
lobbyErrorDuplicateName.gameObject.SetActive(value: false);
|
|
lobbyErrorIncorrectPassword.gameObject.SetActive(value: false);
|
|
}
|
|
|
|
private bool ShowCreateLobbyErrors()
|
|
{
|
|
bool flag = false;
|
|
HideCreateLobbyErrors();
|
|
Regex regex = new Regex("^[\\p{L}0-9 ]+$");
|
|
Regex regex2 = new Regex("^[A-Za-z0-9]+$");
|
|
string lobbyNameText = inputFieldLobbyName.text.Trim();
|
|
if (string.IsNullOrEmpty(lobbyNameText) || lobbyNameText.Length < lobbyNameLength.x || lobbyNameText.Length > lobbyNameLength.y || !regex.IsMatch(lobbyNameText))
|
|
{
|
|
if (!flag)
|
|
{
|
|
OnLobbyErrorIncorrectName.Raise();
|
|
}
|
|
flag = true;
|
|
}
|
|
else if (foundLobbies.Exists((LobbyData lobby) => lobby.Name == lobbyNameText))
|
|
{
|
|
if (!flag)
|
|
{
|
|
OnLobbyErrorDuplicateName.Raise();
|
|
}
|
|
flag = true;
|
|
}
|
|
if (switchPrivate.isOn)
|
|
{
|
|
string text = inputFieldLobbyPassword.text.Trim();
|
|
if (string.IsNullOrEmpty(text) || text.Length < lobbyNameLength.x || text.Length > lobbyNameLength.y || !regex2.IsMatch(text))
|
|
{
|
|
if (!flag)
|
|
{
|
|
OnLobbyErrorIncorrectPassword.Raise();
|
|
}
|
|
flag = true;
|
|
}
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
private void RefreshCreateLobbyPanel()
|
|
{
|
|
if (state != State.LobbyCreate)
|
|
{
|
|
return;
|
|
}
|
|
HideCreateLobbyErrors();
|
|
inputFieldLobbyPassword.transform.parent.gameObject.SetActive(switchPrivate.isOn);
|
|
ChapterManager chapterManager = Object.FindFirstObjectByType<ChapterManager>(FindObjectsInactive.Include);
|
|
if (!(chapterManager != null))
|
|
{
|
|
return;
|
|
}
|
|
locationDropdown.items.Clear();
|
|
foreach (ChapterManager.ChapterItem chapter in chapterManager.chapters)
|
|
{
|
|
if (chapter.defaultState != ChapterManager.ChapterState.Locked)
|
|
{
|
|
locationDropdown.CreateNewItem(chapter.chapterID);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RefreshInLobbyPanel()
|
|
{
|
|
if (state != State.InLobby)
|
|
{
|
|
return;
|
|
}
|
|
LobbyData lobby = LobbyManager.Lobby;
|
|
string text = lobby.Name;
|
|
if (lobby.GetMetadata().TryGetValue("Map", out var value) && !string.IsNullOrEmpty(value))
|
|
{
|
|
text = text + " <size=70%>(" + value + ")</size>";
|
|
}
|
|
if (lobby.GetMetadata().TryGetValue("Password", out var value2))
|
|
{
|
|
text = text + " <size=70%>(Code: " + value2 + ")</size>";
|
|
}
|
|
lobbyName.text = text;
|
|
foreach (Transform item in lobbyPlayerList.transform)
|
|
{
|
|
Object.Destroy(item.gameObject);
|
|
}
|
|
LobbyMemberData[] members = LobbyManager.Members;
|
|
foreach (LobbyMemberData lobbyMemberData in members)
|
|
{
|
|
Object.Instantiate(lobbyPlayerListItemTemplate, lobbyPlayerList.transform).Initialize(lobbyMemberData);
|
|
}
|
|
leaveLobbyButton.gameObject.SetActive(!NetworkManager.IsClientStarted);
|
|
readyButton.gameObject.SetActive(!NetworkManager.IsClientStarted);
|
|
leaveServerButton.gameObject.SetActive(NetworkManager.IsClientStarted);
|
|
}
|
|
|
|
private void RefreshJoinPanel()
|
|
{
|
|
if (state != State.LobbySearch)
|
|
{
|
|
return;
|
|
}
|
|
foreach (Transform item in lobbyList.transform)
|
|
{
|
|
Object.Destroy(item.gameObject);
|
|
}
|
|
List<UI_LobbyListItem> list = new List<UI_LobbyListItem>();
|
|
foreach (LobbyData foundLobby in foundLobbies)
|
|
{
|
|
UI_LobbyListItem uI_LobbyListItem = Object.Instantiate(lobbyListItemTemplate, lobbyList.transform);
|
|
uI_LobbyListItem.Initialize(foundLobby);
|
|
list.Add(uI_LobbyListItem);
|
|
}
|
|
switch (lobbyListSorting)
|
|
{
|
|
case 0:
|
|
list = list.OrderBy((UI_LobbyListItem item) => item.LobbyData.Name).ToList();
|
|
break;
|
|
case 1:
|
|
list = list.OrderBy((UI_LobbyListItem item) => item.LobbyData.MemberCount).ToList();
|
|
break;
|
|
case 2:
|
|
list = list.OrderByDescending((UI_LobbyListItem item) => item.LobbyData.MemberCount).ToList();
|
|
break;
|
|
}
|
|
for (int num = 0; num < list.Count; num++)
|
|
{
|
|
list[num].transform.SetSiblingIndex(num);
|
|
}
|
|
bool flag = false;
|
|
if (!string.IsNullOrEmpty(lobbyCode))
|
|
{
|
|
foreach (UI_LobbyListItem item2 in list)
|
|
{
|
|
string value;
|
|
bool flag2 = item2.LobbyData.GetMetadata().TryGetValue("Password", out value) && value == lobbyCode;
|
|
item2.gameObject.SetActive(flag2);
|
|
if (flag2)
|
|
{
|
|
flag = true;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (UI_LobbyListItem item3 in list)
|
|
{
|
|
bool flag3 = !item3.LobbyData.GetMetadata().ContainsKey("Password");
|
|
item3.gameObject.SetActive(flag3);
|
|
if (flag3)
|
|
{
|
|
flag = true;
|
|
}
|
|
}
|
|
}
|
|
noLobbiesFound.SetActive(!flag);
|
|
}
|
|
|
|
private IEnumerator SearchLobbiesCoroutine()
|
|
{
|
|
while (true)
|
|
{
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
LobbyManager.Search(maxLobbiesSearchCount);
|
|
}
|
|
yield return new WaitForSeconds(5f);
|
|
}
|
|
}
|
|
}
|