更新派遣概率

This commit is contained in:
wh_zcy 2023-03-29 22:22:49 +08:00
parent 7253886ac5
commit 7c709ccca7
2 changed files with 64 additions and 62 deletions

View File

@ -78,57 +78,36 @@ func (this *modelDispatch) getDBDispatch(uid string) (dis *pb.DBDispatch) {
return
}
// 获取任务权重数据
func (this *modelDispatch) getTasksWeight(uid string, d *pb.DBDispatch) []int32 {
// 获取随机任务ID
func (this *modelDispatch) getTasksWeight(uid string, d *pb.DBDispatch) int32 {
if d == nil {
return []int32{}
return 0
}
if d.Nb == nil {
return []int32{}
return 0
}
conf, err := this.module.configure.getDispatchLvConf(d.Nb.Lv)
if err != nil || conf == nil {
this.module.Error("配置不存在", log.Field{Key: "error", Value: err})
return []int32{}
return 0
}
items := make([]*comm.WeightItem, 0)
for i, v := range conf.Probability {
items = append(items, &comm.WeightItem{Id: (i + 1), Weight: int(v)})
}
// 取权重数组下标
i := comm.GetRandW(conf.Probability)
this.module.Debug("随机下标", log.Field{Key: "idx", Value: i})
confList, err := this.module.configure.getDispatchTaskConfByType(int32(i + 1))
wr := comm.NewWeightedRandom(items)
//根据权重选出任务类型
var taskType int
if c := wr.Pick(); c != nil {
taskType = c.Id.(int)
}
confList, err := this.module.configure.getDispatchTaskConfByType(int32(taskType))
var tIds []int32
if len(d.Nb.Tasks) == 0 {
for _, v := range confList {
if int(v.Type) == taskType {
tIds = append(tIds, v.Id)
}
}
} else {
var existIds []int32
for _, v := range d.Nb.Tasks {
existIds = append(existIds, v.TaskId)
}
for _, v := range confList {
if int(v.Type) == taskType {
if _, ok := utils.Findx(existIds, v.Id); !ok {
tIds = append(tIds, v.Id)
}
}
}
for _, v := range confList {
tIds = append(tIds, v.Id)
}
return tIds
idex := utils.RandomNumbers(0, len(tIds), 1)
if len(idex) == 0 {
return 0
}
return tIds[idex[0]]
}
// 随机任务
@ -137,19 +116,33 @@ func (this *modelDispatch) taskRandom(uid string, dispatch *pb.DBDispatch) (task
dispatch.Nb.Lv = 1
}
tIds := this.getTasksWeight(uid, dispatch)
if len(tIds) == 0 {
return
}
//判断随机数量
var n int
n = len(dispatch.Nb.Tasks)
var existIds []int32
for _, v := range dispatch.Nb.Tasks {
existIds = append(existIds, v.TaskId)
}
if n == 0 {
//随机6个任务
ids := utils.RandomNumbers(1, len(tIds), 6)
for _, id := range ids {
total := 0
for total < 6 {
tId := this.getTasksWeight(uid, dispatch)
if tId == 0 {
return
}
//去重
if _, ok := utils.Findx(existIds, int32(tId)); !ok {
existIds = append(existIds, int32(tId))
total++
}
}
for _, id := range existIds {
taskConf, err := this.module.configure.getDispatchTaskConf(int32(id))
if err != nil {
continue
@ -162,13 +155,12 @@ func (this *modelDispatch) taskRandom(uid string, dispatch *pb.DBDispatch) (task
})
}
} else {
tasks = dispatch.Nb.Tasks
var randCount int
for i := 0; i < len(tasks); i++ {
for i := 0; i < len(dispatch.Nb.Tasks); i++ {
//只随机未接取的任务
if tasks[i].Status == 0 {
if dispatch.Nb.Tasks[i].Status == 0 {
//删除
tasks = append(tasks[:i], tasks[i+1:]...)
dispatch.Nb.Tasks = append(dispatch.Nb.Tasks[:i], dispatch.Nb.Tasks[i+1:]...)
i--
randCount++
}
@ -185,18 +177,27 @@ func (this *modelDispatch) addRandomTask(uid string, dispatch *pb.DBDispatch, n
tasks = dispatch.Nb.Tasks
return
}
randomTaskIds := this.getTasksWeight(uid, dispatch)
if len(randomTaskIds) == 0 {
return nil
var existIds []int32
for _, v := range dispatch.Nb.Tasks {
existIds = append(existIds, v.TaskId)
}
//追加n条随机任务
ids := utils.RandomNumbers(1, len(randomTaskIds), n)
if len(ids) <= 0 {
return nil
total := 0
for total < n {
rid := this.getTasksWeight(uid, dispatch)
if rid == 0 {
return nil
}
//去重
if _, ok := utils.Findx(existIds, int32(rid)); !ok {
existIds = append(existIds, int32(rid))
total++
}
}
for _, id := range ids {
for _, id := range existIds {
taskConf, err := this.module.configure.getDispatchTaskConf(int32(id))
if err != nil {
return nil

View File

@ -41,7 +41,7 @@ type ModuleRtask struct {
modelRtaskRecord *ModelRtaskRecord
api *apiComp
configure *configureComp
handleMap sync.Map //map[int32]*rtaskCondi //任务校验处理器
handleMap sync.Map //map[int32]*rtaskCondi //任务校验处理器
}
func NewModule() core.IModule {
@ -556,21 +556,22 @@ func (this *ModuleRtask) SendToRtask(session comm.IUserSession, rtaskType comm.T
}
func (this *ModuleRtask) TriggerTask(uid string, taskParams ...*comm.TaskParam) {
this.Debug("任务处理",
log.Field{Key: "uid", Value: uid},
log.Field{Key: "params", Value: taskParams})
session, ok := this.GetUserSession(uid)
if !ok {
return
}
for _, tp := range taskParams {
if code := this.processOneTask(session, tp.TT, tp.Params...); code != pb.ErrorCode_Success {
// this.Debug("任务处理",
// log.Field{Key: "uid", Value: uid},
// log.Field{Key: "taskType", Value: tp.TT},
// log.Field{Key: "params", Value: tp.Params},
// log.Field{Key: "code", Value: code})
}
session.Push()
comm.PuttaskParam(tp)
}
this.PutUserSession(session)
return
}