266 lines
8.3 KiB
C#
266 lines
8.3 KiB
C#
using System.Collections;
|
|
using Steamworks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TournamentItem : MonoBehaviour
|
|
{
|
|
public Animator animator;
|
|
|
|
[SerializeField]
|
|
private Image tournamentMapImage;
|
|
|
|
[SerializeField]
|
|
private Text tournamentTitleText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentTaskText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentDurationText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentParticipantsCountText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentEntryfeeText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentMethodText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentTimeLeftText;
|
|
|
|
[SerializeField]
|
|
private Text tournamentPrizepoolText;
|
|
|
|
[SerializeField]
|
|
private Button joinEnterButton;
|
|
|
|
[SerializeField]
|
|
private GameObject connectBlocker;
|
|
|
|
[SerializeField]
|
|
private GameObject tournamentParticipantPrefab;
|
|
|
|
[SerializeField]
|
|
private Transform tournamentParticipantsListContent;
|
|
|
|
private bool isParticipants;
|
|
|
|
public TournamentManager.CTournament tournament;
|
|
|
|
private void Start()
|
|
{
|
|
ServerManager.Instance.getParticipantReady = false;
|
|
SetupTournamentItem();
|
|
InvokeRepeating("GetRefreshParticipants", 1f, 1f);
|
|
connectBlocker.SetActive(value: false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
MultiplayerManager.Instance.LeaveRoom();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
private void GetRefreshParticipants()
|
|
{
|
|
if (tournament != null)
|
|
{
|
|
ServerManager.Instance.GetTournamentParticipants(tournament.id);
|
|
}
|
|
}
|
|
|
|
private void SetupTournamentItem()
|
|
{
|
|
if (tournament == null)
|
|
{
|
|
DestroyEvent();
|
|
return;
|
|
}
|
|
GetRefreshParticipants();
|
|
joinEnterButton.interactable = false;
|
|
tournamentTitleText.text = TournamentManager.Instance.GetTournamentNameText(tournament);
|
|
tournamentTaskText.text = TournamentManager.Instance.GetTournamentTaskText(tournament);
|
|
tournamentDurationText.text = LanguageManager.Instance.GetText("DURATION") + ": " + tournament.duration + LanguageManager.Instance.GetText("MINUTES_SHORT");
|
|
tournamentParticipantsCountText.text = LanguageManager.Instance.GetText("PARTICIPANTS") + ": " + tournament.tournamentParticipants.Count + " / " + tournament.participantMaximum;
|
|
tournamentEntryfeeText.text = LanguageManager.Instance.GetText("ENTRY_FEE") + ": $ " + tournament.entryFee;
|
|
tournamentMethodText.text = TournamentManager.Instance.GetTournamentMethodText(tournament);
|
|
tournamentTimeLeftText.text = LanguageManager.Instance.GetText("TIME_LEFT") + ": " + tournament.timeLeft + " " + LanguageManager.Instance.GetText("MINUTES_SHORT");
|
|
tournamentPrizepoolText.text = LanguageManager.Instance.GetText("PRIZEPOOL") + ": $ " + tournament.entryFee * tournament.tournamentParticipants.Count;
|
|
tournamentMapImage.sprite = GameManager.Instance.gameLocations[tournament.mapId].GetScreenMapImage(0);
|
|
CreateParticipantsList();
|
|
}
|
|
|
|
private void CreateParticipantsList()
|
|
{
|
|
GameManager.TruncateContainer(tournamentParticipantsListContent);
|
|
tournament.tournamentParticipants.Sort(TournamentManager.SortParticipantsList);
|
|
for (int i = 0; i < tournament.tournamentParticipants.Count; i++)
|
|
{
|
|
ParticipantItem component = Object.Instantiate(tournamentParticipantPrefab, tournamentParticipantsListContent).GetComponent<ParticipantItem>();
|
|
component.tournamentParticipant = tournament.tournamentParticipants[i];
|
|
component.tournamentTask = tournament.tournamentTask;
|
|
component.tournamentParticipant.position = i;
|
|
}
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
if (tournament == null)
|
|
{
|
|
return;
|
|
}
|
|
tournamentParticipantsCountText.text = LanguageManager.Instance.GetText("PARTICIPANTS") + ": " + tournament.tournamentParticipants.Count + " / " + tournament.participantMaximum;
|
|
tournamentTimeLeftText.text = LanguageManager.Instance.GetText("TIME_LEFT") + ": " + GameManager.Instance.ConvertSeccondsToHMS(tournament.timeToEnd);
|
|
tournamentPrizepoolText.text = LanguageManager.Instance.GetText("PRIZEPOOL") + ": $ " + tournament.entryFee * tournament.tournamentParticipants.Count;
|
|
if (tournament.timeToEnd <= 180)
|
|
{
|
|
tournamentTimeLeftText.color = Color.red;
|
|
if (tournament.timeToEnd <= 0)
|
|
{
|
|
tournamentTimeLeftText.text = LanguageManager.Instance.GetText("TOURNAMENT_DONE");
|
|
}
|
|
}
|
|
RefreshParticipantsList();
|
|
JoinEnterButtonCheck();
|
|
}
|
|
|
|
private void RefreshParticipantsList()
|
|
{
|
|
ParticipantItem[] componentsInChildren = tournamentParticipantsListContent.GetComponentsInChildren<ParticipantItem>();
|
|
for (int i = 0; i < tournament.tournamentParticipants.Count; i++)
|
|
{
|
|
bool flag = false;
|
|
for (int j = 0; j < componentsInChildren.Length; j++)
|
|
{
|
|
if (componentsInChildren[j].tournamentParticipant.playerId == tournament.tournamentParticipants[i].playerId && componentsInChildren[j].tournamentParticipant.profile_index == tournament.tournamentParticipants[i].profile_index)
|
|
{
|
|
flag = true;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
ParticipantItem component = Object.Instantiate(tournamentParticipantPrefab, tournamentParticipantsListContent).GetComponent<ParticipantItem>();
|
|
component.tournamentParticipant = tournament.tournamentParticipants[i];
|
|
component.tournamentTask = tournament.tournamentTask;
|
|
}
|
|
}
|
|
tournament.tournamentParticipants.Sort(TournamentManager.SortParticipantsList);
|
|
for (int k = 0; k < tournament.tournamentParticipants.Count; k++)
|
|
{
|
|
tournament.tournamentParticipants[k].position = k;
|
|
}
|
|
}
|
|
|
|
private void JoinEnterButtonCheck()
|
|
{
|
|
if (tournament.timeToEnd <= 60 && isParticipants)
|
|
{
|
|
joinEnterButton.interactable = false;
|
|
return;
|
|
}
|
|
for (int i = 0; i < tournament.tournamentParticipants.Count; i++)
|
|
{
|
|
if (tournament.tournamentParticipants[i].playerId == SteamUser.GetSteamID().m_SteamID.ToString() && tournament.tournamentParticipants[i].profile_index == GameManager.Instance._playerData.currentPlayerProfileIndex)
|
|
{
|
|
isParticipants = true;
|
|
joinEnterButton.GetComponentInChildren<Text>().text = LanguageManager.Instance.GetText("ENTER");
|
|
joinEnterButton.interactable = true;
|
|
}
|
|
}
|
|
if (tournament.timeToEnd <= 180 && !isParticipants)
|
|
{
|
|
joinEnterButton.interactable = false;
|
|
}
|
|
else if (!isParticipants && ServerManager.Instance.getParticipantReady)
|
|
{
|
|
if (tournament.tournamentParticipants.Count < tournament.participantMaximum)
|
|
{
|
|
joinEnterButton.GetComponentInChildren<Text>().text = LanguageManager.Instance.GetText("JOIN");
|
|
joinEnterButton.interactable = true;
|
|
}
|
|
else
|
|
{
|
|
joinEnterButton.interactable = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void JoinEnter()
|
|
{
|
|
if (!isParticipants)
|
|
{
|
|
if (!GameManager.Instance._playerData.AddSubPlayerCashValue(-tournament.entryFee))
|
|
{
|
|
GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("NOT_ENOUGH_MONEY_TO_JOIN"), Object.FindObjectOfType<MainGameScene>().transform, deleteLast: true);
|
|
return;
|
|
}
|
|
ServerManager.Instance.JoinToTournament(tournament);
|
|
joinEnterButton.interactable = false;
|
|
}
|
|
else
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(ConnectCoroutine());
|
|
}
|
|
}
|
|
|
|
public void DestroyEvent()
|
|
{
|
|
Object.Destroy(base.gameObject);
|
|
}
|
|
|
|
private IEnumerator ConnectCoroutine()
|
|
{
|
|
bool timeoutFlag = false;
|
|
connectBlocker.SetActive(value: true);
|
|
if (MultiplayerManager.Instance.CurrentState != MultiplayerManager.State.Disconnected)
|
|
{
|
|
MultiplayerManager.Instance.Disconnect();
|
|
yield return StartCoroutine(WaitWithTimeout(MultiplayerManager.State.Disconnected, 3f));
|
|
if (timeoutFlag)
|
|
{
|
|
connectBlocker.SetActive(value: false);
|
|
yield break;
|
|
}
|
|
}
|
|
MultiplayerManager.Instance.Connect();
|
|
yield return StartCoroutine(WaitWithTimeout(MultiplayerManager.State.InLobby, 5f));
|
|
if (timeoutFlag)
|
|
{
|
|
connectBlocker.SetActive(value: false);
|
|
yield break;
|
|
}
|
|
MultiplayerManager.Instance.JoinOrCreateRoomTournament(tournament);
|
|
yield return StartCoroutine(WaitWithTimeout(MultiplayerManager.State.InRoom, 5f));
|
|
if (timeoutFlag)
|
|
{
|
|
connectBlocker.SetActive(value: false);
|
|
yield break;
|
|
}
|
|
MultiplayerManager.Instance.EnterRoomLocation(isTournament: true);
|
|
TournamentManager.Instance.LoadTournamentMap(tournament);
|
|
connectBlocker.SetActive(value: false);
|
|
IEnumerator WaitWithTimeout(MultiplayerManager.State state, float timeout)
|
|
{
|
|
timeoutFlag = false;
|
|
float timer = 0f;
|
|
while (MultiplayerManager.Instance.CurrentState != state && timer < timeout)
|
|
{
|
|
timer += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
if (MultiplayerManager.Instance.CurrentState != state)
|
|
{
|
|
timeoutFlag = true;
|
|
}
|
|
}
|
|
}
|
|
}
|