Bladeren bron

工具类尾缀改为Kit,提取工具类通用部分到common下

tangbin 1 jaar geleden
bovenliggende
commit
0754662eca

+ 65 - 0
logic/a-logic-common/src/main/java/com/iohao/mmo/common/kit/RandomKit.java

@@ -0,0 +1,65 @@
+package com.iohao.mmo.common.kit;
+
+import org.springframework.stereotype.Component;
+import java.math.BigDecimal;
+import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * @author 唐斌
+ * @ClassName RandomKit
+ * @description: 随机概率工具
+ *  常见四种概率算法:
+ *      1.常规做法,直接配置概率,程序直接判定
+ *      2.在1的基础上,加个保底次数,当连续不发生的次数高于保底时,强制发生
+ *      3.设置基础概率,事件不发生概率翻倍
+ *      4.设置数组,将事件发生概率变成数组元素
+ * @date 2023年07月26日
+ * @version: 1.0
+ */
+@Component
+public class RandomKit {
+
+    private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
+
+    /**
+     * 通过概率随机返回本次是否命中
+     *
+     * @param chance 命中概率 0.000000 - 1.000000
+     * @return boolean 是否命中
+     */
+    public static boolean isLuck(BigDecimal chance) {
+        int chanceInt = chance.multiply(new BigDecimal("1000000")).intValue();
+        int randomNum = RANDOM.nextInt(1000000);
+        return randomNum <= chanceInt;
+    }
+
+    /**
+     * 在范围内随机一个值(小数)
+     *
+     * @param rangeMin 限定范围的下限
+     * @param rangeMax 限定范围的上限
+     * @return boolean
+     */
+    public static BigDecimal randomFromArr(BigDecimal rangeMin,BigDecimal rangeMax) {
+        Random random = new Random();
+        return rangeMin.add(
+                        rangeMax.subtract(rangeMin)
+                                .multiply(BigDecimal.valueOf(random.nextDouble()))
+                );
+    }
+
+    /**
+     * 在范围内随机一个值(整数)
+     *
+     * @param randomMin 限定范围的最小值(int)
+     * @param randomMax 限定范围的最大值(int)
+     * @return boolean
+     */
+    public static int randomFromInt(int randomMin,int randomMax) {
+        return randomFromArr(BigDecimal.valueOf(randomMin),
+                BigDecimal.valueOf(randomMax))
+                .intValue();
+
+    }
+}

+ 12 - 12
logic/equip-logic/src/main/java/com/iohao/mmo/equip/entity/FixedEquipProperty.java

@@ -18,7 +18,7 @@
  */
 package com.iohao.mmo.equip.entity;
 
-import com.iohao.mmo.equip.utils.RandomUtils;
+import com.iohao.mmo.common.kit.RandomKit;
 import lombok.AccessLevel;
 import lombok.Builder;
 import lombok.Data;
@@ -68,37 +68,37 @@ public class FixedEquipProperty {
             return FixedEquipProperty.builder().build();
         return FixedEquipProperty.builder()
                 .hp(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getHp(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getHp(),
                                 fixedEquipPropertyMax.getHp()))
                 .mp(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getMp(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getMp(),
                                 fixedEquipPropertyMax.getMp()))
                 .physicsAttack(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getPhysicsAttack(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsAttack(),
                                 fixedEquipPropertyMax.getPhysicsAttack()))
                 .physicsDefense(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getPhysicsDefense(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getPhysicsDefense(),
                                 fixedEquipPropertyMax.getMagicDefense()))
                 .magicAttack(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getMagicAttack(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicAttack(),
                                 fixedEquipPropertyMax.getMagicAttack()))
                 .magicDefense(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getMagicDefense(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getMagicDefense(),
                                 fixedEquipPropertyMax.getMagicDefense()))
                 .treatAttack(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getTreatAttack(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getTreatAttack(),
                                 fixedEquipPropertyMax.getTreatAttack()))
                 .sealAttack(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getSealAttack(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getSealAttack(),
                                 fixedEquipPropertyMax.getSealAttack()))
                 .sealDefense(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getSealDefense(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getSealDefense(),
                                 fixedEquipPropertyMax.getSealDefense()))
                 .speed(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getSpeed(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getSpeed(),
                                 fixedEquipPropertyMax.getSpeed()))
                 .anger(
-                        RandomUtils.randomFromInt(fixedEquipPropertyMin.getAnger(),
+                        RandomKit.randomFromInt(fixedEquipPropertyMin.getAnger(),
                                 fixedEquipPropertyMax.getAnger()))
                 .build();
     }

+ 7 - 55
logic/equip-logic/src/main/java/com/iohao/mmo/equip/utils/RandomUtils.java → logic/equip-logic/src/main/java/com/iohao/mmo/equip/kit/EquipRandomKit.java

@@ -1,14 +1,13 @@
-package com.iohao.mmo.equip.utils;
+package com.iohao.mmo.equip.kit;
 
+import com.iohao.mmo.common.kit.RandomKit;
 import org.springframework.stereotype.Component;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.util.*;
-import java.util.concurrent.ThreadLocalRandom;
 
 /**
- * @author kioor
- * @ClassName RandomUtils
+ * @author 唐斌
+ * @ClassName EquipRandomKit
  * @description: 随机概率工具
  *  常见四种概率算法:
  *      1.常规做法,直接配置概率,程序直接判定
@@ -19,60 +18,13 @@ import java.util.concurrent.ThreadLocalRandom;
  * @version: 1.0
  */
 @Component
-public class RandomUtils {
+public class EquipRandomKit {
 
     /** 极品率 0.0000-1.0000*/
     public static BigDecimal excellentRate = BigDecimal.valueOf(0.1000);
     /** 极品范围占比*/
     public static BigDecimal excellentProp = BigDecimal.valueOf(0.2000);
 
-    private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
-
-    ////////////////////////// ↓↓↓算法实现↓↓↓ ///////////////////////////
-    /**
-     * 通过概率随机获得本次是否命中的结果
-     *
-     * @param chance 概率 0.000000 - 1.000000
-     * @return boolean
-     */
-    public static boolean isLuck(BigDecimal chance) {
-        int chanceInt = chance.multiply(new BigDecimal("1000000")).intValue();
-        int randomNum = RANDOM.nextInt(1000000);
-        return randomNum <= chanceInt;
-    }
-
-    /**
-     * 在范围内随机一个值(小数)
-     *
-     * @param rangeMin 限定范围的下限
-     * @param rangeMax 限定范围的上限
-     * @return boolean
-     */
-    public static BigDecimal randomFromArr(BigDecimal rangeMin,BigDecimal rangeMax) {
-        Random random = new Random();
-        BigDecimal randomValue =
-                rangeMin.add(
-                        rangeMax.subtract(rangeMin)
-                                .multiply(new BigDecimal(random.nextDouble()))
-                );
-        return randomValue;
-    }
-
-
-    /**
-     * 在范围内随机一个值(整数)
-     *
-     * @param randomMin 限定范围的最小值(int)
-     * @param randomMax 限定范围的最大值(int)
-     * @return boolean
-     */
-    public static int randomFromInt(int randomMin,int randomMax) {
-        BigDecimal rangeMin = new BigDecimal(randomMin);
-        BigDecimal rangeMax = new BigDecimal(randomMax);
-        return RandomUtils.randomFromArr(rangeMin,rangeMax).intValue();
-
-    }
-
     /**
      * 根据是否极品,在范围内随机一个值
      *
@@ -104,7 +56,7 @@ public class RandomUtils {
                 rangeMax = rangeMin;
             }
         }
-        return RandomUtils.randomFromArr(rangeMin,rangeMax).intValue();
+        return RandomKit.randomFromArr(rangeMin,rangeMax).intValue();
 
     }
 
@@ -116,6 +68,6 @@ public class RandomUtils {
      * @return boolean
      */
     public static boolean isExcellent(BigDecimal addExcellentRate) {
-        return isLuck(excellentRate.add(addExcellentRate));
+        return RandomKit.isLuck(excellentRate.add(addExcellentRate));
     }
 }

+ 3 - 3
logic/equip-logic/src/main/java/com/iohao/mmo/equip/service/EquipService.java

@@ -21,7 +21,7 @@ package com.iohao.mmo.equip.service;
 import com.iohao.mmo.common.config.GameCode;
 import com.iohao.mmo.equip.entity.ElseEquipProperty;
 import com.iohao.mmo.equip.entity.Equip;
-import com.iohao.mmo.equip.utils.RandomUtils;
+import com.iohao.mmo.equip.kit.EquipRandomKit;
 import lombok.AllArgsConstructor;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
@@ -117,11 +117,11 @@ public class EquipService {
         int randomMin = equip.getTotalAttrMin();
         int randomMax = equip.getTotalAttrMax();
         //本次是否生成极品
-        boolean excellentFlag = RandomUtils.isExcellent(excellentRate);
+        boolean excellentFlag = EquipRandomKit.isExcellent(excellentRate);
         //品质
         equip.setQuality(excellentFlag?2:1);
         //额外属性值
-        int newAttrTotal = RandomUtils.randomFromExcellent(randomMin,randomMax,excellentFlag);
+        int newAttrTotal = EquipRandomKit.randomFromExcellent(randomMin,randomMax,excellentFlag);
         // 直接替换掉原装备的属性值
         return replaceEquipAttr(equip,newAttrTotal);
     }

+ 0 - 2
logic/equip-logic/src/main/java/com/iohao/mmo/equip/service/EquipTemplateService.java

@@ -21,13 +21,11 @@ package com.iohao.mmo.equip.service;
 import com.iohao.mmo.common.config.GameCode;
 import com.iohao.mmo.common.provide.item.ItemTypeIdConst;
 import com.iohao.mmo.equip.entity.*;
-import com.iohao.mmo.equip.utils.RandomUtils;
 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.math.BigDecimal;
 import java.util.List;
 
 /**