Parcourir la source

:whale: 背包模块,提供对外访问 api

渔民小镇 il y a 1 an
Parent
commit
4ff250aaa6

+ 36 - 0
common/common-core/src/main/java/com/iohao/mmo/common/kit/MessageKit.java

@@ -0,0 +1,36 @@
+/*
+ * 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.common.kit;
+
+import com.iohao.game.action.skeleton.core.DataCodecKit;
+import com.iohao.game.action.skeleton.protocol.ResponseMessage;
+import lombok.experimental.UtilityClass;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@UtilityClass
+public class MessageKit {
+    public <T> T getData(ResponseMessage responseMessage, Class<T> clazz) {
+        // 将字节解析成对象
+        byte[] dataContent = responseMessage.getData();
+        return DataCodecKit.decode(dataContent, clazz);
+    }
+}

+ 6 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/service/BagService.java

@@ -99,6 +99,12 @@ public class BagService {
 
         bagItem.addQuantity(-quantity);
 
+        if (bagItem.getQuantity() == 0) {
+            itemMap.remove(bagItemId);
+        }
+
+        bagRepository.save(bag);
+
         return bagItem;
     }
 

+ 3 - 2
one-application/src/main/resources/logback-spring.xml

@@ -99,10 +99,11 @@
     </appender>
 
     <!-- 为某个包下的所有类的指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName -->
-    <logger name="com.iohao" additivity="false">
+    <logger name="com.iohao.mmo" additivity="false">
         <level value="${log.root.level}"/>
         <appender-ref ref="stdout"/>
-        <appender-ref ref="file.async"/><!-- 即com.iohao.game包下级别为 ${log.root.level}的才会使用file.async来打印 -->
+        <!-- 即com.iohao.mmo 包下级别为 ${log.root.level}的才会使用file.async来打印 -->
+        <appender-ref ref="file.async"/>
         <appender-ref ref="file.error"/>
     </logger>
 

+ 2 - 2
one-client/src/main/java/com/iohao/mmo/client/CommonClient.java

@@ -63,9 +63,9 @@ public class CommonClient {
                 // 地图
                 , mapInputCommandRegion
                 // 人物、英雄
-                , personInputCommandRegion
+//                , personInputCommandRegion
                 // 等级相关
-                , levelInputCommandRegion
+//                , levelInputCommandRegion
                 // 背包
                 , bagInputCommandRegion
         );

+ 57 - 0
provide/bag-provide/src/main/java/com/iohao/mmo/bag/client/BagExchange.java

@@ -0,0 +1,57 @@
+/*
+ * 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.bag.client;
+
+import com.iohao.game.action.skeleton.core.CmdInfo;
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.action.skeleton.protocol.ResponseMessage;
+import com.iohao.mmo.bag.cmd.BagCmd;
+import com.iohao.mmo.bag.proto.BagItemMessage;
+import com.iohao.mmo.common.kit.MessageKit;
+import com.iohao.mmo.common.provide.client.ExchangeKit;
+import lombok.experimental.UtilityClass;
+
+/**
+ * 背包模块,对外提供的访问 api
+ *
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@UtilityClass
+public class BagExchange {
+    ResponseMessage incrementItemResponse(BagItemMessage bagItemMessage, FlowContext flowContext) {
+        CmdInfo cmdInfo = BagCmd.of(BagCmd.incrementItem);
+        return ExchangeKit.invokeModuleMessage(flowContext, cmdInfo, bagItemMessage);
+    }
+
+    public BagItemMessage incrementItem(BagItemMessage bagItemMessage, FlowContext flowContext) {
+        ResponseMessage responseMessage = incrementItemResponse(bagItemMessage, flowContext);
+        return MessageKit.getData(responseMessage, BagItemMessage.class);
+    }
+
+    ResponseMessage decrementItemResponse(BagItemMessage bagItemMessage, FlowContext flowContext) {
+        CmdInfo cmdInfo = BagCmd.of(BagCmd.decrementItem);
+        return ExchangeKit.invokeModuleMessage(flowContext, cmdInfo, bagItemMessage);
+    }
+
+    public BagItemMessage decrementItem(BagItemMessage bagItemMessage, FlowContext flowContext) {
+        ResponseMessage responseMessage = decrementItemResponse(bagItemMessage, flowContext);
+        return MessageKit.getData(responseMessage, BagItemMessage.class);
+    }
+}

+ 2 - 1
provide/bag-provide/src/main/java/com/iohao/mmo/bag/client/BagInputCommandRegion.java

@@ -24,6 +24,7 @@ import com.iohao.game.external.client.kit.ScannerKit;
 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.common.provide.kit.JsonKit;
 import com.iohao.mmo.common.snow.SnowKit;
 import lombok.extern.slf4j.Slf4j;
 
@@ -42,7 +43,7 @@ public class BagInputCommandRegion extends AbstractInputCommandRegion {
     private void request() {
         ofCommand(BagCmd.bag).callback(BagMessage.class, result -> {
             BagMessage value = result.getValue();
-            log.info("value : {}", value);
+            log.info("{}", JsonKit.toJsonString(value));
         }).setDescription("查询玩家背包");
 
         InputRequestData inputRequestData = () -> {

+ 49 - 0
provide/common-provide/src/main/java/com/iohao/mmo/common/provide/client/ExchangeKit.java

@@ -0,0 +1,49 @@
+/*
+ * 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.common.provide.client;
+
+import com.iohao.game.action.skeleton.core.CmdInfo;
+import com.iohao.game.action.skeleton.core.commumication.BrokerClientContext;
+import com.iohao.game.action.skeleton.core.commumication.InvokeModuleContext;
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr;
+import com.iohao.game.action.skeleton.protocol.RequestMessage;
+import com.iohao.game.action.skeleton.protocol.ResponseMessage;
+import lombok.experimental.UtilityClass;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@UtilityClass
+public class ExchangeKit {
+    public ResponseMessage invokeModuleMessage(FlowContext flowContext, CmdInfo cmdInfo) {
+        return invokeModuleMessage(flowContext, cmdInfo, null);
+    }
+
+    public ResponseMessage invokeModuleMessage(FlowContext flowContext, CmdInfo cmdInfo, Object data) {
+        RequestMessage requestMessage = flowContext
+                .getRequest()
+                .createRequestMessage(cmdInfo, data);
+
+        BrokerClientContext brokerClientContext = flowContext.option(FlowAttr.brokerClientContext);
+        InvokeModuleContext invokeModuleContext = brokerClientContext.getInvokeModuleContext();
+        return invokeModuleContext.invokeModuleMessage(requestMessage);
+    }
+}