207 lines
4.1 KiB
Go
207 lines
4.1 KiB
Go
package dispatch
|
|
|
|
import (
|
|
"errors"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/utils"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
type modelDispatch struct {
|
|
modules.MCompModel
|
|
module *Dispatch
|
|
service core.IService
|
|
}
|
|
|
|
func (this *modelDispatch) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.TableName = comm.TableDispatch
|
|
this.module = module.(*Dispatch)
|
|
this.service = service
|
|
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "_id", Value: bsonx.Int32(1)}},
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (this *modelDispatch) initDispatch(uid string) (dis *pb.DBDispatch) {
|
|
taskIds, err := this.taskRandom(uid, 1)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if len(taskIds) == 0 {
|
|
return
|
|
}
|
|
|
|
dis = &pb.DBDispatch{
|
|
Uid: uid,
|
|
Ticket: 6,
|
|
Nb: &pb.Noticeboard{
|
|
Lv: 1,
|
|
},
|
|
}
|
|
|
|
for _, v := range taskIds {
|
|
taskConf, err := this.module.configure.getDispatchTaskConf(v)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
//接续截至时间
|
|
duration := configure.Now().Unix() + int64(taskConf.Taskcd)
|
|
dis.Nb.Tasks = append(dis.Nb.Tasks, &pb.DispatchTask{
|
|
TaskId: v,
|
|
Duration: duration,
|
|
})
|
|
}
|
|
|
|
if err := this.Add(uid, dis); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 获取派遣数据
|
|
func (this *modelDispatch) getDBDispatch(uid string) *pb.DBDispatch {
|
|
dis := &pb.DBDispatch{}
|
|
if err := this.Get(uid, dis); err != nil {
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 随机任务
|
|
func (this *modelDispatch) taskRandom(uid string, noticeLv int32) (taskIds []int32, err error) {
|
|
d := this.getDBDispatch(uid)
|
|
if d == nil {
|
|
return nil, errors.New("no data")
|
|
}
|
|
|
|
if d.Nb == nil {
|
|
return nil, errors.New("notice is nil")
|
|
}
|
|
|
|
conf, err := this.module.configure.getDispatchLvConf(noticeLv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var items []*comm.WeightItem
|
|
for i, v := range conf.Probability {
|
|
items = append(items, &comm.WeightItem{Id: (i + 1), Weight: int(v)})
|
|
}
|
|
|
|
wr := comm.NewWeightedRandom(items)
|
|
//任务类型
|
|
var taskType int
|
|
if c := wr.Pick(); c != nil {
|
|
taskType = c.Id.(int)
|
|
}
|
|
|
|
confList := this.module.configure.getDispatchListConf()
|
|
var tIds []int32
|
|
for _, v := range confList {
|
|
if int(v.Type) == taskType {
|
|
tIds = append(tIds, v.Id)
|
|
}
|
|
}
|
|
|
|
//判断随机数量
|
|
var n int
|
|
n = len(d.Nb.Tasks)
|
|
|
|
if len(taskIds) > 0 {
|
|
ids := utils.RandomNumbers(0, len(tIds), n)
|
|
for i := 0; i < len(ids); i++ {
|
|
taskIds = append(taskIds, tIds[i])
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 验证英雄的条件
|
|
func (this *modelDispatch) validHeroCond(uid string, taskId int32, heroId string) (ok bool, err error) {
|
|
//taskConf
|
|
gd, err := this.module.configure.getDispatchTaskConf(taskId)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
//获取英雄信息
|
|
hero, code := this.module.ModuleHero.GetHeroByObjID(uid, heroId)
|
|
|
|
//校验英雄的条件
|
|
if code == pb.ErrorCode_Success && hero != nil {
|
|
for _, v := range gd.Taskreq {
|
|
switch v.Key {
|
|
case 1: //lv
|
|
if hero.Lv >= v.Param {
|
|
ok = true
|
|
return
|
|
}
|
|
case 2: //star
|
|
if hero.Star >= v.Param {
|
|
ok = true
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//校验英雄是否已被派遣
|
|
dispatch := this.getDBDispatch(uid)
|
|
if dispatch == nil {
|
|
return
|
|
}
|
|
|
|
for _, v := range dispatch.Nb.Tasks {
|
|
for _, h := range v.HeroIds {
|
|
if h == heroId {
|
|
ok = false
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// 派遣
|
|
func (this *modelDispatch) dispatch(uid string, taskId int32, heroIds []string) error {
|
|
for _, heroId := range heroIds {
|
|
if ok, err := this.validHeroCond(uid, taskId, heroId); err == nil {
|
|
if !ok {
|
|
return comm.NewCustomError(pb.ErrorCode_DispatchHeroNoReached)
|
|
}
|
|
} else {
|
|
return err
|
|
}
|
|
}
|
|
|
|
d := this.getDBDispatch(uid)
|
|
if d == nil {
|
|
return errors.New("no data")
|
|
}
|
|
|
|
for _, v := range d.Nb.Tasks {
|
|
if v.TaskId == taskId && v.Status == 0 {
|
|
v.HeroIds = heroIds
|
|
}
|
|
}
|
|
|
|
update := map[string]interface{}{
|
|
"nb": d.Nb.Tasks,
|
|
}
|
|
if err := this.Change(uid, update); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|