Files
bl/common/core/global.go
昔念 741ef6ebd4 feat(common): 重构数据包处理逻辑并添加全局 ID 生成器
- 引入 idgenerator-go 库,实现全局唯一 ID 生成
- 重构 Pack 函数,使用接口参数提高灵活性
- 修改 Player 结构,增加 MainConn 字段用于主连接
- 更新 SocketHandler_Tomee 中的 Data 字段标记
- 优化 Recv 函数中的数据解包和参数处理逻辑
2025-06-27 22:40:49 +08:00

32 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package core
import (
"fmt"
"sync"
"github.com/gogf/gf/v2/util/gconv"
"github.com/yitter/idgenerator-go/idgen"
)
var (
Mainplayer sync.Map //玩家数据
Maincmdcache sync.Map //命令缓存
)
func init() {
// 创建 IdGeneratorOptions 对象,可在构造函数中输入 WorkerId
tt := gconv.Uint16(1)
var options = idgen.NewIdGeneratorOptions(tt)
// options.WorkerIdBitLength = 10 // 默认值6限定 WorkerId 最大值为2^6-1即默认最多支持64个节点。
// options.SeqBitLength = 6; // 默认值6限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
// options.BaseTime = Your_Base_Time // 如果要兼容老系统的雪花算法此处应设置为老系统的BaseTime。
// ...... 其它参数参考 IdGeneratorOptions 定义。
// 保存参数(务必调用,否则参数设置不生效):
idgen.SetIdGenerator(options)
newId := idgen.NextId()
fmt.Println(newId, "初始化雪花算法")
}