BagInputCommandRegion.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * ioGame
  3. * Copyright (C) 2021 - 2023 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
  4. * # iohao.com . 渔民小镇
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. package com.iohao.mmo.client.input;
  20. import com.alibaba.fastjson2.JSONObject;
  21. import com.iohao.game.action.skeleton.protocol.wrapper.BoolValue;
  22. import com.iohao.game.action.skeleton.protocol.wrapper.ByteValueList;
  23. import com.iohao.game.action.skeleton.protocol.wrapper.WrapperKit;
  24. import com.iohao.game.external.client.AbstractInputCommandRegion;
  25. import com.iohao.game.external.client.command.InputRequestData;
  26. import com.iohao.game.external.client.kit.ScannerKit;
  27. import com.iohao.game.external.client.kit.SplitParam;
  28. import com.iohao.mmo.bag.ItemTypeIdConst;
  29. import com.iohao.mmo.bag.cmd.BagCmd;
  30. import com.iohao.mmo.bag.proto.BagItemMessage;
  31. import com.iohao.mmo.bag.proto.BagMessage;
  32. import com.iohao.mmo.bag.proto.UseItemMessage;
  33. import com.iohao.mmo.bag.proto.UseMessage;
  34. import com.iohao.mmo.common.provide.kit.ItemTypeNodeKit;
  35. import com.iohao.mmo.common.provide.kit.JsonKit;
  36. import com.iohao.mmo.common.snow.SnowKit;
  37. import lombok.extern.slf4j.Slf4j;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. /**
  41. * @author 渔民小镇
  42. * @date 2023-08-04
  43. */
  44. @Slf4j
  45. public class BagInputCommandRegion extends AbstractInputCommandRegion {
  46. @Override
  47. public void initInputCommand() {
  48. this.inputCommandCreate.cmd = BagCmd.cmd;
  49. this.inputCommandCreate.cmdName = "背包模块";
  50. request();
  51. useRequest();
  52. listenBroadcast(ByteValueList.class, result -> {
  53. List<BagItemMessage> list = result.toList(BagItemMessage.class);
  54. log.info("物品变更 : {}", list.size());
  55. }, BagCmd.broadcastChangeItems, "接收广播-物品变更");
  56. }
  57. @Override
  58. public void loginSuccessCallback() {
  59. if (true) {
  60. return;
  61. }
  62. List<BagItemMessage> list = new ArrayList<>();
  63. // 添加一些经验值道具
  64. BagItemMessage bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.expId);
  65. bagItemMessage.quantity = 10;
  66. list.add(bagItemMessage);
  67. log.info("添加 {} 个经验值道具 {}", bagItemMessage.quantity, bagItemMessage);
  68. // 添加一些装备制造书材料
  69. bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.equipWeaponBook10);
  70. bagItemMessage.quantity = 1;
  71. list.add(bagItemMessage);
  72. log.info("添加 {} 【装备-武器】制造书材料 {}", bagItemMessage.quantity, bagItemMessage);
  73. // 添加一些装备制造书材料
  74. bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.iron10);
  75. bagItemMessage.quantity = 1;
  76. list.add(bagItemMessage);
  77. log.info("添加 {} 装备-制造材料-铁 {}", bagItemMessage.quantity, bagItemMessage);
  78. ofRequestCommand(BagCmd.incrementItem).request(WrapperKit.ofListByteValue(list));
  79. }
  80. private void request() {
  81. ofCommand(BagCmd.bag).callback(BagMessage.class, result -> {
  82. BagMessage value = result.getValue();
  83. List<JSONObject> list = value.itemMap.values().stream().map(bagItemMessage -> {
  84. String itemTypeId = bagItemMessage.itemTypeId;
  85. JSONObject bagItemMessageJson = JsonKit.toJSON(bagItemMessage);
  86. JSONObject itemMessageJson = ItemTypeNodeKit.toJSON(itemTypeId);
  87. return JsonKit.merge(bagItemMessageJson, itemMessageJson);
  88. }).toList();
  89. log.info("查询玩家背包 {}", JsonKit.toJsonString(list));
  90. }).setDescription("查询玩家背包");
  91. ofCommand(BagCmd.incrementItem).callback(BagItemMessage.class, result -> {
  92. var value = result.getValue();
  93. log.info("value : {}", value);
  94. // 重新查询一次背包
  95. ofRequestCommand(BagCmd.bag).request();
  96. }).setDescription("往背包添加(增加)物品").setInputRequestData(() -> {
  97. ScannerKit.log(() -> log.info("输入【1】表示添加一个可叠加的物品。"));
  98. String inputType = ScannerKit.nextLine("1");
  99. String id = "1";
  100. String itemId = "1";
  101. if (!"1".equals(inputType)) {
  102. id = SnowKit.nextToString();
  103. }
  104. BagItemMessage bagItemMessage = new BagItemMessage();
  105. bagItemMessage.id = id;
  106. bagItemMessage.itemTypeId = itemId;
  107. bagItemMessage.quantity = 1;
  108. ScannerKit.log(() -> log.info("{}", bagItemMessage));
  109. // 将请求参数包装成 list
  110. return WrapperKit.ofListByteValue(List.of(bagItemMessage));
  111. });
  112. ofCommand(BagCmd.decrementItem).callback(BagItemMessage.class, result -> {
  113. var value = result.getValue();
  114. log.info("value : {}", value);
  115. // 重新查询一次背包
  116. ofRequestCommand(BagCmd.bag).request();
  117. }).setDescription("从背包减少物品").setInputRequestData(() -> {
  118. ScannerKit.log(() -> log.info("输入需要减少的物品信息,格式 [背包物品id-数量]"));
  119. String inputType = ScannerKit.nextLine("1-1");
  120. SplitParam param = new SplitParam(inputType);
  121. String id = param.getString(0);
  122. int quantity = param.getInt(1, 1);
  123. BagItemMessage bagItemMessage = new BagItemMessage();
  124. bagItemMessage.id = id;
  125. bagItemMessage.quantity = quantity;
  126. ScannerKit.log(() -> log.info("{}", bagItemMessage));
  127. return bagItemMessage;
  128. });
  129. }
  130. private void useRequest() {
  131. InputRequestData inputRequestData = () -> {
  132. UseMessage useMessage = new UseMessage();
  133. useMessage.useItems = sceneDefault();
  134. ScannerKit.log(() -> log.info("当前所使用的物品信息 : {}", JsonKit.toJsonString(useMessage)));
  135. return useMessage;
  136. };
  137. ofCommand(BagCmd.use).callback(BoolValue.class, result -> {
  138. var value = result.getValue();
  139. log.info("value : {}", value);
  140. }).setDescription("使用背包物品").setInputRequestData(inputRequestData);
  141. InputRequestData buildEquipInputRequestData = () -> {
  142. UseMessage useMessage = new UseMessage();
  143. useMessage.useItems = sceneBuildEquip();
  144. ScannerKit.log(() -> log.info("打造装备-所使用物品 : {}", JsonKit.toJsonString(useMessage)));
  145. return useMessage;
  146. };
  147. ofCommand(BagCmd.useBuildEquip).callback(BoolValue.class, result -> {
  148. var value = result.getValue();
  149. log.info("value : {}", value);
  150. }).setDescription("打造装备").setInputRequestData(buildEquipInputRequestData);
  151. }
  152. private List<UseItemMessage> sceneBuildEquip() {
  153. ScannerKit.log(() -> {
  154. log.info("装备的制造最少需要使用两样背包物品,1.装备制造书、2.装备制造材料");
  155. log.info("格式 [制造书物品id-制造铁物品id]");
  156. });
  157. String defaultValue = ItemTypeIdConst.equipWeaponBook10 + "-" + ItemTypeIdConst.iron10;
  158. String inputType = defaultValue;
  159. // inputType = ScannerKit.nextLine(defaultValue);
  160. SplitParam param = new SplitParam(inputType);
  161. String equipWeaponBook = param.getString(0, ItemTypeIdConst.equipWeaponBook10);
  162. UseItemMessage useItemMessageEquip = BagInternalHelper.ofUseItemMessage(equipWeaponBook);
  163. String iron = param.getString(1, ItemTypeIdConst.iron10);
  164. UseItemMessage useItemMessageIron = BagInternalHelper.ofUseItemMessage(iron);
  165. return List.of(useItemMessageEquip, useItemMessageIron);
  166. }
  167. private List<UseItemMessage> sceneDefault() {
  168. ScannerKit.log(() -> log.info("输入需要使用的背包物品,格式 [背包物品id-数量]"));
  169. String inputType = ScannerKit.nextLine("1-1");
  170. SplitParam param = new SplitParam(inputType);
  171. // 得到下标 0 的值
  172. String id = param.getString(0);
  173. // 得到下标 1 的值,如果值不存在,则使用默认的 1 代替
  174. int quantity = param.getInt(1, 1);
  175. var useItemMessage = BagInternalHelper.ofUseItemMessage(id);
  176. useItemMessage.quantity = quantity;
  177. return List.of(useItemMessage);
  178. }
  179. }