go_dreamfactory/modules/caninerabbit/module.go
2023-11-02 21:18:05 +08:00

145 lines
3.4 KiB
Go

package caninerabbit
import (
"encoding/json"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func NewModule() core.IModule {
m := new(CanineRabbit)
return m
}
/*
模块名称:猜颜色
*/
type CanineRabbit struct {
modules.ModuleBase
service comm.IService
api *apiComp
configure *configureComp
model *modelComp
rooms *roomsComp
gameInvite comm.IGameInvite
}
// 模块名
func (this *CanineRabbit) GetType() core.M_Modules {
return comm.ModuleCanineRabbit
}
// 模块初始化接口 注册用户创建角色事件
func (this *CanineRabbit) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(comm.IService)
return
}
func (this *CanineRabbit) Start() (err error) {
if err = this.ModuleBase.Start(); err != nil {
return
}
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleGameInvite); err != nil {
return
}
this.gameInvite = module.(comm.IGameInvite)
return
}
func (this *CanineRabbit) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
this.model = this.RegisterComp(new(modelComp)).(*modelComp)
this.rooms = this.RegisterComp(new(roomsComp)).(*roomsComp)
}
func (this *CanineRabbit) CreateRoom(sessions []comm.IUserSession, rulesStr string) (roomid string, err error) {
var (
rules *pb.DBCanineRabbitRules = &pb.DBCanineRabbitRules{}
chess []*pb.DBCanineRabbitChess
red *pb.DBUser
redtype, bluetype int32
blue *pb.DBUser
room *Room
)
if err = json.Unmarshal([]byte(rulesStr), rules); err != nil {
this.Error("解析规则json", log.Field{Key: "err", Value: err.Error()})
return
}
//发起者 red
red, err = this.ModuleUser.GetUser(sessions[0].GetUserId())
if err != nil {
this.Error("未找到红方信息", log.Field{Key: "uid", Value: sessions[0].GetUserId()})
return
}
blue, err = this.ModuleUser.GetUser(sessions[1].GetUserId())
if err != nil {
this.Error("未找到蓝方信息", log.Field{Key: "uid", Value: sessions[1].GetUserId()})
return
}
chess = make([]*pb.DBCanineRabbitChess, 0)
roomid = primitive.NewObjectID().Hex()
chess = append(chess, &pb.DBCanineRabbitChess{
Id: 1,
Stype: 0,
X: 4,
Y: 1,
})
chess = append(chess, &pb.DBCanineRabbitChess{
Id: 2,
Stype: 1,
X: 1,
Y: 0,
})
chess = append(chess, &pb.DBCanineRabbitChess{
Id: 3,
Stype: 1,
X: 0,
Y: 1,
})
chess = append(chess, &pb.DBCanineRabbitChess{
Id: 4,
Stype: 1,
X: 1,
Y: 2,
})
if rules.RedType == 0 {
redtype = 0
bluetype = 1
} else {
redtype = 1
bluetype = 0
}
if room, err = this.rooms.newRoom(&pb.DBCanineRabbitRoom{
Rid: roomid,
Rules: rules,
Red: &pb.DBCanineRabbitRoomPlayer{
Info: comm.GetUserBaseInfo(red),
Ctype: redtype,
},
Blue: &pb.DBCanineRabbitRoomPlayer{
Info: comm.GetUserBaseInfo(blue),
Ctype: bluetype,
},
Chess: chess,
}, sessions); err != nil {
this.Error("创建房间错误", log.Field{Key: "err", Value: err.Error()})
return
}
go room.GameStart()
return
}