Merge remote-tracking branch 'origin/main'

This commit is contained in:
wangwenhua 2025-09-18 13:26:47 +08:00
commit 2b9d599b85
6 changed files with 31 additions and 70 deletions

View File

@ -19,8 +19,8 @@ public class RoomManager {
private static final Map<String, Room> roomsMap = new ConcurrentHashMap<>();
public static void startRoom(String id, Scenario scenario,long time) {
Room room = new Room(id,scenario);
public static void startRoom(String id, Scenario scenario, long time) {
Room room = new Room(id, scenario);
roomsMap.put(id, room);
room.start(time);
}
@ -52,4 +52,12 @@ public class RoomManager {
room.addAction(time, action);
}
}
public static long getRoomDuringTime(String id) {
Room room = roomsMap.get(id);
if (room != null) {
return room.getDuringTime();
}
return 0;
}
}

View File

@ -27,16 +27,16 @@ import org.springframework.web.reactive.function.client.WebClient;
public abstract class AbtParentTask implements TaskAction {
protected final AtomicLong duringTime = new AtomicLong(0);
protected final ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
protected ScheduledFuture<?> scheduledFuture;
//任务数据
protected final ScenarioTask scenarioTask;
//房间ID
protected final String roomId;
//http请求
protected WebClient webClient = WebClient.create();
ThreadPoolExecutor executor = new ThreadPoolExecutor(
//线程池
protected ThreadPoolExecutor executor = new ThreadPoolExecutor(
5, // 核心线程数
10, // 最大线程数
60L, // 空闲线程存活时间
@ -52,23 +52,10 @@ public abstract class AbtParentTask implements TaskAction {
}
protected void start() {
scheduledFuture = schedule.scheduleAtFixedRate(() -> {
ScenarioWsParam scenarioWsParam = Global.roomParamMap.get(
scenarioTask.getScenarioId() + "_" + roomId);
if (scenarioWsParam == null) {
duringTime.getAndSet(1);
} else {
duringTime.getAndSet(scenarioWsParam.getMag());
}
}, 0, 1, TimeUnit.SECONDS);
}
protected abstract void finished();
protected abstract void setMag(int mag);
@Override
public void doSomeThing() {
@ -85,13 +72,9 @@ public abstract class AbtParentTask implements TaskAction {
return scenarioTask.getTaskType();
}
public void cancelAllTask() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}
}
protected abstract void business();
}
// 自定义线程工厂

View File

@ -23,23 +23,9 @@ public class BattleRootTask extends AbtParentTask {
public void doSomeThing() {
//会知道想定ID, resourceId;
}
//固定间隔执行
@Override
protected void business() {
// Global.roomParamMap.get(scenarioTask.getScenarioId() + "," + this.roomId); 可以获取当前想定的步长
//战斗需要消耗弹药 按时间消耗30秒
//人员消耗
//可能会产生需求保障需求
}
@Override
protected void finished() {
}
@Override
protected void setMag(int mag) {
}
}

View File

@ -11,6 +11,9 @@ import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
@ -26,7 +29,9 @@ import org.springframework.core.env.Environment;
@Slf4j
public class MoveRootTask extends AbtParentTask implements TaskAction {
protected final ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
protected ScheduledFuture<?> scheduledFuture;
private final double SPEED = 170;
private double accumulatedDistance = 0;
@ -84,21 +89,10 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
}
@Override
protected void business() {
}
@Override
protected void finished() {
}
@Override
protected void setMag(int mag) {
}
}

View File

@ -10,10 +10,8 @@ import com.hivekion.scenario.bean.ScenarioWsParam;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -111,7 +109,12 @@ public class Room implements AutoCloseable {
startTask();
}
public long getDuringTime() {
return duringTime.get();
}
public long getTotalTime() {
return totalTime.get();
}
// 启动定时任务
private void startTask() {
@ -134,10 +137,6 @@ public class Room implements AutoCloseable {
});
});
// 全部执行后再清空
actions.clear();
}
}, 0, 1, TimeUnit.SECONDS);

View File

@ -18,24 +18,15 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
super(scenarioTask,roomId);
}
@Override
protected void finished() {
}
@Override
public void doSomeThing() {
}
@Override
protected void business() {
}
@Override
protected void setMag(int mag) {
}
}