ShopScreenView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. [DisallowMultipleComponent]
  8. public class ShopScreenView : MonoBehaviour
  9. {
  10. public event Action CloseRequested;
  11. public event Action<string> MessageRequested;
  12. readonly List<GameObject> ledgerRows = new List<GameObject>();
  13. readonly ItemRow[] itemRows = new ItemRow[GameDefinitions.PowerUps.Length];
  14. TMP_Text coinBalance;
  15. Transform itemsPanel;
  16. Transform ledgerPanel;
  17. Transform itemsSelected;
  18. Transform itemsNormal;
  19. Transform ledgerSelected;
  20. Transform ledgerNormal;
  21. Button itemsTab;
  22. Button ledgerTab;
  23. TMP_Text earnedValue;
  24. TMP_Text spentValue;
  25. TMP_Text balanceValue;
  26. Transform ledgerContent;
  27. Transform ledgerTemplate;
  28. ScrollRect ledgerScroll;
  29. bool wired;
  30. bool showingLedger;
  31. sealed class ItemRow
  32. {
  33. public PowerUpDefinition definition;
  34. public Transform root;
  35. public Button buyButton;
  36. public TMP_Text countText;
  37. public TMP_Text priceText;
  38. }
  39. void Awake()
  40. {
  41. CacheReferences();
  42. WireButtons();
  43. }
  44. void OnEnable()
  45. {
  46. SaveService.Changed += HandleSaveChanged;
  47. LocaleService.Changed += HandleSaveChanged;
  48. Refresh();
  49. }
  50. void OnDisable()
  51. {
  52. SaveService.Changed -= HandleSaveChanged;
  53. LocaleService.Changed -= HandleSaveChanged;
  54. }
  55. public void Show(bool showLedgerTab = false)
  56. {
  57. gameObject.SetActive(true);
  58. SetTab(showLedgerTab);
  59. }
  60. public void Hide()
  61. {
  62. gameObject.SetActive(false);
  63. }
  64. void HandleSaveChanged()
  65. {
  66. if (isActiveAndEnabled)
  67. Refresh();
  68. }
  69. void CacheReferences()
  70. {
  71. coinBalance = UIViewUtility.Text(transform, "IMG_BG/IMG_CoinPanel/TMP_CoinBalance");
  72. itemsPanel = UIViewUtility.Find(transform, "IMG_BG/GRP_ItemsPanel");
  73. ledgerPanel = UIViewUtility.Find(transform, "IMG_BG/GRP_LedgerPanel");
  74. itemsTab = UIViewUtility.Button(transform, "IMG_BG/BTN_ItemsTab");
  75. ledgerTab = UIViewUtility.Button(transform, "IMG_BG/BTN_LedgerTab");
  76. itemsSelected = UIViewUtility.Find(transform, "IMG_BG/BTN_ItemsTab/IMG_Select");
  77. itemsNormal = UIViewUtility.Find(transform, "IMG_BG/BTN_ItemsTab/IMG_Normal");
  78. ledgerSelected = UIViewUtility.Find(transform, "IMG_BG/BTN_LedgerTab/IMG_Select");
  79. ledgerNormal = UIViewUtility.Find(transform, "IMG_BG/BTN_LedgerTab/IMG_Normal");
  80. earnedValue = UIViewUtility.Text(transform, "IMG_BG/GRP_LedgerPanel/GRP_LedgerSummary/GRP_EarnedStat/TMP_EarnedValue");
  81. spentValue = UIViewUtility.Text(transform, "IMG_BG/GRP_LedgerPanel/GRP_LedgerSummary/GRP_SpentStat/TMP_SpentValue");
  82. balanceValue = UIViewUtility.Text(transform, "IMG_BG/GRP_LedgerPanel/GRP_LedgerSummary/GRP_BalanceStat/TMP_BalanceValue");
  83. ledgerScroll = UIViewUtility.Get<ScrollRect>(transform, "IMG_BG/GRP_LedgerPanel/SCR_ActivityList");
  84. ledgerContent = UIViewUtility.Find(transform, "IMG_BG/GRP_LedgerPanel/SCR_ActivityList/Viewport/Content");
  85. ledgerTemplate = UIViewUtility.Find(transform, "IMG_BG/GRP_LedgerPanel/SCR_ActivityList/Viewport/Content/GRP_Temp");
  86. string[] paths =
  87. {
  88. "IMG_BG/GRP_ItemsPanel/GRP_Items/ITEM_Hammer",
  89. "IMG_BG/GRP_ItemsPanel/GRP_Items/ITEM_ExtraMoves",
  90. "IMG_BG/GRP_ItemsPanel/GRP_Items/ITEM_Shuffle",
  91. "IMG_BG/GRP_ItemsPanel/GRP_Items/ITEM_MasterKey"
  92. };
  93. for (int i = 0; i < itemRows.Length; i++)
  94. {
  95. Transform root = UIViewUtility.Find(transform, paths[i]);
  96. if (root == null)
  97. continue;
  98. itemRows[i] = new ItemRow
  99. {
  100. definition = GameDefinitions.PowerUps[i],
  101. root = root,
  102. buyButton = UIViewUtility.Button(root, "BTN_Buy"),
  103. countText = UIViewUtility.Text(root, "TMP_ItemCount"),
  104. priceText = UIViewUtility.Text(root, "BTN_Buy/TMP_PriceLabel")
  105. };
  106. }
  107. }
  108. void WireButtons()
  109. {
  110. if (wired)
  111. return;
  112. UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause"), () => CloseRequested?.Invoke());
  113. UIViewUtility.Bind(itemsTab, () => SetTab(false));
  114. UIViewUtility.Bind(ledgerTab, () => SetTab(true));
  115. for (int i = 0; i < itemRows.Length; i++)
  116. {
  117. ItemRow row = itemRows[i];
  118. if (row == null)
  119. continue;
  120. string capturedKey = row.definition.key;
  121. UIViewUtility.Bind(row.buyButton, () => Buy(capturedKey));
  122. }
  123. wired = true;
  124. }
  125. void Buy(string itemKey)
  126. {
  127. PowerUpDefinition definition = GameDefinitions.GetPowerUp(itemKey);
  128. if (definition == null)
  129. return;
  130. if (SaveService.TryPurchasePowerUp(itemKey))
  131. {
  132. ActivityFacebookEngagement.ReportShopPurchase(definition.price);
  133. GameAudio.PlaySfx(GameSfx.Buy);
  134. MessageRequested?.Invoke(L.F("toast.item_purchased", GameLocale.PowerUpName(itemKey)));
  135. Refresh();
  136. return;
  137. }
  138. GameAudio.PlaySfx(GameSfx.Error);
  139. int missing = Mathf.Max(0, definition.price - SaveService.Data.coins);
  140. MessageRequested?.Invoke(L.F("toast.need_coins", UIViewUtility.FormatNumber(missing)));
  141. }
  142. public void SetTab(bool showLedgerTab)
  143. {
  144. showingLedger = showLedgerTab;
  145. if (itemsPanel != null)
  146. itemsPanel.gameObject.SetActive(!showLedgerTab);
  147. if (ledgerPanel != null)
  148. ledgerPanel.gameObject.SetActive(showLedgerTab);
  149. if (itemsSelected != null)
  150. itemsSelected.gameObject.SetActive(!showLedgerTab);
  151. if (itemsNormal != null)
  152. itemsNormal.gameObject.SetActive(showLedgerTab);
  153. if (ledgerSelected != null)
  154. ledgerSelected.gameObject.SetActive(showLedgerTab);
  155. if (ledgerNormal != null)
  156. ledgerNormal.gameObject.SetActive(!showLedgerTab);
  157. Refresh();
  158. }
  159. public void Refresh()
  160. {
  161. if (!Application.isPlaying)
  162. return;
  163. UILocaleScreens.ApplyShop(transform);
  164. var save = SaveService.Data;
  165. if (coinBalance != null)
  166. coinBalance.text = UIViewUtility.FormatNumber(save.coins);
  167. for (int i = 0; i < itemRows.Length; i++)
  168. {
  169. ItemRow row = itemRows[i];
  170. if (row == null)
  171. continue;
  172. int owned = save.items.Get(row.definition.key);
  173. bool affordable = save.coins >= row.definition.price;
  174. TMP_Text nameText = UIViewUtility.Text(row.root, "TMP_ItemName");
  175. if (nameText != null)
  176. nameText.text = GameLocale.PowerUpName(row.definition.key);
  177. TMP_Text descText = UIViewUtility.Text(row.root, "TMP_ItemDescription");
  178. if (descText != null)
  179. descText.text = GameLocale.PowerUpDescription(row.definition.key);
  180. if (row.countText != null)
  181. row.countText.text = L.F("shop.owned", owned);
  182. if (row.priceText != null)
  183. {
  184. row.priceText.text = UIViewUtility.FormatNumber(row.definition.price);
  185. // row.priceText.color = new Color32(255, 252, 239, 255);
  186. }
  187. if (row.buyButton != null)
  188. {
  189. row.buyButton.interactable = true;
  190. ColorBlock colors = row.buyButton.colors;
  191. Color tint = affordable ? Color.white : new Color(0.68f, 0.68f, 0.68f, 1f);
  192. colors.normalColor = tint;
  193. colors.highlightedColor = affordable ? new Color(0.96f, 0.96f, 0.96f, 1f) : new Color(0.74f, 0.74f, 0.74f, 1f);
  194. colors.pressedColor = affordable ? new Color(0.86f, 0.86f, 0.86f, 1f) : new Color(0.62f, 0.62f, 0.62f, 1f);
  195. colors.selectedColor = tint;
  196. colors.disabledColor = tint;
  197. // row.buyButton.colors = colors;
  198. }
  199. }
  200. if (showingLedger)
  201. RefreshLedger();
  202. }
  203. void RefreshLedger()
  204. {
  205. if (ledgerContent == null || ledgerTemplate == null)
  206. return;
  207. for (int i = 0; i < ledgerRows.Count; i++)
  208. {
  209. if (ledgerRows[i] != null)
  210. Destroy(ledgerRows[i]);
  211. }
  212. ledgerRows.Clear();
  213. var ledger = SaveService.Data.ledger;
  214. int earned = 0;
  215. int spent = 0;
  216. for (int i = 0; i < ledger.Count; i++)
  217. {
  218. if (ledger[i].amount > 0)
  219. earned += ledger[i].amount;
  220. else
  221. spent += ledger[i].amount;
  222. }
  223. if (earnedValue != null)
  224. earnedValue.text = "+" + UIViewUtility.FormatNumber(earned);
  225. if (spentValue != null)
  226. spentValue.text = spent == 0 ? "0" : "-" + UIViewUtility.FormatNumber(-spent);
  227. if (balanceValue != null)
  228. balanceValue.text = UIViewUtility.FormatNumber(SaveService.Data.coins);
  229. int count = Mathf.Min(60, ledger.Count);
  230. if (count == 0)
  231. CreateLedgerRow(0, L.Get("ledger.empty"), "", 0, false);
  232. for (int i = 0; i < count; i++)
  233. {
  234. LedgerEntry entry = ledger[i];
  235. CreateLedgerRow(i, FormatLedgerNote(entry.note), entry.time, entry.amount, entry.amount > 0);
  236. }
  237. if (ledgerScroll != null)
  238. StartCoroutine(ResetScrollNextFrame());
  239. }
  240. static string FormatLedgerNote(string note)
  241. {
  242. if (string.IsNullOrEmpty(note))
  243. return note;
  244. string[] parts = note.Split('|');
  245. switch (parts[0])
  246. {
  247. case "ledger.game_clear":
  248. if (parts.Length >= 3 && int.TryParse(parts[2], out int level))
  249. return L.F("ledger.game_clear", GameLocale.ModeName(parts[1]), level);
  250. break;
  251. case "ledger.shop":
  252. if (parts.Length >= 2)
  253. return L.F("ledger.shop", GameLocale.PowerUpName(parts[1]));
  254. break;
  255. case "ledger.ad_revive":
  256. if (parts.Length >= 2 && int.TryParse(parts[1], out int moves))
  257. return L.F("ledger.ad_revive", moves);
  258. break;
  259. case "ledger.checkin":
  260. if (parts.Length >= 2 && int.TryParse(parts[1], out int day))
  261. return L.F("ledger.checkin", day);
  262. break;
  263. case "ledger.lucky_draw":
  264. return L.Get("ledger.lucky_draw");
  265. }
  266. if (note == "LUCKY DRAW")
  267. return L.Get("ledger.lucky_draw");
  268. const string checkInPrefix = "CHECK-IN - DAY ";
  269. if (note.StartsWith(checkInPrefix, StringComparison.Ordinal) &&
  270. int.TryParse(note.Substring(checkInPrefix.Length), out int legacyDay))
  271. return L.F("ledger.checkin", legacyDay);
  272. const string adPrefix = "AD REVIVE +";
  273. const string movesSuffix = " MOVES";
  274. if (note.StartsWith(adPrefix, StringComparison.Ordinal) &&
  275. note.EndsWith(movesSuffix, StringComparison.Ordinal))
  276. {
  277. string number = note.Substring(adPrefix.Length, note.Length - adPrefix.Length - movesSuffix.Length);
  278. if (int.TryParse(number, out int legacyMoves))
  279. return L.F("ledger.ad_revive", legacyMoves);
  280. }
  281. const string shopPrefix = "SHOP - ";
  282. if (note.StartsWith(shopPrefix, StringComparison.Ordinal))
  283. {
  284. string itemKey = LegacyItemKey(note.Substring(shopPrefix.Length));
  285. if (!string.IsNullOrEmpty(itemKey))
  286. return L.F("ledger.shop", GameLocale.PowerUpName(itemKey));
  287. }
  288. const string clearPrefix = "GAME CLEAR - ";
  289. if (note.StartsWith(clearPrefix, StringComparison.Ordinal))
  290. {
  291. string rest = note.Substring(clearPrefix.Length);
  292. int levelIndex = rest.LastIndexOf(" LV ", StringComparison.Ordinal);
  293. if (levelIndex > 0 && int.TryParse(rest.Substring(levelIndex + 4), out int legacyLevel))
  294. {
  295. string modeKey = LegacyModeKey(rest.Substring(0, levelIndex));
  296. if (!string.IsNullOrEmpty(modeKey))
  297. return L.F("ledger.game_clear", GameLocale.ModeName(modeKey), legacyLevel);
  298. }
  299. }
  300. return note;
  301. }
  302. static string LegacyItemKey(string itemName)
  303. {
  304. for (int i = 0; i < GameDefinitions.PowerUps.Length; i++)
  305. {
  306. if (GameDefinitions.PowerUps[i].name == itemName)
  307. return GameDefinitions.PowerUps[i].key;
  308. }
  309. return null;
  310. }
  311. static string LegacyModeKey(string modeName)
  312. {
  313. for (int i = 0; i < GameDefinitions.Modes.Length; i++)
  314. {
  315. if (GameDefinitions.Modes[i].name == modeName)
  316. return GameDefinitions.Modes[i].key;
  317. }
  318. return null;
  319. }
  320. void CreateLedgerRow(int index, string title, string time, int amount, bool positive)
  321. {
  322. GameObject row = Instantiate(ledgerTemplate.gameObject, ledgerContent);
  323. row.name = "LedgerRow_" + index;
  324. row.SetActive(true);
  325. ledgerRows.Add(row);
  326. Transform root = row.transform;
  327. UIViewUtility.SetGraphicVisible(root, "IMG_Bg1", index % 2 == 0);
  328. UIViewUtility.SetGraphicVisible(root, "IMG_Bg2", index % 2 != 0);
  329. TMP_Text titleText = UIViewUtility.Text(root, "TMP_ActivityTitle");
  330. if (titleText != null)
  331. titleText.text = title;
  332. TMP_Text timeText = UIViewUtility.Text(root, "TMP_ActivityTime");
  333. if (timeText != null)
  334. timeText.text = time;
  335. TMP_Text amountText = UIViewUtility.Text(root, "TMP_ActivityAmount");
  336. if (amountText != null)
  337. {
  338. amountText.text = amount == 0 ? "" : (positive ? "+" : "") + UIViewUtility.FormatNumber(amount);
  339. amountText.color = positive
  340. ? new Color32(87, 224, 138, 255)
  341. : new Color32(255, 107, 107, 255);
  342. }
  343. }
  344. IEnumerator ResetScrollNextFrame()
  345. {
  346. yield return null;
  347. if (ledgerScroll != null)
  348. ledgerScroll.verticalNormalizedPosition = 1f;
  349. }
  350. }