ShopScreenView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. GameAudio.PlaySfx(GameSfx.Buy);
  133. MessageRequested?.Invoke(L.F("toast.item_purchased", GameLocale.PowerUpName(itemKey)));
  134. Refresh();
  135. return;
  136. }
  137. GameAudio.PlaySfx(GameSfx.Error);
  138. int missing = Mathf.Max(0, definition.price - SaveService.Data.coins);
  139. MessageRequested?.Invoke(L.F("toast.need_coins", UIViewUtility.FormatNumber(missing)));
  140. }
  141. public void SetTab(bool showLedgerTab)
  142. {
  143. showingLedger = showLedgerTab;
  144. if (itemsPanel != null)
  145. itemsPanel.gameObject.SetActive(!showLedgerTab);
  146. if (ledgerPanel != null)
  147. ledgerPanel.gameObject.SetActive(showLedgerTab);
  148. if (itemsSelected != null)
  149. itemsSelected.gameObject.SetActive(!showLedgerTab);
  150. if (itemsNormal != null)
  151. itemsNormal.gameObject.SetActive(showLedgerTab);
  152. if (ledgerSelected != null)
  153. ledgerSelected.gameObject.SetActive(showLedgerTab);
  154. if (ledgerNormal != null)
  155. ledgerNormal.gameObject.SetActive(!showLedgerTab);
  156. Refresh();
  157. }
  158. public void Refresh()
  159. {
  160. if (!Application.isPlaying)
  161. return;
  162. UILocaleScreens.ApplyShop(transform);
  163. var save = SaveService.Data;
  164. if (coinBalance != null)
  165. coinBalance.text = UIViewUtility.FormatNumber(save.coins);
  166. for (int i = 0; i < itemRows.Length; i++)
  167. {
  168. ItemRow row = itemRows[i];
  169. if (row == null)
  170. continue;
  171. int owned = save.items.Get(row.definition.key);
  172. bool affordable = save.coins >= row.definition.price;
  173. TMP_Text nameText = UIViewUtility.Text(row.root, "TMP_ItemName");
  174. if (nameText != null)
  175. nameText.text = GameLocale.PowerUpName(row.definition.key);
  176. TMP_Text descText = UIViewUtility.Text(row.root, "TMP_ItemDescription");
  177. if (descText != null)
  178. descText.text = GameLocale.PowerUpDescription(row.definition.key);
  179. if (row.countText != null)
  180. row.countText.text = L.F("shop.owned", owned);
  181. if (row.priceText != null)
  182. {
  183. row.priceText.text = UIViewUtility.FormatNumber(row.definition.price);
  184. // row.priceText.color = new Color32(255, 252, 239, 255);
  185. }
  186. if (row.buyButton != null)
  187. {
  188. row.buyButton.interactable = true;
  189. ColorBlock colors = row.buyButton.colors;
  190. Color tint = affordable ? Color.white : new Color(0.68f, 0.68f, 0.68f, 1f);
  191. colors.normalColor = tint;
  192. colors.highlightedColor = affordable ? new Color(0.96f, 0.96f, 0.96f, 1f) : new Color(0.74f, 0.74f, 0.74f, 1f);
  193. colors.pressedColor = affordable ? new Color(0.86f, 0.86f, 0.86f, 1f) : new Color(0.62f, 0.62f, 0.62f, 1f);
  194. colors.selectedColor = tint;
  195. colors.disabledColor = tint;
  196. // row.buyButton.colors = colors;
  197. }
  198. }
  199. if (showingLedger)
  200. RefreshLedger();
  201. }
  202. void RefreshLedger()
  203. {
  204. if (ledgerContent == null || ledgerTemplate == null)
  205. return;
  206. for (int i = 0; i < ledgerRows.Count; i++)
  207. {
  208. if (ledgerRows[i] != null)
  209. Destroy(ledgerRows[i]);
  210. }
  211. ledgerRows.Clear();
  212. var ledger = SaveService.Data.ledger;
  213. int earned = 0;
  214. int spent = 0;
  215. for (int i = 0; i < ledger.Count; i++)
  216. {
  217. if (ledger[i].amount > 0)
  218. earned += ledger[i].amount;
  219. else
  220. spent += ledger[i].amount;
  221. }
  222. if (earnedValue != null)
  223. earnedValue.text = "+" + UIViewUtility.FormatNumber(earned);
  224. if (spentValue != null)
  225. spentValue.text = spent == 0 ? "0" : "-" + UIViewUtility.FormatNumber(-spent);
  226. if (balanceValue != null)
  227. balanceValue.text = UIViewUtility.FormatNumber(SaveService.Data.coins);
  228. int count = Mathf.Min(60, ledger.Count);
  229. if (count == 0)
  230. CreateLedgerRow(0, L.Get("ledger.empty"), "", 0, false);
  231. for (int i = 0; i < count; i++)
  232. {
  233. LedgerEntry entry = ledger[i];
  234. CreateLedgerRow(i, FormatLedgerNote(entry.note), entry.time, entry.amount, entry.amount > 0);
  235. }
  236. if (ledgerScroll != null)
  237. StartCoroutine(ResetScrollNextFrame());
  238. }
  239. static string FormatLedgerNote(string note)
  240. {
  241. if (string.IsNullOrEmpty(note))
  242. return note;
  243. string[] parts = note.Split('|');
  244. switch (parts[0])
  245. {
  246. case "ledger.game_clear":
  247. if (parts.Length >= 3 && int.TryParse(parts[2], out int level))
  248. return L.F("ledger.game_clear", GameLocale.ModeName(parts[1]), level);
  249. break;
  250. case "ledger.shop":
  251. if (parts.Length >= 2)
  252. return L.F("ledger.shop", GameLocale.PowerUpName(parts[1]));
  253. break;
  254. case "ledger.ad_revive":
  255. if (parts.Length >= 2 && int.TryParse(parts[1], out int moves))
  256. return L.F("ledger.ad_revive", moves);
  257. break;
  258. case "ledger.checkin":
  259. if (parts.Length >= 2 && int.TryParse(parts[1], out int day))
  260. return L.F("ledger.checkin", day);
  261. break;
  262. case "ledger.lucky_draw":
  263. return L.Get("ledger.lucky_draw");
  264. }
  265. if (note == "LUCKY DRAW")
  266. return L.Get("ledger.lucky_draw");
  267. const string checkInPrefix = "CHECK-IN - DAY ";
  268. if (note.StartsWith(checkInPrefix, StringComparison.Ordinal) &&
  269. int.TryParse(note.Substring(checkInPrefix.Length), out int legacyDay))
  270. return L.F("ledger.checkin", legacyDay);
  271. const string adPrefix = "AD REVIVE +";
  272. const string movesSuffix = " MOVES";
  273. if (note.StartsWith(adPrefix, StringComparison.Ordinal) &&
  274. note.EndsWith(movesSuffix, StringComparison.Ordinal))
  275. {
  276. string number = note.Substring(adPrefix.Length, note.Length - adPrefix.Length - movesSuffix.Length);
  277. if (int.TryParse(number, out int legacyMoves))
  278. return L.F("ledger.ad_revive", legacyMoves);
  279. }
  280. const string shopPrefix = "SHOP - ";
  281. if (note.StartsWith(shopPrefix, StringComparison.Ordinal))
  282. {
  283. string itemKey = LegacyItemKey(note.Substring(shopPrefix.Length));
  284. if (!string.IsNullOrEmpty(itemKey))
  285. return L.F("ledger.shop", GameLocale.PowerUpName(itemKey));
  286. }
  287. const string clearPrefix = "GAME CLEAR - ";
  288. if (note.StartsWith(clearPrefix, StringComparison.Ordinal))
  289. {
  290. string rest = note.Substring(clearPrefix.Length);
  291. int levelIndex = rest.LastIndexOf(" LV ", StringComparison.Ordinal);
  292. if (levelIndex > 0 && int.TryParse(rest.Substring(levelIndex + 4), out int legacyLevel))
  293. {
  294. string modeKey = LegacyModeKey(rest.Substring(0, levelIndex));
  295. if (!string.IsNullOrEmpty(modeKey))
  296. return L.F("ledger.game_clear", GameLocale.ModeName(modeKey), legacyLevel);
  297. }
  298. }
  299. return note;
  300. }
  301. static string LegacyItemKey(string itemName)
  302. {
  303. for (int i = 0; i < GameDefinitions.PowerUps.Length; i++)
  304. {
  305. if (GameDefinitions.PowerUps[i].name == itemName)
  306. return GameDefinitions.PowerUps[i].key;
  307. }
  308. return null;
  309. }
  310. static string LegacyModeKey(string modeName)
  311. {
  312. for (int i = 0; i < GameDefinitions.Modes.Length; i++)
  313. {
  314. if (GameDefinitions.Modes[i].name == modeName)
  315. return GameDefinitions.Modes[i].key;
  316. }
  317. return null;
  318. }
  319. void CreateLedgerRow(int index, string title, string time, int amount, bool positive)
  320. {
  321. GameObject row = Instantiate(ledgerTemplate.gameObject, ledgerContent);
  322. row.name = "LedgerRow_" + index;
  323. row.SetActive(true);
  324. ledgerRows.Add(row);
  325. Transform root = row.transform;
  326. UIViewUtility.SetGraphicVisible(root, "IMG_Bg1", index % 2 == 0);
  327. UIViewUtility.SetGraphicVisible(root, "IMG_Bg2", index % 2 != 0);
  328. TMP_Text titleText = UIViewUtility.Text(root, "TMP_ActivityTitle");
  329. if (titleText != null)
  330. titleText.text = title;
  331. TMP_Text timeText = UIViewUtility.Text(root, "TMP_ActivityTime");
  332. if (timeText != null)
  333. timeText.text = time;
  334. TMP_Text amountText = UIViewUtility.Text(root, "TMP_ActivityAmount");
  335. if (amountText != null)
  336. {
  337. amountText.text = amount == 0 ? "" : (positive ? "+" : "") + UIViewUtility.FormatNumber(amount);
  338. amountText.color = positive
  339. ? new Color32(87, 224, 138, 255)
  340. : new Color32(255, 107, 107, 255);
  341. }
  342. }
  343. IEnumerator ResetScrollNextFrame()
  344. {
  345. yield return null;
  346. if (ledgerScroll != null)
  347. ledgerScroll.verticalNormalizedPosition = 1f;
  348. }
  349. }