private async void Start() { _loadingMenu.Show(true); if (_connection.Session == null) { string deviceId = GetDeviceId(); if (!string.IsNullOrEmpty(deviceId)) { PlayerPrefs.SetString(GameConstants.DeviceIdKey, deviceId); } await InitializeGame(deviceId); } // Provide Nakama connection to UI elements that need it. _battleMenuUI.Init(_connection); _loadingMenu.Init(_connection); _notificationPopup.Init(_connection); _cardsMenuUI.Init(_connection); _clanCreationPanel.Init(_connection); _profilePopup.Init(_connection, _profileUpdatePanel); _profileUpdatePanel.Init(_connection, GetDeviceId()); _clansMenuUI.Init(_connection, _profilePopup); _friendsMenuUI.Init(_connection); _leaderboardMenuUI.Init(_connection, _profilePopup); _profileMenuUI.Init(_connection, _profileUpdatePanel); _loadingMenu.Hide(true); }
private async void InitializeGame() { string deviceId = GetDeviceId(); if (!string.IsNullOrEmpty(deviceId)) { PlayerPrefs.SetString(GameConstants.DeviceIdKey, deviceId); } if (_connection.Session == null) { _loadingMenu.Show(true); try { #if !UNITY_EDITOR FB.Init(() => { FB.ActivateApp(); }); #endif } catch (Exception e) { // Not supported on mac #if !UNITY_OSX_STANDALONE Debug.LogWarning("Error initializing facebook: " + e.Message); #endif } var client = new Client("http", "localhost", 7350, "defaultkey", UnityWebRequestAdapter.Instance); client.Timeout = 5; var socket = client.NewSocket(useMainThread: true); string storedToken = PlayerPrefs.GetString(GameConstants.AuthTokenKey, null); bool isStoredToken = !string.IsNullOrEmpty(storedToken); ISession session = null; if (isStoredToken) { session = Nakama.Session.Restore(storedToken); } bool isExpiredToken = isStoredToken && session.HasExpired(DateTime.UtcNow); if (!isStoredToken || isExpiredToken) { try { session = await client.AuthenticateDeviceAsync(deviceId); } catch (ApiResponseException e) { Debug.LogWarning("Error authenticating device: " + e.Message); Application.Quit(); return; } } try { await socket.ConnectAsync(session); } catch (Exception e) { Debug.LogWarning("Error connecting socket: " + e.Message); } _loadingMenu.Hide(true); PlayerPrefs.SetString(GameConstants.AuthTokenKey, session.AuthToken); IApiAccount account; try { account = await client.GetAccountAsync(session); } catch (ApiResponseException e) { Debug.LogError("Error getting user account: " + e.Message); if (e.StatusCode == 404) { Debug.LogWarning("invalid auth token. deleting... " + "please restart application."); PlayerPrefs.DeleteKey(GameConstants.AuthTokenKey); } // ------------------------------------------- // NOTE: Quit the game, if error. // In production, consider additional // logic to retry the connection with // exponential backoff. // ------------------------------------------- Application.Quit(); return; } _connection.Init(client, socket, account, session); } // Provide Nakama connection to UI elements that need it. _battleMenuUI.Init(_connection); _loadingMenu.Init(_connection); _notificationPopup.Init(_connection); _cardsMenuUI.Init(_connection); _clanCreationPanel.Init(_connection); _profilePopup.Init(_connection, _profileUpdatePanel); _profileUpdatePanel.Init(_connection, deviceId); _clansMenuUI.Init(_connection, _profilePopup); _friendsMenuUI.Init(_connection); _leaderboardMenuUI.Init(_connection, _profilePopup); _profileMenuUI.Init(_connection, _profileUpdatePanel); }