diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/kit/RangeBroadcast.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/kit/RangeBroadcast.java index 23db033a0..e22871305 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/kit/RangeBroadcast.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/kit/RangeBroadcast.java @@ -22,7 +22,6 @@ import com.iohao.game.action.skeleton.core.flow.FlowContext; import com.iohao.game.action.skeleton.core.flow.attr.FlowAttr; import com.iohao.game.action.skeleton.protocol.ResponseMessage; -import com.iohao.game.common.kit.CollKit; import com.iohao.game.common.kit.exception.ThrowKit; import lombok.AccessLevel; import lombok.Getter; @@ -270,7 +269,7 @@ protected void trick() { * 广播数据 */ protected void broadcast() { - boolean emptyUser = CollKit.isEmpty(this.userIds); + boolean emptyUser = this.userIds.isEmpty(); if (checkEmptyUser && emptyUser) { // 请添加消息推送人 ThrowKit.ofRuntimeException("Please add a message sender"); diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/RandomKit.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/RandomKit.java index 254f94ab8..8d5a650bf 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/RandomKit.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/RandomKit.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.random.RandomGenerator; /** @@ -38,7 +37,6 @@ public class RandomKit { * * @param limit 限制随机数的范围,不包括这个数 * @return 随机数 - * @see Random#nextInt(int) */ public int randomInt(int limit) { return generator.nextInt(limit); @@ -55,6 +53,29 @@ public int randomInt(int min, int max) { return generator.nextInt(min, max); } + /** + * 获得指定范围内的随机数 [0,limit) + * + * @param limit 限制随机数的范围,不包括这个数 + * @return 随机数 + * @since 21.23 + */ + public long randomLong(long limit) { + return generator.nextLong(limit); + } + + /** + * 获得指定范围内的随机数 + * + * @param min 最小数(包含) + * @param max 最大数(不包含) + * @return 随机数 + * @since 21.23 + */ + public long randomLong(long min, long max) { + return generator.nextLong(min, max); + } + /** * 获得指定范围内的随机数 * @@ -76,6 +97,29 @@ public int random(int end) { return generator.nextInt(end + 1); } + /** + * 获得指定范围内的随机数 + * + * @param start 开始值(包含) + * @param end 结束值(包含) + * @return 随机数 + * @since 21.23 + */ + public long random(long start, long end) { + return start + generator.nextLong(end - start + 1); + } + + /** + * 获得指定范围内的随机数 (0 ~ end) + * + * @param end 结束值(包含) + * @return 随机数 + * @since 21.23 + */ + public long random(long end) { + return generator.nextLong(end + 1); + } + /** * 随机一个 bool 值 * diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/OnceTaskListener.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/OnceTaskListener.java index ced78d5d6..1299c8e21 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/OnceTaskListener.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/OnceTaskListener.java @@ -18,10 +18,10 @@ */ package com.iohao.game.common.kit.concurrent; +import com.iohao.game.common.kit.MoreKit; import io.netty.util.Timeout; import io.netty.util.TimerTask; -import java.util.Objects; import java.util.concurrent.Executor; /** @@ -66,14 +66,8 @@ public interface OnceTaskListener extends TimerTask, TaskListener { @Override default void run(Timeout timeout) throws Exception { - Executor executor = this.getExecutor(); - - if (Objects.nonNull(executor)) { - executor.execute(this::executeFlow); - } else { - this.executeFlow(); - } + MoreKit.execute(executor, this::executeFlow); } private void executeFlow() { diff --git a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/TaskKit.java b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/TaskKit.java index 7d1ecc5b0..5509157a4 100644 --- a/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/TaskKit.java +++ b/common/common-micro-kit/src/main/java/com/iohao/game/common/kit/concurrent/TaskKit.java @@ -20,6 +20,8 @@ import com.iohao.game.common.kit.CollKit; import com.iohao.game.common.kit.ExecutorKit; +import com.iohao.game.common.kit.MoreKit; +import com.iohao.game.common.kit.RuntimeKit; import com.iohao.game.common.kit.collect.SetMultiMap; import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; @@ -27,7 +29,6 @@ import lombok.Getter; import lombok.experimental.UtilityClass; -import java.util.Objects; import java.util.Set; import java.util.concurrent.*; import java.util.function.Supplier; @@ -144,7 +145,7 @@ public class TaskKit { private final HashedWheelTimer wheelTimer = new HashedWheelTimer(); /** 内置的 cacheExecutor 执行器 */ @Getter - final ExecutorService cacheExecutor = ExecutorKit.newCacheThreadPool("ioGameThread-"); + final ExecutorService cacheExecutor = ExecutorKit.newFixedThreadPool(RuntimeKit.availableProcessors2n, "ioGameThread-"); /** 虚拟线程执行器 */ @Getter final ExecutorService virtualExecutor = ExecutorKit.newVirtualExecutor("ioGameVirtual-"); @@ -275,13 +276,8 @@ public void run(Timeout timeout) { set.forEach(intervalTaskListener -> { var executor = intervalTaskListener.getExecutor(); - // 如果指定了执行器,就将执行流程放到执行器中,否则使用当前线程 - if (Objects.nonNull(executor)) { - executor.execute(() -> executeFlowTimerListener(intervalTaskListener, set)); - } else { - executeFlowTimerListener(intervalTaskListener, set); - } + MoreKit.execute(executor, () -> executeFlowTimerListener(intervalTaskListener, set)); }); TaskKit.newTimeout(this, tick, timeUnit); diff --git a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java index 12821aace..44cf5a09f 100644 --- a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java +++ b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java @@ -19,7 +19,7 @@ package com.iohao.game.widget.light.room; import com.iohao.game.common.kit.PresentKit; -import com.iohao.game.common.kit.concurrent.timer.delay.DelayTaskKit; +import com.iohao.game.common.kit.concurrent.TaskKit; import com.iohao.game.widget.light.room.flow.RoomCreateContext; import com.iohao.game.widget.light.room.operation.OperationContext; import com.iohao.game.widget.light.room.operation.OperationHandler; @@ -492,11 +492,11 @@ default void executeTask(Runnable task) { /** * Delayed execution of tasks, this method is thread-safe * - * @param task task - * @param millis millis + * @param task task + * @param delayMilliseconds delayMilliseconds * @since 21.23 */ - default void executeDelayTask(Runnable task, long millis) { - DelayTaskKit.of(() -> this.executeTask(task)).plusTimeMillis(millis).task(); + default void executeDelayTask(Runnable task, long delayMilliseconds) { + TaskKit.runOnceMillis(() -> this.executeTask(task), delayMilliseconds); } } \ No newline at end of file diff --git a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java index 2f21b3458..19bf75b72 100644 --- a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java +++ b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java @@ -29,6 +29,7 @@ import java.io.Serial; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; /** @@ -74,6 +75,8 @@ public class SimpleRoom implements Room { public SimpleRoom() { // 为房间设置通讯接口 - aggregationContext = BrokerClientHelper.getBrokerClient().getCommunicationAggregationContext(); + if (Objects.nonNull(BrokerClientHelper.getBrokerClient())) { + aggregationContext = BrokerClientHelper.getBrokerClient().getCommunicationAggregationContext(); + } } } \ No newline at end of file