Rodlaiz

GameInit

Mar 25th, 2022
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.73 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using Game.Model;
  4. using UnityEngine.Events;
  5. using Game.Ads;
  6. using Game.Tutorial;
  7. using System.Collections.Generic;
  8. using Game.Websockets;
  9. using Newtonsoft.Json;
  10. using Assets.Sources.Perks;
  11.  
  12. namespace Game.Core
  13. {
  14.     public class GameInit : MonoSingleton<GameInit>
  15.     {
  16.         public Camera mainCam;
  17.  
  18.         private string _deviceId;
  19.         public string DeviceId => _deviceId;
  20.  
  21.         private RequestsController _requestsController;
  22.         public RequestsController RequestsController { get => _requestsController; set => _requestsController = value; }
  23.  
  24.         private IWebSocketClient _webSocketClient;
  25.         public IWebSocketClient WebSocketClient => _webSocketClient;
  26.  
  27.         [SerializeField]
  28.         private TutorialController _tutorialController = default;
  29.         public TutorialController TutorialController => _tutorialController;
  30.  
  31.         private PlayerProfile _playerProfile;
  32.         public PlayerProfile PlayerProfile => _playerProfile;
  33.  
  34.         private Wallet _wallet;
  35.         public Wallet Wallet => _wallet;
  36.  
  37.         private GameplayController _gameplayController;
  38.         public GameplayController GameplayController => _gameplayController;
  39.  
  40.         private IslandsController _islandsController;
  41.         public IslandsController IslandsController => _islandsController;
  42.  
  43.         private PerkFactory _perkFactory;
  44.         public PerkFactory PerkFactory => _perkFactory;
  45.  
  46.         private SkinsController _skinsController;
  47.         public SkinsController SkinsController => _skinsController;
  48.  
  49.         private ChestsController _chestsController;
  50.         public ChestsController ChestsController => _chestsController;
  51.  
  52.         private StoreController _storeController;
  53.         public StoreController StoreController => _storeController;
  54.  
  55.         private NotificationsController _notificationsController;
  56.         public NotificationsController NotificationsController => _notificationsController;
  57.  
  58.         private FacebookController _facebookController;
  59.         public FacebookController FacebookController => _facebookController;
  60.  
  61.         private MediationController _mediationController;
  62.         public MediationController MediationController => _mediationController;
  63.  
  64.         private OfferWallController _offerWallController;
  65.         public OfferWallController OfferWallController => _offerWallController;
  66.  
  67.         private BannerController _bannerController;
  68.         public BannerController BannerController => _bannerController;
  69.  
  70.         private RewardedVideoController _rewardedVideoController;
  71.         public RewardedVideoController RewardedVideoController => _rewardedVideoController;
  72.  
  73.         private InterstitialController _interstitialController;
  74.         public InterstitialController InterstitialController => _interstitialController;
  75.  
  76.         private IAPController _iapController;
  77.         public IAPController IAPController => _iapController;
  78.  
  79.         private GameValidations _gameValidations;
  80.         public GameValidations GameValidations => _gameValidations;
  81.  
  82.         private LocalizationController _localizationController;
  83.         public LocalizationController LocalizationController => _localizationController;
  84.  
  85.         private bool _initComplete;
  86.         public bool InitComplete => _initComplete;
  87.  
  88.         private string _gameInfo;
  89.         public string GameInfo => _gameInfo;
  90.  
  91.         public UnityAction onInitComplete;
  92.         public UnityAction<bool> onAppPauseStateChange;
  93.         public UnityAction<bool> onAppFocusStateChange;
  94.         public UnityAction onAppWantsToQuit;
  95.  
  96.         private ServerMaintenanceData _maintenanceData;
  97.  
  98.  
  99.         private void Start()
  100.         {
  101.             InitGame();
  102.         }
  103.  
  104.         public void InitGame()
  105.         {
  106.             Input.multiTouchEnabled = false;
  107.             Application.targetFrameRate = 60;
  108.  
  109.             UIController.Instance.DisplaySplashScreen();
  110.  
  111.             WebApi.DefineEnvironment();
  112.             ObtainDeviceId();
  113.             GetInitializationData();
  114.         }
  115.  
  116.         private void ObtainDeviceId()
  117.         {
  118.             if (GameData.DevMode && !string.IsNullOrEmpty(GameData.DevConfig.forceDeviceId))
  119.             {
  120.                 _deviceId = GameData.DevConfig.forceDeviceId;
  121.                 return;
  122.             }
  123.  
  124.             _deviceId = SystemInfo.deviceUniqueIdentifier;
  125.         }
  126.  
  127.         private void GetInitializationData()
  128.         {
  129.             WebApi.GetInitData(OnInitDataSuccess, OnInitDataError);
  130.         }
  131.  
  132.         private void OnInitDataSuccess(object data)
  133.         {
  134.             InitData initData = JsonConvert.DeserializeObject<InitData>((string)data);
  135.             SetInitData(initData);
  136.         }
  137.  
  138.         private void OnInitDataError(int responseCode, string message)
  139.         {
  140.             switch (responseCode)
  141.             {
  142.                 case 403:
  143.                     string forbidenMsg = string.Format("Connection Forbiden, code {0}.", responseCode);
  144.                     Debug.Log(forbidenMsg);
  145.                     UIController.Instance.ShowMsgBox("Oh no!", forbidenMsg);
  146.                     break;
  147.  
  148.                 case 503:
  149.                     _maintenanceData = new ServerMaintenanceData();
  150.                     _maintenanceData.message = message;
  151.                
  152.                     MaintenanceMode();
  153.                     break;
  154.  
  155.                 default:
  156.                     string msgLog = string.Format("Connection error, check your internet!\n{0}\n({1})", message, responseCode);
  157.                     Debug.Log(msgLog);
  158.                     UIController.Instance.ShowMsgBox("Oh no!", msgLog, new Game.UI.ButtonContent() { text = "Try Again", action = GetInitializationData });
  159.                     break;
  160.             }
  161.         }
  162.  
  163.         private void SetInitData(InitData initData)
  164.         {
  165.             GameData.Init(initData);
  166.  
  167.             _playerProfile = new PlayerProfile(initData.sessionToken, initData.playerProfile);
  168.             _localizationController = new LocalizationController();
  169.             _gameValidations = new GameValidations(initData.currentGameVersion, initData.latestMandatoryVersion);
  170.  
  171.             if (_gameValidations.IsValidBuild)
  172.                 InitGameplay();
  173.         }
  174.  
  175.         private void InitGameplay()
  176.         {
  177.             _webSocketClient = new WebSocketClientNative();
  178.             _webSocketClient.AddSusbscription(WSMessageTypes.maintenanceMode, OnServerMaintenance);
  179.  
  180.             _wallet = new Wallet(GameData.InitData.playerProfile.wallet);
  181.  
  182.             _gameplayController = new GameplayController();
  183.  
  184.             // TODO: improve this sequential calls into a more cohesive system.
  185.             InitIslands();
  186.         }
  187.  
  188.         private void InitIslands()
  189.         {
  190.             _islandsController = new IslandsController(InitPerks);
  191.         }
  192.  
  193.         private void InitPerks()
  194.         {
  195.             _perkFactory = new PerkFactory(InitSkins);
  196.         }
  197.  
  198.         private void InitSkins()
  199.         {
  200.             _skinsController = new SkinsController(InitChests);
  201.         }
  202.  
  203.         private void InitChests()
  204.         {
  205.             _chestsController = new ChestsController(InitStore);
  206.         }
  207.  
  208.         private void InitStore()
  209.         {
  210.             _storeController = new StoreController(InitFrontend);
  211.         }
  212.  
  213.         private void InitFrontend()
  214.         {
  215.             AudioController.Instance.Init();
  216.             UIController.Instance.Init();
  217.  
  218.             // TODO: code commented out is being jumped over temporarily.
  219.             //_notificationsController = new NotificationsController();
  220.  
  221.             FinalizeInit();
  222.         }
  223.  
  224.         private void ContinueAfterRewardCalendar()
  225.         {
  226.             _iapController = new IAPController();
  227.  
  228.             _tutorialController.Init(ContinueAfterTutorial);
  229.         }
  230.  
  231.         private void ContinueAfterTutorial()
  232.         {
  233.             if (!_playerProfile.Flags.isInPolicyAgreement)
  234.                 StartCoroutine(_localizationController.DisplayPolicy());
  235.  
  236.             _facebookController = new FacebookController();
  237.  
  238.             // Ads
  239.             _mediationController = new MediationController();
  240.             _offerWallController = new OfferWallController();
  241.             _rewardedVideoController = new RewardedVideoController();
  242.             _interstitialController = new InterstitialController();
  243.             _bannerController = new BannerController();
  244.  
  245.             FinalizeInit();
  246.         }
  247.  
  248.         private void FinalizeInit()
  249.         {
  250.             SetGameInfo();
  251.            
  252.             _initComplete = true;
  253.             onInitComplete?.Invoke();
  254.  
  255.             if (_gameValidations.NewVersionAvailable)
  256.             {
  257.                 NotificationData notificationData = new NotificationData
  258.                 {
  259.                     duration = 5,
  260.                     message = LocalizationController.Config.updateAvailableMsg,
  261.                     texture = GameData.GeneralConfig.defaultNotificationTexture
  262.                 };
  263.  
  264.                 UIController.Instance.notificationsBar.Show(notificationData);
  265.             }
  266.         }
  267.  
  268.         private void SetGameInfo()
  269.         {
  270.             string version = GameData.BuildConfig.gameVersion;
  271.             string playerId = _playerProfile.Id.ToString();
  272.             string deviceId = _deviceId;
  273.  
  274.             _gameInfo = string.Format("{0} - {1}\n{2}", version, playerId, deviceId);
  275.  
  276.             //Debug.LogFormat("{0} - PushToken: {1}", _gameInfo, _notificationsController.PushToken);
  277.             Debug.Log(_gameInfo);
  278.         }
  279.  
  280.         private void OnServerMaintenance(string jsonData)
  281.         {
  282.             _maintenanceData = JsonUtility.FromJson<ServerMaintenanceData>(jsonData);
  283.             if (_maintenanceData == null)
  284.             {
  285.                 Debug.LogFormat("Could not parse ServerMaintenanceData: '{0}'.", jsonData);
  286.                 return;
  287.             }
  288.  
  289.             MaintenanceMode();
  290.         }
  291.  
  292. #region Utility Members
  293.  
  294.         public static Coroutine RunCoroutine(IEnumerator coroutine)
  295.         {
  296.             return Instance.StartCoroutine(coroutine);
  297.         }
  298.  
  299.         public void QuitGame()
  300.         {
  301.             Application.Quit();
  302.         }
  303.  
  304.         private void MaintenanceMode()
  305.         {
  306.             UIController.Instance.ShowMsgBox("Uh-oh!", _maintenanceData.message, new Game.UI.ButtonContent() { text = "Ok", action = QuitGame });
  307.         }
  308.  
  309. #endregion
  310.  
  311.         private void OnApplicationPause(bool isPaused)
  312.         {
  313.             onAppPauseStateChange?.Invoke(isPaused);
  314.         }
  315.  
  316.         private void OnApplicationFocus(bool hasFocus)
  317.         {
  318.             onAppFocusStateChange?.Invoke(hasFocus);            
  319.         }
  320.  
  321.         private void OnApplicationQuit()
  322.         {
  323.             onAppWantsToQuit?.Invoke();
  324.         }
  325.     }
  326. }
Advertisement
Add Comment
Please, Sign In to add comment