JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/Start/PlayButton.cs

145 lines
5.6 KiB
C#

using Analytics;
using Audio;
using ConsoleConnection;
using Network;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Start
{
public class PlayButton : MonoBehaviour
{
[Header("Text automatic discovery")]
[SerializeField]
private Localization.LocalizedText playText = null;
[SerializeField]
private Localization.LocalizedText discoveryText = null;
private Animator parentPlayAnimator = null;
// Keeping the last console found locally, to connect to it directly when the player press "play"
private ConsoleServer consoleServer;
private Button playButton;
// We ask if we already see the "game console" Popup, if so we do the classic flow.
// Otherwise, we do a specific one where we ask the player to tell us which game version he's playing on
private void Awake()
{
// Getting the discovery event and popups
ConnectionManager.Instance.OnConsoleDiscoveryStarted += ConsoleDiscoveryStarted;
ConnectionManager.Instance.OnConsoleDiscoveryFailed += ConsoleDiscoveryFail;
ConnectionManager.Instance.OnConsoleDiscoveryFinished += ConsoleDiscoveryFinished;
parentPlayAnimator = transform.parent.GetComponent<Animator>();
playButton = GetComponent<Button>();
}
private void OnDestroy()
{
ConnectionManager.Instance.OnConsoleDiscoveryStarted -= ConsoleDiscoveryStarted;
ConnectionManager.Instance.OnConsoleDiscoveryFailed -= ConsoleDiscoveryFail;
ConnectionManager.Instance.OnConsoleDiscoveryFinished -= ConsoleDiscoveryFinished;
}
public void OpenStart()
{
AudioManager.Instance.Play(AudioManager.SoundIds.Validation);
Statistics.LaunchWizardSession("PlayButton");
if (!PlayerPrefs.HasKey("PlayModeAlreadyLaunched") && consoleServer == null)
{
UiManager.Instance.ShowControllerTutorial();
// add key
PlayerPrefs.SetInt("PlayModeAlreadyLaunched", 1);
PlayerPrefs.Save();
}
else if (consoleServer != null)
{
// If a console is defined, we connect directly to it.
ConnectionManager.Instance.ConnectToConsole(consoleServer);
}
else
{
// Otherwise, we launch the connection flow
ConnectionManager.Instance.LaunchConnectionFlow();
}
}
public void ConsoleDiscoveryStarted()
{
// Resseting the console server to prevent connecting to a bad one each time
consoleServer = null;
ChangeAlphaPlayBtn(false);
playButton.interactable = false;
parentPlayAnimator.SetInteger("DiscoveryState", 1);
}
public void ConsoleDiscoveryFail(ConnectionManager.DiscoveryError discoveryFail)
{
if (discoveryFail == ConnectionManager.DiscoveryError.NoWifi)
{
ChangeAlphaPlayBtn(true);
playText.SetLocId(20001955); // Try again /n<size=20>Connection not found</size>
playButton.interactable = true;
parentPlayAnimator.SetInteger("DiscoveryState", 4);
Debug.LogError("PlayButton : Discovery not done, wifi not available");
}
}
public void ConsoleDiscoveryFinished(List<Network.ConsoleServer> consoleServerList)
{
// If we find only ONE console, we want to display it on the button, otherwise we just display
// The "Play" text
Debug.Assert(consoleServerList != null, "PlayButton : Console server list is null");
if (consoleServerList.Count == 1)
{
consoleServer = consoleServerList[0];
// Just dance parameter for the version
var gameVersion = TitleDatabase.ExtractGameVersion(consoleServer.TitleId);
//Play\n<size=20>{0} on {1}</size>""
var gameVersionPlatformNameValue = new Dictionary<string, string>();
string gameVersionTxt = "Just Dance " + gameVersion.ToString();
if (gameVersion == 0)
gameVersionTxt = "Just Dance";
gameVersionPlatformNameValue.Add("GAME_VERSION", gameVersionTxt);
gameVersionPlatformNameValue.Add("PLATFORM_NAME", consoleServer.DisplayName);
// "Play\n<size=20>[var:GAME_VERSION] on [var:PLATFORM_NAME]</size>
string gameVersionPlatformName = Localization.LocalizationManager.Instance.GetLocalizedString(20001954, gameVersionPlatformNameValue);
discoveryText.SetText(gameVersionPlatformName);
// One console found, we display it.
parentPlayAnimator.SetInteger("DiscoveryState", 3);
} else
{
// Discovery not found / multiple console
parentPlayAnimator.SetInteger("DiscoveryState", 2);
}
// Displaying button text
// Play text
playText.SetLocId(12764);
// Any console found, we display the discovery text, otherwise the "Play" text
playButton.interactable = true;
ChangeAlphaPlayBtn(true);
}
// Changing the alpha animation of the play button
private void ChangeAlphaPlayBtn(bool alphaIn)
{
parentPlayAnimator.SetBool("AlphaIn", alphaIn);
}
}
}