game_sync/worldsrv/platformgamegroup.go

86 lines
2.0 KiB
Go

package main
import (
webapi_proto "mongo.games.com/game/protocol/webapi"
"mongo.games.com/game/srvdata"
)
type PlatformGameGroupObserver interface {
OnGameGroupUpdate(oldCfg, newCfg *webapi_proto.GameConfigGroup)
}
var PlatformGameGroupMgrSington = &PlatformGameGroupMgr{
groups: make(map[int32]*webapi_proto.GameConfigGroup),
}
type PlatformGameGroupMgr struct {
groups map[int32]*webapi_proto.GameConfigGroup
observers []PlatformGameGroupObserver
}
func (this *PlatformGameGroupMgr) RegisteObserver(observer PlatformGameGroupObserver) {
for _, ob := range this.observers {
if ob == observer {
return
}
}
this.observers = append(this.observers, observer)
}
func (this *PlatformGameGroupMgr) UnregisteObserver(observer PlatformGameGroupObserver) {
for i, ob := range this.observers {
if ob == observer {
count := len(this.observers)
if i == 0 {
this.observers = this.observers[1:]
} else if i == count-1 {
this.observers = this.observers[:count-1]
} else {
arr := this.observers[:i]
arr = append(arr, this.observers[i+1:]...)
this.observers = arr
}
}
}
}
func (this *PlatformGameGroupMgr) GetGameGroup(groupId int32) *webapi_proto.GameConfigGroup {
if g, exist := this.groups[groupId]; exist {
return g
}
return nil
}
func (this *PlatformGameGroupMgr) UpsertGameGroup(conf *webapi_proto.GameConfigGroup) {
if conf == nil {
return
}
dbGameFree := srvdata.PBDB_GameFreeMgr.GetData(conf.GetLogicId())
if dbGameFree == nil {
return
}
if conf.DbGameFree == nil {
conf.DbGameFree = dbGameFree
} else {
CopyDBGameFreeField(dbGameFree, conf.DbGameFree)
}
old, ok := this.groups[conf.GetId()]
//更新相关配置
this.groups[conf.GetId()] = conf
//发布更新事件
if ok && old != nil {
this.OnGameGroupUpdate(old, conf)
}
}
func (this *PlatformGameGroupMgr) OnGameGroupUpdate(oldCfg, newCfg *webapi_proto.GameConfigGroup) {
for _, observer := range this.observers {
observer.OnGameGroupUpdate(oldCfg, newCfg)
}
}
func init() {
}