forked from Sweaty-Chair/SC-Redeem-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedeemCodeManager.cs
142 lines (113 loc) · 3.78 KB
/
RedeemCodeManager.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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using SweatyChair;
using SweatyChair.UI;
/// <summary>
/// A manage to validate the redeem code in game, used for compensating players or rewarding them in events.
/// </summary>
public static class RedeemCodeManager
{
// The character count in a redeem code, this should be matched with server
private const int NUM_CHAR_REDEEM_CODE = 12;
private const string PREFS_USED_CODES = "RedeemCodeUsedCodes";
private static string _enteredCode;
private static float _lastCheckShowTime;
private static int _checkShownCount;
public static void CheckShow()
{
if (Time.unscaledTime < _lastCheckShowTime + 1)
_checkShownCount++;
else
_checkShownCount = 1;
_lastCheckShowTime = Time.unscaledTime;
if (_checkShownCount >= 3) {
Show();
_checkShownCount = 0;
}
}
public static void Show()
{
#if UNITY_IOS || UNITY_ANDROID || UNITY_WEBGL
new Message {
format = MessageFormat.Input,
title = LocalizeUtils.Get("Redeem"),
content = LocalizeUtils.Get("Code:"),
inputPlaceholderString = LocalizeUtils.Get("Enter Your Redeem Code"),
inputConfirmCallback = OnRedeemCodeConfirm,
inputValidationData = new InputFieldValidationData(NUM_CHAR_REDEEM_CODE, InputField.CharacterValidation.Alphanumeric)
}.Show();
#endif
}
#if UNITY_IOS || UNITY_ANDROID || UNITY_WEBGL
public static void OnRedeemCodeConfirm(string code)
{
#if UNITY_IOS || UNITY_ANDROID
Handheld.StartActivityIndicator();
#endif
_enteredCode = code.ToUpper();
ServerManager.Get("redeem/" + code, OnRedeemCodeSucceed, OnRedeemCodeReturnFailed);
}
#if UNITY_IOS || UNITY_ANDROID
private static void OnRedeemCodeSucceed(Hashtable ht)
{
Handheld.StopActivityIndicator();
ArrayList rewardsAl = ht["rewards"] as ArrayList;
if (rewardsAl.Count <= 0) { // Find redeem code but no reward? somthing wrong in backend
new Message {
title = LocalizeUtils.Get("Redeem Failed"),
content = LocalizeUtils.Get("No redeem code found."),
}.Show();
return;
}
if (ht.ContainsKey("reusable") && (ht["reusable"] as string) == "1") { // If code is reusable, use local PlayerPrefs to check if the code is used before
List<string> usedCodes = new List<string>(PlayerPrefsX.GetStringArray(PREFS_USED_CODES));
if (usedCodes.Contains(_enteredCode)) {
new Message {
title = LocalizeUtils.Get("Redeem Failed"),
content = LocalizeUtils.Get("Redeem code has already redeemed."),
}.Show();
return;
} else {
usedCodes.Add(_enteredCode);
PlayerPrefsX.SetStringArray(PREFS_USED_CODES, usedCodes.ToArray());
}
}
//List<Item> items = new List<Item>();
foreach (var rewardObj in rewardsAl) {
Hashtable rewardHt = rewardObj as Hashtable;
DebugUtils.Log(rewardHt);
ItemType type = (ItemType)DataUtils.GetInt(rewardHt["type"] as string);
int amount = DataUtils.GetInt(rewardHt["amount"] as string);
int id = DataUtils.GetInt(rewardHt["id"] as string);
// TODO 201011: Backward compatiable NH only: convert money to coin
if (id == 0) id = 1;
SerializableItem item = new SerializableItem(id, type, amount);
item.Obtain();
}
// Analytics
var dict = new Dictionary<string, object> {
{"code", _enteredCode },
};
UnityEngine.Analytics.AnalyticsEvent.Custom("redeem_code", dict);
//new Reward(items).Claim(LocalizeUtils.Get("Redeem Succeed"));
}
private static void OnRedeemCodeReturnFailed(string e)
{
new Message {
title = LocalizeUtils.Get("Redeem Failed"),
content = LocalizeUtils.Get(e),
}.Show();
Handheld.StopActivityIndicator();
}
#endif
#if UNITY_EDITOR
[UnityEditor.MenuItem("Debug/Redeem Codes/Reset All Used Redeem Codes")]
private static void ResetAllUsedRedeemCodes()
{
PlayerPrefs.DeleteKey(PREFS_USED_CODES);
}
#endif
#endif
}