go_dreamfactory/modules/moonfantasy/modelDream.go
2023-06-06 13:55:33 +08:00

256 lines
8.0 KiB
Go

package moonfantasy
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/lego/sys/timewheel"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
///论坛 数据组件
type modelDreamComp struct {
modules.MCompModel
module *Moonfantasy
}
//组件初始化接口
func (this *modelDreamComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Moonfantasy)
this.TableName = comm.TableMoonFantasy
this.Expired = 0
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *modelDreamComp) newDreamLock(mid string) (result *redis.RedisMutex, err error) {
return this.module.modelDream.NewRedisMutex(fmt.Sprintf("%s-%s_lock", this.TableName, mid), 5)
}
///查询月之秘境记录
func (this *modelDreamComp) querymfantasy(mid string) (result *pb.DBMoonFantasy, err error) {
result = &pb.DBMoonFantasy{}
if err = this.GetListObj("", mid, result); err != nil && err != mongo.ErrNilDocument {
this.module.Errorln(err)
}
return
}
///插叙用户秘境列表
func (this *modelDreamComp) querymfantasys(uid string) (result []*pb.DBMoonFantasy, err error) {
fantasys := make([]*pb.DBMoonFantasy, 0)
if err = this.GetList("", &fantasys); err != nil {
this.module.Errorf("err:%v", err)
}
result = make([]*pb.DBMoonFantasy, 0, len(fantasys))
for _, v := range fantasys {
for _, u := range v.Join {
if u.Uid == uid {
result = append(result, v)
}
}
}
return
}
///添加月之秘境记录
func (this *modelDreamComp) addDreamData(user *pb.UserInfo, boss *cfg.GameDreamlandBoosData) (result *pb.DBMoonFantasy, err error) {
result = &pb.DBMoonFantasy{
Id: primitive.NewObjectID().Hex(),
Uid: user.Uid,
Monster: boss.Bossid,
Ctime: configure.Now().Unix(),
Join: []*pb.UserInfo{user},
Numup: boss.Challengenum,
Unitmup: boss.Fightnum,
Expir: configure.Now().Add(time.Second * time.Duration(boss.DreamlandLimit)).Unix(),
Record: make(map[string]int32),
}
if err = this.AddList("", result.Id, result); err != nil {
this.module.Errorln(err)
}
return
}
//触发月之秘境
func (this *modelDreamComp) trigger(session comm.IUserSession) {
var (
user *pb.DBUser
umfantasy *pb.DBUserMFantasy
globalconf *cfg.GameGlobalData
boss *cfg.GameDreamlandBoosData
mdata *pb.DBMoonFantasy
chat *pb.DBChat
err error
)
globalconf = this.module.ModuleTools.GetGlobalConf()
if umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil {
return
}
this.module.modelUserMF.recoverTicket(session, umfantasy)
if umfantasy.TriggerNum >= globalconf.DreamlandTriggernum {
return
}
if boss, err = this.module.configure.GetMonster(); err != nil {
this.module.Errorln(err)
return
}
if user = this.module.ModuleUser.GetUser(session.GetUserId()); user == nil {
this.module.Errorf("no found uer:%d", session.GetUserId())
return
}
if mdata, err = this.module.modelDream.addDreamData(&pb.UserInfo{Uid: user.Uid, Name: user.Name, Avatar: user.Avatar, Lv: user.Lv}, boss); err != nil {
return
}
// umfantasy.Mfantasys = append(umfantasy.Mfantasys, mdata.Id)
umfantasy.TriggerNum++
umfantasy.LastTrigger = configure.Now().Unix()
this.module.modelUserMF.updateUserInfo(umfantasy)
chat = &pb.DBChat{
Ctype: pb.ChatType_Moonfantasy,
Suid: session.GetUserId(),
Avatar: user.Avatar,
Uname: user.Name,
Slv: user.Lv,
Ctime: configure.Now().Unix(),
Stag: session.GetServiecTag(),
Content: mdata.Monster,
AppendStr: mdata.Id,
}
this.module.modelDream.noticeuserfriend(session, mdata.Id, chat)
session.SendMsg(string(this.module.GetType()), "trigger", &pb.MoonfantasyTriggerPush{Issucc: true, Mid: mdata.Id, Monster: mdata.Monster})
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype87, 1)
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype87, 1))
return
}
//触发月之秘境
func (this *modelDreamComp) triggerbyid(session comm.IUserSession, boosid string) (err error) {
var (
user *pb.DBUser
umfantasy *pb.DBUserMFantasy
globalconf *cfg.GameGlobalData
boss *cfg.GameDreamlandBoosData
mdata *pb.DBMoonFantasy
chat *pb.DBChat
)
globalconf = this.module.ModuleTools.GetGlobalConf()
if umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil {
return
}
this.module.modelUserMF.recoverTicket(session, umfantasy)
if umfantasy.TriggerNum >= globalconf.DreamlandTriggernum {
return
}
if boss, err = this.module.configure.GetMonsterById(boosid); err != nil {
this.module.Errorln(err)
return
}
if user = this.module.ModuleUser.GetUser(session.GetUserId()); user == nil {
this.module.Errorf("no found uer:%d", session.GetUserId())
return
}
if mdata, err = this.module.modelDream.addDreamData(&pb.UserInfo{Uid: user.Uid, Name: user.Name, Avatar: user.Avatar, Lv: user.Lv}, boss); err != nil {
return
}
// umfantasy.Mfantasys = append(umfantasy.Mfantasys, mdata.Id)
umfantasy.TriggerNum++
umfantasy.LastTrigger = configure.Now().Unix()
this.module.modelUserMF.updateUserInfo(umfantasy)
chat = &pb.DBChat{
Ctype: pb.ChatType_Moonfantasy,
Suid: session.GetUserId(),
Avatar: user.Avatar,
Uname: user.Name,
Slv: user.Lv,
Ctime: configure.Now().Unix(),
Stag: session.GetServiecTag(),
Content: mdata.Monster,
AppendStr: mdata.Id,
}
this.module.modelDream.noticeuserfriend(session, mdata.Id, chat)
session.SendMsg(string(this.module.GetType()), "trigger", &pb.MoonfantasyTriggerPush{Issucc: true, Mid: mdata.Id, Monster: mdata.Monster})
// this.module.ModuleRtask.SendToRtask(session, comm.Rtype87, 1)
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), comm.GetBuriedParam(comm.Rtype87, 1))
return
}
///查询好友数据
func (this *modelDreamComp) noticeuserfriend(session comm.IUserSession, mid string, chat *pb.DBChat) (errdata *pb.ErrorData) {
var (
// model *db.DBModel
// err error
friend []string
)
// if model, err = this.module.GetDBNodule(session, comm.TableFriend, 0); err != nil {
// errdata = &pb.ErrorData{
// Code: pb.ErrorCode_DBError,
// Title: pb.ErrorCode_DBError.ToString(),
// Message: err.Error(),
// }
// this.module.Errorf("session:%v err:%v", session, err)
// return
// }
// friend := &pb.DBFriend{Uid: session.GetUserId(), FriendIds: make([]string, 0)}
// if err = model.Get(session.GetUserId(), friend); err != nil && err != mgo.MongodbNil {
// this.module.Errorln(err)
// return
// }
friend = this.module.friend.GetFriendList(session.GetUserId())
for _, v := range friend {
temp := *chat
temp.Id = primitive.NewObjectID().Hex()
temp.Channel = pb.ChatChannel_Private
temp.Ruid = v
this.module.chat.SendUserChat(&temp)
}
chat.Id = primitive.NewObjectID().Hex()
chat.Channel = pb.ChatChannel_World
chat.Stag = session.GetServiecTag()
// code = this.module.chat.SendWorldChat(stag, chat)
this.delaynoticeWorld(mid, chat)
return
}
//延迟推送到 世界聊天频道
func (this *modelDreamComp) delaynoticeWorld(mid string, chat *pb.DBChat) {
timewheel.Add(time.Minute*15, func(t *timewheel.Task, i ...interface{}) {
_mid := i[0].(string)
_chat := i[1].(*pb.DBChat)
if mdata, err := this.querymfantasy(_mid); err != nil {
return
} else {
if len(mdata.Join) >= int(mdata.Numup) { //参与人数已达上限 不予推送
return
}
}
this.module.chat.SendWorldChat(_chat)
}, mid, chat)
}
func (this *modelDreamComp) checkMFantasyExpiration(mf *pb.DBMoonFantasy) bool {
if configure.Now().After(time.Unix(mf.Expir, 0)) { //已过期
this.DelListlds("", []string{mf.Id})
return true
}
return false
}