模拟系统创建房间,RoomCreateContext 的使用
twniuniu opened this issue · comments
twniuniu commented
你的问题 | 使用场景
现在框架提供的 RoomCreateContext.of 需要指定 FlowContext 对象,但想模拟系统创建房间时无法实现。比如在系统初始化时,使用 CommandLineRunner 进行创建房间,此时是没有 FlowContext 的。
预期值
是否可以提供一个不需要 FlowContext 就能创建 RoomCreateContext 的方法,如
long systemUserId = -1;
RoomCreateContext.of(systemUserId);
版本
- ioGame version: 21.8
渔民小镇 commented
好的,下版本提供一个 RoomCreateContext .of()
的重载,无需任何参数的。
渔民小镇 commented
RoomCreateContext of 增加重载方法
RoomCreateContext.of(); // 无房间创建者,通常表示系统创建
RoomCreateContext.of(userId); // 房间创建者为 userId
开发者也可以基于 RoomCreateContext 接口做扩展实现。
渔民小镇 commented
... 省略部分代码
public void test() {
long userId = 1;
... 省略部分代码
// 创建房间
RoomCreateContext roomCreateContext = RoomCreateContext.of(userId)
// 设置房间空间大小:最大可容纳 3 人,开始游戏时最少需要 2 人
.setSpaceSize(3, 2)
// 自定义房间玩法规则
.option(FightAttr.rule, rule);
FightRoomEntity newRoom = this.roomService.createRoom(roomCreateContext);
}
public class FightRoomService implements GameRoomService {
@Override
public FightRoomEntity createRoom(RoomCreateContext createContext) {
long roomId = roomIdCounter.incrementAndGet();
var room = new FightRoomEntity();
room.setRoomId(roomId);
room.setRoomCreateContext(createContext);
room.setSpaceSize(createContext.getSpaceSize());
// 保存房间
this.addRoom(room);
return room;
}
}