using Noesis; using StellarConquest.Model.Actions; using StellarConquest.Model.Authentication; using StellarConquest.Model.Core; using StellarConquest.Model.Performance; using StellarConquest.Model.Platform; using System; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using EventArgs = Noesis.EventArgs; using Grid = Noesis.Grid; using GUI = Noesis.GUI; using Task = System.Threading.Tasks.Task; namespace StellarConquest.Presentation.Unity.UI { [AssetDependency] public partial class TitleScreen : UserControl, IGameScreenPrimary, IGameScreenInputProvider { private bool _IsPlayerServerError = false; private bool _IsGameServerError = false; private bool _IsSystemServerError = false; private bool _IsServiceAuthenticationError = false; private string _GameNews; private Statistics _Statistics = new Statistics(); public List _Players = null; private FrameworkElement _Root; private Grid _LayoutRoot; private StackPanel _NewsStackPanel1; private NewsControl _NewsControl1; private StackPanel _NewsStackPanel2; private NewsControl _NewsControl2; private StackPanel _SecondaryScreenStackPanel; private ServerControl _ServerControl; private ServerListControl _ServerListControl; private LocalizationControl _LocalizationControl; private ButtonPanelControl _ButtonPanelControl; private TextBlock _VersionTextBlock; private TextBlock _Handle; public bool IsCursorOverUI { get { return false; } } private Grid _PreviewGrid; private static bool _PreviewGridShown = false; public TitleScreen() { Initialized += OnInitialized; InitializeComponent(); } private void InitializeComponent() { GUI.LoadComponent(this, "Assets/User Interface/Screens/Title/TitleScreen.xaml"); _Root = Content as FrameworkElement; _LayoutRoot = _Root.FindName(nameof(_LayoutRoot)) as Grid; _NewsStackPanel1 = _Root.FindName(nameof(_NewsStackPanel1)) as StackPanel; _NewsControl1 = _Root.FindName(nameof(_NewsControl1)) as NewsControl; _NewsStackPanel2 = _Root.FindName(nameof(_NewsStackPanel2)) as StackPanel; _NewsControl2 = _Root.FindName(nameof(_NewsControl2)) as NewsControl; _SecondaryScreenStackPanel = _Root.FindName(nameof(_SecondaryScreenStackPanel)) as StackPanel; _ButtonPanelControl = _Root.FindName(nameof(_ButtonPanelControl)) as ButtonPanelControl; _VersionTextBlock = _Root.FindName(nameof(_VersionTextBlock)) as TextBlock; _Handle = _Root.FindName(nameof(_Handle)) as TextBlock; _PreviewGrid = _Root.FindName(nameof(_PreviewGrid)) as Grid; _LayoutRoot.Visibility = Visibility.Collapsed; } private void OnInitialized(object sender, EventArgs args) { if (!Application.isPlaying) return; RegisterEvents(); // TODO: enable and test account id caching feature when Steam is down. //var result = PlayerPreferences.Instance.GetPreviousAccount(); //if (result.Item1 != null && result.Item2.HasValue) //{ // SessionState.Instance.AuthenticationToken = new AuthenticationToken(result.Item1); // SessionState.Instance.AccountId = result.Item2.Value; //} _ = Initialize(PlayerPreferences.Instance.ConquestEnvironment, Application.isEditor); //https://howtomarketagame.com/2021/06/21/neat-ideas-for-your-games-demo/ //TutorialControl tutorialControl = new TutorialControl(); //string text = "Thank you for playing this alpha build of Stellar Conquest. Please join us on our Discord channel for news about the next play test. See you for the next update!" + Environment.NewLine + Environment.NewLine; //tutorialControl.SetText("Build Expired", false, text); //tutorialControl.Show(); if (!_PreviewGridShown) { _PreviewGrid.Visibility = Visibility.Visible; _PreviewGrid.FadeInScreen(this); _PreviewGridShown = true; } } public async Task Initialize(EnvironmentType cloudEnvironment, bool isEditor) { UnityDispatcher.Instance.Invoke(() => { _Handle.Text = string.Empty; ChatBuffer.Instance.Execute(new ChatAction() { Clear = true }); }); _IsSystemServerError = false; _IsGameServerError = false; try { await NetworkClient.Instance.Initialize(cloudEnvironment, true, isEditor); _IsSystemServerError = !await NetworkClient.Instance.TestSystemServerHttpConnection(GameStateMachine.Instance.CancellationTokenSource.Token); if (!_IsSystemServerError) { var taskA = GameServices.Instance.InitializeGameService(); // initialize Steam or XBL and create Stellar Conquest authorization token var taskB = GetGameServerVersion(); var taskC = GetGameNewsAsync(); // initialize game services await taskA; _IsServiceAuthenticationError = !taskA.Result; if (!_IsServiceAuthenticationError && !_IsSystemServerError) { var taskD = GetCloudLicenseAsync(); // get license using game service authorization token await taskD; } if (taskB != null) await taskB; if (taskC != null) await taskC; } UnityDispatcher.Instance.Invoke(() => FadeInUI()); } catch (TaskCanceledException) { } catch (Exception e) { Debug.LogError(e); _IsSystemServerError = true; } #if (UNITY_STANDALONE_WIN) if (!Application.isEditor && Constants.System.Build == "Debug") UIStateMachine.Instance.UIShowMessage("Build Error", "Warning: DEBUG library detected in RELEASE build."); #endif if (_IsServiceAuthenticationError) { UnityDispatcher.Instance.Invoke(() => { #if(UNITY_STANDALONE_WIN) _NewsControl1.ShowError("Please launch Steam before playing Stellar Conquest and ensure you have access to this game."); #else _NewsControl1.ShowError("A problem occurred while authenticating your account Xbox Live."); #endif _ButtonPanelControl.SetPlayButtonText("RETRY"); }); } else if (_IsSystemServerError) { UnityDispatcher.Instance.Invoke(() => { FadeInUI(); _NewsControl1.ShowError($"Stellar Conquest ({PlayerPreferences.Instance.ConquestEnvironment}) " + "is down for maintenance and will be back up shortly. Please retry again."); _ButtonPanelControl.SetPlayButtonText("RETRY"); }); } else if (!_IsSystemServerError) { UnityDispatcher.Instance.Invoke(() => { _NewsControl1.Update(_Statistics, _GameNews, null); if (SessionState.Instance.ClientVersion == SessionState.Instance.ServerVersion) _ButtonPanelControl.SetConquestButtonEnabled(SessionState.Instance.HasLicense && !_IsSystemServerError && !_IsGameServerError); else _ButtonPanelControl.SetConquestButtonEnabled(false); }); } } private async Task Login() { CheckedResponse> result = null; try { // get players result = await NetworkClient.Instance.GetPlayers(); // get price list _ = GetPriceListAsync(); } catch (Exception e) { Debug.LogError(e); result = new CheckedResponse>(null, e.Message); } UnityDispatcher.Instance.Invoke(() => { PlayerPreferences.Instance.FPSLogin = Mathf.RoundToInt(GameState.Instance.FrameRateCounter.FPS); PlayerPreferences.Instance.SavePreferences(); if (result != null && result.Result != null) { SessionState.Instance.AuthenticatedPlayers = result.Result; if (result.Result.Count == 0) GameStateMachine.Instance.SetState(GameStateType.CharacterCreation); else GameStateMachine.Instance.SetState(GameStateType.CharacterSelection); RuntimeSettings.Instance.SendTelemetry(); } else if (result == null || result.Result == null || !string.IsNullOrEmpty(result.Error)) { ShowPlayOptions(); if (result != null && !string.IsNullOrEmpty(result.Error)) _NewsControl2.ShowError("Login Error: " + result.Error); else _NewsControl2.ShowError("Login Error: unable to access account."); } }); } private void ShowPlayOptions() { UIStateMachine.Instance.TransitionLayer.ShowLoadingImage(false, false); _ServerControl = null; _ServerListControl = null; _LocalizationControl = null; _SecondaryScreenStackPanel.Children.Clear(); _ButtonPanelControl.SetPlayButtonEnabled(true); _ButtonPanelControl.SetPlayButtonText("PLAY"); _NewsStackPanel1.Visibility = Visibility.Collapsed; _NewsStackPanel2.Visibility = Visibility.Visible; _ButtonPanelControl.Visibility = Visibility.Visible; _ButtonPanelControl.ShowPlayOptions(); _Handle.Visibility = Visibility.Collapsed; _VersionTextBlock.Visibility = Visibility.Collapsed; } private void ShowTitleOptions() { UIStateMachine.Instance.TransitionLayer.ShowLoadingImage(false, false); _NewsStackPanel1.Visibility = Visibility.Visible; _NewsStackPanel2.Visibility = Visibility.Collapsed; _ButtonPanelControl.Visibility = Visibility.Visible; if (_ServerControl != null) _ServerControl.Visibility = Visibility.Collapsed; if (_ServerListControl != null) _ServerListControl.Visibility = Visibility.Collapsed; _ButtonPanelControl.ShowTitleOptions(); _Handle.Visibility = Visibility.Visible; _VersionTextBlock.Visibility = Visibility.Visible; } private void FadeInUI() { if (_LayoutRoot.Visibility == Visibility.Collapsed) { _LayoutRoot.Visibility = Visibility.Visible; _LayoutRoot.FadeInScreen(this); } } private void Shutdown() { if (!Application.isEditor) { IsHitTestVisible = false; _ = Task.Run(async () => { try { await GameServices.Instance.ShutdownGameService(); if (ServerBootstrap.Instance.IsServerRunning && !ServerBootstrap.Instance.IsDedicatedServer) await NetworkClient.Instance.GracefulPlayerServerShutdown(); ServerBootstrap.Instance.StopServer(false); } catch (Exception e) { Debug.Log(e); } finally { UnityDispatcher.Instance.Invoke(() => Application.Quit()); } }); } } public void UpdateInput() { if (_PreviewGrid.Visibility == Visibility.Visible) if (Input.anyKeyDown || Input.GetMouseButtonDown(0) || Input.GetButton(InputNames.GamePad.A) || Input.GetButton(InputNames.GamePad.B)) { if (_PreviewGrid.Visibility == Visibility.Visible) { _PreviewGrid.FadeOutScreen(this, () => _PreviewGrid.Visibility = Visibility.Collapsed); return; } } if (Input.GetKeyDown(KeyCode.Escape)) { if (GameServerCancellationTokenSource != null) GameServerCancellationTokenSource.Cancel(false); else { if (_ServerControl != null) { _SecondaryScreenStackPanel.Children.Remove(_ServerControl); _ServerControl = null; ShowPlayOptions(); } else if (_ServerListControl != null) { _SecondaryScreenStackPanel.Children.Remove(_ServerListControl); _ServerListControl = null; ShowPlayOptions(); } else if (_LocalizationControl != null) { _SecondaryScreenStackPanel.Children.Remove(_LocalizationControl); _LocalizationControl = null; ShowPlayOptions(); } else if (_ButtonPanelControl.Visibility == Visibility.Visible && _NewsStackPanel2.Visibility == Visibility.Visible) { ShowTitleOptions(); } else { InputControl inputControl = new InputControl("Quit", "Quit Stellar Conquest?", null, InputControlButtonTypes.Yes | InputControlButtonTypes.No); inputControl.Hidden += (sender, e) => { if (inputControl.Response == InputControlButtonTypes.Yes) Shutdown(); else this.FadeInScreen(this); }; this.FadeOutScreen(this); inputControl.Show(); } } } #if (UNITY_GAMECORE_XBOXSERIES) if (Input.GetKeyDown(KeyCode.Alpha1)) _ = Login(); #endif } public void UpdateUserInterface() { } } }