123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*
- * ioGame
- * Copyright (C) 2021 - 2023 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved.
- * # iohao.com . 渔民小镇
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- package com.iohao.mmo.client.input;
- import com.alibaba.fastjson2.JSONObject;
- import com.iohao.game.action.skeleton.protocol.wrapper.BoolValue;
- import com.iohao.game.action.skeleton.protocol.wrapper.ByteValueList;
- import com.iohao.game.action.skeleton.protocol.wrapper.WrapperKit;
- import com.iohao.game.external.client.AbstractInputCommandRegion;
- import com.iohao.game.external.client.command.InputRequestData;
- import com.iohao.game.external.client.kit.ScannerKit;
- import com.iohao.game.external.client.kit.SplitParam;
- import com.iohao.mmo.bag.ItemTypeIdConst;
- import com.iohao.mmo.bag.cmd.BagCmd;
- import com.iohao.mmo.bag.proto.BagItemMessage;
- import com.iohao.mmo.bag.proto.BagMessage;
- import com.iohao.mmo.bag.proto.UseItemMessage;
- import com.iohao.mmo.bag.proto.UseMessage;
- import com.iohao.mmo.common.provide.kit.ItemTypeNodeKit;
- import com.iohao.mmo.common.provide.kit.JsonKit;
- import com.iohao.mmo.common.snow.SnowKit;
- import lombok.extern.slf4j.Slf4j;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author 渔民小镇
- * @date 2023-08-04
- */
- @Slf4j
- public class BagInputCommandRegion extends AbstractInputCommandRegion {
- @Override
- public void initInputCommand() {
- this.inputCommandCreate.cmd = BagCmd.cmd;
- this.inputCommandCreate.cmdName = "背包模块";
- request();
- useRequest();
- listenBroadcast(ByteValueList.class, result -> {
- List<BagItemMessage> list = result.toList(BagItemMessage.class);
- log.info("物品变更 : {}", list.size());
- }, BagCmd.broadcastChangeItems, "接收广播-物品变更");
- }
- @Override
- public void loginSuccessCallback() {
- if (true) {
- return;
- }
- List<BagItemMessage> list = new ArrayList<>();
- // 添加一些经验值道具
- BagItemMessage bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.expId);
- bagItemMessage.quantity = 10;
- list.add(bagItemMessage);
- log.info("添加 {} 个经验值道具 {}", bagItemMessage.quantity, bagItemMessage);
- // 添加一些装备制造书材料
- bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.equipWeaponBook10);
- bagItemMessage.quantity = 1;
- list.add(bagItemMessage);
- log.info("添加 {} 【装备-武器】制造书材料 {}", bagItemMessage.quantity, bagItemMessage);
- // 添加一些装备制造书材料
- bagItemMessage = BagInternalHelper.ofBagItemMessage(ItemTypeIdConst.iron10);
- bagItemMessage.quantity = 1;
- list.add(bagItemMessage);
- log.info("添加 {} 装备-制造材料-铁 {}", bagItemMessage.quantity, bagItemMessage);
- ofRequestCommand(BagCmd.incrementItem).request(WrapperKit.ofListByteValue(list));
- }
- private void request() {
- ofCommand(BagCmd.bag).callback(BagMessage.class, result -> {
- BagMessage value = result.getValue();
- List<JSONObject> list = value.itemMap.values().stream().map(bagItemMessage -> {
- String itemTypeId = bagItemMessage.itemTypeId;
- JSONObject bagItemMessageJson = JsonKit.toJSON(bagItemMessage);
- JSONObject itemMessageJson = ItemTypeNodeKit.toJSON(itemTypeId);
- return JsonKit.merge(bagItemMessageJson, itemMessageJson);
- }).toList();
- log.info("查询玩家背包 {}", JsonKit.toJsonString(list));
- }).setDescription("查询玩家背包");
- ofCommand(BagCmd.incrementItem).callback(BagItemMessage.class, result -> {
- var value = result.getValue();
- log.info("value : {}", value);
- // 重新查询一次背包
- ofRequestCommand(BagCmd.bag).request();
- }).setDescription("往背包添加(增加)物品").setInputRequestData(() -> {
- ScannerKit.log(() -> log.info("输入【1】表示添加一个可叠加的物品。"));
- String inputType = ScannerKit.nextLine("1");
- String id = "1";
- String itemId = "1";
- if (!"1".equals(inputType)) {
- id = SnowKit.nextToString();
- }
- BagItemMessage bagItemMessage = new BagItemMessage();
- bagItemMessage.id = id;
- bagItemMessage.itemTypeId = itemId;
- bagItemMessage.quantity = 1;
- ScannerKit.log(() -> log.info("{}", bagItemMessage));
- // 将请求参数包装成 list
- return WrapperKit.ofListByteValue(List.of(bagItemMessage));
- });
- ofCommand(BagCmd.decrementItem).callback(BagItemMessage.class, result -> {
- var value = result.getValue();
- log.info("value : {}", value);
- // 重新查询一次背包
- ofRequestCommand(BagCmd.bag).request();
- }).setDescription("从背包减少物品").setInputRequestData(() -> {
- ScannerKit.log(() -> log.info("输入需要减少的物品信息,格式 [背包物品id-数量]"));
- String inputType = ScannerKit.nextLine("1-1");
- SplitParam param = new SplitParam(inputType);
- String id = param.getString(0);
- int quantity = param.getInt(1, 1);
- BagItemMessage bagItemMessage = new BagItemMessage();
- bagItemMessage.id = id;
- bagItemMessage.quantity = quantity;
- ScannerKit.log(() -> log.info("{}", bagItemMessage));
- return bagItemMessage;
- });
- }
- private void useRequest() {
- InputRequestData inputRequestData = () -> {
- UseMessage useMessage = new UseMessage();
- useMessage.useItems = sceneDefault();
- ScannerKit.log(() -> log.info("当前所使用的物品信息 : {}", JsonKit.toJsonString(useMessage)));
- return useMessage;
- };
- ofCommand(BagCmd.use).callback(BoolValue.class, result -> {
- var value = result.getValue();
- log.info("value : {}", value);
- }).setDescription("使用背包物品").setInputRequestData(inputRequestData);
- InputRequestData buildEquipInputRequestData = () -> {
- UseMessage useMessage = new UseMessage();
- useMessage.useItems = sceneBuildEquip();
- ScannerKit.log(() -> log.info("打造装备-所使用物品 : {}", JsonKit.toJsonString(useMessage)));
- return useMessage;
- };
- ofCommand(BagCmd.useBuildEquip).callback(BoolValue.class, result -> {
- var value = result.getValue();
- log.info("value : {}", value);
- }).setDescription("打造装备").setInputRequestData(buildEquipInputRequestData);
- }
- private List<UseItemMessage> sceneBuildEquip() {
- ScannerKit.log(() -> {
- log.info("装备的制造最少需要使用两样背包物品,1.装备制造书、2.装备制造材料");
- log.info("格式 [制造书物品id-制造铁物品id]");
- });
- String defaultValue = ItemTypeIdConst.equipWeaponBook10 + "-" + ItemTypeIdConst.iron10;
- String inputType = defaultValue;
- // inputType = ScannerKit.nextLine(defaultValue);
- SplitParam param = new SplitParam(inputType);
- String equipWeaponBook = param.getString(0, ItemTypeIdConst.equipWeaponBook10);
- UseItemMessage useItemMessageEquip = BagInternalHelper.ofUseItemMessage(equipWeaponBook);
- String iron = param.getString(1, ItemTypeIdConst.iron10);
- UseItemMessage useItemMessageIron = BagInternalHelper.ofUseItemMessage(iron);
- return List.of(useItemMessageEquip, useItemMessageIron);
- }
- private List<UseItemMessage> sceneDefault() {
- ScannerKit.log(() -> log.info("输入需要使用的背包物品,格式 [背包物品id-数量]"));
- String inputType = ScannerKit.nextLine("1-1");
- SplitParam param = new SplitParam(inputType);
- // 得到下标 0 的值
- String id = param.getString(0);
- // 得到下标 1 的值,如果值不存在,则使用默认的 1 代替
- int quantity = param.getInt(1, 1);
- var useItemMessage = BagInternalHelper.ofUseItemMessage(id);
- useItemMessage.quantity = quantity;
- return List.of(useItemMessage);
- }
- }
|