Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using Game.Model;
- using UnityEngine.Events;
- using Game.Ads;
- using Game.Tutorial;
- using System.Collections.Generic;
- using Game.Websockets;
- using Newtonsoft.Json;
- using Assets.Sources.Perks;
- namespace Game.Core
- {
- public class GameInit : MonoSingleton<GameInit>
- {
- public Camera mainCam;
- private string _deviceId;
- public string DeviceId => _deviceId;
- private RequestsController _requestsController;
- public RequestsController RequestsController { get => _requestsController; set => _requestsController = value; }
- private IWebSocketClient _webSocketClient;
- public IWebSocketClient WebSocketClient => _webSocketClient;
- [SerializeField]
- private TutorialController _tutorialController = default;
- public TutorialController TutorialController => _tutorialController;
- private PlayerProfile _playerProfile;
- public PlayerProfile PlayerProfile => _playerProfile;
- private Wallet _wallet;
- public Wallet Wallet => _wallet;
- private GameplayController _gameplayController;
- public GameplayController GameplayController => _gameplayController;
- private IslandsController _islandsController;
- public IslandsController IslandsController => _islandsController;
- private PerkFactory _perkFactory;
- public PerkFactory PerkFactory => _perkFactory;
- private SkinsController _skinsController;
- public SkinsController SkinsController => _skinsController;
- private ChestsController _chestsController;
- public ChestsController ChestsController => _chestsController;
- private StoreController _storeController;
- public StoreController StoreController => _storeController;
- private NotificationsController _notificationsController;
- public NotificationsController NotificationsController => _notificationsController;
- private FacebookController _facebookController;
- public FacebookController FacebookController => _facebookController;
- private MediationController _mediationController;
- public MediationController MediationController => _mediationController;
- private OfferWallController _offerWallController;
- public OfferWallController OfferWallController => _offerWallController;
- private BannerController _bannerController;
- public BannerController BannerController => _bannerController;
- private RewardedVideoController _rewardedVideoController;
- public RewardedVideoController RewardedVideoController => _rewardedVideoController;
- private InterstitialController _interstitialController;
- public InterstitialController InterstitialController => _interstitialController;
- private IAPController _iapController;
- public IAPController IAPController => _iapController;
- private GameValidations _gameValidations;
- public GameValidations GameValidations => _gameValidations;
- private LocalizationController _localizationController;
- public LocalizationController LocalizationController => _localizationController;
- private bool _initComplete;
- public bool InitComplete => _initComplete;
- private string _gameInfo;
- public string GameInfo => _gameInfo;
- public UnityAction onInitComplete;
- public UnityAction<bool> onAppPauseStateChange;
- public UnityAction<bool> onAppFocusStateChange;
- public UnityAction onAppWantsToQuit;
- private ServerMaintenanceData _maintenanceData;
- private void Start()
- {
- InitGame();
- }
- public void InitGame()
- {
- Input.multiTouchEnabled = false;
- Application.targetFrameRate = 60;
- UIController.Instance.DisplaySplashScreen();
- WebApi.DefineEnvironment();
- ObtainDeviceId();
- GetInitializationData();
- }
- private void ObtainDeviceId()
- {
- if (GameData.DevMode && !string.IsNullOrEmpty(GameData.DevConfig.forceDeviceId))
- {
- _deviceId = GameData.DevConfig.forceDeviceId;
- return;
- }
- _deviceId = SystemInfo.deviceUniqueIdentifier;
- }
- private void GetInitializationData()
- {
- WebApi.GetInitData(OnInitDataSuccess, OnInitDataError);
- }
- private void OnInitDataSuccess(object data)
- {
- InitData initData = JsonConvert.DeserializeObject<InitData>((string)data);
- SetInitData(initData);
- }
- private void OnInitDataError(int responseCode, string message)
- {
- switch (responseCode)
- {
- case 403:
- string forbidenMsg = string.Format("Connection Forbiden, code {0}.", responseCode);
- Debug.Log(forbidenMsg);
- UIController.Instance.ShowMsgBox("Oh no!", forbidenMsg);
- break;
- case 503:
- _maintenanceData = new ServerMaintenanceData();
- _maintenanceData.message = message;
- MaintenanceMode();
- break;
- default:
- string msgLog = string.Format("Connection error, check your internet!\n{0}\n({1})", message, responseCode);
- Debug.Log(msgLog);
- UIController.Instance.ShowMsgBox("Oh no!", msgLog, new Game.UI.ButtonContent() { text = "Try Again", action = GetInitializationData });
- break;
- }
- }
- private void SetInitData(InitData initData)
- {
- GameData.Init(initData);
- _playerProfile = new PlayerProfile(initData.sessionToken, initData.playerProfile);
- _localizationController = new LocalizationController();
- _gameValidations = new GameValidations(initData.currentGameVersion, initData.latestMandatoryVersion);
- if (_gameValidations.IsValidBuild)
- InitGameplay();
- }
- private void InitGameplay()
- {
- _webSocketClient = new WebSocketClientNative();
- _webSocketClient.AddSusbscription(WSMessageTypes.maintenanceMode, OnServerMaintenance);
- _wallet = new Wallet(GameData.InitData.playerProfile.wallet);
- _gameplayController = new GameplayController();
- // TODO: improve this sequential calls into a more cohesive system.
- InitIslands();
- }
- private void InitIslands()
- {
- _islandsController = new IslandsController(InitPerks);
- }
- private void InitPerks()
- {
- _perkFactory = new PerkFactory(InitSkins);
- }
- private void InitSkins()
- {
- _skinsController = new SkinsController(InitChests);
- }
- private void InitChests()
- {
- _chestsController = new ChestsController(InitStore);
- }
- private void InitStore()
- {
- _storeController = new StoreController(InitFrontend);
- }
- private void InitFrontend()
- {
- AudioController.Instance.Init();
- UIController.Instance.Init();
- // TODO: code commented out is being jumped over temporarily.
- //_notificationsController = new NotificationsController();
- FinalizeInit();
- }
- private void ContinueAfterRewardCalendar()
- {
- _iapController = new IAPController();
- _tutorialController.Init(ContinueAfterTutorial);
- }
- private void ContinueAfterTutorial()
- {
- if (!_playerProfile.Flags.isInPolicyAgreement)
- StartCoroutine(_localizationController.DisplayPolicy());
- _facebookController = new FacebookController();
- // Ads
- _mediationController = new MediationController();
- _offerWallController = new OfferWallController();
- _rewardedVideoController = new RewardedVideoController();
- _interstitialController = new InterstitialController();
- _bannerController = new BannerController();
- FinalizeInit();
- }
- private void FinalizeInit()
- {
- SetGameInfo();
- _initComplete = true;
- onInitComplete?.Invoke();
- if (_gameValidations.NewVersionAvailable)
- {
- NotificationData notificationData = new NotificationData
- {
- duration = 5,
- message = LocalizationController.Config.updateAvailableMsg,
- texture = GameData.GeneralConfig.defaultNotificationTexture
- };
- UIController.Instance.notificationsBar.Show(notificationData);
- }
- }
- private void SetGameInfo()
- {
- string version = GameData.BuildConfig.gameVersion;
- string playerId = _playerProfile.Id.ToString();
- string deviceId = _deviceId;
- _gameInfo = string.Format("{0} - {1}\n{2}", version, playerId, deviceId);
- //Debug.LogFormat("{0} - PushToken: {1}", _gameInfo, _notificationsController.PushToken);
- Debug.Log(_gameInfo);
- }
- private void OnServerMaintenance(string jsonData)
- {
- _maintenanceData = JsonUtility.FromJson<ServerMaintenanceData>(jsonData);
- if (_maintenanceData == null)
- {
- Debug.LogFormat("Could not parse ServerMaintenanceData: '{0}'.", jsonData);
- return;
- }
- MaintenanceMode();
- }
- #region Utility Members
- public static Coroutine RunCoroutine(IEnumerator coroutine)
- {
- return Instance.StartCoroutine(coroutine);
- }
- public void QuitGame()
- {
- Application.Quit();
- }
- private void MaintenanceMode()
- {
- UIController.Instance.ShowMsgBox("Uh-oh!", _maintenanceData.message, new Game.UI.ButtonContent() { text = "Ok", action = QuitGame });
- }
- #endregion
- private void OnApplicationPause(bool isPaused)
- {
- onAppPauseStateChange?.Invoke(isPaused);
- }
- private void OnApplicationFocus(bool hasFocus)
- {
- onAppFocusStateChange?.Invoke(hasFocus);
- }
- private void OnApplicationQuit()
- {
- onAppWantsToQuit?.Invoke();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment