From 21dd60be53656ea4113ea595423ff49b95f67425 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 16 Oct 2023 14:57:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=B6=88=E6=B6=88=E4=B9=90?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/entertainment/api.go | 32 ++++++++++++++++ modules/entertainment/configure.go | 27 +++++++++++++ modules/entertainment/gamemgr.go | 61 ++++++++++++++++++++++++++++++ modules/entertainment/model.go | 26 +++++++++++++ modules/entertainment/module.go | 51 +++++++++++++++++++++++++ modules/entertainment/room.go | 18 +++++++++ 6 files changed, 215 insertions(+) create mode 100644 modules/entertainment/api.go create mode 100644 modules/entertainment/configure.go create mode 100644 modules/entertainment/gamemgr.go create mode 100644 modules/entertainment/model.go create mode 100644 modules/entertainment/module.go create mode 100644 modules/entertainment/room.go diff --git a/modules/entertainment/api.go b/modules/entertainment/api.go new file mode 100644 index 000000000..e6e6dbcb1 --- /dev/null +++ b/modules/entertainment/api.go @@ -0,0 +1,32 @@ +package entertainment + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" +) + +type apiComp struct { + modules.MCompGate + service core.IService + module *Entertainment + chat comm.IChat +} + +//组件初始化接口 +func (this *apiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompGate.Init(service, module, comp, options) + this.module = module.(*Entertainment) + this.service = service + return +} + +func (this *apiComp) Start() (err error) { + err = this.MCompGate.Start() + var module core.IModule + if module, err = this.service.GetModule(comm.ModuleChat); err != nil { + return + } + this.chat = module.(comm.IChat) + return +} diff --git a/modules/entertainment/configure.go b/modules/entertainment/configure.go new file mode 100644 index 000000000..d4e7c6eff --- /dev/null +++ b/modules/entertainment/configure.go @@ -0,0 +1,27 @@ +package entertainment + +import ( + "go_dreamfactory/modules" + + "go_dreamfactory/lego/core" +) + +const ( + dragon_trainlv = "game_trainlv.json" + dragon_play = "game_dragonplay.json" + game_buzkashimount = "game_buzkashimount.json" + game_dragonlvitem = "game_dragonlvitem.json" +) + +// /配置管理组件 +type configureComp struct { + modules.MCompConfigure + module *Entertainment +} + +// 组件初始化接口 +func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.MCompConfigure.Init(service, module, comp, options) + this.module = module.(*Entertainment) + return +} diff --git a/modules/entertainment/gamemgr.go b/modules/entertainment/gamemgr.go new file mode 100644 index 000000000..f0b7cd215 --- /dev/null +++ b/modules/entertainment/gamemgr.go @@ -0,0 +1,61 @@ +package entertainment + +import ( + "fmt" + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/core/cbase" + "go_dreamfactory/pb" + "sync" + + "google.golang.org/protobuf/proto" +) + +/* +游戏管理组件 +*/ +type gameMgrComp struct { + cbase.ModuleCompBase + module *Entertainment + lock sync.RWMutex + rooms map[string]*Room +} + +func (this *gameMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + err = this.ModuleCompBase.Init(service, module, comp, options) + this.module = module.(*Entertainment) + this.rooms = make(map[string]*Room) + return +} + +func (this *gameMgrComp) CreateRoom(room *Room) { + this.lock.Lock() + this.rooms[room.Id] = room + this.lock.Unlock() +} + +func (this *gameMgrComp) CloseRoom(id string) { + this.lock.Lock() + delete(this.rooms, id) + this.lock.Unlock() +} + +func (this *gameMgrComp) RoomDistribute(rid string, session comm.IUserSession, req proto.Message) (errdata *pb.ErrorData) { + var ( + room *Room + ok bool + ) + this.lock.RLock() + room, ok = this.rooms[rid] + this.lock.RUnlock() + if ok { + errdata = room.ReceiveMessage(session, req) + } else { + errdata = &pb.ErrorData{ + Code: pb.ErrorCode_ReqParameterError, + Title: pb.ErrorCode_ReqParameterError.String(), + Message: fmt.Sprintf("on found room:%s", rid), + } + } + return +} diff --git a/modules/entertainment/model.go b/modules/entertainment/model.go new file mode 100644 index 000000000..d31f67c9b --- /dev/null +++ b/modules/entertainment/model.go @@ -0,0 +1,26 @@ +package entertainment + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/x/bsonx" +) + +type modelComp struct { + modules.MCompModel + module *Entertainment +} + +func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { + this.TableName = comm.TableDragon + err = this.MCompModel.Init(service, module, comp, options) + this.module = module.(*Entertainment) + // 通过uid创建索引 + this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ + Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, + }) + return +} diff --git a/modules/entertainment/module.go b/modules/entertainment/module.go new file mode 100644 index 000000000..1bbc582b0 --- /dev/null +++ b/modules/entertainment/module.go @@ -0,0 +1,51 @@ +package entertainment + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/lego/core" + "go_dreamfactory/modules" +) + +func NewModule() core.IModule { + m := new(Entertainment) + return m +} + +type Entertainment struct { + modules.ModuleBase + service core.IService + api *apiComp + configure *configureComp + model *modelComp + gameMgr *gameMgrComp +} + +// 模块名 +func (this *Entertainment) GetType() core.M_Modules { + return comm.ModuleDragon +} + +// 模块初始化接口 注册用户创建角色事件 +func (this *Entertainment) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { + if err = this.ModuleBase.Init(service, module, options); err != nil { + return + } + this.service = service + return +} + +// 装备组件 +func (this *Entertainment) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.api = this.RegisterComp(new(apiComp)).(*apiComp) + this.model = this.RegisterComp(new(modelComp)).(*modelComp) + this.configure = this.RegisterComp(new(configureComp)).(*configureComp) + this.gameMgr = this.RegisterComp(new(gameMgrComp)).(*gameMgrComp) +} + +func (this *Entertainment) Start() (err error) { + if err = this.ModuleBase.Start(); err != nil { + return + } + return +} diff --git a/modules/entertainment/room.go b/modules/entertainment/room.go new file mode 100644 index 000000000..7dfa41df8 --- /dev/null +++ b/modules/entertainment/room.go @@ -0,0 +1,18 @@ +package entertainment + +import ( + "go_dreamfactory/comm" + "go_dreamfactory/pb" + + "google.golang.org/protobuf/proto" +) + +//游戏房间 +type Room struct { + Id string +} + +func (this *Room) ReceiveMessage(session comm.IUserSession, req proto.Message) (errdata *pb.ErrorData) { + + return +}