-
Notifications
You must be signed in to change notification settings - Fork 2
/
LevelCompleteScreen.cs
615 lines (556 loc) · 21.3 KB
/
LevelCompleteScreen.cs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
using System.ComponentModel;
using HyperCasual.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Immutable.Passport;
using Immutable.Passport.Model;
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace HyperCasual.Runner
{
/// <summary>
/// This View contains celebration screen functionalities
/// </summary>
public class LevelCompleteScreen : View
{
[SerializeField]
HyperCasualButton m_NextButton;
[SerializeField]
HyperCasualButton m_ContinuePassportButton;
[SerializeField]
Image[] m_Stars;
[SerializeField]
AbstractGameEvent m_NextLevelEvent;
[SerializeField]
TextMeshProUGUI m_GoldText;
[SerializeField]
Slider m_XpSlider;
[SerializeField]
GameObject m_Loading;
[SerializeField]
GameObject m_CompletedContainer;
[SerializeField]
GameObject m_MintedContainer;
[SerializeField]
GameObject m_SkinUnlockedContainer;
[SerializeField]
GameObject m_BonusSkinContainer;
// Minted Fox
[SerializeField]
TextMeshProUGUI m_MintedTitle;
[SerializeField]
HyperCasualButton m_MintedNextButton;
// Unlocked Skin
[SerializeField]
TextMeshProUGUI m_SkinUnlockedErrorMessage;
[SerializeField]
HyperCasualButton m_SkinUnlockedGetButton;
[SerializeField]
HyperCasualButton m_SkinUnlockedNextButton;
[SerializeField]
GameObject m_SkinUnlockedLoading;
// Bonus
[SerializeField]
HyperCasualButton m_BonusSkinGetButton;
[SerializeField]
HyperCasualButton m_BonusSkinNextButton;
[SerializeField]
GameObject m_BonusSkinLoading;
[SerializeField]
TextMeshProUGUI m_BonusSkinMessage;
/// <summary>
/// The slider that displays the XP value
/// </summary>
public Slider XpSlider => m_XpSlider;
int m_GoldValue;
private ConnectResponse? ConnectResponse;
private ApiService Api = new();
private bool MintingTokens = false;
private string? Address;
/// <summary>
/// The amount of gold to display on the celebration screen.
/// The setter method also sets the celebration screen text.
/// </summary>
public int GoldValue
{
get => m_GoldValue;
set
{
if (m_GoldValue != value)
{
m_GoldValue = value;
m_GoldText.text = GoldValue.ToString();
}
}
}
float m_XpValue;
/// <summary>
/// The amount of XP to display on the celebration screen.
/// The setter method also sets the celebration screen slider value.
/// </summary>
public float XpValue
{
get => m_XpValue;
set
{
if (!Mathf.Approximately(m_XpValue, value))
{
m_XpValue = value;
m_XpSlider.value = m_XpValue;
}
}
}
int m_StarCount = -1;
/// <summary>
/// The number of stars to display on the celebration screen.
/// </summary>
public int StarCount
{
get => m_StarCount;
set
{
if (m_StarCount != value)
{
m_StarCount = value;
DisplayStars(m_StarCount);
}
}
}
public async void OnEnable()
{
var connected = await Passport.Instance.HasCredentialsSaved();
HideLoading();
ShowContinueWithPassportButton(!connected);
ShowNextButton(connected);
ShowCompletedContainer(true);
ShowMintedContainer(false);
ShowSkinUnlockedContainer(false);
ShowBonusSkinContainer(false);
// Continue with Passport button
m_ContinuePassportButton.RemoveListener(OnContinueButtonClicked);
m_ContinuePassportButton.AddListener(OnContinueButtonClicked);
// Burn to get new skin button
m_SkinUnlockedGetButton.RemoveListener(OnBurnTokens);
m_SkinUnlockedGetButton.AddListener(OnBurnTokens);
// Get bonus skin button
m_BonusSkinGetButton.RemoveListener(BurnSkinForCoolerSkin);
m_BonusSkinGetButton.AddListener(BurnSkinForCoolerSkin);
// Next level button on Completed screen
m_NextButton.RemoveListener(OnNextButtonClicked);
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
// Show new skin when level 2 completes
// And user is not already using it
if (MemoryCache.CurrentLevel == 2 && !MemoryCache.UseNewSkin)
{
m_NextButton.AddListener(OnNextButtonClickedShowNewSkin);
}
// Show bonus skin when level 3 completes
// And user already uses the new skin
// And user is not using cooler skin
else if (MemoryCache.CurrentLevel == 3 && MemoryCache.UseNewSkin && !MemoryCache.UseCoolerSkin)
{
m_NextButton.AddListener(OnNextButtonClickedShowBonusSkin);
}
else
{
m_NextButton.AddListener(OnNextButtonClicked);
}
#else
m_NextButton.AddListener(OnNextButtonClicked);
#endif
// Next level button on Minted Fox (and Tokens) screen
m_MintedNextButton.RemoveListener(OnNextButtonClicked);
m_MintedNextButton.AddListener(OnNextButtonClicked);
// Next level button on New Skin Unlocked screen
m_SkinUnlockedNextButton.RemoveListener(OnNextButtonClicked);
m_SkinUnlockedNextButton.AddListener(OnNextButtonClicked);
// Next/Pass button on bonus skin screen
m_BonusSkinNextButton.RemoveListener(OnNextButtonClicked);
m_BonusSkinNextButton.AddListener(OnNextButtonClicked);
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
// If the user is connected to Passport, try and mint the tokens in the background
// if minting did not complete in time, we just ignore any errors
if (connected)
{
MintTokens();
}
#endif
}
void OnNextButtonClicked()
{
Debug.Log("On Next Button Clicked");
// Reset screens
ShowCompletedContainer(true);
ShowMintedContainer(false);
ShowSkinUnlockedContainer(false);
ShowBonusSkinContainer(false);
m_NextLevelEvent.Raise();
}
async void OnContinueButtonClicked()
{
try
{
ShowContinueWithPassportButton(false);
ShowLoading();
#if UNITY_ANDROID || UNITY_IPHONE || (UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX)
await Passport.Instance.ConnectPKCE();
#else
await Passport.Instance.Connect();
#endif
// If in zkEVM mode, connect to EVM
if (SaveManager.Instance.ZkEvm)
{
await Passport.Instance.ConnectEvm();
// Get address and save it
List<string> accounts = await Passport.Instance.ZkEvmRequestAccounts();
Address = accounts[0];
}
else
{
// Get address and save it
Address = await Passport.Instance.GetAddress();
}
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
// Successfully connected
MintFoxAndTokens();
#else
ShowContinueWithPassportButton(false);
HideLoading();
ShowNextButton(true);
#endif
} catch (Exception ex)
{
Debug.Log($"Failed to connect: {ex.Message}");
ShowContinueWithPassportButton(false);
HideLoading();
ShowNextButton(true);
}
}
void OnNextButtonClickedShowNewSkin()
{
Debug.Log("On Next Button Clicked Show New Skin");
ShowCompletedContainer(false);
ShowMintedContainer(false);
ShowSkinUnlockedContainer(true);
ShowBonusSkinContainer(false);
}
void OnNextButtonClickedShowBonusSkin()
{
Debug.Log("On Next Button Clicked Show Bonus Skin");
ShowCompletedContainer(false);
ShowMintedContainer(false);
ShowSkinUnlockedContainer(false);
ShowBonusSkinContainer(true);
}
void ShowCompletedContainer(bool show)
{
m_CompletedContainer.gameObject.SetActive(show);
}
void ShowMintedContainer(bool show)
{
m_MintedContainer.gameObject.SetActive(show);
}
void ShowSkinUnlockedContainer(bool show)
{
m_SkinUnlockedContainer.gameObject.SetActive(show);
if (show)
{
// Reset all other views
m_SkinUnlockedLoading.gameObject.SetActive(false);
m_SkinUnlockedErrorMessage.gameObject.SetActive(false);
m_SkinUnlockedGetButton.gameObject.SetActive(true);
}
}
void ShowBonusSkinContainer(bool show)
{
m_BonusSkinContainer.gameObject.SetActive(show);
if (show)
{
// Reset all other views
m_BonusSkinLoading.gameObject.SetActive(false);
ShowBonusSkinMessage("Burn your current skin\nfor a cooler skin!");
m_BonusSkinGetButton.gameObject.SetActive(true);
}
}
void ShowContinueWithPassportButton(bool show)
{
m_ContinuePassportButton.gameObject.SetActive(show);
}
void ShowNextButton(bool show)
{
m_NextButton.gameObject.SetActive(show);
}
void ShowLoading()
{
m_Loading.gameObject.SetActive(true);
}
void HideLoading()
{
m_Loading.gameObject.SetActive(false);
}
public void ConfirmCode()
{
if (ConnectResponse != null)
{
Application.OpenURL(ConnectResponse.Url);
}
}
void DisplayStars(int count)
{
count = Mathf.Clamp(count, 0, m_Stars.Length);
if (m_Stars.Length > 0 && count >= 0 && count <= m_Stars.Length)
{
for (int i = 0; i < m_Stars.Length; i++)
{
m_Stars[i].gameObject.SetActive(i < count);
}
}
}
private async UniTask GetWalletAddress()
{
if (Address == null)
{
if (SaveManager.Instance.ZkEvm)
{
List<string> accounts = await Passport.Instance.ZkEvmRequestAccounts();
Address = accounts[0];
}
else
{
Address = await Passport.Instance.GetAddress();
}
}
}
public void OnWalletClicked()
{
if (Address != null)
{
if (SaveManager.Instance.ZkEvm)
{
Application.OpenURL($"https://explorer.testnet.immutable.com/address/{Address}");
}
else
{
Application.OpenURL($"http://localhost:6060/wallet?user={Address}");
}
}
}
private async void MintFoxAndTokens()
{
// Mint fox
bool mintedFox = await Api.MintFox(Address);
if (StarCount > 0)
{
// User collected tokens
bool mintedTokens = await Api.MintTokens(StarCount, Address);
if (!mintedFox && !mintedTokens)
{
Debug.Log($"Failed to mint both a fox and tokens");
ShowContinueWithPassportButton(false);
HideLoading();
ShowNextButton(true);
}
else
{
string numTokens = StarCount == 1 ? "a token" : $"{StarCount} tokens";
if (mintedFox && mintedTokens)
{
m_MintedTitle.text = $"You now own a Fox and {numTokens}!";
}
else if (mintedFox && !mintedTokens)
{
m_MintedTitle.text = $"You now own a Fox!";
}
else if (!mintedFox && mintedTokens)
{
m_MintedTitle.text = $"You now own {numTokens}";
}
HideLoading();
ShowCompletedContainer(false);
ShowMintedContainer(true);
}
}
else
{
// User did not collect and tokens
if (mintedFox)
{
m_MintedTitle.text = $"You now own a Fox!";
HideLoading();
ShowCompletedContainer(false);
ShowMintedContainer(true);
}
else
{
Debug.Log($"Failed to mint a fox");
ShowContinueWithPassportButton(false);
HideLoading();
ShowNextButton(true);
}
}
}
private async void MintTokens()
{
if (!MintingTokens)
{
MintingTokens = true;
try
{
await GetWalletAddress();
if (StarCount > 0)
{
bool mintedTokens = await Api.MintTokens(StarCount, Address);
Debug.Log(mintedTokens ? "Minted tokens" : $"Failed to mint tokens");
}
}
catch (Exception ex)
{
Debug.Log($"Error minting tokens: {ex.Message}");
}
MintingTokens = false;
}
}
private async void OnBurnTokens()
{
Debug.Log("On Burn Tokens...");
m_SkinUnlockedLoading.gameObject.SetActive(true);
m_SkinUnlockedGetButton.gameObject.SetActive(false);
await GetWalletAddress();
List<TokenModel> tokens = await Api.GetTokens(3, Address);
if (tokens.Count >= 3)
{
try
{
if (SaveManager.Instance.ZkEvm)
{
long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Debug.Log($"Start OnBurnTokens zkEVMSendTransaction {DateTimeOffset.Now.ToUnixTimeMilliseconds() - now}");
string encodedData = await Api.GetTokenCraftSkinEcodedData(tokens[0].token_id, tokens[1].token_id, tokens[2].token_id);
string transactionHash = await Passport.Instance.ZkEvmSendTransaction(new TransactionRequest() {
To = ApiService.ZK_TOKEN_TOKEN_ADDRESS,
Data = encodedData,
Value = "0"
});
Debug.Log($"End OnBurnTokens zkEVMSendTransaction {DateTimeOffset.Now.ToUnixTimeMilliseconds() - now}");
Debug.Log($"Transaction hash: {transactionHash}");
MemoryCache.UseNewSkin = true;
ShowSkinUnlockedMessage("You now have the skin and can use it!");
}
else
{
// Burn tokens
List<NftTransferDetails> transferDetails = new List<NftTransferDetails>();
tokens.ForEach(delegate (TokenModel token)
{
Debug.Log($"Got token ID: {token.token_id}");
transferDetails.Add(new NftTransferDetails(
"0x0000000000000000000000000000000000000000",
token.token_id,
ApiService.TOKEN_TOKEN_ADDRESS.ToLower()));
});
var response = await Passport.Instance.ImxBatchNftTransfer(transferDetails.ToArray());
// Mint skin
bool mintedSkin = await Api.MintSkin(Address);
if (mintedSkin)
{
MemoryCache.UseNewSkin = true;
ShowSkinUnlockedMessage("You now have the skin and can use it!");
}
else
{
Debug.Log($"Something went wrong while minting skin");
ShowSkinUnlockedMessage("Something went wrong :(");
}
}
}
catch (Exception ex)
{
Debug.Log($"Something went wrong while burning tokens {ex.Message}");
ShowSkinUnlockedMessage("Something went wrong :(");
}
}
else
{
ShowSkinUnlockedMessage("Not enough tokens to burn");
}
}
private void ShowSkinUnlockedMessage(string message)
{
m_SkinUnlockedErrorMessage.text = message;
m_SkinUnlockedErrorMessage.gameObject.SetActive(true);
m_SkinUnlockedGetButton.gameObject.SetActive(false);
m_SkinUnlockedLoading.gameObject.SetActive(false);
}
private async void BurnSkinForCoolerSkin()
{
Debug.Log("On Burn Skin For Cooler Skins...");
m_BonusSkinLoading.gameObject.SetActive(true);
m_BonusSkinGetButton.gameObject.SetActive(false);
await GetWalletAddress();
List<TokenModel> tokens = await Api.GetSkin(Address);
if (tokens.Count > 0)
{
try
{
if (SaveManager.Instance.ZkEvm)
{
long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Debug.Log($"Start BurnSkinForCoolerSkin zkEVMSendTransaction {DateTimeOffset.Now.ToUnixTimeMilliseconds() - now}");
string encodedData = await Api.GetSkinCraftSkinEcodedData(tokens[0].token_id);
string transactionHash = await Passport.Instance.ZkEvmSendTransaction(new TransactionRequest() {
To = ApiService.ZK_SKIN_TOKEN_ADDRESS,
Data = encodedData,
Value = "0"
});
Debug.Log($"End BurnSkinForCoolerSkin zkEVMSendTransaction {DateTimeOffset.Now.ToUnixTimeMilliseconds() - now}");
Debug.Log($"Transaction hash: {transactionHash}");
MemoryCache.UseCoolerSkin = true;
ShowBonusSkinMessage("You now have a cooler skin and can use it!");
}
else
{
// Burn skin
TokenModel skin = tokens[0];
var response = await Passport.Instance.ImxTransfer(
UnsignedTransferRequest.ERC721(
"0x0000000000000000000000000000000000000000",
skin.token_id,
ApiService.SKIN_TOKEN_ADDRESS
)
);
// Mint skin
bool mintedSkin = await Api.MintSkin(Address);
if (mintedSkin)
{
MemoryCache.UseCoolerSkin = true;
ShowBonusSkinMessage("You now have a cooler skin and can use it!");
}
else
{
Debug.Log($"Something went wrong while minting skin");
ShowBonusSkinMessage("Something went wrong :(");
}
}
}
catch (Exception ex)
{
Debug.Log($"Something went wrong while burning tokens {ex.Message}");
ShowBonusSkinMessage("Something went wrong :(");
}
}
else
{
ShowBonusSkinMessage("No skin to burn");
}
}
private void ShowBonusSkinMessage(string message)
{
m_BonusSkinMessage.text = message;
m_BonusSkinMessage.gameObject.SetActive(true);
m_BonusSkinGetButton.gameObject.SetActive(false);
m_BonusSkinLoading.gameObject.SetActive(false);
}
}
}