Skip to content

Commit 4aba971

Browse files
committed
fix login
1 parent 1d9c501 commit 4aba971

File tree

2 files changed

+119
-15
lines changed

2 files changed

+119
-15
lines changed

Assets/Shared/Scripts/UI/LevelCompleteScreen.cs

+119-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
using UnityEngine.UI;
66
using System;
77
using System.Collections.Generic;
8+
using Immutable.Passport;
9+
using Cysharp.Threading.Tasks;
10+
using System.Numerics;
11+
using System.Net.Http;
812

913
namespace HyperCasual.Runner
1014
{
@@ -102,34 +106,142 @@ public int CoinCount
102106
}
103107
}
104108

105-
public void OnEnable()
109+
public async void OnEnable()
106110
{
107111
// Set listener to 'Next' button
108112
m_NextButton.RemoveListener(OnNextButtonClicked);
109113
m_NextButton.AddListener(OnNextButtonClicked);
110114

111-
// Set listener to "Continue with Passport" button
115+
// Set listener to "Continue with Passport" button
112116
m_ContinuePassportButton.RemoveListener(OnContinueWithPassportButtonClicked);
113117
m_ContinuePassportButton.AddListener(OnContinueWithPassportButtonClicked);
114118

115119
// Set listener to "Try again" button
116120
m_TryAgainButton.RemoveListener(OnTryAgainButtonClicked);
117121
m_TryAgainButton.AddListener(OnTryAgainButtonClicked);
118122

119-
ShowNextButton(true);
123+
ShowError(false);
124+
ShowLoading(false);
125+
126+
// If player is logged into Passport mint coins to player
127+
if (SaveManager.Instance.IsLoggedIn)
128+
{
129+
// Mint collected coins to player
130+
await MintCoins();
131+
}
132+
else
133+
{
134+
// Show 'Next' button if player is already logged into Passport
135+
ShowNextButton(SaveManager.Instance.IsLoggedIn);
136+
// Show "Continue with Passport" button if the player is not logged into Passport
137+
ShowContinueWithPassportButton(!SaveManager.Instance.IsLoggedIn);
138+
}
120139
}
121140

122-
private void OnContinueWithPassportButtonClicked()
141+
/// <summary>
142+
/// Mints collected coins (i.e. Immutable Runner Token) to the player's wallet
143+
/// </summary>
144+
private async UniTask MintCoins()
123145
{
146+
// This function is similar to MintCoins() in MintScreen.cs. Consider refactoring duplicate code in production.
147+
Debug.Log("Minting coins...");
148+
bool success = false;
149+
150+
// Show loading
151+
ShowLoading(true);
152+
ShowNextButton(false);
153+
ShowError(false);
154+
155+
try
156+
{
157+
// Don't mint any coins if player did not collect any
158+
if (m_CoinCount == 0)
159+
{
160+
success = true;
161+
}
162+
else
163+
{
164+
// Get the player's wallet address to mint the coins to
165+
List<string> accounts = await Passport.Instance.ZkEvmRequestAccounts();
166+
string address = accounts[0];
167+
if (address != null)
168+
{
169+
// Calculate the quantity to mint
170+
// Need to take into account Immutable Runner Token decimal value i.e. 18
171+
BigInteger quantity = BigInteger.Multiply(new BigInteger(m_CoinCount), BigInteger.Pow(10, 18));
172+
Debug.Log($"Quantity: {quantity}");
173+
var nvc = new List<KeyValuePair<string, string>>
174+
{
175+
// Set 'to' to the player's wallet address
176+
new KeyValuePair<string, string>("to", address),
177+
// Set 'quanity' to the number of coins collected
178+
new KeyValuePair<string, string>("quantity", quantity.ToString())
179+
};
180+
using var client = new HttpClient();
181+
string url = $"http://localhost:3000/mint/token"; // Endpoint to mint token
182+
using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
183+
using var res = await client.SendAsync(req);
184+
success = res.IsSuccessStatusCode;
185+
}
186+
}
187+
}
188+
catch (Exception ex)
189+
{
190+
Debug.Log($"Failed to mint coins: {ex.Message}");
191+
}
192+
193+
ShowLoading(false);
194+
ShowNextButton(success);
195+
ShowError(!success);
124196
}
125197

126-
private void OnTryAgainButtonClicked()
198+
private async void OnContinueWithPassportButtonClicked()
127199
{
200+
try
201+
{
202+
// Show loading
203+
ShowContinueWithPassportButton(false);
204+
ShowLoading(true);
205+
206+
// Log into Passport
207+
await Passport.Instance.Login();
208+
209+
// Successfully logged in
210+
// Save a persistent flag in the game that the player is logged in
211+
SaveManager.Instance.IsLoggedIn = true;
212+
// Show 'Next' button
213+
ShowNextButton(true);
214+
ShowLoading(false);
215+
// Take the player to the Setup Wallet screen
216+
m_SetupWalletEvent.Raise();
217+
}
218+
catch (Exception ex)
219+
{
220+
Debug.Log($"Failed to log into Passport: {ex.Message}");
221+
// Show Continue with Passport button again
222+
ShowContinueWithPassportButton(true);
223+
ShowLoading(false);
224+
}
225+
}
226+
227+
private async void OnTryAgainButtonClicked()
228+
{
229+
await MintCoins();
128230
}
129231

130232
private void OnNextButtonClicked()
131233
{
132-
m_NextLevelEvent.Raise();
234+
// Check if the player is already using a new skin
235+
if (!SaveManager.Instance.UseNewSkin)
236+
{
237+
// Player is not using a new skin, take player to Unlocked Skin screen
238+
m_UnlockedSkinEvent.Raise();
239+
}
240+
else
241+
{
242+
// Player is already using a new skin, take player to the next level
243+
m_NextLevelEvent.Raise();
244+
}
133245
}
134246

135247
private void ShowCompletedContainer(bool show)
@@ -171,4 +283,4 @@ void DisplayCoins(int count)
171283
}
172284
}
173285
}
174-
}
286+
}

Assets/Shared/Scripts/UI/MainMenu.cs

-8
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ async void OnEnable()
4343
string redirectUri = null;
4444
string logoutUri = null;
4545

46-
#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX
47-
redirectUri = "immutablerunner://callback";
48-
logoutUri = "immutablerunner://logout";
49-
#endif
5046
passport = await Passport.Init(clientId, environment, redirectUri, logoutUri);
5147

5248
// Check if the player is supposed to be logged in and if there are credentials saved
@@ -95,11 +91,7 @@ async void OnLogoutButtonClick()
9591
ShowLoading(true);
9692

9793
// Logout
98-
#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX
99-
await passport.LogoutPKCE();
100-
#else
10194
await passport.Logout();
102-
#endif
10395

10496
// Reset the login flag
10597
SaveManager.Instance.IsLoggedIn = false;

0 commit comments

Comments
 (0)