Skip to content

Commit

Permalink
create immutable X sample game
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihirota committed Oct 14, 2024
1 parent 4e46334 commit f0f8da3
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 348 deletions.
91 changes: 7 additions & 84 deletions Assets/Shared/Scripts/UI/LevelCompleteScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
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 @@ -106,7 +103,7 @@ public int CoinCount
}
}

public async void OnEnable()
public void OnEnable()
{
// Set listener to 'Next' button
m_NextButton.RemoveListener(OnNextButtonClicked);
Expand All @@ -120,79 +117,10 @@ public async void OnEnable()
m_TryAgainButton.RemoveListener(OnTryAgainButtonClicked);
m_TryAgainButton.AddListener(OnTryAgainButtonClicked);

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);
}
}

/// <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);
// 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 async void OnContinueWithPassportButtonClicked()
Expand All @@ -204,11 +132,7 @@ private async void OnContinueWithPassportButtonClicked()
ShowLoading(true);

// Log into Passport
#if (UNITY_ANDROID && !UNITY_EDITOR_WIN) || (UNITY_IPHONE && !UNITY_EDITOR_WIN) || UNITY_STANDALONE_OSX
await Passport.Instance.LoginPKCE();
#else
await Passport.Instance.Login();
#endif

// Successfully logged in
// Save a persistent flag in the game that the player is logged in
Expand All @@ -228,9 +152,8 @@ private async void OnContinueWithPassportButtonClicked()
}
}

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

private void OnNextButtonClicked()
Expand Down Expand Up @@ -287,4 +210,4 @@ void DisplayCoins(int count)
}
}
}
}
}
23 changes: 6 additions & 17 deletions Assets/Shared/Scripts/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ async void OnEnable()
// Initialise Passport
string clientId = "YOUR_IMMUTABLE_CLIENT_ID";
string environment = Immutable.Passport.Model.Environment.SANDBOX;
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);
passport = await Passport.Init(clientId, environment);

// Check if the player is supposed to be logged in and if there are credentials saved
if (SaveManager.Instance.IsLoggedIn && await Passport.Instance.HasCredentialsSaved())
Expand All @@ -56,20 +49,20 @@ async void OnEnable()
bool success = await Passport.Instance.Login(useCachedSession: true);
// Update the login flag
SaveManager.Instance.IsLoggedIn = success;

// Set up wallet if successful
if (success)
{
await Passport.Instance.ConnectEvm();
await Passport.Instance.ZkEvmRequestAccounts();
await Passport.Instance.ConnectImx();
}
}
else
{
} else {
// No saved credentials to re-login the player, reset the login flag
SaveManager.Instance.IsLoggedIn = false;
}

ShowLoading(false);
ShowStartButton(true);

// Show the logout button if the player is logged in
ShowLogoutButton(SaveManager.Instance.IsLoggedIn);
}
Expand All @@ -95,11 +88,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
Loading

0 comments on commit f0f8da3

Please sign in to comment.