go_dreamfactory/modules/entertainment/match.go

120 lines
2.7 KiB
Go

package entertainment
import (
"context"
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"math/big"
"go_dreamfactory/lego/core"
"google.golang.org/protobuf/types/known/anypb"
)
/*
匹配组件
*/
type matchComp struct {
modules.MCompMatch
service core.IService
module *Entertainment
}
//组件初始化接口
func (this *matchComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompMatch.Init(service, module, comp, options)
this.module = module.(*Entertainment)
this.service = service
this.PoolName = "entertain"
return
}
func (this *matchComp) Start() (err error) {
err = this.MCompMatch.Start()
return
}
func (this *matchComp) MatchReq(v *pb.DBXXLMatch) (err error) {
data, _ := anypb.New(v)
err = this.module.service.RpcCall(
context.Background(),
comm.Service_Mainte,
string(comm.RPC_JoinMatchPools),
&pb.JoinMatchPoolReq{
Poolname: this.PoolName,
Uid: v.Userinfo.Uid,
Data: data,
Matchnum: 2, // 匹配数量2
Timeout: 5,
},
&pb.JoinMatchPoolResp{})
if err != nil {
this.module.Errorln(err)
return
}
return
}
func (this *matchComp) MatchNotic(players map[string]interface{}) (err error) {
var (
playerSlice []*pb.DBXXLMatch
p1 *pb.PlayerData
p2 *pb.PlayerData
)
playerSlice = make([]*pb.DBXXLMatch, 0, len(players))
for _, v := range players {
playerSlice = append(playerSlice, v.(*pb.DBXXLMatch))
}
for pos, v := range playerSlice {
if pos == 0 {
p1 = &pb.PlayerData{
Userinfo: v.Userinfo,
Cardid: v.Cardid,
Consumeexp: v.Consumeexp,
Skill: v.Skill,
}
} else if pos == 1 {
p2 = &pb.PlayerData{
Userinfo: v.Userinfo,
Cardid: v.Cardid,
Consumeexp: v.Consumeexp,
Skill: v.Skill,
}
} else {
break
}
}
if p1 == nil {
return
}
if p2 == nil { // 玩家2 是空 那么构建一个AI 对象
if robots, err := this.module.ModuleTools.RandRobotConfig(1); err == nil {
if len(robots) > 0 {
p2 = &pb.PlayerData{
Userinfo: comm.GetRobotBaseInfo(robots[0]),
Cardid: this.module.configure.GetRobotGameConsumeHero(),
}
p2.Userinfo.Uid = "999" // 机器人uid 无意义 这样做只是方便管理
n1, _ := rand.Int(rand.Reader, big.NewInt(100)) // AI 玩家积分波动
if n1.Int64()%2 == 0 {
p2.Consumeexp = p1.Consumeexp + int32(n1.Int64())
} else {
p2.Consumeexp = p1.Consumeexp - int32(n1.Int64())
if p2.Consumeexp < 0 { // 负数过滤
n1, _ := rand.Int(rand.Reader, big.NewInt(100))
p2.Consumeexp = int32(n1.Int64())
}
}
}
}
}
go func() {
this.module.gameMgr.CreateRoomByType(p1, p2, -1)
}()
return
}