Documentation ¶
Index ¶
- Constants
- Variables
- func ConvertInterfaceToRealType(typ reflect.Type, v interface{}) interface{}
- func ConvertStringToRealType(typ reflect.Type, v string) interface{}
- func ConvertValueToInt(srcType reflect.Type, v reflect.Value) int64
- func ConvertValueToInterface(srcType, dstType reflect.Type, srcValue reflect.Value) interface{}
- func FixEntityDataFromCache(entity Entity, db EntityDb, kvCache KvCache, cacheKeyPrefix string, ...)
- func GetChildCacheKey(parentName, childName string) string
- func GetComponentSaveData(component Component) (interface{}, error)
- func GetComponentSaveName(component Component) string
- func GetEntityCacheKey(prefix string, entityId interface{}) string
- func GetEntityComponentCacheKey(prefix string, entityId interface{}, componentName string) string
- func GetEntityComponentChildCacheKey(prefix string, entityId interface{}, componentName string, childName string) string
- func GetEntitySaveData(entity Entity, componentDatas map[string]interface{})
- func GetFieldValue(obj reflect.Value, fieldName string) reflect.Value
- func GetSaveData(obj any, parentName string) (interface{}, error)
- func IsDuplicateKeyError(err error) bool
- func IsRedisError(redisError error) bool
- func LoadEntityData(entity Entity, entityData interface{}) error
- func LoadFromCache(obj interface{}, kvCache KvCache, cacheKey string, parentObj any) (bool, error)
- func LoadObjData(obj any, sourceData interface{}) error
- func LogStack()
- func MapDel[M ~map[K]V, K comparable, V any](mapDirtyMark MapDirtyMark, m M, k K)
- func MapSet[M ~map[K]V, K comparable, V any](mapDirtyMark MapDirtyMark, m M, k K, v V)
- func ParseEntitySaveableStruct(entity Entity)
- func SaveChangedDataToCache(kvCache KvCache, obj any, cacheKeyName string, saveableField *SaveableField)
- func SaveComponentChangedDataToCache(kvCache KvCache, cacheKeyPrefix string, entityKey interface{}, ...)
- func SaveEntityChangedDataToDb(entityDb EntityDb, entity Entity, kvCache KvCache, removeCacheAfterSaveDb bool, ...) error
- func SaveEntityChangedDataToDbByKey(entityDb EntityDb, entity Entity, entityKey interface{}, kvCache KvCache, ...) error
- func SaveMapValueToCache(kvCache KvCache, cacheKeyName string, val reflect.Value, ...)
- func SaveObjectChangedDataToCache(kvCache KvCache, parentCacheKey string, obj any)
- func SaveValueToCache(kvCache KvCache, cacheKeyName string, val reflect.Value)
- func Set[Field cmp.Ordered](obj DirtyMark, field *Field, value Field)
- func SetApplication(application Application)
- func SetFn(obj DirtyMark, setFieldValueFn func())
- func SetLogLevel(level int8)
- func SetLogger(w Logger, level int8)
- type Application
- type ApplicationHook
- type BaseComponent
- type BaseDirtyMark
- type BaseEntity
- func (this *BaseEntity) AddComponent(component Component)
- func (this *BaseEntity) AddEventReceiver(eventReceiver EventReceiver)
- func (this *BaseEntity) GetComponentByIndex(componentIndex int) Component
- func (this *BaseEntity) GetComponentByName(componentName string) Component
- func (this *BaseEntity) GetComponents() []Component
- func (this *BaseEntity) GetId() int64
- func (this *BaseEntity) RangeComponent(fun func(component Component) bool)
- func (this *BaseEntity) RangeEventReceiver(f func(eventReceiver EventReceiver) bool)
- func (this *BaseEntity) SaveCache(kvCache KvCache, cacheKeyPrefix string, entityKey interface{}) error
- type BaseMapDirtyMark
- func (this *BaseMapDirtyMark) HasCached() bool
- func (this *BaseMapDirtyMark) IsChanged() bool
- func (this *BaseMapDirtyMark) IsDirty() bool
- func (this *BaseMapDirtyMark) RangeDirtyMap(f func(dirtyKey interface{}, isAddOrUpdate bool))
- func (this *BaseMapDirtyMark) ResetChanged()
- func (this *BaseMapDirtyMark) ResetDirty()
- func (this *BaseMapDirtyMark) SetCached()
- func (this *BaseMapDirtyMark) SetDirty(k interface{}, isAddOrUpdate bool)
- type BaseRoutineEntity
- type Component
- type ComponentCtor
- type ComponentRegister
- type ComponentRegisterInfo
- type DataComponent
- type DbMgr
- type DirtyMark
- type DistributedEntityHelper
- type DistributedEntityMgr
- func (this *DistributedEntityMgr) DeleteDistributeLocks()
- func (this *DistributedEntityMgr) DistributeLock(entityId int64) bool
- func (this *DistributedEntityMgr) DistributeUnlock(entityId int64)
- func (this *DistributedEntityMgr) GetEntity(entityId int64) RoutineEntity
- func (this *DistributedEntityMgr) GetEntityDb() EntityDb
- func (this *DistributedEntityMgr) LoadEntity(entityId int64, entityData interface{}) RoutineEntity
- func (this *DistributedEntityMgr) Range(f func(entity RoutineEntity) bool)
- func (this *DistributedEntityMgr) ReBalance()
- func (this *DistributedEntityMgr) StopAll()
- type Entity
- type EntityDb
- type EventHandlerInfo
- type EventHandlerMgr
- type EventReceiver
- type InterfaceMapLoader
- type KvCache
- type KvDb
- type Logger
- type MapComponent
- type MapData
- type MapDataComponent
- type MapDirtyMark
- type MapValueDirtyMark
- type MongoCollection
- func (this *MongoCollection) CreateIndex(key string, unique bool)
- func (this *MongoCollection) DeleteComponentField(entityKey interface{}, componentName string, fieldName ...string) error
- func (this *MongoCollection) DeleteEntity(entityKey interface{}) error
- func (this *MongoCollection) FindEntityById(entityKey interface{}, data interface{}) (bool, error)
- func (this *MongoCollection) GetCollection() *mongo.Collection
- func (this *MongoCollection) InsertEntity(entityKey interface{}, entityData interface{}) (err error, isDuplicateKey bool)
- func (this *MongoCollection) SaveComponent(entityKey interface{}, componentName string, componentData interface{}) error
- func (this *MongoCollection) SaveComponentField(entityKey interface{}, componentName string, fieldName string, ...) error
- func (this *MongoCollection) SaveComponents(entityKey interface{}, components map[string]interface{}) error
- func (this *MongoCollection) SaveEntity(entityKey interface{}, entityData interface{}) error
- func (this *MongoCollection) Shard() error
- type MongoCollectionPlayer
- func (this *MongoCollectionPlayer) FindAccountIdByPlayerId(playerId int64) (int64, error)
- func (this *MongoCollectionPlayer) FindPlayerByAccountId(accountId int64, regionId int32, playerData interface{}) (bool, error)
- func (this *MongoCollectionPlayer) FindPlayerIdByAccountId(accountId int64, regionId int32) (int64, error)
- func (this *MongoCollectionPlayer) FindPlayerIdsByAccountId(accountId int64, regionId int32) ([]int64, error)
- type MongoDb
- func (this *MongoDb) Connect() bool
- func (this *MongoDb) Disconnect()
- func (this *MongoDb) GetEntityDb(name string) EntityDb
- func (this *MongoDb) GetKvDb(name string) KvDb
- func (this *MongoDb) GetMongoClient() *mongo.Client
- func (this *MongoDb) GetMongoDatabase() *mongo.Database
- func (this *MongoDb) RegisterEntityDb(collectionName string, hashedShardKey bool, uniqueId string) EntityDb
- func (this *MongoDb) RegisterKvDb(collectionName string, hashedShardKey bool, keyName, valueName string) KvDb
- func (this *MongoDb) RegisterPlayerDb(collectionName string, hashedShardKey bool, playerId, accountId, region string) PlayerDb
- func (this *MongoDb) ShardCollection(collectionFullName, keyName string, hashedShardKey bool) error
- func (this *MongoDb) ShardDatabase(dbName string) error
- type MongoKvDb
- func (this *MongoKvDb) Delete(key interface{}) error
- func (this *MongoKvDb) Find(key interface{}) (interface{}, error)
- func (this *MongoKvDb) FindAndDecode(key interface{}, decodeData interface{}) error
- func (this *MongoKvDb) GetCollection() *mongo.Collection
- func (this *MongoKvDb) Inc(key interface{}, value interface{}, upsert bool) (interface{}, error)
- func (this *MongoKvDb) Insert(key interface{}, value interface{}) (err error, isDuplicateKey bool)
- func (this *MongoKvDb) Shard() error
- func (this *MongoKvDb) Update(key interface{}, value interface{}, upsert bool) error
- type PlayerDb
- type ProtoData
- type RedisCache
- func (this *RedisCache) Del(key ...string) (int64, error)
- func (this *RedisCache) Get(key string) (string, error)
- func (this *RedisCache) GetMap(key string, m interface{}) error
- func (this *RedisCache) GetProto(key string, value proto.Message) error
- func (this *RedisCache) HDel(key string, fields ...string) (int64, error)
- func (this *RedisCache) HGetAll(key string) (map[string]string, error)
- func (this *RedisCache) HSet(key string, values ...interface{}) (int64, error)
- func (this *RedisCache) HSetNX(key, field string, value interface{}) (bool, error)
- func (this *RedisCache) Set(key string, value interface{}, expiration time.Duration) error
- func (this *RedisCache) SetMap(k string, m interface{}) error
- func (this *RedisCache) SetNX(key string, value interface{}, expiration time.Duration) (bool, error)
- func (this *RedisCache) Type(key string) (string, error)
- type RoutineEntity
- type RoutineEntityRoutineArgs
- type Saveable
- type SaveableDirtyMark
- type SaveableField
- type SaveableStruct
- type Sharding
- type SliceData
- type StdLogger
- type TimerEntries
- func (this *TimerEntries) AddTimer(t time.Time, f TimerJob)
- func (this *TimerEntries) After(d time.Duration, f TimerJob)
- func (this *TimerEntries) GetMinInterval() time.Duration
- func (this *TimerEntries) GetTimeOffset() time.Duration
- func (this *TimerEntries) Now() time.Time
- func (this *TimerEntries) Run(now time.Time) bool
- func (this *TimerEntries) SetMinInterval(minInterval time.Duration)
- func (this *TimerEntries) SetTimeOffset(timeOffset time.Duration)
- func (this *TimerEntries) Start()
- func (this *TimerEntries) Stop()
- func (this *TimerEntries) TimerChan() <-chan time.Time
- type TimerJob
Constants ¶
const ( DebugLevel int8 = iota - 1 InfoLevel WarnLevel ErrorLevel )
日志级别,参考zap
log level
Variables ¶
var ( ErrNotSaveableStruct = errors.New("not saveable struct") ErrUnsupportedKeyType = errors.New("unsupported key type") ErrUnsupportedType = errors.New("unsupported type") ErrNotConnected = errors.New("not connected") ErrSliceElemType = errors.New("slice elem type error") ErrArrayLen = errors.New("array len error") ErrNoUniqueColumn = errors.New("no uniqueId column") ErrRouteServerId = errors.New("route serverId error") ErrEntityNotExists = errors.New("entity not exists") ErrConvertRoutineMessage = errors.New("convert routine message error") ErrSourceDataType = errors.New("sourceData type error") ErrNotSaveable = errors.New("not saveable") )
var ( // 单个保存字段的关键字 KeywordDb = "db" // 子字段的关键字 KeywordChild = "child" // 明文保存的关键字 KeywordPlain = "plain" )
定义数据的关键字,允许应用层自行修改
Functions ¶
func ConvertInterfaceToRealType ¶
interface{} -> int or string or proto.Message
func ConvertStringToRealType ¶ added in v1.1.2
支持int,float,string,[]byte,complex,bool,proto.Message
func ConvertValueToInt ¶
reflect.Value -> int
func ConvertValueToInterface ¶
reflect.Value -> interface{}
func FixEntityDataFromCache ¶
func FixEntityDataFromCache(entity Entity, db EntityDb, kvCache KvCache, cacheKeyPrefix string, entityKey interface{})
根据缓存数据,修复数据 如:服务器crash时,缓存数据没来得及保存到数据库,服务器重启后读取缓存中的数据,保存到数据库,防止数据回档
func GetChildCacheKey ¶
func GetComponentSaveData ¶
获取组件的完整保存数据
func GetComponentSaveName ¶ added in v1.1.7
func GetEntityCacheKey ¶ added in v1.3.0
func GetEntityComponentCacheKey ¶
获取对象组件的缓存key
func GetEntityComponentChildCacheKey ¶
func GetEntityComponentChildCacheKey(prefix string, entityId interface{}, componentName string, childName string) string
获取对象组件子对象的缓存key
func GetEntitySaveData ¶
获取实体需要保存到数据库的完整数据
func GetFieldValue ¶ added in v1.4.0
func LoadEntityData ¶ added in v1.3.0
func LoadObjData ¶ added in v1.4.0
func MapDel ¶ added in v1.1.6
func MapDel[M ~map[K]V, K comparable, V any](mapDirtyMark MapDirtyMark, m M, k K)
map类型的数据的辅助接口,自动调用MapDirtyMark.SetDirty
func MapSet ¶ added in v1.2.0
func MapSet[M ~map[K]V, K comparable, V any](mapDirtyMark MapDirtyMark, m M, k K, v V)
map类型的数据的辅助接口,自动调用MapDirtyMark.SetDirty
func ParseEntitySaveableStruct ¶ added in v1.4.0
func ParseEntitySaveableStruct(entity Entity)
func SaveChangedDataToCache ¶
func SaveChangedDataToCache(kvCache KvCache, obj any, cacheKeyName string, saveableField *SaveableField)
把修改数据保存到缓存
func SaveComponentChangedDataToCache ¶
func SaveComponentChangedDataToCache(kvCache KvCache, cacheKeyPrefix string, entityKey interface{}, component Component)
把组件的修改数据保存到缓存
func SaveEntityChangedDataToDb ¶
func SaveEntityChangedDataToDb(entityDb EntityDb, entity Entity, kvCache KvCache, removeCacheAfterSaveDb bool, cachePrefix string) error
Entity的变化数据保存到数据库
key为entity.GetId()
func SaveEntityChangedDataToDbByKey ¶ added in v1.1.0
func SaveEntityChangedDataToDbByKey(entityDb EntityDb, entity Entity, entityKey interface{}, kvCache KvCache, removeCacheAfterSaveDb bool, cachePrefix string) error
Entity的变化数据保存到数据库,只保存有数据变化的组件数据,但组件的数据不会分割,只要一个组件有数据变化,组件的数据就是全量覆盖
指定key
func SaveMapValueToCache ¶ added in v1.1.3
func SaveMapValueToCache(kvCache KvCache, cacheKeyName string, val reflect.Value, dirtyMark MapDirtyMark)
保存map类型字段到redis
func SaveObjectChangedDataToCache ¶ added in v1.3.0
func SaveValueToCache ¶ added in v1.1.3
保存单个字段到redis
func SetApplication ¶ added in v0.3.0
func SetApplication(application Application)
func SetLogLevel ¶ added in v1.3.0
func SetLogLevel(level int8)
Types ¶
type Application ¶ added in v0.3.0
type Application interface { // 进程的唯一id GetId() int32 GetContext() context.Context GetWaitGroup() *sync.WaitGroup // 初始化 Init(ctx context.Context, configFile string) bool // 运行 Run(ctx context.Context) // 定时更新 OnUpdate(ctx context.Context, updateCount int64) // 退出 Exit() }
进程接口
func GetApplication ¶ added in v0.3.0
func GetApplication() Application
type ApplicationHook ¶ added in v0.3.0
type ApplicationHook interface { OnRegisterServerHandler(arg any) OnApplicationInit(initArg any) OnApplicationExit() }
Application回调接口
type BaseComponent ¶
type BaseComponent struct {
// contains filtered or unexported fields
}
func NewBaseComponent ¶ added in v0.2.0
func NewBaseComponent(entity Entity, name string) *BaseComponent
func (*BaseComponent) GetEntity ¶
func (this *BaseComponent) GetEntity() Entity
func (*BaseComponent) SetEntity ¶
func (this *BaseComponent) SetEntity(entity Entity)
type BaseDirtyMark ¶
type BaseDirtyMark struct {
// contains filtered or unexported fields
}
func (*BaseDirtyMark) IsDirty ¶
func (this *BaseDirtyMark) IsDirty() bool
func (*BaseDirtyMark) ResetChanged ¶
func (this *BaseDirtyMark) ResetChanged()
func (*BaseDirtyMark) ResetDirty ¶
func (this *BaseDirtyMark) ResetDirty()
func (*BaseDirtyMark) SetDirty ¶
func (this *BaseDirtyMark) SetDirty()
type BaseEntity ¶
type BaseEntity struct { // Entity唯一id Id int64 // contains filtered or unexported fields }
func (*BaseEntity) AddComponent ¶
func (this *BaseEntity) AddComponent(component Component)
func (*BaseEntity) AddEventReceiver ¶ added in v1.1.6
func (this *BaseEntity) AddEventReceiver(eventReceiver EventReceiver)
func (*BaseEntity) GetComponentByIndex ¶
func (this *BaseEntity) GetComponentByIndex(componentIndex int) Component
func (*BaseEntity) GetComponentByName ¶
func (this *BaseEntity) GetComponentByName(componentName string) Component
获取组件
func (*BaseEntity) RangeComponent ¶
func (this *BaseEntity) RangeComponent(fun func(component Component) bool)
func (*BaseEntity) RangeEventReceiver ¶ added in v1.1.6
func (this *BaseEntity) RangeEventReceiver(f func(eventReceiver EventReceiver) bool)
type BaseMapDirtyMark ¶
type BaseMapDirtyMark struct {
// contains filtered or unexported fields
}
func (*BaseMapDirtyMark) HasCached ¶
func (this *BaseMapDirtyMark) HasCached() bool
func (*BaseMapDirtyMark) IsChanged ¶
func (this *BaseMapDirtyMark) IsChanged() bool
func (*BaseMapDirtyMark) IsDirty ¶
func (this *BaseMapDirtyMark) IsDirty() bool
func (*BaseMapDirtyMark) RangeDirtyMap ¶
func (this *BaseMapDirtyMark) RangeDirtyMap(f func(dirtyKey interface{}, isAddOrUpdate bool))
func (*BaseMapDirtyMark) ResetChanged ¶
func (this *BaseMapDirtyMark) ResetChanged()
func (*BaseMapDirtyMark) ResetDirty ¶
func (this *BaseMapDirtyMark) ResetDirty()
func (*BaseMapDirtyMark) SetCached ¶
func (this *BaseMapDirtyMark) SetCached()
func (*BaseMapDirtyMark) SetDirty ¶
func (this *BaseMapDirtyMark) SetDirty(k interface{}, isAddOrUpdate bool)
type BaseRoutineEntity ¶ added in v0.3.0
type BaseRoutineEntity struct { BaseEntity // contains filtered or unexported fields }
独立协程的实体
func NewRoutineEntity ¶ added in v0.2.0
func NewRoutineEntity(messageChanLen int) *BaseRoutineEntity
func (*BaseRoutineEntity) GetTimerEntries ¶ added in v0.3.0
func (this *BaseRoutineEntity) GetTimerEntries() *TimerEntries
func (*BaseRoutineEntity) PushMessage ¶ added in v0.3.0
func (this *BaseRoutineEntity) PushMessage(message any)
push a Message 将会在RoutineEntity的独立协程中被调用
func (*BaseRoutineEntity) RunProcessRoutine ¶ added in v0.3.0
func (this *BaseRoutineEntity) RunProcessRoutine(routineEntity RoutineEntity, routineArgs *RoutineEntityRoutineArgs) bool
开启消息处理协程 每个RoutineEntity一个独立的消息处理协程
type Component ¶
type Component interface { // 组件名 GetName() string // 所属的实体 GetEntity() Entity SetEntity(entity Entity) }
实体组件接口
type ComponentCtor ¶ added in v1.1.5
组件构造接口
type ComponentRegister ¶ added in v1.1.5
type ComponentRegister[E Entity] struct { RegisterInfos []*ComponentRegisterInfo[E] }
组件注册信息管理
func (*ComponentRegister[E]) InitComponents ¶ added in v1.1.5
func (cr *ComponentRegister[E]) InitComponents(entity E, arg any)
初始化组件
func (*ComponentRegister[E]) Register ¶ added in v1.1.5
func (cr *ComponentRegister[E]) Register(componentName string, ctorOrder int, ctor ComponentCtor[E])
注册组件
type ComponentRegisterInfo ¶ added in v1.1.5
type ComponentRegisterInfo[E Entity] struct { // 组件名 ComponentName string // 组件构造接口 Ctor ComponentCtor[E] // 构造顺序,数值小的组件,先执行 // 因为有的组件有依赖关系 CtorOrder int }
组件注册信息
type DataComponent ¶
type DataComponent struct { BaseComponent BaseDirtyMark }
func NewDataComponent ¶
func NewDataComponent(entity Entity, componentName string) *DataComponent
type DirtyMark ¶
type DirtyMark interface { // 需要保存的数据是否修改了 IsDirty() bool // 设置数据修改标记 SetDirty() // 重置标记 ResetDirty() }
保存数据作为一个整体,只要一个字段修改了,整个数据都需要缓存
examples:
type ProteTest struct { BaseDirtyMark Data *pb.Data `db:"ProteTest"` } type MapTest struct { BaseDirtyMark Data map[int32]*pb.Data `db:"MapTest"` } type MapTest struct { BaseDirtyMark Data map[int32]string `db:"MapTest"` } type SliceTest struct { BaseDirtyMark Data []*pb.Data `db:"SliceTest"` }
type DistributedEntityHelper ¶ added in v0.4.0
type DistributedEntityHelper interface { // 创建实体 CreateEntity(entityData interface{}) RoutineEntity // 根据entityId路由到目标服务器 // 返回值:服务器id RouteServerId(entityId int64) int32 }
DistributedEntity的回调接口
type DistributedEntityMgr ¶ added in v0.3.0
type DistributedEntityMgr struct {
// contains filtered or unexported fields
}
分布式实体管理类
func NewDistributedEntityMgr ¶ added in v0.3.0
func NewDistributedEntityMgr(distributedLockName string, entityDb EntityDb, cache KvCache, routineArgs *RoutineEntityRoutineArgs, distributedEntityHelper DistributedEntityHelper) *DistributedEntityMgr
func (*DistributedEntityMgr) DeleteDistributeLocks ¶ added in v0.3.0
func (this *DistributedEntityMgr) DeleteDistributeLocks()
删除跟本服关联的分布式锁
func (*DistributedEntityMgr) DistributeLock ¶ added in v0.3.0
func (this *DistributedEntityMgr) DistributeLock(entityId int64) bool
分布式锁Lock redis实现的分布式锁,保证同一个实体的逻辑处理协程只会在一个服务器上
func (*DistributedEntityMgr) DistributeUnlock ¶ added in v0.3.0
func (this *DistributedEntityMgr) DistributeUnlock(entityId int64)
分布式锁UnLock
func (*DistributedEntityMgr) GetEntity ¶ added in v0.3.0
func (this *DistributedEntityMgr) GetEntity(entityId int64) RoutineEntity
获取已加载的分布式实体
func (*DistributedEntityMgr) GetEntityDb ¶ added in v0.3.0
func (this *DistributedEntityMgr) GetEntityDb() EntityDb
数据库接口
func (*DistributedEntityMgr) LoadEntity ¶ added in v0.3.0
func (this *DistributedEntityMgr) LoadEntity(entityId int64, entityData interface{}) RoutineEntity
加载分布式实体 加载成功后,开启独立协程
func (*DistributedEntityMgr) Range ¶ added in v0.3.0
func (this *DistributedEntityMgr) Range(f func(entity RoutineEntity) bool)
遍历
func (*DistributedEntityMgr) ReBalance ¶ added in v0.3.0
func (this *DistributedEntityMgr) ReBalance()
重新平衡 通知已不属于本服务器管理的实体关闭协程
func (*DistributedEntityMgr) StopAll ¶ added in v0.3.0
func (this *DistributedEntityMgr) StopAll()
关闭所有实体协程
type Entity ¶
type Entity interface { // 唯一id GetId() int64 AddComponent(component Component) // 查找某个组件 GetComponentByName(componentName string) Component // 遍历组件 RangeComponent(fun func(component Component) bool) }
实体接口
type EntityDb ¶
type EntityDb interface { // 根据id查找数据 FindEntityById(entityKey interface{}, data interface{}) (bool, error) // 新建Entity(insert) InsertEntity(entityKey interface{}, entityData interface{}) (err error, isDuplicateKey bool) // 保存Entity数据(update entity by entityKey) SaveEntity(entityKey interface{}, entityData interface{}) error // 删除Entity数据(delete entity by entityKey) DeleteEntity(entityKey interface{}) error // 保存1个组件(update entity's component) SaveComponent(entityKey interface{}, componentName string, componentData interface{}) error // 批量保存组件(update entity's components...) SaveComponents(entityKey interface{}, components map[string]interface{}) error // 保存1个组件的一个字段(update entity's component.field) SaveComponentField(entityKey interface{}, componentName string, fieldName string, fieldData interface{}) error // 删除1个组件的某些字段 DeleteComponentField(entityKey interface{}, componentName string, fieldName ...string) error }
Entity的数据库接口
type EventHandlerInfo ¶ added in v1.1.6
type EventHandlerInfo struct { // 组件名,如果为空,就表示是直接写在Entity上的接口 ComponentName string // 函数信息 Method reflect.Method // 事件的reflect.Type EventType reflect.Type }
组件事件响应接口信息
type EventHandlerMgr ¶ added in v1.1.6
type EventHandlerMgr struct {
// contains filtered or unexported fields
}
组件事件响应接口管理类
func NewEventHandlerMgr ¶ added in v1.1.6
func NewEventHandlerMgr() *EventHandlerMgr
func (*EventHandlerMgr) AddHandlerInfo ¶ added in v1.1.6
func (this *EventHandlerMgr) AddHandlerInfo(handlerInfo *EventHandlerInfo)
func (*EventHandlerMgr) AutoRegister ¶ added in v1.1.6
func (this *EventHandlerMgr) AutoRegister(entity Entity, methodNamePrefix string)
扫描entity以及entity的组件,寻找匹配格式的事件响应接口
type InterfaceMapLoader ¶ added in v1.1.7
type InterfaceMapLoader interface { // bytesMap: map[k][]byte LoadFromBytesMap(bytesMap any) error }
map[k]any类型的字段,无法直接反序列化,因为不知道map的value具体是什么类型
因此提供一个自定义加载接口,由业务层自行实现特殊的反序列化逻辑 LoadFromCache处理map[k]any时,会把数据转换成map[k][]byte,传入LoadFromBytesMap 保存数据时,由于知道具体的value类型,所以无需特殊保存接口
type KvCache ¶
type KvCache interface { // redis Get Get(key string) (string, error) // redis Set // value如果是proto.Message,会先进行序列化 Set(key string, value interface{}, expiration time.Duration) error // redis SetNX // value如果是proto.Message,会先进行序列化 SetNX(key string, value interface{}, expiration time.Duration) (bool, error) // redis Del Del(key ...string) (int64, error) // redis Type Type(key string) (string, error) // 缓存数据加载到map // m必须是一个类型明确有效的map,且key类型只能是int或string,value类型只能是int或string或proto.Message // // example: // testDataMap := make(map[int64]*pb.TestData) // HGetAll("myhash", testDataMap) GetMap(key string, m interface{}) error // map数据缓存 // m必须是一个类型明确有效的map,且key类型只能是int或string,value类型只能是int或string或proto.Message // NOTE:批量写入数据,并不会删除之前缓存的数据 // // example: // - SetMap("myhash", map[int64]*pb.TestData{1:&pb.TestData{},2:&pb.TestData{}}) // - SetMap("myhash", map[string]interface{}{"key1": "value1", "key2": "value2"}) SetMap(key string, m interface{}) error // redis HGetAll HGetAll(key string) (map[string]string, error) // redis HSet // HSet accepts values in following formats: // - HSet("myhash", "key1", "value1", "key2", "value2") // - HSet("myhash", []string{"key1", "value1", "key2", "value2"}) // - HSet("myhash", map[string]interface{}{"key1": "value1", "key2": "value2"}) HSet(key string, values ...interface{}) (int64, error) // redis HSetNX HSetNX(key, field string, value interface{}) (bool, error) // 删除map的项 HDel(key string, fields ...string) (int64, error) // 缓存数据加载到proto.Message GetProto(key string, value proto.Message) error }
常用的kv缓存接口
type KvDb ¶
type KvDb interface { Find(key interface{}) (interface{}, error) FindAndDecode(key interface{}, decodeData interface{}) error Insert(key interface{}, value interface{}) (err error, isDuplicateKey bool) Update(key interface{}, value interface{}, upsert bool) error Inc(key interface{}, value interface{}, upsert bool) (interface{}, error) Delete(key interface{}) error }
Kv数据接口 游戏应用里,除了账号数据和玩家数据之外,其他以Key-Value存储的数据 KvDb接口是为了应用层能够灵活的更换存储数据库(mysql,mongo,redis等)
type Logger ¶
type Logger interface { Debug(format string, args ...interface{}) Info(format string, args ...interface{}) Warn(format string, args ...interface{}) Error(format string, args ...interface{}) }
func NewStdLogger ¶ added in v1.3.0
type MapComponent ¶ added in v1.2.0
type MapComponent struct { BaseComponent BaseMapDirtyMark }
func NewMapComponent ¶ added in v1.2.0
func NewMapComponent(entity Entity, componentName string) *MapComponent
type MapData ¶ added in v1.2.0
type MapData[K comparable, V any] struct { BaseMapDirtyMark Data map[K]V `db:""` }
map类型的数据的辅助类
func NewMapData ¶ added in v1.2.0
func NewMapData[K comparable, V any]() *MapData[K, V]
type MapDataComponent ¶
type MapDataComponent[K comparable, V any] struct { BaseComponent *MapData[K, V] `db:""` }
MapData+BaseComponent
func NewMapDataComponent ¶
func NewMapDataComponent[K comparable, V any](entity Entity, componentName string) *MapDataComponent[K, V]
type MapDirtyMark ¶
type MapDirtyMark interface { // 需要保存的数据是否修改了 IsDirty() bool // 设置数据修改标记 SetDirty(k interface{}, isAddOrUpdate bool) // 重置标记 ResetDirty() // 是否把整体数据缓存过了 HasCached() bool // 第一次有数据修改时,会把整体数据缓存一次,之后只保存修改过的项(增量更新) SetCached() RangeDirtyMap(f func(dirtyKey interface{}, isAddOrUpdate bool)) }
map格式的保存数据 第一次有数据修改时,会把整体数据缓存一次,之后只保存修改过的项(增量更新)
examples:
type MapTest struct { BaseMapDirtyMark Data map[int32]*pb.Data `db:"MapTest"` } type MapTest struct { BaseMapDirtyMark Data map[int32]string `db:"MapTest"` }
type MapValueDirtyMark ¶ added in v1.4.0
type MapValueDirtyMark[K comparable] struct { // 父类才是真正的脏标记 Parent MapDirtyMark MapKey K // key of parent map }
用于InterfaceMap的map's value的DirtyMark
func NewMapValueDirtyMark ¶ added in v1.4.0
func NewMapValueDirtyMark[K comparable](parent MapDirtyMark, mapKey K) *MapValueDirtyMark[K]
func (*MapValueDirtyMark[K]) IsChanged ¶ added in v1.4.0
func (m *MapValueDirtyMark[K]) IsChanged() bool
只是为了实现Saveable接口,无实际作用
func (*MapValueDirtyMark[K]) ResetChanged ¶ added in v1.4.0
func (m *MapValueDirtyMark[K]) ResetChanged()
只是为了实现Saveable接口,无实际作用
func (*MapValueDirtyMark[K]) SetDirty ¶ added in v1.4.0
func (m *MapValueDirtyMark[K]) SetDirty()
设置脏标记,实际设置的是父类
type MongoCollection ¶
type MongoCollection struct {
// contains filtered or unexported fields
}
db.EntityDb的mongo实现
func (*MongoCollection) CreateIndex ¶ added in v0.5.0
func (this *MongoCollection) CreateIndex(key string, unique bool)
func (*MongoCollection) DeleteComponentField ¶
func (this *MongoCollection) DeleteComponentField(entityKey interface{}, componentName string, fieldName ...string) error
删除1个组件的某些字段
func (*MongoCollection) DeleteEntity ¶ added in v1.1.7
func (this *MongoCollection) DeleteEntity(entityKey interface{}) error
func (*MongoCollection) FindEntityById ¶
func (this *MongoCollection) FindEntityById(entityKey interface{}, data interface{}) (bool, error)
根据id查找数据
func (*MongoCollection) GetCollection ¶
func (this *MongoCollection) GetCollection() *mongo.Collection
func (*MongoCollection) InsertEntity ¶
func (this *MongoCollection) InsertEntity(entityKey interface{}, entityData interface{}) (err error, isDuplicateKey bool)
func (*MongoCollection) SaveComponent ¶
func (this *MongoCollection) SaveComponent(entityKey interface{}, componentName string, componentData interface{}) error
func (*MongoCollection) SaveComponentField ¶
func (this *MongoCollection) SaveComponentField(entityKey interface{}, componentName string, fieldName string, fieldData interface{}) error
func (*MongoCollection) SaveComponents ¶
func (this *MongoCollection) SaveComponents(entityKey interface{}, components map[string]interface{}) error
func (*MongoCollection) SaveEntity ¶
func (this *MongoCollection) SaveEntity(entityKey interface{}, entityData interface{}) error
type MongoCollectionPlayer ¶
type MongoCollectionPlayer struct { MongoCollection // contains filtered or unexported fields }
db.PlayerDb的mongo实现
func (*MongoCollectionPlayer) FindAccountIdByPlayerId ¶
func (this *MongoCollectionPlayer) FindAccountIdByPlayerId(playerId int64) (int64, error)
func (*MongoCollectionPlayer) FindPlayerByAccountId ¶
func (this *MongoCollectionPlayer) FindPlayerByAccountId(accountId int64, regionId int32, playerData interface{}) (bool, error)
根据账号id查找玩家数据 适用于一个账号在一个区服只有一个玩家角色的游戏
func (*MongoCollectionPlayer) FindPlayerIdByAccountId ¶
func (this *MongoCollectionPlayer) FindPlayerIdByAccountId(accountId int64, regionId int32) (int64, error)
func (*MongoCollectionPlayer) FindPlayerIdsByAccountId ¶ added in v0.2.0
func (this *MongoCollectionPlayer) FindPlayerIdsByAccountId(accountId int64, regionId int32) ([]int64, error)
type MongoDb ¶
type MongoDb struct {
// contains filtered or unexported fields
}
db.DbMgr的mongo实现
func NewMongoDb ¶
func (*MongoDb) Disconnect ¶
func (this *MongoDb) Disconnect()
func (*MongoDb) GetEntityDb ¶
func (*MongoDb) GetMongoClient ¶ added in v0.5.0
func (*MongoDb) GetMongoDatabase ¶
func (*MongoDb) RegisterEntityDb ¶
func (this *MongoDb) RegisterEntityDb(collectionName string, hashedShardKey bool, uniqueId string) EntityDb
注册普通Entity对应的collection
func (*MongoDb) RegisterKvDb ¶ added in v0.4.0
func (*MongoDb) RegisterPlayerDb ¶ added in v0.4.0
func (this *MongoDb) RegisterPlayerDb(collectionName string, hashedShardKey bool, playerId, accountId, region string) PlayerDb
注册玩家对应的collection
func (*MongoDb) ShardCollection ¶ added in v0.5.0
设置database分片
func (*MongoDb) ShardDatabase ¶ added in v0.5.0
设置database分片
type MongoKvDb ¶ added in v0.4.0
type MongoKvDb struct {
// contains filtered or unexported fields
}
KvDb的mongo实现
func (*MongoKvDb) FindAndDecode ¶ added in v0.4.0
func (*MongoKvDb) GetCollection ¶ added in v0.4.0
func (this *MongoKvDb) GetCollection() *mongo.Collection
type PlayerDb ¶
type PlayerDb interface { EntityDb // 根据账号id查找角色id // 适用于一个账号在一个区服只有一个玩家角色的游戏 FindPlayerIdByAccountId(accountId int64, regionId int32) (int64, error) // MMORPG类型的游戏,可能一个账号在一个服有多个角色 FindPlayerIdsByAccountId(accountId int64, regionId int32) ([]int64, error) // 根据账号id查找玩家数据 // 适用于一个账号在一个区服只有一个玩家角色的游戏 FindPlayerByAccountId(accountId int64, regionId int32, playerData interface{}) (bool, error) // 根据角色id查找账号id FindAccountIdByPlayerId(playerId int64) (int64, error) }
玩家数据接口 Db接口是为了应用层能够灵活的更换存储数据库(mysql,mongo,redis等)
type ProtoData ¶ added in v1.3.0
type ProtoData[E proto.Message] struct { BaseDirtyMark Data E `db:""` }
保存数据是一个proto的辅助类
func NewProtoData ¶ added in v1.3.0
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
KvCache的redis实现
func NewRedisCache ¶
func NewRedisCache(redisClient redis.Cmdable) *RedisCache
func (*RedisCache) GetMap ¶
func (this *RedisCache) GetMap(key string, m interface{}) error
redis hash -> map
func (*RedisCache) GetProto ¶
func (this *RedisCache) GetProto(key string, value proto.Message) error
func (*RedisCache) HDel ¶ added in v0.3.1
func (this *RedisCache) HDel(key string, fields ...string) (int64, error)
func (*RedisCache) HGetAll ¶ added in v0.3.1
func (this *RedisCache) HGetAll(key string) (map[string]string, error)
func (*RedisCache) HSet ¶ added in v0.3.1
func (this *RedisCache) HSet(key string, values ...interface{}) (int64, error)
func (*RedisCache) HSetNX ¶ added in v0.3.1
func (this *RedisCache) HSetNX(key, field string, value interface{}) (bool, error)
func (*RedisCache) Set ¶
func (this *RedisCache) Set(key string, value interface{}, expiration time.Duration) error
func (*RedisCache) SetMap ¶
func (this *RedisCache) SetMap(k string, m interface{}) error
map -> redis hash
type RoutineEntity ¶ added in v0.2.0
type RoutineEntity interface { Entity // push a Message // 将会在RoutineEntity的独立协程中被调用 PushMessage(message any) // 开启消息处理协程 // 每个RoutineEntity一个独立的消息处理协程 RunProcessRoutine(routineEntity RoutineEntity, routineArgs *RoutineEntityRoutineArgs) bool // 停止协程 Stop() }
独立协程的实体接口
type RoutineEntityRoutineArgs ¶ added in v0.2.0
type RoutineEntityRoutineArgs struct { // 初始化,返回false时,协程不会启动 InitFunc func(routineEntity RoutineEntity) bool // 消息处理函数 ProcessMessageFunc func(routineEntity RoutineEntity, message any) // 有计时函数执行后调用 AfterTimerExecuteFunc func(routineEntity RoutineEntity, t time.Time) // 协程结束时调用 EndFunc func(routineEntity RoutineEntity) }
RoutineEntity协程参数
type Saveable ¶
type Saveable interface { // 数据是否改变过 IsChanged() bool // 重置 ResetChanged() }
保存数据的接口 用于检查数据是否修改过
type SaveableDirtyMark ¶
同时需要保存数据库和缓存的接口 Saveable:
保存数据库的频率低,比如玩家下线时才会保存数据库,那么Saveable只会在上线期间记录有没有改变过就可以
DirtyMark:
缓存的保存频率高,比如玩家每一次操作都可能引起缓存的更新
type SaveableField ¶
type SaveableField struct { StructField reflect.StructField // 如果该字段不是叶子节点,则SaveableStruct有值 SaveableStruct *SaveableStruct FieldIndex int // 是否明文保存 IsPlain bool // 保存的字段名 Name string // 节点深度 Depth int32 // contains filtered or unexported fields }
字段
func (*SaveableField) InitNilField ¶ added in v1.1.6
func (this *SaveableField) InitNilField(val reflect.Value) bool
如果字段为nil,根据类型进行初始化
func (*SaveableField) IsInterfaceMap ¶ added in v1.1.7
func (this *SaveableField) IsInterfaceMap() bool
func (*SaveableField) NewBytesMap ¶ added in v1.1.7
func (this *SaveableField) NewBytesMap() any
map[k]any类型的字段,new一个map[k][]byte对象
type SaveableStruct ¶
type SaveableStruct struct { // 单个db字段 Field *SaveableField // 多个child字段 Children []*SaveableField // 父节点 ParentField *SaveableField }
有需要保存字段的结构 SaveableStruct应该只针对第一层的对象(如Component),并设计为树型结构,在第一次解析结构时,就把层次关系记录下来
func GetObjSaveableStruct ¶ added in v1.4.0
func GetObjSaveableStruct(obj any) *SaveableStruct
获取对象的保存结构(一般对组件使用),如果没有保存字段,则返回nil
func (*SaveableStruct) GetChildSaveable ¶ added in v1.4.0
func (this *SaveableStruct) GetChildSaveable(obj any, childIndex int) (Saveable, *SaveableField)
func (*SaveableStruct) GetSingleSaveable ¶ added in v1.4.0
func (this *SaveableStruct) GetSingleSaveable(obj any) (Saveable, *SaveableField)
type SliceData ¶ added in v1.2.0
type SliceData[E any] struct { BaseDirtyMark Data []E `db:""` }
slice类型的数据的辅助类
type StdLogger ¶ added in v1.3.0
type StdLogger struct {
// contains filtered or unexported fields
}
type TimerEntries ¶
计时管理 参考https://github.com/robfig/cron 与cron主要用于任务计划不同,TimerEntries主要用于倒计时的管理 如果放在玩家的独立协程中使用,则倒计时的回调可以保证协程安全,使玩家的倒计时回调更简单
example:
go func() { defer timerEntries.Stop() timerEntries := NewTimerEntries() timerEntries.Start() for { select { case timeNow := <-timerEntries.TimerChan(): timerEntries.Run(timeNow) case ... } } }
func NewTimerEntries ¶
func NewTimerEntries() *TimerEntries
func NewTimerEntriesWithArgs ¶
func NewTimerEntriesWithArgs(nowFunc func() time.Time, minInterval time.Duration) *TimerEntries
func (*TimerEntries) AddTimer ¶
func (this *TimerEntries) AddTimer(t time.Time, f TimerJob)
指定时间点执行回调
func (*TimerEntries) After ¶
func (this *TimerEntries) After(d time.Duration, f TimerJob)
现在往后多少时间执行回调
func (*TimerEntries) GetMinInterval ¶ added in v0.6.1
func (this *TimerEntries) GetMinInterval() time.Duration
func (*TimerEntries) GetTimeOffset ¶ added in v0.6.1
func (this *TimerEntries) GetTimeOffset() time.Duration
func (*TimerEntries) Now ¶ added in v0.6.1
func (this *TimerEntries) Now() time.Time
func (*TimerEntries) SetMinInterval ¶ added in v0.6.1
func (this *TimerEntries) SetMinInterval(minInterval time.Duration)
func (*TimerEntries) SetTimeOffset ¶ added in v0.6.1
func (this *TimerEntries) SetTimeOffset(timeOffset time.Duration)
func (*TimerEntries) Start ¶
func (this *TimerEntries) Start()
func (*TimerEntries) Stop ¶
func (this *TimerEntries) Stop()
func (*TimerEntries) TimerChan ¶
func (this *TimerEntries) TimerChan() <-chan time.Time