234 lines
6.1 KiB
Go
234 lines
6.1 KiB
Go
package expedition
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/lego/sys/redis"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/sys/db"
|
|
"sync"
|
|
)
|
|
|
|
type ModelExpedition struct {
|
|
modules.MCompModel
|
|
module *Expedition
|
|
conflock sync.RWMutex
|
|
bossconf *pb.DBGuildGveBossConf
|
|
}
|
|
|
|
func (this *ModelExpedition) 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.TableExpedition
|
|
this.module = module.(*Expedition)
|
|
return
|
|
}
|
|
|
|
func (this *ModelExpedition) Start() (err error) {
|
|
err = this.MCompModel.Start()
|
|
return
|
|
}
|
|
|
|
// 批量查询工会信息
|
|
func (this *ModelExpedition) querySociatys(guildids []string) (result []*pb.DBGuildGve, err error) {
|
|
result = make([]*pb.DBGuildGve, 0)
|
|
if _, err = this.Gets(guildids, &result); err != nil && err != mgo.MongodbNil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
err = nil
|
|
return
|
|
}
|
|
|
|
// 获取用户全部的埋点数据
|
|
func (this *ModelExpedition) getInfo(guildid string) (result *pb.DBExpedition, err error) {
|
|
var (
|
|
confs []*cfg.GameExpeditionBoosData
|
|
)
|
|
result = &pb.DBExpedition{}
|
|
if err = this.GetByID(guildid, result); err != nil && err != mgo.MongodbNil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
if err == mgo.MongodbNil {
|
|
if confs, err = this.module.configure.getGameExpeditionBoosDatas(1); err != nil {
|
|
return
|
|
}
|
|
result = &pb.DBExpedition{
|
|
Guildid: guildid,
|
|
Lv: 1,
|
|
Refresh: configure.Now().Unix(),
|
|
Indexboos: 0,
|
|
Boos: make([]*pb.DBExpeditionBoos, 0),
|
|
}
|
|
for _, v := range confs {
|
|
result.Boos = append(result.Boos, &pb.DBExpeditionBoos{
|
|
Boosid: v.BossId,
|
|
Hp: v.Fighting,
|
|
Members: make(map[string]*pb.DBExpeditionMember),
|
|
})
|
|
}
|
|
err = this.Add(guildid, result)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *ModelExpedition) updateExpedition(data *pb.DBExpedition) (err error) {
|
|
if err = this.ChangeById(data.Guildid, map[string]interface{}{
|
|
"lv": data.Lv,
|
|
"indexboos": data.Indexboos,
|
|
"boos": data.Boos,
|
|
}); err != nil {
|
|
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 刷新全局配置
|
|
func (this *ModelExpedition) refreshBoos(info *pb.DBExpedition) (conf *pb.DBGuildGveBossConf, err error) {
|
|
var (
|
|
confs []*cfg.GameExpeditionBoosData
|
|
)
|
|
if confs, err = this.module.configure.getGameExpeditionBoosDatas(info.Lv); err != nil {
|
|
return
|
|
}
|
|
info.Boos = make([]*pb.DBExpeditionBoos, 0)
|
|
for _, v := range confs {
|
|
info.Boos = append(info.Boos, &pb.DBExpeditionBoos{
|
|
Boosid: v.BossId,
|
|
Hp: v.Fighting,
|
|
Members: make(map[string]*pb.DBExpeditionMember),
|
|
})
|
|
}
|
|
info.Refresh = configure.Now().Unix()
|
|
info.Indexboos = 0
|
|
return
|
|
}
|
|
|
|
//结算boos
|
|
func (this *ModelExpedition) settlementboos(info *pb.DBExpedition, boos *pb.DBExpeditionBoos) (changed bool) {
|
|
var (
|
|
fightingValue int32
|
|
)
|
|
if len(boos.Members) == 0 {
|
|
return
|
|
}
|
|
for _, v := range boos.Members {
|
|
fightingValue += v.Totalpower
|
|
}
|
|
boos.Hp -= fightingValue
|
|
if boos.Hp < 0 {
|
|
if info.Indexboos < int32(len(info.Boos))-1 {
|
|
info.Indexboos++
|
|
} else {
|
|
info.Lv++
|
|
}
|
|
}
|
|
boos.Crusaded = true
|
|
changed = true
|
|
return
|
|
}
|
|
|
|
// 分布式锁
|
|
func (this *ModelExpedition) userlock(id string) (result *redis.RedisMutex, err error) {
|
|
return this.DBModel.Redis.NewRedisMutex(fmt.Sprintf("%s-lockexpedition:%s", this.DBModel.ServiceId, id))
|
|
}
|
|
|
|
// boos 血量变化推送
|
|
func (this *ModelExpedition) booshpchangepush(unionid string, info *pb.DBGuildGve) {
|
|
var (
|
|
members []*pb.SociatyMemberInfo
|
|
users []string = make([]string, 0)
|
|
errdata *pb.ErrorData
|
|
)
|
|
if members, errdata = this.module.sociaty.MembersBySociatyId(unionid); errdata != nil {
|
|
this.module.Debug("获取工会成员列表!", log.Field{Key: "errdata", Value: errdata})
|
|
return
|
|
}
|
|
for _, v := range members {
|
|
users = append(users, v.Uinfo.Uid)
|
|
|
|
}
|
|
this.module.SendMsgToUsers(string(this.module.GetType()), "booschange", &pb.GuildGveBoosChangePush{
|
|
Info: info,
|
|
}, users...)
|
|
}
|
|
|
|
// boos 信息变化推送
|
|
func (this *ModelExpedition) infochangepush(unionid string, info *pb.DBGuildGve) {
|
|
var (
|
|
members []*pb.SociatyMemberInfo
|
|
users []string = make([]string, 0)
|
|
errdata *pb.ErrorData
|
|
)
|
|
if members, errdata = this.module.sociaty.MembersBySociatyId(unionid); errdata != nil {
|
|
this.module.Debug("获取工会成员列表!", log.Field{Key: "errdata", Value: errdata})
|
|
return
|
|
}
|
|
for _, v := range members {
|
|
users = append(users, v.Uinfo.Uid)
|
|
|
|
}
|
|
this.module.SendMsgToUsers(string(this.module.GetType()), "infochange", &pb.GuildGveInfoChangePush{
|
|
Info: info,
|
|
}, users...)
|
|
}
|
|
|
|
// 更新埋点数据到db中
|
|
func (this *ModelExpedition) guildgveModel() (model *guildgveModel, err error) {
|
|
var (
|
|
conn *db.DBConn
|
|
m *db.DBModel
|
|
)
|
|
if db.IsCross() {
|
|
model = &guildgveModel{module: this.module, model: this.DBModel}
|
|
} else {
|
|
if conn, err = db.Cross(); err != nil {
|
|
return
|
|
}
|
|
m = db.NewDBModel(db.CrossTag(), this.TableName, conn)
|
|
model = &guildgveModel{module: this.module, model: m}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 埋点专属模型 会封装特殊的数据转换接口
|
|
type guildgveModel struct {
|
|
module *Expedition
|
|
model *db.DBModel
|
|
}
|
|
|
|
// 分布式锁
|
|
func (this *guildgveModel) userlock(id string) (result *redis.RedisMutex, err error) {
|
|
return this.model.Redis.NewRedisMutex(fmt.Sprintf("%s-lockexpedition:%s", this.model.ServiceId, id))
|
|
}
|
|
|
|
// 获取用户全部的埋点数据
|
|
func (this *guildgveModel) getGuildGve(guildid string) (results *pb.DBGuildGve, err error) {
|
|
|
|
results = &pb.DBGuildGve{
|
|
Boos: make([]*pb.DBGuildGveBoss, 0),
|
|
}
|
|
if err = this.model.GetByID(guildid, results); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *guildgveModel) updateGuildGve(data *pb.DBExpedition) (err error) {
|
|
if err = this.model.ChangeById(data.Guildid, map[string]interface{}{
|
|
"boos": data.Boos,
|
|
}); err != nil {
|
|
this.module.Error("更新用户任务数据 错误!", log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
return
|
|
}
|