BotClient.Inventory.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using CommonLang;
  2. using CommonLang.IO;
  3. using pomelo.area;
  4. using Pomelo.DotNetClient;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace XmdsBattleClientBot.Bot
  10. {
  11. partial class BotClient
  12. {
  13. public InventoryManager CurrentInventories { get; private set; }
  14. private void callback_gs_BagItemUpdatePush(BagItemUpdatePush on)
  15. {
  16. if (on.s2c_data != null)
  17. {
  18. CurrentInventories.Refresh(on);
  19. }
  20. }
  21. public virtual void gs_EquipItemByID(string id, Action<PomeloException, EquipResponse> action = null)
  22. {
  23. int index;
  24. var item = CurrentInventories.Bag.GetItemByID(id, out index);
  25. if (item != null)
  26. {
  27. if(item.code.CompareTo("lweap1-test") != 0 && item.code.CompareTo("lclot1-test") != 0)
  28. {
  29. return;
  30. }
  31. client.GameSocket.equipHandler.equipRequest(index, (err, rsp) => { if (action != null) { action(err, rsp); } });
  32. }
  33. }
  34. public enum BagType
  35. {
  36. Bag = 1,
  37. Warehouse = 2,
  38. Recycled = 4,
  39. }
  40. public class InventoryStore
  41. {
  42. public BagType Type { get; private set; }
  43. public int TotalCount { get; private set; }
  44. public int GridCount { get; private set; }
  45. public int GridPrice { get; private set; }
  46. private readonly BotClient bot;
  47. private HashMap<int, pomelo.item.Item> grid = new HashMap<int, pomelo.item.Item>();
  48. internal InventoryStore(BotClient bot, pomelo.player.Store store)
  49. {
  50. this.bot = bot;
  51. this.Type = (BagType)store.type;
  52. this.TotalCount = store.bagTotalCount;
  53. this.GridCount = store.bagGridCount;
  54. this.GridPrice = store.gridPrice;
  55. foreach (var g in store.bagGrids)
  56. {
  57. this.grid.Put(g.gridIndex, g.item);
  58. }
  59. }
  60. internal void Refresh(BagItemUpdatePush on)
  61. {
  62. foreach (var g in on.s2c_data)
  63. {
  64. this.grid.Put(g.gridIndex, g.item);
  65. }
  66. }
  67. public void OpenStorage(int count, Action<PomeloException, OpenBagGridResponse> action = null)
  68. {
  69. if (this.TotalCount > this.GridCount)
  70. {
  71. bot.client.GameSocket.bagHandler.openBagGridRequest(
  72. (int)BotClient.BagType.Bag,
  73. Math.Min(count, this.TotalCount - this.GridCount),
  74. (e, r) => { if (action != null) action(e, r); });
  75. }
  76. }
  77. public pomelo.item.Item GetItemByIndex(int index)
  78. {
  79. return grid[index];
  80. }
  81. public pomelo.item.Item GetItemByID(string id, out int index)
  82. {
  83. foreach (var item in grid)
  84. {
  85. if (item.Value != null && item.Value.id == id)
  86. {
  87. index = item.Key;
  88. return item.Value;
  89. }
  90. }
  91. index = -1;
  92. return null;
  93. }
  94. }
  95. public class InventoryManager
  96. {
  97. private readonly BotClient bot;
  98. private InventoryStore bag_grid;
  99. private InventoryStore warehouse_grid;
  100. private InventoryStore recyled_grid;
  101. public InventoryStore Bag { get { return bag_grid; } }
  102. public InventoryStore Warehouse { get { return warehouse_grid; } }
  103. public InventoryStore Recycled { get { return recyled_grid; } }
  104. internal InventoryManager(BotClient bot)
  105. {
  106. this.bot = bot;
  107. this.bag_grid = new InventoryStore(bot, bot.PlayerData.store.bag);
  108. this.warehouse_grid = new InventoryStore(bot, bot.PlayerData.store.wareHouse);
  109. this.recyled_grid = new InventoryStore(bot, bot.PlayerData.store.recycle);
  110. }
  111. internal void Refresh(BagItemUpdatePush on)
  112. {
  113. switch ((BagType)on.s2c_type)
  114. {
  115. case BagType.Bag:
  116. bag_grid.Refresh(on);
  117. break;
  118. case BagType.Warehouse:
  119. warehouse_grid.Refresh(on);
  120. break;
  121. case BagType.Recycled:
  122. recyled_grid.Refresh(on);
  123. break;
  124. }
  125. }
  126. // public pomelo.item.Item GetRandomItem(BotClient.BagType type, out int index)
  127. // {
  128. // switch ((BagType)on.s2c_type)
  129. // {
  130. // case BagType.Bag:
  131. // bag_grid.Refresh(on);
  132. // break;
  133. // case BagType.Warehouse:
  134. // warehouse_grid.Refresh(on);
  135. // break;
  136. // case BagType.Recycled:
  137. // recyled_grid.Refresh(on);
  138. // break;
  139. // }
  140. // }
  141. }
  142. }
  143. }