Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORE-2329 zkevm migration #15

Draft
wants to merge 4 commits into
base: complete
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Assets/Shared/Scripts/SequenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using HyperCasual.Runner;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
using System.Collections;
using UnityEngine;

namespace HyperCasual.Gameplay
{
Expand Down
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)
}
}
}
}
}
39 changes: 36 additions & 3 deletions Assets/Shared/Scripts/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Immutable.Passport;

namespace HyperCasual.Runner
{
Expand All @@ -23,7 +24,9 @@ public class MainMenu : View
[SerializeField]
GameObject m_Loading;

void OnEnable()
Passport passport;

async void OnEnable()
{
ShowLoading(true);

Expand All @@ -34,8 +37,37 @@ void OnEnable()
m_LogoutButton.RemoveListener(OnLogoutButtonClick);
m_LogoutButton.AddListener(OnLogoutButtonClick);

// Initialise Passport
string clientId = "Jf3dcKYrYUC6MLCMsDsOvKGoirlAzGJR";
string environment = Immutable.Passport.Model.Environment.SANDBOX;
string redirectUri = null;
string logoutUri = null;

passport = await Passport.Init(clientId, environment, redirectUri, logoutUri);

// Check if the player is supposed to be logged in and if there are credentials saved
if (SaveManager.Instance.IsLoggedIn && await Passport.Instance.HasCredentialsSaved())
{
// Try to log in using saved credentials
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();
}
}
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);
}

void OnDisable()
Expand All @@ -49,7 +81,7 @@ void OnStartButtonClick()
AudioManager.Instance.PlayEffect(SoundID.ButtonSound);
}

void OnLogoutButtonClick()
async void OnLogoutButtonClick()
{
try
{
Expand All @@ -59,6 +91,7 @@ void OnLogoutButtonClick()
ShowLoading(true);

// Logout
await passport.Logout();

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