Selaa lähdekoodia

:whale:
扩展业务框架 ActionAfter 接口
新增 json 工具

渔民小镇 1 vuosi sitten
vanhempi
sitoutus
974a67a68a

+ 68 - 0
common/common-core/src/main/java/com/iohao/mmo/common/core/flow/internal/DebugActionAfter.java

@@ -0,0 +1,68 @@
+/*
+ * 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.core.flow.internal;
+
+import com.iohao.game.action.skeleton.core.ActionCommand;
+import com.iohao.game.action.skeleton.core.commumication.ChannelContext;
+import com.iohao.game.action.skeleton.core.flow.ActionAfter;
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.action.skeleton.core.flow.FlowContextKit;
+import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr;
+import com.iohao.game.action.skeleton.protocol.ResponseMessage;
+
+/**
+ * 重写业务框架 ActionAfter 接口
+ * <pre>
+ *     <a href="https://www.yuque.com/iohao/game/avlo99">断言 + 异常机制 = 清晰简洁的代码</a>
+ *
+ *     默认的实现没有将异常信息描述给到请求端,这里我们重写 ActionAfter 接口,
+ *     让其支持将异常描述信息给到请求端,目的是方便调试(不用在模拟客户端维护异常码),
+ *     在实际上线后,我们将使用框架提供的默认实现。
+ * </pre>
+ *
+ * @author 渔民小镇
+ * @date 2023-08-01
+ */
+public class DebugActionAfter implements ActionAfter {
+    @Override
+    public void execute(final FlowContext flowContext) {
+
+        ChannelContext channelContext = FlowContextKit.getChannelContext(flowContext);
+
+        // 有错误就响应给调用方
+        final ResponseMessage response = flowContext.getResponse();
+        if (response.hasError()) {
+            // 得到异常消息,并发送到请求端
+            String msg = flowContext.option(FlowAttr.msgException);
+            response.setValidatorMsg(msg);
+
+            channelContext.sendResponse(response);
+            return;
+        }
+
+        // action 方法返回值是 void 的,不做处理
+        ActionCommand actionCommand = flowContext.getActionCommand();
+        if (actionCommand.getActionMethodReturnInfo().isVoid()) {
+            return;
+        }
+
+        // 将数据回传给调用方
+        channelContext.sendResponse(response);
+    }
+}

+ 4 - 0
common/common-core/src/main/java/com/iohao/mmo/common/logic/server/LogicServerKit.java

@@ -26,6 +26,7 @@ import com.iohao.game.bolt.broker.core.client.BrokerClientBuilder;
 import com.iohao.game.bolt.broker.core.common.processor.hook.ClientProcessorHooks;
 import com.iohao.mmo.common.config.GameCode;
 import com.iohao.mmo.common.core.flow.MyFlowContext;
+import com.iohao.mmo.common.core.flow.internal.DebugActionAfter;
 import com.iohao.mmo.common.processor.hook.MyRequestMessageClientProcessorHook;
 import lombok.experimental.UtilityClass;
 
@@ -44,8 +45,11 @@ public class LogicServerKit {
                 .addErrorCode(GameCode.values());
 
         BarSkeletonBuilder builder = LogicServerCreateKit.createBuilder(config);
+        // 重写业务框架 ActionAfter
+        builder.setActionAfter(new DebugActionAfter());
         // 使用自定义 FlowContext
         builder.setFlowContextFactory(MyFlowContext::new);
+
         return builder;
     }
 

+ 1 - 1
pom.xml

@@ -52,7 +52,7 @@
         <encoding>UTF-8</encoding>
 
         <!-- 项目版本 -->
-        <ioGame.version>17.1.49</ioGame.version>
+        <ioGame.version>17.1.50</ioGame.version>
         <spring-boot.version>3.1.2</spring-boot.version>
         <!-- lombok 消除冗长的 Java 代码 https://mvnrepository.com/artifact/org.projectlombok/lombok -->
         <lombok.version>1.18.24</lombok.version>

+ 40 - 0
provide/common-provide/src/main/java/com/iohao/mmo/common/provide/kit/JsonKit.java

@@ -0,0 +1,40 @@
+/*
+ * 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.kit;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONWriter;
+import lombok.experimental.UtilityClass;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-01
+ */
+@UtilityClass
+public class JsonKit {
+    /**
+     * 打印一些信息,当对象属性较多时,json 格式数据更清晰
+     *
+     * @param value value
+     * @return jsonFormat
+     */
+    public String toJsonString(Object value) {
+        return JSON.toJSONString(value, JSONWriter.Feature.PrettyFormat);
+    }
+}

+ 0 - 1
provide/level-provide/src/main/java/com/iohao/mmo/level/client/LevelInputCommandRegion.java

@@ -74,6 +74,5 @@ public class LevelInputCommandRegion extends AbstractInputCommandRegion {
             LevelMessage value = result.getValue();
             log.info("value : {}", value);
         }).setDescription("人物升级").setInputRequestData(inputRequestData);
-
     }
 }

+ 2 - 3
provide/person-provide/src/main/java/com/iohao/mmo/person/client/PersonInputCommandRegion.java

@@ -18,9 +18,8 @@
  */
 package com.iohao.mmo.person.client;
 
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONWriter;
 import com.iohao.game.external.client.AbstractInputCommandRegion;
+import com.iohao.mmo.common.provide.kit.JsonKit;
 import com.iohao.mmo.person.cmd.PersonCmd;
 import com.iohao.mmo.person.proto.PersonMessage;
 import lombok.extern.slf4j.Slf4j;
@@ -37,7 +36,7 @@ public class PersonInputCommandRegion extends AbstractInputCommandRegion {
 
         ofCommand(PersonCmd.getPerson).callback(PersonMessage.class, result -> {
             PersonMessage value = result.getValue();
-            String jsonFormat = JSON.toJSONString(value, JSONWriter.Feature.PrettyFormat);
+            String jsonFormat = JsonKit.toJsonString(value);
             log.info("人物信息 : {}", jsonFormat);
         }).setDescription("得到人物、英雄信息");