Skip to content

Commit

Permalink
refactor(kit): RandomKit add randomLong method
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Dec 27, 2024
1 parent 4b7e011 commit 58e14a4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.random.RandomGenerator;

/**
Expand All @@ -38,7 +37,6 @@ public class RandomKit {
*
* @param limit 限制随机数的范围,不包括这个数
* @return 随机数
* @see Random#nextInt(int)
*/
public int randomInt(int limit) {
return generator.nextInt(limit);
Expand All @@ -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);
}

/**
* 获得指定范围内的随机数
*
Expand All @@ -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 值
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

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;
import io.netty.util.TimerTask;
import lombok.Getter;
import lombok.experimental.UtilityClass;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.*;
import java.util.function.Supplier;
Expand Down Expand Up @@ -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-");
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.Serial;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
Expand Down Expand Up @@ -74,6 +75,8 @@ public class SimpleRoom implements Room {

public SimpleRoom() {
// 为房间设置通讯接口
aggregationContext = BrokerClientHelper.getBrokerClient().getCommunicationAggregationContext();
if (Objects.nonNull(BrokerClientHelper.getBrokerClient())) {
aggregationContext = BrokerClientHelper.getBrokerClient().getCommunicationAggregationContext();
}
}
}

0 comments on commit 58e14a4

Please sign in to comment.