This repository was archived by the owner on Feb 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMintScreen.cs
More file actions
213 lines (183 loc) · 6.55 KB
/
MintScreen.cs
File metadata and controls
213 lines (183 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using HyperCasual.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using Cysharp.Threading.Tasks;
using Immutable.Passport;
using Immutable.Passport.Model;
[Serializable]
public class MintResult
{
public string token_id;
public string contract_address;
public string tx_id;
}
namespace HyperCasual.Runner
{
/// <summary>
/// This View contains celebration screen functionalities
/// </summary>
public class MintScreen : View
{
[SerializeField]
TextMeshProUGUI m_Title;
[SerializeField]
GameObject m_Loading;
[SerializeField]
TextMeshProUGUI m_CheckoutWalletMessage;
[SerializeField]
TextMeshProUGUI m_WalletText;
[SerializeField]
HyperCasualButton m_NextButton;
[SerializeField]
TextMeshProUGUI m_ErrorMessage;
[SerializeField]
HyperCasualButton m_TryAgainButton;
[SerializeField]
AbstractGameEvent m_NextEvent;
[SerializeField]
HyperCasualButton m_WalletButton;
// If there's an error minting, these values will be used when the player clicks the "Try again" button
private bool mintedFox = false;
public void OnEnable()
{
// Set listener to 'Next' button
m_NextButton.RemoveListener(OnNextButtonClicked);
m_NextButton.AddListener(OnNextButtonClicked);
// Set listener to "Try again" button
m_TryAgainButton.RemoveListener(Mint);
m_TryAgainButton.AddListener(Mint);
// Set listener to 'Wallet' button
m_WalletButton.RemoveListener(OnWalletClicked);
m_WalletButton.AddListener(OnWalletClicked);
// Reset values
mintedFox = false;
Mint();
}
private async void Mint()
{
try
{
ShowMintingMessage();
ShowLoading(true);
ShowError(false);
ShowNextButton(false);
// Mint fox if not minted yet
if (!mintedFox)
{
MintResult mintResult = await MintFox();
// Show minted message if minted fox successfully
ShowMintedMessage();
}
}
catch (Exception ex)
{
// Failed to mint, let the player try again
Debug.Log($"Failed to mint or transfer: {ex.Message}");
}
ShowLoading(false);
// Show error if failed to mint fox
ShowError(!mintedFox);
// Show next button if fox minted successfully
ShowNextButton(mintedFox);
}
/// <summary>
/// Gets the wallet address of the player.
/// </summary>
private async UniTask<string> GetWalletAddress()
{
string address = await Passport.Instance.GetAddress();
return address;
}
/// <summary>
/// Mints a fox (i.e. Immutable Runner Fox) to the player's wallet
/// </summary>
/// <returns>True if minted a fox successfully to player's wallet. Otherwise, false.</returns>
private async UniTask<MintResult> MintFox()
{
Debug.Log("Minting fox...");
try
{
string address = await GetWalletAddress(); // Get the player's wallet address to mint the fox to
if (address != null)
{
var nvc = new List<KeyValuePair<string, string>>
{
// Set 'to' to the player's wallet address
new KeyValuePair<string, string>("to", address)
};
using var client = new HttpClient();
string url = $"http://localhost:3000/mint/fox"; // Endpoint to mint fox
using var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
using var res = await client.SendAsync(req);
// Parse JSON and extract token_id
string content = await res.Content.ReadAsStringAsync();
Debug.Log($"Mint fox response: {content}");
MintResult mintResult = JsonUtility.FromJson<MintResult>(content);
Debug.Log($"Minted fox with token_id: {mintResult.token_id}");
mintedFox = res.IsSuccessStatusCode;
return mintResult;
}
mintedFox = false;
return null;
}
catch (Exception ex)
{
Debug.Log($"Failed to mint fox: {ex.Message}");
mintedFox = false;
return null;
}
}
private void OnNextButtonClicked()
{
m_NextEvent.Raise();
}
private void ShowNextButton(bool show)
{
m_NextButton.gameObject.SetActive(show);
}
private void ShowLoading(bool show)
{
m_Loading.gameObject.SetActive(show);
}
private void ShowError(bool show)
{
m_ErrorMessage.gameObject.SetActive(show);
m_TryAgainButton.gameObject.SetActive(show);
}
private void ShowCheckoutWallet(bool show)
{
m_CheckoutWalletMessage.gameObject.SetActive(show);
m_WalletText.gameObject.SetActive(show);
}
private void ShowMintingMessage()
{
ShowCheckoutWallet(false);
m_Title.text = "Let's mint a fox to your wallet!";
}
/// <summary>
/// Get the number of coins the player collected from the Level Complete Screen
/// </summary>
private int GetNumCoinsCollected()
{
LevelCompleteScreen levelCompleteScreen = UIManager.Instance.GetView<LevelCompleteScreen>();
return levelCompleteScreen.CoinCount;
}
private void ShowMintedMessage()
{
ShowCheckoutWallet(true);
m_Title.text = "You now own a fox!";
}
private async void OnWalletClicked()
{
// Get the player's wallet address to mint the fox to
string address = await GetWalletAddress();
// Show the player's tokens on the block explorer page.
Application.OpenURL($"https://sandbox.immutascan.io/address/{address}?tab=1");
}
}
}