3 Star 1 Fork 0

NightTC / Gobige

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
IEntityes.go 5.57 KB
一键复制 编辑 原始数据 按行查看 历史
package entity
import (
"sync"
"gitee.com/night-tc/gobige/global"
. "gitee.com/night-tc/gobige/msghandler"
"gitee.com/night-tc/gobige/threads"
)
type IEntities interface {
//这个需要子类实现
IMsgHandler
//这个需要子类实现
IDelayCall
/* 生存周期函数 */
//初始化
Init() error
//循环调用
Loop()
// //循环调用中间
// Looping()
// //循环调用结尾
// Looped()
//销毁
Destroy()
// //发生异常的时候
// OnPanic(err error)
// //自运行协程的函数
// GoRun(ctx context.Context)
/* 集合逻辑 */
//获取子对象
GetEntity(eid uint64) IEntity
// 获取子对象使用DBID
GetEntityByDBID(dbid uint64, etype string) IEntity
//创建子对象
CreateEntity(etype string, eid, dbid uint64, fly bool, param ...interface{}) (result IEntity, err error)
//销毁子对象
DestroyEntity(eid uint64) error
//遍历所有成员
Range(f func(k, v interface{}) bool)
//按分组遍历组内所有成员
RangeByGroup(gp string, f func(k, v any) bool)
//按类型遍历组内所有成员
RangeByType(etype string, f func(k, v any) bool)
/* 其他接口 */
//字符串化信息
String() string
/* 属性 */
//本空间的ID
GetID() uint64
//操作子协程的管理器
GetThreadGo() *threads.ThreadGo
// //获取路由器
// GetMsgHandler() *MsgHandlers
}
////////////////////////////////////////////////////////
/* 生存周期函数 */
// 初始化
func (this *Entities) Init() error {
//消息注册
this.realPtr.RegMsgProc(&EntitiesMsgProc{this.realPtr})
return nil
}
// 循环调用
func (this *Entities) Loop() {
if !this.isMutilThread {
this.realPtr.Range(func(k, v interface{}) bool {
v.(IEntity).mainLoop()
return true
})
}
}
// 销毁 会等所有子协程结束
func (this *Entities) Destroy() {
this.entities.Range(func(key, value any) bool {
this.realPtr.DestroyEntity(key.(uint64))
return true
})
this.thgo.CloseWait()
}
/* 生存周期函数 */
////////////////////////////////////////////////////////
/* 集合逻辑 */
// 获取子对象
func (this *Entities) GetEntity(eid uint64) IEntity {
if result, ok := this.entities.Load(eid); ok {
return result.(IEntity)
} else {
return nil
}
}
// 获取子对象使用DBID
func (this *Entities) GetEntityByDBID(dbid uint64, etype string) IEntity {
if eli, ok := this.entitiesByDBID.Load(etype); ok {
if result, ok := eli.(*sync.Map).Load(dbid); ok {
return result.(IEntity)
} else {
return nil
}
} else {
return nil
}
}
// 创建子对象
func (this *Entities) CreateEntity(etype string, eid, dbid uint64, fly bool, param ...interface{}) (result IEntity, err error) {
if eid == 0 {
eid = global.GetSrvInst().GetNewEID()
}
if _, ok := this.entities.Load(eid); ok {
return nil, ErrNtt_EID_Exist.NewErr(nil, eid)
}
result = GetTypeMgr().NewEntity(etype)
result.OnCreated(etype, eid, dbid, result, this.realPtr, fly, param...)
this.addEntity(result)
if this.isMutilThread {
//启动子协程
result.setDestroyF(this.thgo.SubGo(result.GoRun))
} else {
//因为没有子协程,所以子对象收消息需要在本消息处理里进行
result.SetMainHander(this.realPtr)
}
return result, nil
}
// 销毁子对象
func (this *Entities) DestroyEntity(eid uint64) error {
v, ok := this.entities.Load(eid)
if !ok {
return ErrNtt_EID_Not_Exist.NewErr(nil, eid)
}
e := v.(IEntity)
this.delEntity(e)
//销毁需要在帧尾做,确保一些逻辑做完了再销毁,但在集合中已找不到这个对象了
this.realPtr.CallDelay(e.EnterDestroy)
return nil
}
// 遍历所有成员
func (this *Entities) Range(f func(k, v interface{}) bool) {
this.entities.Range(f)
}
// 按分组遍历组内所有成员
func (this *Entities) RangeByGroup(gp string, f func(k, v any) bool) {
if l, ok := this.entitiesGroup.Load(gp); ok {
l.(*sync.Map).Range(f)
}
}
// 按类型遍历组内所有成员
func (this *Entities) RangeByType(etype string, f func(k, v any) bool) {
if l, ok := this.entitiesGroup.Load(etype); ok {
l.(*sync.Map).Range(f)
}
}
/* 集合逻辑 */
////////////////////////////////////////////////////////
/* 私有函数 */
// 添加对象
func (this *Entities) addEntity(ntt IEntity) {
this.entities.Store(ntt.GetID(), ntt)
if ntt.GetDBID() != 0 {
//按类型分组,dbid为KEY
var t *sync.Map
if tmp, ok := this.entitiesByDBID.Load(ntt.GetType()); ok {
t = tmp.(*sync.Map)
} else {
t = &sync.Map{}
this.entitiesByDBID.Store(ntt.GetType(), t)
}
t.Store(ntt.GetDBID(), ntt)
}
{
//按类型分组,eid为KEY
var t *sync.Map
if tmp, ok := this.entitiesByType.Load(ntt.GetType()); ok {
t = tmp.(*sync.Map)
} else {
t = &sync.Map{}
this.entitiesByType.Store(ntt.GetType(), t)
}
t.Store(ntt.GetID(), ntt)
}
this.entityCount.Add(1)
}
// 销毁子对象
func (this *Entities) delEntity(e IEntity) {
this.entities.Delete(e.GetID())
if e.GetDBID() != 0 {
//按类型分组,dbid为KEY
if it, ok := this.entitiesByDBID.Load(e.GetType()); ok {
it.(*sync.Map).Delete(e.GetDBID())
}
}
//按类型分组,eid为KEY
if it, ok := this.entitiesByType.Load(e.GetType()); ok {
it.(*sync.Map).Delete(e.GetID())
}
this.delEntityGroup(e)
this.entityCount.Add(-1)
}
// 删除对象分组
func (this *Entities) delEntityGroup(e IEntity) {
e.GetGroups().Range(func(key, val interface{}) bool {
if it, ok := this.entitiesGroup.Load(key); ok {
t := it.(*sync.Map)
t.Delete(e.GetID())
}
return true
})
}
/* 私有函数 */
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
00125336c61c

搜索帮助

53164aa7 5694891 3bd8fe86 5694891