Explorar o código

Merge remote-tracking branch 'origin/main'

tangbin hai 1 ano
pai
achega
2381bd9afb

+ 16 - 6
logic/bag-logic/src/main/java/com/iohao/mmo/bag/action/BagAction.java

@@ -22,6 +22,7 @@ import com.iohao.game.action.skeleton.annotation.ActionController;
 import com.iohao.game.action.skeleton.annotation.ActionMethod;
 import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
 import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.common.kit.SafeKit;
 import com.iohao.mmo.bag.cmd.BagCmd;
 import com.iohao.mmo.bag.entity.Bag;
 import com.iohao.mmo.bag.entity.BagItem;
@@ -31,6 +32,8 @@ import com.iohao.mmo.bag.pojo.UsePOJO;
 import com.iohao.mmo.bag.proto.BagItemMessage;
 import com.iohao.mmo.bag.proto.BagMessage;
 import com.iohao.mmo.bag.proto.UseMessage;
+import com.iohao.mmo.bag.region.UseContext;
+import com.iohao.mmo.bag.region.UseRegion;
 import com.iohao.mmo.bag.service.BagService;
 import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
@@ -48,6 +51,9 @@ import org.springframework.stereotype.Component;
 public class BagAction {
     @Resource
     BagService bagService;
+    @Resource
+    UseRegion useRegion;
+
 
     /**
      * 查询玩家背包
@@ -108,20 +114,24 @@ public class BagAction {
      */
     @ActionMethod(BagCmd.use)
     public boolean use(UseMessage useMessage, FlowContext flowContext) {
-        long userId = flowContext.getUserId();
         ActionErrorEnum.validateErrCode.assertTrue(useMessage.verify());
-
         /*
          * 各物品的处理逻辑不相同
          * 如气血药,增加气血值;魔法药,增加魔法值;
          * 攻击符、增加临时攻击力;
          */
-
         UsePOJO usePOJO = ItemMapper.ME.convert(useMessage);
-        log.info("usePOJO : {}", usePOJO);
+        usePOJO.scene = SafeKit.getString(usePOJO.scene, "default");
 
-        // 减少背包物品
-        // 物品功效
+        // 使用上下文
+        UseContext context = new UseContext();
+        context.setUsePOJO(usePOJO);
+        context.setFlowContext(flowContext);
+        context.setScene(usePOJO.scene);
+        // 处理物品的使用
+        useRegion.process(context);
+
+        log.info("usePOJO : {}", usePOJO);
 
         return true;
     }

+ 11 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/config/ItemCommandLineRunner.java

@@ -20,7 +20,11 @@ package com.iohao.mmo.bag.config;
 
 import com.iohao.mmo.bag.ItemIdConst;
 import com.iohao.mmo.bag.entity.ItemConfig;
+import com.iohao.mmo.bag.region.ItemConfigRegion;
+import com.iohao.mmo.bag.region.UseRegion;
+import com.iohao.mmo.bag.region.internal.DefaultUseProcess;
 import jakarta.annotation.Resource;
+import lombok.AllArgsConstructor;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.stereotype.Component;
@@ -33,14 +37,21 @@ import java.util.List;
  * @date 2023-08-06
  */
 @Component
+@AllArgsConstructor
 public class ItemCommandLineRunner implements CommandLineRunner {
     @Resource
     MongoTemplate mongoTemplate;
 
+    final DefaultUseProcess defaultUseProcess;
+
+    final UseRegion useRegion;
+
     @Override
     public void run(String... args) {
         List<ItemConfig> itemConfigs = initConfigExcel();
         itemConfigs.forEach(ItemConfigRegion::addItemConfig);
+
+        useRegion.addUseProcess(defaultUseProcess);
     }
 
     private List<ItemConfig> initConfigExcel() {

+ 3 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/mapper/ItemMapper.java

@@ -19,6 +19,7 @@
 package com.iohao.mmo.bag.mapper;
 
 import com.iohao.game.common.kit.CollKit;
+import com.iohao.mmo.bag.entity.BagItem;
 import com.iohao.mmo.bag.pojo.UseItemPOJO;
 import com.iohao.mmo.bag.pojo.UsePOJO;
 import com.iohao.mmo.bag.proto.UseItemMessage;
@@ -40,6 +41,8 @@ import java.util.Map;
 public interface ItemMapper {
     ItemMapper ME = Mappers.getMapper(ItemMapper.class);
 
+    BagItem convert(UseItemPOJO useItem);
+
     @Mapping(target = "useItemMap", source = "useItems")
     UsePOJO convert(UseMessage useMessage);
 

+ 6 - 2
logic/bag-logic/src/main/java/com/iohao/mmo/bag/pojo/UsePOJO.java

@@ -31,8 +31,8 @@ import java.util.Map;
 @ToString
 @FieldDefaults(level = AccessLevel.PUBLIC)
 public class UsePOJO {
-    /** 业务操作 */
-    int operation;
+    /** 业务场景 */
+    String scene;
     /**
      * 使用项
      * <pre>
@@ -49,4 +49,8 @@ public class UsePOJO {
      * </pre>
      */
     Map<String, UseItemPOJO> useItemMap;
+
+    public UseItemPOJO getUseItem() {
+        return this.useItemMap.values().stream().findAny().orElse(null);
+    }
 }

+ 1 - 1
logic/bag-logic/src/main/java/com/iohao/mmo/bag/config/ItemConfigRegion.java → logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/ItemConfigRegion.java

@@ -16,7 +16,7 @@
  * 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.config;
+package com.iohao.mmo.bag.region;
 
 import com.iohao.mmo.bag.entity.ItemConfig;
 import lombok.NonNull;

+ 41 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/UseContext.java

@@ -0,0 +1,41 @@
+/*
+ * 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.region;
+
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.mmo.bag.pojo.UsePOJO;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.FieldDefaults;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@Getter
+@Setter
+@FieldDefaults(level = AccessLevel.PRIVATE)
+public class UseContext {
+    UsePOJO usePOJO;
+    FlowContext flowContext;
+    String scene;
+
+    boolean success;
+}

+ 31 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/UseProcess.java

@@ -0,0 +1,31 @@
+/*
+ * 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.region;
+
+/**
+ * 物品处理
+ *
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+public interface UseProcess {
+    void process(UseContext context);
+
+    String getScene();
+}

+ 49 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/UseRegion.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.bag.region;
+
+import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
+import lombok.NonNull;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@Component
+public class UseRegion {
+    Map<String, UseProcess> map = new ConcurrentHashMap<>();
+
+    public void addUseProcess(@NonNull UseProcess useProcess) {
+        String scene = useProcess.getScene();
+        Objects.requireNonNull(scene);
+        map.put(scene, useProcess);
+    }
+
+    public void process(UseContext context) {
+        String scene = context.getScene();
+        UseProcess useProcess = map.get(scene);
+        ActionErrorEnum.classNotExist.assertNonNull(useProcess);
+        useProcess.process(context);
+    }
+}

+ 96 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/internal/DefaultUseProcess.java

@@ -0,0 +1,96 @@
+/*
+ * 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.region.internal;
+
+import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.mmo.bag.ItemIdConst;
+import com.iohao.mmo.bag.entity.BagItem;
+import com.iohao.mmo.bag.mapper.ItemMapper;
+import com.iohao.mmo.bag.pojo.UseItemPOJO;
+import com.iohao.mmo.bag.pojo.UsePOJO;
+import com.iohao.mmo.bag.region.UseContext;
+import com.iohao.mmo.bag.region.UseProcess;
+import com.iohao.mmo.bag.service.BagService;
+import com.iohao.mmo.level.client.LevelExchange;
+import com.iohao.mmo.level.proto.ExpMessage;
+import lombok.AllArgsConstructor;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+/**
+ * 默认的处理物品实现类
+ * <pre>
+ *     1 将物品从背包中减少
+ *     2 执行物品功效
+ * </pre>
+ *
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@Component
+@AllArgsConstructor
+public class DefaultUseProcess implements UseProcess {
+
+    final BagService bagService;
+
+    @Override
+    public void process(UseContext context) {
+        UsePOJO usePOJO = context.getUsePOJO();
+        Map<String, UseItemPOJO> useItemMap = usePOJO.useItemMap;
+        ActionErrorEnum.dataNotExist.assertTrueThrows(useItemMap.isEmpty());
+
+        FlowContext flowContext = context.getFlowContext();
+        long userId = flowContext.getUserId();
+
+        UseItemPOJO useItem = usePOJO.getUseItem();
+
+        // 减少物品
+        BagItem bagItem = ItemMapper.ME.convert(useItem);
+        bagService.decrementItem(bagItem, userId);
+
+        /*
+         * 这里暂时写死,开发者也可以通过 map 映射 itemId 的方式来扩展。
+         *
+         * 之前计划在第一阶段上规则引擎来做相关业务的,但对于新手来说可能过于复杂了;
+         * 由于我们第一阶段的道具内容并不多,使用 if 这样的代码,对于新手来说也更好理解。
+         */
+
+        String itemId = useItem.itemId;
+        if (ItemIdConst.expId.equals(itemId)) {
+            extractedExp(useItem, flowContext);
+            return;
+        }
+    }
+
+    private static void extractedExp(UseItemPOJO useItem, FlowContext flowContext) {
+        ExpMessage expMessage = new ExpMessage();
+        expMessage.id = flowContext.getUserId();
+        // 暂时写死
+        expMessage.exp = 20 * useItem.quantity;
+        // 调用添加经验值 api
+        LevelExchange.addExpPerson(expMessage, flowContext);
+    }
+
+    @Override
+    public String getScene() {
+        return "default";
+    }
+}

+ 35 - 0
logic/bag-logic/src/main/java/com/iohao/mmo/bag/region/internal/ExpItemProcess.java

@@ -0,0 +1,35 @@
+/*
+ * 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.region.internal;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.FieldDefaults;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@Getter
+@Setter
+@FieldDefaults(level = AccessLevel.PRIVATE)
+public class ExpItemProcess {
+
+}

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

@@ -21,6 +21,7 @@ package com.iohao.mmo.bag.service;
 import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
 import com.iohao.mmo.bag.entity.Bag;
 import com.iohao.mmo.bag.entity.BagItem;
+import com.iohao.mmo.bag.region.UseContext;
 import com.iohao.mmo.bag.repository.BagRepository;
 import com.iohao.mmo.common.config.GameCode;
 import lombok.AllArgsConstructor;
@@ -107,4 +108,8 @@ public class BagService {
 
         return bagItem;
     }
+
+    public void use(UseContext context) {
+
+    }
 }

+ 1 - 1
pom.xml

@@ -58,7 +58,7 @@
         <encoding>UTF-8</encoding>
 
         <!-- 项目版本 -->
-        <ioGame.version>17.1.51</ioGame.version>
+        <ioGame.version>17.1.52</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>

+ 1 - 1
provide/bag-provide/pom.xml

@@ -26,7 +26,7 @@
 
         <dependency>
             <groupId>com.iohao.mmo</groupId>
-            <artifactId>item-provide</artifactId>
+            <artifactId>level-provide</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
     </dependencies>

+ 43 - 0
provide/bag-provide/src/main/java/com/iohao/mmo/bag/proto/CreateEquip.java

@@ -0,0 +1,43 @@
+/*
+ * 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.proto;
+
+import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
+import lombok.AccessLevel;
+import lombok.ToString;
+import lombok.experimental.FieldDefaults;
+
+/**
+ * 打造装备协议
+ *
+ * @author 渔民小镇
+ * @date 2023-08-07
+ */
+@ToString
+@ProtobufClass
+@FieldDefaults(level = AccessLevel.PUBLIC)
+public class CreateEquip {
+    /** 打造的装备部位,这个 itemId 是系统的所有物品表 */
+    String itemId;
+    /**
+     * 极品率,就是生成装备时,相对极品的概率。
+     * 因为玩家可能使用更好的材料,而在装备模块的角度,是不关心使用的是什么材料
+     */
+    int excellentRate;
+}

+ 2 - 2
provide/bag-provide/src/main/java/com/iohao/mmo/bag/proto/UseMessage.java

@@ -35,8 +35,8 @@ import java.util.List;
 @ProtobufClass
 @FieldDefaults(level = AccessLevel.PUBLIC)
 public class UseMessage {
-    /** 业务操作 */
-    int operation;
+    /** 业务场景 */
+    String scene;
     /** 使用多个物品 */
     List<UseItemMessage> useItems;
 

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

@@ -46,4 +46,14 @@ public class ExchangeKit {
         InvokeModuleContext invokeModuleContext = brokerClientContext.getInvokeModuleContext();
         return invokeModuleContext.invokeModuleMessage(requestMessage);
     }
+
+    public void invokeModuleVoidMessage(FlowContext flowContext, CmdInfo cmdInfo, Object data) {
+        RequestMessage requestMessage = flowContext
+                .getRequest()
+                .createRequestMessage(cmdInfo, data);
+
+        BrokerClientContext brokerClientContext = flowContext.option(FlowAttr.brokerClientContext);
+        InvokeModuleContext invokeModuleContext = brokerClientContext.getInvokeModuleContext();
+        invokeModuleContext.invokeModuleVoidMessage(requestMessage);
+    }
 }

+ 40 - 0
provide/level-provide/src/main/java/com/iohao/mmo/level/client/LevelExchange.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.level.client;
+
+import com.iohao.game.action.skeleton.core.CmdInfo;
+import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.mmo.common.provide.client.ExchangeKit;
+import com.iohao.mmo.level.cmd.LevelCmd;
+import com.iohao.mmo.level.proto.ExpMessage;
+import lombok.experimental.UtilityClass;
+
+/**
+ * 等级模块,对外提供的访问 api
+ *
+ * @author 渔民小镇
+ * @date 2023-08-06
+ */
+@UtilityClass
+public class LevelExchange {
+    public void addExpPerson(ExpMessage expMessage, FlowContext flowContext) {
+        CmdInfo cmdInfo = LevelCmd.of(LevelCmd.personAddExp);
+        ExchangeKit.invokeModuleVoidMessage(flowContext, cmdInfo, expMessage);
+    }
+}