Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简化元附加信息的使用 #30

Closed
iohao opened this issue Dec 11, 2022 · 1 comment
Closed

简化元附加信息的使用 #30

iohao opened this issue Dec 11, 2022 · 1 comment
Assignees
Labels
Support extension;可扩展 Support extension;可扩展 trick;使用技巧 trick;使用技巧

Comments

@iohao
Copy link
Owner

iohao commented Dec 11, 2022

简化元附加信息的使用

在处理 action 时,我们可以通过 FlowContext.userId 可以很方便得到当前用户(玩家)id。

如果开发者想在处理 action 时,携带上一些自定义的信息时,可以通过元附加信息特性来完成。

比如保存当前玩家的英雄角色 id,或者玩家的昵称,又或者是你的项目的 userId 是 string 或其他类型则可以通过元信息这一特性来兼容。

简单的说,就是你想在 FlowContext 中获取一些用户(玩家)特有的信息数据时,可以通过这个特性来实现。

新增 元信息接口 Attachment

/**
 * 元信息接口
 * <pre>
 *     注意:
 *     框架默认使用的是 protobuf 编解码,所以建议子类添加 ProtobufClass 注解
 * </pre>
 *
 * @author 渔民小镇
 * @date 2022-12-11
 */
public interface Attachment extends Serializable {
    /**
     * get userId
     *
     * @return userId
     */
    long getUserId();
}
@iohao
Copy link
Owner Author

iohao commented Dec 11, 2022

使用文档 https://www.yuque.com/iohao/game/sw1y8u

使用示例

// 自定义一个元信息类,实现 Attachment 元附加信息接口
@ProtobufClass
@FieldDefaults(level = AccessLevel.PUBLIC)
public class MyAttachment implements Attachment {
    @Getter
    long userId;
    String nickname;
}

@ActionController(1)
public class DemoAction {
    @ActionMethod(1)
    public void attachment(FlowContext flowContext) {
    	// 创建自定义的元附加信息对象
        MyAttachment myAttachment = new MyAttachment();
        myAttachment.userId = flowContext.getUserId();
        myAttachment.nickname = "英雄无敌3";
    	
        // 设置元信息 ----- 关键代码
        ExternalCommunicationKit.setAttachment(myAttachment);
    }

    @ActionMethod(2)
    public MyAttachment printAttachment(FlowContext flowContext) {
    	// 得到元信息,这个是在上面的方法中设置的元信息对象
        var attachment = flowContext.getAttachment(MyAttachment.class);

        return attachment;
    }
}

@iohao iohao closed this as completed Dec 12, 2022
iohao added a commit that referenced this issue Dec 14, 2022
命令解析器与源码文档逻辑分离。
优化命令对象,减少 action 类的实例化对象
#30 简化元附加信息的使用
@iohao iohao self-assigned this Feb 1, 2023
@iohao iohao added trick;使用技巧 trick;使用技巧 Support extension;可扩展 Support extension;可扩展 labels Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Support extension;可扩展 Support extension;可扩展 trick;使用技巧 trick;使用技巧
Projects
None yet
Development

No branches or pull requests

1 participant