Skip to content

Commit

Permalink
fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihirota committed Oct 20, 2024
1 parent 1d9c501 commit 4aba971
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 15 deletions.
126 changes: 119 additions & 7 deletions Assets/Shared/Scripts/UI/LevelCompleteScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using Immutable.Passport;
using Cysharp.Threading.Tasks;
using System.Numerics;
using System.Net.Http;

namespace HyperCasual.Runner
{
Expand Down Expand Up @@ -102,34 +106,142 @@ public int CoinCount
}
}

public void OnEnable()
public async void OnEnable()
{
// Set listener to 'Next' button
m_NextButton.RemoveListener(OnNextButtonClicked);
m_NextButton.AddListener(OnNextButtonClicked);

// Set listener to "Continue with Passport" button
// Set listener to "Continue with Passport" button
m_ContinuePassportButton.RemoveListener(OnContinueWithPassportButtonClicked);
m_ContinuePassportButton.AddListener(OnContinueWithPassportButtonClicked);

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

ShowNextButton(true);
ShowError(false);
ShowLoading(false);

// If player is logged into Passport mint coins to player
if (SaveManager.Instance.IsLoggedIn)
{
// Mint collected coins to player
await MintCoins();
}
else
{
// Show 'Next' button if player is already logged into Passport
ShowNextButton(SaveManager.Instance.IsLoggedIn);
// Show "Continue with Passport" button if the player is not logged into Passport
ShowContinueWithPassportButton(!SaveManager.Instance.IsLoggedIn);
}
}

private void OnContinueWithPassportButtonClicked()
/// <summary>
/// Mints collected coins (i.e. Immutable Runner Token) to the player's wallet
/// </summary>
private async UniTask MintCoins()
{
// This function is similar to MintCoins() in MintScreen.cs. Consider refactoring duplicate code in production.
Debug.Log("Minting coins...");
bool success = false;

// Show loading
ShowLoading(true);
ShowNextButton(false);
ShowError(false);

try
{
// Don't mint any coins if player did not collect any
if (m_CoinCount == 0)
{
success = true;
}
else
{
// Get the player's wallet address to mint the coins to
List<string> accounts = await Passport.Instance.ZkEvmRequestAccounts();
string address = accounts[0];
if (address != null)
{
// Calculate the quantity to mint
// Need to take into account Immutable Runner Token decimal value i.e. 18
BigInteger quantity = BigInteger.Multiply(new BigInteger(m_CoinCount), BigInteger.Pow(10, 18));
Debug.Log($"Quantity: {quantity}");
var nvc = new List<KeyValuePair<string, string>>
{
// Set 'to' to the player's wallet address
new KeyValuePair<string, string>("to", address),
// Set 'quanity' to the number of coins collected
new KeyValuePair<string, string>("quantity", quantity.ToString())
};
using var client = new HttpClient();
string url = $"http://localhost:3000/mint/token"; // Endpoint to mint token
using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
using var res = await client.SendAsync(req);
success = res.IsSuccessStatusCode;
}
}
}
catch (Exception ex)
{
Debug.Log($"Failed to mint coins: {ex.Message}");
}

ShowLoading(false);
ShowNextButton(success);
ShowError(!success);
}

private void OnTryAgainButtonClicked()
private async void OnContinueWithPassportButtonClicked()
{
try
{
// Show loading
ShowContinueWithPassportButton(false);
ShowLoading(true);

// Log into Passport
await Passport.Instance.Login();

// Successfully logged in
// Save a persistent flag in the game that the player is logged in
SaveManager.Instance.IsLoggedIn = true;
// Show 'Next' button
ShowNextButton(true);
ShowLoading(false);
// Take the player to the Setup Wallet screen
m_SetupWalletEvent.Raise();
}
catch (Exception ex)
{
Debug.Log($"Failed to log into Passport: {ex.Message}");
// Show Continue with Passport button again
ShowContinueWithPassportButton(true);
ShowLoading(false);
}
}

private async void OnTryAgainButtonClicked()
{
await MintCoins();
}

private void OnNextButtonClicked()
{
m_NextLevelEvent.Raise();
// Check if the player is already using a new skin
if (!SaveManager.Instance.UseNewSkin)
{
// Player is not using a new skin, take player to Unlocked Skin screen
m_UnlockedSkinEvent.Raise();
}
else
{
// Player is already using a new skin, take player to the next level
m_NextLevelEvent.Raise();
}
}

private void ShowCompletedContainer(bool show)
Expand Down Expand Up @@ -171,4 +283,4 @@ void DisplayCoins(int count)
}
}
}
}
}
8 changes: 0 additions & 8 deletions Assets/Shared/Scripts/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ async void OnEnable()
string redirectUri = null;
string logoutUri = null;

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

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

// Logout
#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX
await passport.LogoutPKCE();
#else
await passport.Logout();
#endif

// Reset the login flag
SaveManager.Instance.IsLoggedIn = false;
Expand Down

0 comments on commit 4aba971

Please sign in to comment.