Advertisement
hschlichting2003

Untitled

Apr 24th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. import {ZepetoScriptBehaviour} from 'ZEPETO.Script'
  2. import {Button, Text, ToggleGroup} from 'UnityEngine.UI'
  3. import {GameObject, Object, Sprite, Transform, WaitUntil} from 'UnityEngine'
  4. import {InventoryRecord, InventoryService} from "ZEPETO.Inventory";
  5. import {CurrencyService} from "ZEPETO.Currency";
  6. import {ProductRecord, ProductService, PurchaseType} from "ZEPETO.Product";
  7. import {ZepetoWorldMultiplay} from "ZEPETO.World";
  8. import {Room, RoomData} from "ZEPETO.Multiplay";
  9. import ITM_Inventory from './ITM_Inventory';
  10. import {BalanceSync, InventorySync, Currency} from "./UIBalances";
  11.  
  12. export default class UIInventory extends ZepetoScriptBehaviour {
  13. @SerializeField() private usedSlotNumTxt : Text;
  14. @SerializeField() private possessionStarTxt : Text;
  15. @SerializeField() private useBtn : Button;
  16.  
  17. @SerializeField() private contentParent : Transform;
  18. @SerializeField() private prefItem : GameObject;
  19. @SerializeField() private itemImage : Sprite[];
  20.  
  21. private _inventoryCache: InventoryRecord[];
  22. private _productCache: Map<string, ProductRecord> = new Map<string, ProductRecord>();
  23. private _multiplay : ZepetoWorldMultiplay;
  24. private _room : Room;
  25.  
  26. private Start() {
  27. this._multiplay = Object.FindObjectOfType<ZepetoWorldMultiplay>();
  28. this._multiplay.RoomJoined += (room: Room) => {
  29. this._room = room;
  30. this.InitMessageHandler();
  31. }
  32.  
  33. this.StartCoroutine(this.LoadAllItems());
  34. }
  35.  
  36. private InitMessageHandler(){
  37. ProductService.OnPurchaseCompleted.AddListener((product, response) => {
  38. this.StartCoroutine(this.RefreshInventoryUI());
  39. this.StartCoroutine(this.RefreshBalanceUI());
  40. });
  41. this._room.AddMessageHandler<InventorySync>("SyncInventories", (message) => {
  42. this.StartCoroutine(this.RefreshInventoryUI());
  43. });
  44. this._room.AddMessageHandler<BalanceSync>("SyncBalances", (message) => {
  45. this.StartCoroutine(this.RefreshBalanceUI());
  46. });
  47. this.useBtn.onClick.AddListener(()=> this.OnClickUseInventoryItem());
  48. }
  49.  
  50. private* LoadAllItems() {
  51. const request = ProductService.GetProductsAsync();
  52. yield new WaitUntil(() => request.keepWaiting == false);
  53. if (request.responseData.isSuccess) {
  54. request.responseData.products.forEach((pr) => {
  55. this._productCache.set(pr.productId,pr);
  56. });
  57. }
  58.  
  59. this.StartCoroutine(this.RefreshInventoryUI());
  60. this.StartCoroutine(this.RefreshBalanceUI());
  61. }
  62.  
  63. private * RefreshInventoryUI(){
  64. const request = InventoryService.GetListAsync();
  65. yield new WaitUntil(()=>request.keepWaiting == false);
  66. if(request.responseData.isSuccess) {
  67. const items: InventoryRecord[] = request.responseData.products;
  68.  
  69. items.forEach((ir, index) => {
  70. // If there are zero consumable items, delete them from the inventory.
  71. if (ir.quantity <= 0 && this._productCache.get(ir.productId).PurchaseType == PurchaseType.Consumable) {
  72. // remove inventory
  73. const data = new RoomData();
  74. data.Add("productId", ir.productId);
  75. this._multiplay.Room?.Send("onRemoveInventory", data.GetObject());
  76. return;
  77. }
  78.  
  79. });
  80.  
  81. // If the value matches the previously received value, do not update it.
  82. if (this._inventoryCache === items)
  83. return;
  84. else if (items != null && this._inventoryCache?.length == items.length)
  85. this.UpdateInventory(items);
  86. else
  87. this.CreateInventory(items);
  88.  
  89. this.usedSlotNumTxt.text = items.length.toString();
  90. this._inventoryCache = items;
  91. }
  92. }
  93.  
  94. private UpdateInventory(items:InventoryRecord[]){
  95. const itemScripts = this.contentParent.GetComponentsInChildren<ITM_Inventory>();
  96. items.forEach((ir)=>{
  97. itemScripts.forEach((itemScript)=>{
  98. if(itemScript.itemRecord.productId == ir.productId) {
  99. const isShowQuantity:boolean = this._productCache.get(ir.productId).PurchaseType == PurchaseType.Consumable;
  100. itemScript.RefreshItem(ir, isShowQuantity);
  101. return;
  102. }
  103. });
  104. });
  105. }
  106.  
  107. private CreateInventory(items :InventoryRecord[]){
  108. this.contentParent.GetComponentsInChildren<ITM_Inventory>().forEach((child)=>{
  109. GameObject.Destroy(child.gameObject);
  110. });
  111.  
  112. // Sort by Create Order (descending order)
  113. items.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
  114.  
  115. items.forEach((ir, index) => {
  116. const itemObj = Object.Instantiate(this.prefItem, this.contentParent) as GameObject;
  117.  
  118. const itemScript = itemObj.GetComponent<ITM_Inventory>();
  119. this.itemImage.forEach((s, index) => {
  120. // Import by name comparison from image resources.
  121. if (s.name == ir.productId) {
  122. itemScript.itemImage.sprite = this.itemImage[index];
  123. return;
  124. }
  125. });
  126. // Non-consumable items do not display numbers.
  127. const isShowQuantity:boolean = this._productCache.get(ir.productId).PurchaseType == PurchaseType.Consumable;
  128. itemScript.RefreshItem(ir, isShowQuantity);
  129. itemScript.isOn(index == 0);
  130. });
  131. }
  132.  
  133. private *RefreshBalanceUI(){
  134. const request = CurrencyService.GetUserCurrencyBalancesAsync();
  135. yield new WaitUntil(()=>request.keepWaiting == false);
  136. if(request.responseData.isSuccess) {
  137. this.possessionStarTxt.text = request.responseData.currencies.ContainsKey(Currency.star) ? request.responseData.currencies.get_Item(Currency.star).toString() : "0";
  138. }
  139. else{
  140. console.log(request.responseData.ErrorCode);
  141. }
  142. }
  143.  
  144.  
  145. private OnClickUseInventoryItem(){
  146. const toggleGroup = this.contentParent.GetComponent<ToggleGroup>();
  147. const item = toggleGroup.GetFirstActiveToggle()?.GetComponent<ITM_Inventory>().itemRecord;
  148.  
  149. if(item == null){
  150. console.warn("no have item data");
  151. return;
  152. }
  153. if(this._multiplay.Room == null){
  154. console.warn("server disconnect");
  155. return;
  156. }
  157.  
  158. const data = new RoomData();
  159. data.Add("productId", item.productId);
  160. data.Add("quantity", 1);
  161. this._multiplay.Room?.Send("onUseInventory", data.GetObject());
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement