iohao / ioGame

无锁异步化、事件驱动架构设计的 java netty 网络编程框架; 轻量级,无需依赖任何第三方中间件或数据库就能支持集群、分布式; 适用于网络游戏服务器、物联网、内部系统及各种需要长连接的场景; 通过 ioGame 你可以很容易的搭建出一个集群无中心节点、集群自动化、分布式的网络服务器;FXGL、Unity、UE、Cocos Creator、Godot、Netty、Protobuf、webSocket、tcp、socket;java Netty 游戏服务器框架;

Home Page:http://game.iohao.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

模拟系统创建房间,RoomCreateContext 的使用

twniuniu opened this issue · comments

你的问题 | 使用场景

现在框架提供的 RoomCreateContext.of 需要指定 FlowContext 对象,但想模拟系统创建房间时无法实现。比如在系统初始化时,使用 CommandLineRunner 进行创建房间,此时是没有 FlowContext 的。

预期值

是否可以提供一个不需要 FlowContext 就能创建 RoomCreateContext 的方法,如

long systemUserId = -1;

RoomCreateContext.of(systemUserId);

版本

  • ioGame version: 21.8

好的,下版本提供一个 RoomCreateContext .of() 的重载,无需任何参数的。

RoomCreateContext of 增加重载方法

RoomCreateContext.of(); // 无房间创建者,通常表示系统创建
RoomCreateContext.of(userId); // 房间创建者为 userId

开发者也可以基于 RoomCreateContext 接口做扩展实现。

... 省略部分代码
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;
    }
}