186 lines
5.7 KiB
Go
186 lines
5.7 KiB
Go
package moonfantasy
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/lego/sys/timewheel"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/sys/db"
|
|
"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.Get(mid, result); err != nil && err != mongo.ErrNilDocument {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
///插叙用户秘境列表
|
|
func (this *modelDreamComp) querymfantasys(mids []string) (fantasys []*pb.DBMoonFantasy, err error) {
|
|
fantasys = make([]*pb.DBMoonFantasy, 0)
|
|
if err = this.Gets(mids, &fantasys); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
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: time.Now().Unix(),
|
|
Join: []*pb.UserInfo{user},
|
|
Numup: boss.Challengenum,
|
|
Unitmup: boss.Fightnum,
|
|
Record: make(map[string]int32),
|
|
}
|
|
if err = this.Add(result.Id, result); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//触发月之秘境
|
|
func (this *modelDreamComp) trigger(session comm.IUserSession, source *pb.BattleReport) {
|
|
var (
|
|
user *pb.DBUser
|
|
umfantasy *pb.DBUserMFantasy
|
|
globalconf *cfg.GameGlobalData
|
|
boss *cfg.GameDreamlandBoosData
|
|
mdata *pb.DBMoonFantasy
|
|
chat *pb.DBChat
|
|
err error
|
|
)
|
|
globalconf = this.module.configure.GetGlobalConf()
|
|
|
|
if time.Unix(umfantasy.LastTrigger, 0).Day() != time.Now().Day() {
|
|
umfantasy.TriggerNum = 0
|
|
umfantasy.LastTrigger = time.Now().Unix()
|
|
umfantasy.BuyNum = 0
|
|
if umfantasy.BattleNum < globalconf.DreamlandFightnum {
|
|
umfantasy.BattleNum = globalconf.DreamlandFightnum
|
|
}
|
|
}
|
|
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 umfantasy, err = this.module.modelUserMF.queryUsermfantasy(session.GetUserId()); err != nil {
|
|
return
|
|
}
|
|
if mdata, err = this.module.modelDream.addDreamData(&pb.UserInfo{Uid: user.Uid, Name: user.Name, Avatar: user.Avatar}, boss); err != nil {
|
|
return
|
|
}
|
|
umfantasy.Mfantasys = append(umfantasy.Mfantasys, mdata.Id)
|
|
this.module.modelUserMF.Change(session.GetUserId(), map[string]interface{}{
|
|
"mfantasys": umfantasy.Mfantasys,
|
|
"triggerNum": umfantasy.TriggerNum,
|
|
"lastTrigger": umfantasy.LastTrigger,
|
|
"buyNum": umfantasy.BuyNum,
|
|
"battleNum": umfantasy.BattleNum,
|
|
})
|
|
chat = &pb.DBChat{
|
|
Ctype: pb.ChatType_Moonfantasy,
|
|
Suid: session.GetUserId(),
|
|
Avatar: user.Avatar,
|
|
Uname: user.Name,
|
|
Slv: user.Lv,
|
|
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})
|
|
return
|
|
}
|
|
|
|
///查询好友数据
|
|
func (this *modelDreamComp) noticeuserfriend(session comm.IUserSession, mid string, chat *pb.DBChat) (code pb.ErrorCode) {
|
|
var (
|
|
model *db.DBModel
|
|
err error
|
|
)
|
|
if model, err = this.module.GetDBNodule(session, comm.TableFriend, 0); err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
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
|
|
}
|
|
for _, v := range friend.FriendIds {
|
|
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
|
|
// code = this.module.chat.SendWorldChat(stag, chat)
|
|
this.delaynoticeWorld(session.GetServiecTag(), mid, chat)
|
|
return
|
|
}
|
|
|
|
//延迟推送到 世界聊天频道
|
|
func (this *modelDreamComp) delaynoticeWorld(stag, 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)
|
|
}
|