go_dreamfactory/modules/warorder/modelWarorder.go
2023-08-24 18:45:06 +08:00

185 lines
4.8 KiB
Go

package warorder
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
"sync"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelWarorder struct {
modules.MCompModel
module *Warorder
lock sync.RWMutex
opentime map[int32]*pb.DBHuodong
}
func (this *modelWarorder) 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.TableWarorder
this.module = module.(*Warorder)
this.lock.Lock()
this.opentime = make(map[int32]*pb.DBHuodong)
this.lock.Unlock()
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelWarorder) getopentime(wtype int32) (activity *pb.DBHuodong, open bool) {
this.lock.RLock()
activity, open = this.opentime[wtype]
this.lock.RUnlock()
return
}
func (this *modelWarorder) setopentime(wtype int32, activity *pb.DBHuodong) {
this.lock.RLock()
this.opentime[wtype] = activity
this.lock.RUnlock()
return
}
// 获取用户全部的埋点数据
func (this *modelWarorder) getUserWarorders(uid string) (results *pb.DBWarorders, err error) {
results = &pb.DBWarorders{}
if err = this.Get(uid, results); err != nil && err != mgo.MongodbNil {
this.module.Errorln(err)
return
}
if err == mgo.MongodbNil {
err = nil
results = &pb.DBWarorders{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Warorder: make(map[int32]*pb.Warorder),
Dreamwarorder: make(map[int32]*pb.DreamWarorder),
}
err = this.Add(uid, results)
}
return
}
func (this *modelWarorder) updateUserWarorders(uid string, data *pb.DBWarorders) (err error) {
if err = this.Change(uid, map[string]interface{}{
"warorder": data.Warorder,
"dreamwarorder": data.Dreamwarorder,
}); err != nil {
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
return
}
return
}
//刷新日常任务
func (this *modelWarorder) refreshDayTask(info *pb.DreamWarorder, confs map[int32]*cfg.GamePassCheckTaskData, num int32) {
var (
tasks []*cfg.GamePassCheckTaskData = make([]*cfg.GamePassCheckTaskData, 0)
weight []int32 = make([]int32, 0)
indexs []int = make([]int, 0)
)
for _, v := range confs {
if v.Page == 1 {
tasks = append(tasks, v)
weight = append(weight, v.Pro)
}
}
indexs = comm.GetRandWs(weight, num)
for _, i := range indexs {
info.Daytasks = append(info.Daytasks, tasks[i].Id)
}
info.Daytime = configure.Now().Unix()
}
//刷新日常任务
func (this *modelWarorder) refreshWeekTask(info *pb.DreamWarorder, confs map[int32]*cfg.GamePassCheckTaskData, num int32) {
var (
tasks []*cfg.GamePassCheckTaskData = make([]*cfg.GamePassCheckTaskData, 0)
weight []int32 = make([]int32, 0)
indexs []int = make([]int, 0)
)
for _, v := range confs {
if v.Page == 2 {
tasks = append(tasks, v)
weight = append(weight, v.Pro)
}
}
indexs = comm.GetRandWs(weight, num)
for _, i := range indexs {
info.Weektasks = append(info.Weektasks, tasks[i].Id)
}
info.Weektime = configure.Now().Unix()
}
//战令活动结束
func (this *modelWarorder) settlement(wtype int32) {
var (
confs []*cfg.GamePassCheckData
cursor *mongo.Cursor
warorder *pb.Warorder
err error
ok bool
days int32
awards []*cfg.Gameatn
)
if confs, err = this.module.configure.getorder(wtype); err != nil {
this.module.Errorln(err)
return
}
if cursor, err = this.module.model.DB.Find(core.SqlTable(this.TableName), bson.M{}); err != nil {
this.module.Errorln(err)
return
}
for cursor.Next(context.Background()) {
temp := &pb.DBWarorders{}
if err = cursor.Decode(temp); err != nil {
this.module.Errorln(err)
continue
}
if warorder, ok = temp.Warorder[wtype]; !ok {
continue
}
days = int32(utils.DiffDays(configure.Now().Unix(), warorder.Opentime)) + 1
awards = make([]*cfg.Gameatn, 0)
for _, v := range confs {
if v.Parameter <= days {
if warorder.Freeprogress < v.Parameter {
awards = append(awards, v.FreeReward)
}
if warorder.Vip {
if warorder.Payprogress < v.Parameter {
awards = append(awards, v.PayReward...)
}
}
}
}
warorder.Freeprogress = days
if warorder.Vip {
warorder.Payprogress = days
}
if len(awards) > 0 {
this.module.ModuleMail.SendMailByUID(temp.Uid, "", awards, nil)
}
}
}