534 lines
16 KiB
C#
534 lines
16 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Obvious.Soap;
|
|
using Photon.Realtime;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MultiplayerAddectiveScene : Singleton<MultiplayerAddectiveScene>
|
|
{
|
|
public Image mainContentMaskImage;
|
|
|
|
public Camera mCamera;
|
|
|
|
public PlayerHeader playerHeader;
|
|
|
|
public GameObject left_MapScreen;
|
|
|
|
public GameObject left_RoomCreationSettings;
|
|
|
|
public Image left_MapScreenImage;
|
|
|
|
public Text left_LocationNameText;
|
|
|
|
public InputField left_RoomName;
|
|
|
|
public Text left_Error;
|
|
|
|
public Dropdown left_TimeOfDayDropDown;
|
|
|
|
public Dropdown left_WeatherDropDown;
|
|
|
|
public GameObject right_RoomListHeader;
|
|
|
|
public GameObject right_RoomList;
|
|
|
|
public GameObject right_ConnectingIndicator;
|
|
|
|
public Button right_ConnectToServerBtn;
|
|
|
|
public Dropdown right_DropdownLanguages;
|
|
|
|
public ScrollRect right_RoomsScrollRect;
|
|
|
|
public GameObject roomRow_Template;
|
|
|
|
public GameObject right_NoRoomsIndicator;
|
|
|
|
public IntVariable currentMapSelectionIndex;
|
|
|
|
private List<GameObject> roomRows = new List<GameObject>();
|
|
|
|
private Dictionary<string, FriendInfo> friendsInfoAll = new Dictionary<string, FriendInfo>();
|
|
|
|
private string newRoomCreator_inputName = "";
|
|
|
|
private bool newRoomCreator_privateRoomToggle;
|
|
|
|
private bool friendsOnlyToggle;
|
|
|
|
private bool friendsInfoUpdateComplete;
|
|
|
|
private Coroutine enterLocationCoroutine;
|
|
|
|
public static bool IsFocused
|
|
{
|
|
get
|
|
{
|
|
if (Singleton<MultiplayerAddectiveScene>.Instance != null)
|
|
{
|
|
return Singleton<MultiplayerAddectiveScene>.Instance.left_RoomName.isFocused;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void MultiplayerManager_On_FriendsInfoUpdate()
|
|
{
|
|
friendsInfoUpdateComplete = true;
|
|
}
|
|
|
|
public void OnConnectToServerBtnClick()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.Disconnected)
|
|
{
|
|
MultiplayerManager.Instance.Connect();
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void OnNewRoomCreator_Name(string text)
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
newRoomCreator_inputName = text;
|
|
left_Error.text = "";
|
|
}
|
|
}
|
|
|
|
public void OnNewRoomCreator_Private()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
newRoomCreator_privateRoomToggle = !newRoomCreator_privateRoomToggle;
|
|
}
|
|
}
|
|
|
|
public void OnNewRoomCreator_Create()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
if (newRoomCreator_inputName == null || newRoomCreator_inputName.Length < 4)
|
|
{
|
|
left_Error.text = LanguageManager.Instance.GetText("ERROR_ROOM_NAME_TOO_SHORT");
|
|
return;
|
|
}
|
|
if (newRoomCreator_inputName.Length > 20)
|
|
{
|
|
left_Error.text = LanguageManager.Instance.GetText("ERROR_ROOM_NAME_TOO_LONG");
|
|
return;
|
|
}
|
|
if (char.IsDigit(newRoomCreator_inputName[0]))
|
|
{
|
|
left_Error.text = LanguageManager.Instance.GetText("ERROR_ROOM_NAME_START_WITH_NUMBER");
|
|
return;
|
|
}
|
|
if (MultiplayerManager.Instance.ActiveRooms.Exists((MultiplayerManager.RoomInfoData element) => element.name == newRoomCreator_inputName))
|
|
{
|
|
left_Error.text = LanguageManager.Instance.GetText("ERROR_ROOM_NAME_DUPLICATE");
|
|
return;
|
|
}
|
|
left_Error.text = "";
|
|
MultiplayerManager.Instance.CreateRoom(newRoomCreator_inputName, 10, currentMapSelectionIndex, LanguageManager.Instance.languagesList.IndexOf(LanguageManager.Instance.currentLanguage), left_TimeOfDayDropDown.value, left_WeatherDropDown.value, !newRoomCreator_privateRoomToggle);
|
|
newRoomCreator_inputName = "";
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void OnRoomFilter_DropdownLanguages()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void OnRoomFilter_Private()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
friendsOnlyToggle = !friendsOnlyToggle;
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
public void OnRoomList_Join(Button button)
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
MultiplayerManager.Instance.JoinRoom(button.transform.parent.name);
|
|
Refresh();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.Disconnected)
|
|
{
|
|
OnConnectToServerBtnClick();
|
|
}
|
|
else
|
|
{
|
|
Refresh();
|
|
}
|
|
StartCoroutine(FriendsFindCoroutine());
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InRoom)
|
|
{
|
|
MultiplayerManager.Instance.LeaveRoom();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
MultiplayerManager.On_ConnectedToMaster += Refresh;
|
|
MultiplayerManager.On_Disconnected += Refresh;
|
|
MultiplayerManager.On_JoinedLobby += Refresh;
|
|
MultiplayerManager.On_JoinedRoom += Refresh;
|
|
MultiplayerManager.On_LeftRoom += Refresh;
|
|
MultiplayerManager.On_CreateRoomFailed += Refresh;
|
|
MultiplayerManager.On_JoinRoomFailed += Refresh;
|
|
MultiplayerManager.On_RoomListUpdate += Refresh;
|
|
MultiplayerManager.On_FriendsInfoUpdate += MultiplayerManager_On_FriendsInfoUpdate;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
MultiplayerManager.On_ConnectedToMaster -= Refresh;
|
|
MultiplayerManager.On_Disconnected -= Refresh;
|
|
MultiplayerManager.On_JoinedLobby -= Refresh;
|
|
MultiplayerManager.On_JoinedRoom -= Refresh;
|
|
MultiplayerManager.On_LeftRoom -= Refresh;
|
|
MultiplayerManager.On_CreateRoomFailed -= Refresh;
|
|
MultiplayerManager.On_JoinRoomFailed -= Refresh;
|
|
MultiplayerManager.On_RoomListUpdate -= Refresh;
|
|
MultiplayerManager.On_FriendsInfoUpdate -= MultiplayerManager_On_FriendsInfoUpdate;
|
|
}
|
|
|
|
private void DisableAllUI()
|
|
{
|
|
left_MapScreen.SetActive(value: false);
|
|
left_RoomCreationSettings.SetActive(value: false);
|
|
right_RoomListHeader.SetActive(value: false);
|
|
right_RoomList.SetActive(value: false);
|
|
right_ConnectingIndicator.SetActive(value: false);
|
|
right_ConnectToServerBtn.gameObject.SetActive(value: false);
|
|
left_Error.gameObject.SetActive(value: false);
|
|
right_DropdownLanguages.gameObject.SetActive(value: false);
|
|
right_NoRoomsIndicator.gameObject.SetActive(value: false);
|
|
}
|
|
|
|
private void SetupDropdownWeatherAndTimeDay()
|
|
{
|
|
List<string> list = new List<string>();
|
|
List<string> list2 = new List<string>();
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_CLEAR"));
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_CLOUDY"));
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_OVERCAST"));
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_LIGHTRAIN"));
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_MEDIUMRAIN"));
|
|
list.Add(LanguageManager.Instance.GetText("WEATHER_STORM"));
|
|
list2.Add(LanguageManager.Instance.GetText("TIMEOFDAY_MORNING"));
|
|
list2.Add(LanguageManager.Instance.GetText("TIMEOFDAY_NOON"));
|
|
list2.Add(LanguageManager.Instance.GetText("TIMEOFDAY_EVENING"));
|
|
list2.Add(LanguageManager.Instance.GetText("TIMEOFDAY_NIGHT"));
|
|
if (left_TimeOfDayDropDown.options.Count != list2.Count || left_WeatherDropDown.options.Count != list.Count)
|
|
{
|
|
left_TimeOfDayDropDown.ClearOptions();
|
|
left_WeatherDropDown.ClearOptions();
|
|
left_WeatherDropDown.AddOptions(list);
|
|
left_TimeOfDayDropDown.AddOptions(list2);
|
|
left_WeatherDropDown.value = GameManager.Instance.gameLocations[(int)currentMapSelectionIndex].startWeatherIndex;
|
|
left_TimeOfDayDropDown.value = GameManager.Instance.gameLocations[(int)currentMapSelectionIndex].startDayTimeIndex;
|
|
}
|
|
}
|
|
|
|
private void SetupDropdownLanguage()
|
|
{
|
|
List<string> list = LanguageManager.Instance.languagesList.ToList();
|
|
list.Insert(0, LanguageManager.Instance.GetText("ANY"));
|
|
if (right_DropdownLanguages.options.Count != list.Count)
|
|
{
|
|
right_DropdownLanguages.ClearOptions();
|
|
right_DropdownLanguages.AddOptions(list);
|
|
right_DropdownLanguages.value = 0;
|
|
right_DropdownLanguages.RefreshShownValue();
|
|
}
|
|
}
|
|
|
|
private void DisplayRoomCreationSettings()
|
|
{
|
|
left_MapScreen.SetActive(value: true);
|
|
left_RoomCreationSettings.SetActive(value: true);
|
|
left_Error.gameObject.SetActive(value: true);
|
|
Sprite screenMapImage = GameManager.Instance.gameLocations[(int)currentMapSelectionIndex].GetScreenMapImage(0);
|
|
string text = GameManager.Instance.gameLocations[(int)currentMapSelectionIndex].name;
|
|
left_MapScreenImage.sprite = screenMapImage;
|
|
left_LocationNameText.text = text;
|
|
left_Error.text = "";
|
|
}
|
|
|
|
private void DisplayRoomListHeader()
|
|
{
|
|
right_RoomListHeader.SetActive(value: true);
|
|
right_DropdownLanguages.gameObject.SetActive(value: true);
|
|
}
|
|
|
|
private void DisplayRoomList()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState != MultiplayerManager.State.InLobby)
|
|
{
|
|
return;
|
|
}
|
|
right_RoomList.SetActive(value: true);
|
|
roomRow_Template.SetActive(value: false);
|
|
if (friendsOnlyToggle)
|
|
{
|
|
List<GameObject> list = new List<GameObject>();
|
|
foreach (GameObject rr in roomRows)
|
|
{
|
|
if (!friendsInfoAll.Values.Any((FriendInfo element) => element.Room == rr.name))
|
|
{
|
|
list.Add(rr);
|
|
}
|
|
}
|
|
foreach (GameObject item in list)
|
|
{
|
|
roomRows.Remove(item);
|
|
item.GetComponentInChildren<Button>().interactable = false;
|
|
Object.Destroy(item.gameObject);
|
|
}
|
|
foreach (KeyValuePair<string, FriendInfo> fia in friendsInfoAll)
|
|
{
|
|
if (!fia.Value.IsInRoom || string.IsNullOrEmpty(fia.Value.Room) || roomRows.Exists((GameObject element) => element.name == fia.Value.Room))
|
|
{
|
|
continue;
|
|
}
|
|
string text = "";
|
|
if (MultiplayerManager.Instance.Friends.Exists((MultiplayerManager.FriendInfoData element) => element.userID == fia.Value.UserId))
|
|
{
|
|
text = MultiplayerManager.Instance.Friends.First((MultiplayerManager.FriendInfoData element) => element.userID == fia.Value.UserId).name;
|
|
}
|
|
GameObject gameObject = Object.Instantiate(roomRow_Template);
|
|
gameObject.transform.SetParent(roomRow_Template.transform.parent);
|
|
gameObject.transform.localScale = Vector3.one;
|
|
gameObject.name = fia.Value.Room;
|
|
gameObject.transform.Find("ServerName").GetComponent<Text>().text = fia.Value.Room;
|
|
gameObject.transform.Find("ServerLoc").GetComponent<Text>().text = "";
|
|
gameObject.transform.Find("ServerLang").GetComponent<Text>().text = text;
|
|
gameObject.transform.Find("ServerVisibility").GetComponent<Text>().text = LanguageManager.Instance.GetText("FRIENDS");
|
|
Button button = gameObject.GetComponentInChildren<Button>();
|
|
button.onClick.AddListener(delegate
|
|
{
|
|
OnRoomList_Join(button);
|
|
});
|
|
gameObject.SetActive(value: true);
|
|
roomRows.Add(gameObject);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int preferredLanguageIndex = -1;
|
|
if (right_DropdownLanguages.value != 0)
|
|
{
|
|
preferredLanguageIndex = right_DropdownLanguages.value - 1;
|
|
}
|
|
List<MultiplayerManager.RoomInfoData> list2 = MultiplayerManager.Instance.ActiveRoomsFiltered(-1, preferredLanguageIndex);
|
|
List<GameObject> list3 = new List<GameObject>();
|
|
foreach (GameObject rr2 in roomRows)
|
|
{
|
|
if (!list2.Exists((MultiplayerManager.RoomInfoData element) => element.name == rr2.name))
|
|
{
|
|
list3.Add(rr2);
|
|
}
|
|
}
|
|
foreach (GameObject item2 in list3)
|
|
{
|
|
roomRows.Remove(item2);
|
|
item2.GetComponentInChildren<Button>().interactable = false;
|
|
Object.Destroy(item2.gameObject);
|
|
}
|
|
foreach (MultiplayerManager.RoomInfoData ar in list2)
|
|
{
|
|
if (!roomRows.Exists((GameObject element) => element.name == ar.name))
|
|
{
|
|
int locationIndex = ar.locationIndex;
|
|
bool interactable = true;
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerLevel < GameManager.Instance.gameLocations[locationIndex].levelrequired)
|
|
{
|
|
interactable = false;
|
|
}
|
|
if (!GameManager.Instance._playerData.CheckLicences(locationIndex) && GameManager.Instance.gameLocations[locationIndex].licensePrize > 0)
|
|
{
|
|
interactable = false;
|
|
}
|
|
GameObject gameObject2 = Object.Instantiate(roomRow_Template);
|
|
gameObject2.transform.SetParent(roomRow_Template.transform.parent);
|
|
gameObject2.transform.localScale = Vector3.one;
|
|
gameObject2.name = ar.name;
|
|
gameObject2.transform.Find("ServerName").GetComponent<Text>().text = ar.name + $" ({ar.playerCount}/{ar.maxPlayers})";
|
|
gameObject2.transform.Find("ServerLoc").GetComponent<Text>().text = GameManager.Instance.gameLocations[ar.locationIndex].name;
|
|
gameObject2.transform.Find("ServerLang").GetComponent<Text>().text = LanguageManager.Instance.languagesList[ar.preferredLanguageIndex];
|
|
gameObject2.transform.Find("ServerVisibility").GetComponent<Text>().text = LanguageManager.Instance.GetText("PUBLIC");
|
|
Button button2 = gameObject2.GetComponentInChildren<Button>();
|
|
button2.onClick.AddListener(delegate
|
|
{
|
|
OnRoomList_Join(button2);
|
|
});
|
|
button2.interactable = interactable;
|
|
gameObject2.SetActive(value: true);
|
|
roomRows.Add(gameObject2);
|
|
}
|
|
}
|
|
}
|
|
right_NoRoomsIndicator.SetActive(roomRows.Count <= 0);
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
StopCoroutines();
|
|
DisableAllUI();
|
|
SetupDropdownWeatherAndTimeDay();
|
|
SetupDropdownLanguage();
|
|
playerHeader.gameObject.SetActive(value: false);
|
|
mainContentMaskImage.enabled = false;
|
|
if (Object.FindObjectsOfType<AudioListener>().Length > 1)
|
|
{
|
|
mCamera.GetComponent<AudioListener>().enabled = false;
|
|
}
|
|
switch (MultiplayerManager.Instance.CurrentState)
|
|
{
|
|
case MultiplayerManager.State.Disconnected:
|
|
right_ConnectToServerBtn.gameObject.SetActive(value: true);
|
|
break;
|
|
case MultiplayerManager.State.Connecting:
|
|
right_ConnectingIndicator.SetActive(value: true);
|
|
break;
|
|
case MultiplayerManager.State.InLobby:
|
|
DisplayRoomCreationSettings();
|
|
DisplayRoomListHeader();
|
|
DisplayRoomList();
|
|
break;
|
|
case MultiplayerManager.State.JoiningRoom:
|
|
right_ConnectingIndicator.SetActive(value: true);
|
|
break;
|
|
case MultiplayerManager.State.InRoom:
|
|
{
|
|
int locationIndex = MultiplayerManager.Instance.CurrentRoom.Value.locationIndex;
|
|
bool flag = true;
|
|
if (Singleton<SaveDataManager>.Instance.GetCurrentPlayerData().PlayerLevel < GameManager.Instance.gameLocations[locationIndex].levelrequired)
|
|
{
|
|
flag = false;
|
|
}
|
|
if (!GameManager.Instance._playerData.CheckLicences(locationIndex) && GameManager.Instance.gameLocations[locationIndex].licensePrize > 0)
|
|
{
|
|
flag = false;
|
|
}
|
|
if (flag)
|
|
{
|
|
StopCoroutines();
|
|
StartCoroutine(EnterLocationCoroutine());
|
|
}
|
|
else
|
|
{
|
|
ShowMessagePopup(LanguageManager.Instance.GetText("CANNOT_JOIN_THIS_ROOM"));
|
|
MultiplayerManager.Instance.LeaveRoom();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
void StopCoroutines()
|
|
{
|
|
if (enterLocationCoroutine != null)
|
|
{
|
|
StopCoroutine(enterLocationCoroutine);
|
|
enterLocationCoroutine = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ShowMessagePopup(string msgText)
|
|
{
|
|
if (!(GameManager.Instance == null))
|
|
{
|
|
GameManager.Instance.ShowMessagePopup(msgText, GetComponent<Canvas>().transform, deleteLast: true);
|
|
}
|
|
}
|
|
|
|
private IEnumerator EnterLocationCoroutine()
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InRoom)
|
|
{
|
|
MultiplayerManager.Instance.EnterRoomLocation();
|
|
}
|
|
yield break;
|
|
}
|
|
|
|
private IEnumerator FriendsFindCoroutine()
|
|
{
|
|
friendsInfoAll = new Dictionary<string, FriendInfo>();
|
|
List<MultiplayerManager.FriendInfoData> friendsAll = MultiplayerManager.Instance.Friends;
|
|
int batchSize = 30;
|
|
int currentIndex = 0;
|
|
while (true)
|
|
{
|
|
if (MultiplayerManager.Instance.CurrentState == MultiplayerManager.State.InLobby)
|
|
{
|
|
if (currentIndex >= friendsAll.Count)
|
|
{
|
|
currentIndex = 0;
|
|
}
|
|
List<MultiplayerManager.FriendInfoData> batchToFetch = new List<MultiplayerManager.FriendInfoData>();
|
|
for (int i = 0; i < batchSize; i++)
|
|
{
|
|
if (currentIndex >= friendsAll.Count)
|
|
{
|
|
break;
|
|
}
|
|
batchToFetch.Add(friendsAll[currentIndex]);
|
|
currentIndex++;
|
|
}
|
|
if (batchToFetch.Count > 0)
|
|
{
|
|
friendsInfoUpdateComplete = false;
|
|
MultiplayerManager.Instance.FindFriends(batchToFetch.Select((MultiplayerManager.FriendInfoData element) => element.userID).ToArray());
|
|
int timeout = 5;
|
|
while (timeout > 0)
|
|
{
|
|
yield return new WaitForSeconds(1f);
|
|
timeout--;
|
|
if (friendsInfoUpdateComplete)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
foreach (FriendInfo item in MultiplayerManager.Instance.FriendsInfo)
|
|
{
|
|
if (friendsInfoAll.ContainsKey(item.UserId))
|
|
{
|
|
friendsInfoAll[item.UserId] = item;
|
|
}
|
|
else
|
|
{
|
|
friendsInfoAll.Add(item.UserId, item);
|
|
}
|
|
}
|
|
foreach (MultiplayerManager.FriendInfoData friend in batchToFetch)
|
|
{
|
|
if (!MultiplayerManager.Instance.FriendsInfo.Exists((FriendInfo element) => element.UserId == friend.userID) && friendsInfoAll.ContainsKey(friend.userID))
|
|
{
|
|
friendsInfoAll.Remove(friend.userID);
|
|
}
|
|
}
|
|
}
|
|
DisplayRoomList();
|
|
}
|
|
yield return new WaitForSeconds(5f);
|
|
}
|
|
}
|
|
}
|