Przeglądaj źródła

:whale: 人物、英雄初始化

渔民小镇 1 rok temu
rodzic
commit
05e116328c

+ 0 - 6
logic/a-logic-common/pom.xml

@@ -27,12 +27,6 @@
             <version>${project.parent.version}</version>
         </dependency>
 
-        <dependency>
-            <groupId>io.github.lmlx66</groupId>
-            <artifactId>yitter-idgenerator-spring-boot-starter</artifactId>
-            <version>1.1.2-RELEASE</version>
-        </dependency>
-
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-mongodb</artifactId>

+ 76 - 0
logic/person-logic/src/main/java/com/iohao/mmo/hero/service/HeroService.java

@@ -0,0 +1,76 @@
+/*
+ * 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.hero.service;
+
+import com.iohao.mmo.person.entity.BasicsProperty;
+import com.iohao.mmo.person.entity.Hero;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author 渔民小镇
+ * @date 2023-07-27
+ */
+@Service
+public class HeroService {
+
+    MongoTemplate mongoTemplate;
+
+    public HeroService(MongoTemplate mongoTemplate) {
+        this.mongoTemplate = mongoTemplate;
+        initHeroData();
+    }
+
+    public Hero getFirstHero() {
+        // 得到第一个英雄,即使后期我们有几十个英雄,也初始化这一个(类似新手英雄)
+        String heroId = "hero-1";
+        return mongoTemplate.findById(heroId, Hero.class);
+    }
+
+    private void initHeroData() {
+        // 初始化一些英雄数配置,后期将移到 excel 中做。
+
+        Hero hero = new Hero();
+        hero.setId("hero-1");
+        hero.setName("阿里克斯");
+
+        BasicsProperty basicProperty = getInitBasicProperty();
+        hero.setBasicsProperty(basicProperty);
+
+        mongoTemplate.save(hero);
+    }
+
+    private BasicsProperty getInitBasicProperty() {
+        BasicsProperty basicProperty = new BasicsProperty();
+        basicProperty.setHp(200);
+        basicProperty.setMp(200);
+        basicProperty.setPhysicsAttack(60);
+        basicProperty.setPhysicsDefense(20);
+
+        basicProperty.setMagicAttack(10);
+        basicProperty.setMagicDefense(10);
+
+        basicProperty.setTreatAttack(20);
+        basicProperty.setSealAttack(20);
+        basicProperty.setSealDefense(10);
+        basicProperty.setSpeed(50);
+        basicProperty.setAnger(0);
+        return basicProperty;
+    }
+}

+ 4 - 2
logic/person-logic/src/main/java/com/iohao/mmo/person/action/PersonAction.java

@@ -23,7 +23,6 @@ import com.iohao.game.action.skeleton.annotation.ActionMethod;
 import com.iohao.game.action.skeleton.core.flow.FlowContext;
 import com.iohao.mmo.person.cmd.PersonCmd;
 import com.iohao.mmo.person.proto.PersonMessage;
-import com.iohao.mmo.person.service.PersonService;
 import lombok.extern.slf4j.Slf4j;
 
 /**
@@ -35,7 +34,10 @@ import lombok.extern.slf4j.Slf4j;
 @Slf4j
 @ActionController(PersonCmd.cmd)
 public class PersonAction {
-    PersonService personService = new PersonService();
+
+    public void createPerson() {
+
+    }
 
     @ActionMethod(PersonCmd.getPerson)
     public PersonMessage getPersonMessage(FlowContext flowContext) {

+ 3 - 0
logic/person-logic/src/main/java/com/iohao/mmo/person/entity/Hero.java

@@ -34,5 +34,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
 public class Hero {
     @Id
     String id;
+    /** 英雄名 */
+    String name;
+    /** 基础属性 */
     BasicsProperty basicsProperty;
 }

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

@@ -18,31 +18,68 @@
  */
 package com.iohao.mmo.person.service;
 
+import com.iohao.mmo.hero.service.HeroService;
 import com.iohao.mmo.person.entity.BasicsProperty;
+import com.iohao.mmo.person.entity.Hero;
 import com.iohao.mmo.person.entity.Person;
-import lombok.AccessLevel;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.experimental.FieldDefaults;
+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.List;
+import java.util.Objects;
 
 /**
  * @author 渔民小镇
  * @date 2023-07-24
  */
-@Getter
-@Setter
-@FieldDefaults(level = AccessLevel.PRIVATE)
+@Service
 public class PersonService {
-    public Person getPersonEntity(String userId) {
-        //  临时数据
-        BasicsProperty basicProperty = getBasicPropertyTemp();
+    final MongoTemplate mongoTemplate;
+
+    final HeroService heroService;
+
+    public PersonService(MongoTemplate mongoTemplate, HeroService heroService) {
+        this.mongoTemplate = mongoTemplate;
+        this.heroService = heroService;
+    }
+
+    public void initPerson(long userId) {
+        Query query = new Query(Criteria.where("userId").is(userId));
+        Person person = mongoTemplate.findOne(query, Person.class);
+
+        if (Objects.nonNull(person)) {
+            return;
+        }
+
+        createPerson(userId);
+    }
+
+    private void createPerson(long userId) {
+        // 人物基础属性
+        BasicsProperty basicProperty = getInitBasicProperty();
+
+        Hero firstHero = heroService.getFirstHero();
+
+        BasicsProperty plus = basicProperty.plus(firstHero.getBasicsProperty());
+
+        firstHero.setBasicsProperty(plus);
 
         Person person = new Person();
+        person.setId(String.valueOf(userId));
         person.setBasicProperty(basicProperty);
-        return person;
+        person.setHeroes(List.of(firstHero));
+        person.setCurrentHero(firstHero);
+
+        mongoTemplate.save(person);
+    }
+
+    public Person getPersonById(long userId) {
+        return mongoTemplate.findById(String.valueOf(userId), Person.class);
     }
 
-    public static BasicsProperty getBasicPropertyTemp() {
+    private BasicsProperty getInitBasicProperty() {
         BasicsProperty basicProperty = new BasicsProperty();
         basicProperty.setHp(100);
         basicProperty.setMp(100);

+ 13 - 71
logic/person-logic/src/test/java/com/iohao/mmo/person/entity/PersonTest.java

@@ -1,20 +1,12 @@
 package com.iohao.mmo.person.entity;
 
-import com.hy.corecode.idgen.WFGIdGenerator;
+import com.iohao.mmo.hero.service.HeroService;
 import com.iohao.mmo.person.service.PersonService;
 import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.data.mongodb.core.MongoTemplate;
-import org.springframework.data.mongodb.core.query.Criteria;
-import org.springframework.data.mongodb.core.query.Query;
-
-import java.util.List;
-import java.util.Objects;
-
-import static org.springframework.data.mongodb.core.query.Criteria.where;
 
 /**
  * @author 渔民小镇
@@ -25,73 +17,23 @@ import static org.springframework.data.mongodb.core.query.Criteria.where;
 public class PersonTest {
     @Resource
     MongoTemplate mongoTemplate;
-    @Autowired
-    private WFGIdGenerator idGenerator;
-
-    @Test
-    void test0() {
-        for (int i = 0; i < 10; i++) {
-            long next = idGenerator.next();
-            log.info("next : {}", next);
-        }
-
-    }
+    @Resource
+    HeroService heroService;
+    @Resource
+    PersonService personService;
 
     @Test
-    void test1() {
-
-        BasicsProperty person = new BasicsProperty();
-        person.setId("abc");
-        person.setAnger(1);
-
-        mongoTemplate.save(person);
-
-
-        Criteria criteria = where("anger").is(1);
-        Query query = new Query(criteria);
-
-        List<BasicsProperty> entities = mongoTemplate.find(query, BasicsProperty.class);
-
-        log.info("students : {}", entities.size());
-        for (BasicsProperty person1 : entities) {
-            System.out.println(person1);
-        }
+    void testHero() {
+        Hero firstHero = heroService.getFirstHero();
+        log.info("firstHero : {}", firstHero);
     }
-
+    
     @Test
     void testPerson() {
-        BasicsProperty basicProperty = PersonService.getBasicPropertyTemp();
-
-        Hero hero = getHero(basicProperty);
-
-        String heroId = "hero-1";
-        Hero byId = mongoTemplate.findById(heroId, Hero.class);
-        Objects.requireNonNull(byId);
-        BasicsProperty plus = basicProperty.plus(byId.getBasicsProperty());
-
-        Hero currentHero = new Hero();
-        currentHero.setId(heroId);
-        currentHero.setBasicsProperty(plus);
-
-        Person person = new Person();
-        person.setId("person-1");
-        person.setBasicProperty(basicProperty);
-        person.setHeroes(List.of(hero));
-        person.setCurrentHero(currentHero);
-
-        mongoTemplate.save(person);
-    }
-
-    private Hero getHero(BasicsProperty basicProperty) {
-        Hero hero = new Hero();
-        hero.setId("hero-1");
-        hero.setBasicsProperty(basicProperty);
-        mongoTemplate.save(hero);
+        long userId = 1;
+        personService.initPerson(userId);
+        Person personById = personService.getPersonById(userId);
 
-        Hero hero2 = new Hero();
-        hero2.setId("hero-2");
-        hero2.setBasicsProperty(basicProperty);
-        mongoTemplate.save(hero2);
-        return hero;
+        log.info("personById : {}", personById);
     }
 }

+ 1 - 0
provide/person-provide/src/main/java/com/iohao/mmo/person/cmd/PersonCmd.java

@@ -27,4 +27,5 @@ import com.iohao.mmo.common.provide.cmd.CmdModule;
 public interface PersonCmd {
     int cmd = CmdModule.personCmd;
     int getPerson = 1;
+    int createPerson = 2;
 }