渔民小镇 1 سال پیش
والد
کامیت
6fcf5cb6f0
21فایلهای تغییر یافته به همراه221 افزوده شده و 65 حذف شده
  1. 2 1
      common/common-core/src/main/java/com/iohao/mmo/common/config/GameCode.java
  2. 2 2
      common/common-core/src/main/java/com/iohao/mmo/common/core/flow/MyAttachment.java
  3. 0 15
      common/common-core/src/main/java/com/iohao/mmo/common/core/flow/MyFlowContext.java
  4. 41 7
      logic/level-logic/src/main/java/com/iohao/mmo/level/action/LevelAction.java
  5. 59 0
      logic/level-logic/src/main/java/com/iohao/mmo/level/config/LevelCommandLineRunner.java
  6. 4 0
      logic/level-logic/src/main/java/com/iohao/mmo/level/entity/Level.java
  7. 16 21
      logic/level-logic/src/main/java/com/iohao/mmo/level/entity/PersonLevelConfig.java
  8. 0 6
      logic/level-logic/src/main/java/com/iohao/mmo/level/repository/LevelRepository.java
  9. 8 0
      logic/level-logic/src/main/java/com/iohao/mmo/level/service/LevelService.java
  10. 2 0
      logic/person-logic/pom.xml
  11. 29 3
      logic/person-logic/src/main/java/com/iohao/mmo/person/action/PersonAction.java
  12. 1 1
      logic/person-logic/src/main/java/com/iohao/mmo/person/entity/Person.java
  13. 2 2
      logic/person-logic/src/main/java/com/iohao/mmo/person/service/PersonService.java
  14. 1 0
      one-client/pom.xml
  15. 7 0
      provide/common-provide/pom.xml
  16. 21 2
      provide/level-provide/src/main/java/com/iohao/mmo/level/client/LevelInputCommandRegion.java
  17. 10 3
      provide/level-provide/src/main/java/com/iohao/mmo/level/cmd/LevelCmd.java
  18. 3 1
      provide/level-provide/src/main/java/com/iohao/mmo/level/proto/LevelMessage.java
  19. 6 0
      provide/person-provide/pom.xml
  20. 4 1
      provide/person-provide/src/main/java/com/iohao/mmo/person/client/PersonInputCommandRegion.java
  21. 3 0
      provide/person-provide/src/main/java/com/iohao/mmo/person/proto/PersonMessage.java

+ 2 - 1
common/common-core/src/main/java/com/iohao/mmo/common/config/GameCode.java

@@ -32,7 +32,8 @@ import lombok.experimental.FieldDefaults;
 public enum GameCode implements MsgExceptionInfo {
 
     /** 地图不存在 */
-    MAP_NOT_EXIST(-1, "地图不存在");
+    MAP_NOT_EXIST(1, "地图不存在"),
+    UP_LEVEL_ERROR(2, "升级错误");
 
     /** 消息码 */
     final int code;

+ 2 - 2
common/common-core/src/main/java/com/iohao/mmo/common/core/flow/MyAttachment.java

@@ -22,6 +22,7 @@ import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
 import com.iohao.game.core.common.client.Attachment;
 import lombok.AccessLevel;
 import lombok.Getter;
+import lombok.ToString;
 import lombok.experimental.FieldDefaults;
 
 /**
@@ -30,14 +31,13 @@ import lombok.experimental.FieldDefaults;
  * @author 渔民小镇
  * @date 2023-07-24
  */
+@ToString
 @ProtobufClass
 @FieldDefaults(level = AccessLevel.PUBLIC)
 public class MyAttachment implements Attachment {
     @Getter
     long userId;
 
-    /** mongoDB 的用户唯一 id */
-    String objectId;
     /** 昵称 */
     String nickname;
 }

+ 0 - 15
common/common-core/src/main/java/com/iohao/mmo/common/core/flow/MyFlowContext.java

@@ -30,21 +30,6 @@ import java.util.Objects;
 public class MyFlowContext extends FlowContext {
     private MyAttachment attachment;
 
-    /**
-     * mongoDB 的用户唯一 id
-     *
-     * @return userId String
-     */
-    public String getObjectId() {
-        initAttachment();
-
-        if (attachment == null) {
-            return null;
-        }
-
-        return attachment.objectId;
-    }
-
     public String getNickname() {
         initAttachment();
 

+ 41 - 7
logic/level-logic/src/main/java/com/iohao/mmo/level/action/LevelAction.java

@@ -20,10 +20,15 @@ package com.iohao.mmo.level.action;
 
 import com.iohao.game.action.skeleton.annotation.ActionController;
 import com.iohao.game.action.skeleton.annotation.ActionMethod;
+import com.iohao.game.action.skeleton.core.CmdInfo;
+import com.iohao.game.action.skeleton.core.commumication.BroadcastContext;
 import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
-import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.bolt.broker.core.client.BrokerClientHelper;
+import com.iohao.mmo.common.config.GameCode;
+import com.iohao.mmo.common.core.flow.MyFlowContext;
 import com.iohao.mmo.level.cmd.LevelCmd;
 import com.iohao.mmo.level.entity.Level;
+import com.iohao.mmo.level.entity.PersonLevelConfig;
 import com.iohao.mmo.level.mapper.LevelMapper;
 import com.iohao.mmo.level.proto.ExpMessage;
 import com.iohao.mmo.level.proto.LevelMessage;
@@ -50,16 +55,32 @@ public class LevelAction {
      *
      * @param expMessage 经验值
      */
-    @ActionMethod(LevelCmd.addExp)
-    public LevelMessage internalAddExp(ExpMessage expMessage) {
+    @ActionMethod(LevelCmd.personAddExp)
+    public void internalAddExpPerson(ExpMessage expMessage) {
         // internal 打头的方法名表示内部方法,只能由内部调用
-        long id = expMessage.id;
+        long userId = expMessage.id;
         int exp = expMessage.exp;
 
-        Level level = levelService.ofLevel(id);
+        Level level = levelService.ofLevel(userId);
         level.addExp(exp);
         levelService.save(level);
 
+        // 推送经验值给玩家
+        LevelMessage levelMessage = LevelMapper.ME.convert(level);
+        BroadcastContext broadcastContext = BrokerClientHelper.getBroadcastContext();
+        CmdInfo cmdInfo = LevelCmd.getCmdInfo(LevelCmd.personAddExp);
+        broadcastContext.broadcast(cmdInfo, levelMessage, userId);
+    }
+
+    /**
+     * 得到等级信息
+     *
+     * @param id id
+     * @return LevelMessage
+     */
+    @ActionMethod(LevelCmd.ofLevel)
+    public LevelMessage internalOfLevel(long id) {
+        Level level = levelService.ofLevel(id);
         return LevelMapper.ME.convert(level);
     }
 
@@ -85,10 +106,23 @@ public class LevelAction {
      *
      * @param flowContext flowContext
      */
-    @ActionMethod(LevelCmd.upLevel)
-    public void upLevel(FlowContext flowContext) {
+    @ActionMethod(LevelCmd.personUpLevel)
+    public LevelMessage upLevelPerson(MyFlowContext flowContext) {
         long userId = flowContext.getUserId();
+
+        Level level = levelService.ofLevel(userId);
+        PersonLevelConfig personLevelConfig = levelService.getPersonLevelConfigByLevel(level.getLevel());
+
         // 检测经验是否足够
+        int configExp = personLevelConfig.getExp();
+        long exp = level.getExp();
+        GameCode.UP_LEVEL_ERROR.assertTrue(exp >= configExp);
 
+        //  达到升级条件
+        level.addExp(-configExp);
+        level.incrementLevel();
+        levelService.save(level);
+        // TODO: 将来添加恢复状态相关业务
+        return LevelMapper.ME.convert(level);
     }
 }

+ 59 - 0
logic/level-logic/src/main/java/com/iohao/mmo/level/config/LevelCommandLineRunner.java

@@ -0,0 +1,59 @@
+/*
+ * 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.config;
+
+import com.iohao.mmo.level.entity.PersonLevelConfig;
+import jakarta.annotation.Resource;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-07-30
+ */
+@Component
+public class LevelCommandLineRunner implements CommandLineRunner {
+    @Resource
+    MongoTemplate mongoTemplate;
+
+    @Override
+    public void run(String... args) {
+        initConfigExcel();
+    }
+
+    private void initConfigExcel() {
+        mongoTemplate.dropCollection(PersonLevelConfig.class);
+
+        List<PersonLevelConfig> configList = new ArrayList<>();
+        for (int i = 1; i < 100; i++) {
+            PersonLevelConfig config = new PersonLevelConfig();
+            configList.add(config);
+
+            config.setLevel(i);
+            config.setExp(i * 10);
+        }
+
+        mongoTemplate.insert(configList, PersonLevelConfig.class);
+
+    }
+}

+ 4 - 0
logic/level-logic/src/main/java/com/iohao/mmo/level/entity/Level.java

@@ -53,4 +53,8 @@ public class Level {
     public void addExp(int exp) {
         this.exp += exp;
     }
+
+    public void incrementLevel() {
+        this.level++;
+    }
 }

+ 16 - 21
provide/level-provide/src/main/java/com/iohao/mmo/level/proto/ExpTargetEnum.java → logic/level-logic/src/main/java/com/iohao/mmo/level/entity/PersonLevelConfig.java

@@ -16,31 +16,26 @@
  * 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.proto;
+package com.iohao.mmo.level.entity;
 
-import com.baidu.bjf.remoting.protobuf.EnumReadable;
+import lombok.AccessLevel;
+import lombok.Data;
+import lombok.experimental.FieldDefaults;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
 
 /**
- * 目标类型:人物、宠物
- *
  * @author 渔民小镇
  * @date 2023-07-30
  */
-public enum ExpTargetEnum implements EnumReadable {
-    /** 人物 */
-    PERSON(0, "人物"),
-    /** 宠物 */
-    PET(1, "宠物");
-    final int value;
-    final String name;
-
-    ExpTargetEnum(int value, String name) {
-        this.value = value;
-        this.name = name;
-    }
-
-    @Override
-    public int value() {
-        return value;
-    }
+@Data
+@Document
+@FieldDefaults(level = AccessLevel.PRIVATE)
+public class PersonLevelConfig {
+    @Id
+    String id;
+    /** 等级 */
+    int level;
+    /** 升级所需经验值 */
+    int exp;
 }

+ 0 - 6
logic/level-logic/src/main/java/com/iohao/mmo/level/repository/LevelRepository.java

@@ -22,17 +22,11 @@ import com.iohao.mmo.level.entity.Level;
 import org.springframework.data.repository.CrudRepository;
 import org.springframework.stereotype.Repository;
 
-import java.util.Optional;
-
 /**
  * @author 渔民小镇
  * @date 2023-07-30
  */
 @Repository
 public interface LevelRepository extends CrudRepository<Level, Long> {
-    <S extends Level> S save(S entity);
-
-    Optional<Level> findById(long primaryKey);
 
-    boolean existsById(long primaryKey);
 }

+ 8 - 0
logic/level-logic/src/main/java/com/iohao/mmo/level/service/LevelService.java

@@ -19,9 +19,12 @@
 package com.iohao.mmo.level.service;
 
 import com.iohao.mmo.level.entity.Level;
+import com.iohao.mmo.level.entity.PersonLevelConfig;
 import com.iohao.mmo.level.repository.LevelRepository;
 import lombok.AllArgsConstructor;
 import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.stereotype.Service;
 
 import java.util.Objects;
@@ -59,4 +62,9 @@ public class LevelService {
     public void save(Level level) {
         mongoTemplate.save(level);
     }
+
+    public PersonLevelConfig getPersonLevelConfigByLevel(int level) {
+        Query query = new Query(Criteria.where("level").is(level));
+        return mongoTemplate.findOne(query, PersonLevelConfig.class);
+    }
 }

+ 2 - 0
logic/person-logic/pom.xml

@@ -32,6 +32,8 @@
             <version>${project.parent.version}</version>
         </dependency>
 
+
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>

+ 29 - 3
logic/person-logic/src/main/java/com/iohao/mmo/person/action/PersonAction.java

@@ -20,7 +20,14 @@ package com.iohao.mmo.person.action;
 
 import com.iohao.game.action.skeleton.annotation.ActionController;
 import com.iohao.game.action.skeleton.annotation.ActionMethod;
+import com.iohao.game.action.skeleton.core.CmdInfo;
+import com.iohao.game.action.skeleton.core.commumication.InvokeModuleContext;
 import com.iohao.game.action.skeleton.core.flow.FlowContext;
+import com.iohao.game.bolt.broker.core.client.BrokerClientHelper;
+import com.iohao.mmo.common.annotation.IoThread;
+import com.iohao.mmo.common.core.flow.MyFlowContext;
+import com.iohao.mmo.level.cmd.LevelCmd;
+import com.iohao.mmo.level.proto.LevelMessage;
 import com.iohao.mmo.person.cmd.PersonCmd;
 import com.iohao.mmo.person.entity.Person;
 import com.iohao.mmo.person.mapper.PersonMapper;
@@ -30,6 +37,8 @@ import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
+import java.util.concurrent.CompletableFuture;
+
 /**
  * 人物相关 action
  *
@@ -62,14 +71,31 @@ public class PersonAction {
      * @param flowContext flowContext
      * @return 人物信息
      */
+    @IoThread
     @ActionMethod(PersonCmd.getPerson)
-    public PersonMessage getPerson(FlowContext flowContext) {
-        long userId = flowContext.getUserId();
+    public PersonMessage getPerson(MyFlowContext flowContext) {
+        // 等级
+        CompletableFuture<LevelMessage> future = CompletableFuture.supplyAsync(() -> {
+            // 路由:这个路由是将要访问逻辑服的路由(表示你将要去的地方)
+            CmdInfo ofLevelCmd = LevelCmd.getCmdInfo(LevelCmd.ofLevel);
+            // 游戏逻辑服通讯上下文
+            InvokeModuleContext invokeModuleContext = BrokerClientHelper.getInvokeModuleContext();
+            // 根据路由信息来请求其他子服务器(其他逻辑服)的数据
+            return invokeModuleContext.invokeModuleMessageData(ofLevelCmd, LevelMessage.class);
+        });
 
         // 由于没写注册,暂时这样创建人物信息
         internalInitPerson(flowContext);
 
+        long userId = flowContext.getUserId();
         Person person = personService.getPersonById(userId);
-        return PersonMapper.ME.convert(person);
+        PersonMessage personMessage = PersonMapper.ME.convert(person);
+
+        future.thenAccept(levelMessage -> {
+            // 回调写法
+            personMessage.level = levelMessage;
+        });
+
+        return personMessage;
     }
 }

+ 1 - 1
logic/person-logic/src/main/java/com/iohao/mmo/person/entity/Person.java

@@ -36,7 +36,7 @@ import java.util.List;
 @FieldDefaults(level = AccessLevel.PRIVATE)
 public class Person {
     @Id
-    String id;
+    long id;
     /** 基本属性 */
     BasicProperty basicProperty;
     /** 英雄列表 */

+ 2 - 2
logic/person-logic/src/main/java/com/iohao/mmo/person/service/PersonService.java

@@ -63,7 +63,7 @@ public class PersonService {
         firstHero.setBasicProperty(plus);
 
         Person person = new Person();
-        person.setId(String.valueOf(userId));
+        person.setId(userId);
         person.setBasicProperty(basicProperty);
         person.setHeroList(List.of(firstHero));
         person.setCurrentHero(firstHero);
@@ -72,7 +72,7 @@ public class PersonService {
     }
 
     public Person getPersonById(long userId) {
-        return mongoTemplate.findById(String.valueOf(userId), Person.class);
+        return mongoTemplate.findById(userId, Person.class);
     }
 
     private BasicProperty getInitBasicProperty() {

+ 1 - 0
one-client/pom.xml

@@ -43,6 +43,7 @@
             <version>${logback.version}</version>
         </dependency>
 
+
     </dependencies>
 
 </project>

+ 7 - 0
provide/common-provide/pom.xml

@@ -31,5 +31,12 @@
             <artifactId>light-jprotobuf</artifactId>
             <version>${ioGame.version}</version>
         </dependency>
+
+        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
+        <dependency>
+            <groupId>com.alibaba.fastjson2</groupId>
+            <artifactId>fastjson2</artifactId>
+            <version>2.0.38</version>
+        </dependency>
     </dependencies>
 </project>

+ 21 - 2
provide/level-provide/src/main/java/com/iohao/mmo/level/client/LevelInputCommandRegion.java

@@ -35,6 +35,20 @@ public class LevelInputCommandRegion extends AbstractInputCommandRegion {
     @Override
     public void initInputCommand() {
         this.inputCommandCreate.cmd = LevelCmd.cmd;
+        request();
+
+        listen();
+
+    }
+
+    private void listen() {
+        listenBroadcast(LevelMessage.class, result -> {
+            LevelMessage value = result.getValue();
+            log.info("value : {}", value);
+        }, LevelCmd.personAddExp, "玩家经验值增加");
+    }
+
+    private void request() {
         InputRequestData inputRequestData;
 
         ofCommand(LevelCmd.getLevel).callback(LevelMessage.class, result -> {
@@ -51,10 +65,15 @@ public class LevelInputCommandRegion extends AbstractInputCommandRegion {
             return expMessage;
         };
 
-        ofCommand(LevelCmd.addExp).callback(LevelMessage.class, result -> {
+        ofCommand(LevelCmd.personAddExp).callback(LevelMessage.class, result -> {
+            LevelMessage value = result.getValue();
+            log.info("value : {}", value);
+        }).setDescription("添加玩家经验值").setInputRequestData(inputRequestData);
+
+        ofCommand(LevelCmd.personUpLevel).callback(LevelMessage.class, result -> {
             LevelMessage value = result.getValue();
             log.info("value : {}", value);
-        }).setDescription("添加经验值").setInputRequestData(inputRequestData);
+        }).setDescription("人物升级").setInputRequestData(inputRequestData);
 
     }
 }

+ 10 - 3
provide/level-provide/src/main/java/com/iohao/mmo/level/cmd/LevelCmd.java

@@ -18,6 +18,7 @@
  */
 package com.iohao.mmo.level.cmd;
 
+import com.iohao.game.action.skeleton.core.CmdInfo;
 import com.iohao.mmo.common.provide.cmd.CmdModule;
 
 /**
@@ -27,9 +28,15 @@ import com.iohao.mmo.common.provide.cmd.CmdModule;
 public interface LevelCmd {
     int cmd = CmdModule.levelCmd;
     /** 添加经验值 */
-    int addExp = 1;
+    int personAddExp = 1;
     /** 等级信息 */
     int getLevel = 2;
-    /** 升级 */
-    int upLevel = 3;
+    /** 人物升级 */
+    int personUpLevel = 3;
+    /** 得到等级信息,没有则创建 */
+    int ofLevel = 4;
+
+    static CmdInfo getCmdInfo(int subCmd) {
+        return CmdInfo.getCmdInfo(cmd, subCmd);
+    }
 }

+ 3 - 1
provide/level-provide/src/main/java/com/iohao/mmo/level/proto/LevelMessage.java

@@ -24,6 +24,8 @@ import lombok.ToString;
 import lombok.experimental.FieldDefaults;
 
 /**
+ * 等级信息
+ *
  * @author 渔民小镇
  * @date 2023-07-30
  */
@@ -38,7 +40,7 @@ public class LevelMessage {
      * </pre>
      */
     long id;
-    /** 经验值 */
+    /** 当前经验值 */
     long exp;
     /** 当前等级 */
     int level;

+ 6 - 0
provide/person-provide/pom.xml

@@ -24,5 +24,11 @@
             <artifactId>common-provide</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.iohao.mmo</groupId>
+            <artifactId>level-provide</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
     </dependencies>
 </project>

+ 4 - 1
provide/person-provide/src/main/java/com/iohao/mmo/person/client/PersonInputCommandRegion.java

@@ -18,6 +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.person.cmd.PersonCmd;
 import com.iohao.mmo.person.proto.PersonMessage;
@@ -35,7 +37,8 @@ public class PersonInputCommandRegion extends AbstractInputCommandRegion {
 
         ofCommand(PersonCmd.getPerson).callback(PersonMessage.class, result -> {
             PersonMessage value = result.getValue();
-            log.info("value : {}", value);
+            String jsonFormat = JSON.toJSONString(value, JSONWriter.Feature.PrettyFormat);
+            log.info("人物信息 : {}", jsonFormat);
         }).setDescription("得到人物、英雄信息");
 
     }

+ 3 - 0
provide/person-provide/src/main/java/com/iohao/mmo/person/proto/PersonMessage.java

@@ -20,6 +20,7 @@ package com.iohao.mmo.person.proto;
 
 import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
 import com.iohao.mmo.hero.proto.HeroMessage;
+import com.iohao.mmo.level.proto.LevelMessage;
 import lombok.AccessLevel;
 import lombok.ToString;
 import lombok.experimental.FieldDefaults;
@@ -38,6 +39,8 @@ import java.util.List;
 public class PersonMessage {
     /** 玩家id */
     long userId;
+    /** 等级信息 */
+    LevelMessage level;
     /** 基本属性 */
     BasicPropertyMessage basicProperty;
     /** 英雄列表 */