277 lines
8.1 KiB
Go
277 lines
8.1 KiB
Go
package realarena
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/event"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/sys/db"
|
|
"math"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/x/bsonx"
|
|
)
|
|
|
|
var (
|
|
GlobalWhichIssueKey = "ExclusiveWhichIssue"
|
|
)
|
|
|
|
// /装备 数据组件
|
|
type modelComp struct {
|
|
modules.MCompModel
|
|
module *RealArena
|
|
whichissue *pb.DBRealArenaWhichIssue
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *modelComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableRealarena
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*RealArena)
|
|
//创建uid索引
|
|
_, err = this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
|
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
|
})
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) Start() (err error) {
|
|
err = this.MCompModel.Start()
|
|
event.RegisterGO(core.Event_ServiceStartEnd, func() {
|
|
this.whichissue = &pb.DBRealArenaWhichIssue{}
|
|
this.module.ModuleTools.GetGlobalData(GlobalWhichIssueKey, this.whichissue)
|
|
this.computeWhich()
|
|
})
|
|
return
|
|
}
|
|
|
|
//计算比赛周期
|
|
func (this *modelComp) computeWhich() {
|
|
var (
|
|
conf *cfg.GameArenarealtimeOpentime
|
|
err error
|
|
)
|
|
if conf, err = this.module.configure.GetGameArenarealtimeOpentime(); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
if this.whichissue.Currwhichissue == 0 { //第0期
|
|
this.whichissue.Currwhichissue = conf.GetDataList()[0].Id
|
|
this.whichissue.Curropentime = this.module.service.GetOpentime().Unix() + int64(conf.GetDataList()[0].OpenTime)
|
|
this.whichissue.Currendtime = this.whichissue.Curropentime + int64(conf.GetDataList()[0].DurationTime)
|
|
this.module.ModuleTools.UpdateGlobalData(GlobalWhichIssueKey, this.whichissue)
|
|
} else {
|
|
for i, v := range conf.GetDataList() {
|
|
if v.Id == this.whichissue.Currwhichissue { //当前第几期
|
|
if v.CycleIndex == 1 {
|
|
if configure.Now().Unix() > this.whichissue.Currendtime { //是否结束当前周期
|
|
this.whichissue.Curropentime = this.whichissue.Currendtime + int64(v.IntervalTime)
|
|
this.whichissue.Currendtime = this.whichissue.Curropentime + int64(v.DurationTime)
|
|
this.module.ModuleTools.UpdateGlobalData(GlobalWhichIssueKey, this.whichissue)
|
|
break
|
|
}
|
|
} else {
|
|
if configure.Now().Unix() > this.whichissue.Currendtime { //进入下一期
|
|
if i < len(conf.GetDataList())-1 {
|
|
this.whichissue.Currwhichissue = conf.GetDataList()[i+1].Id
|
|
this.whichissue.Curropentime = this.whichissue.Currendtime + int64(conf.GetDataList()[i+1].IntervalTime)
|
|
this.whichissue.Currendtime = this.whichissue.Curropentime + int64(conf.GetDataList()[i+1].DurationTime)
|
|
} else {
|
|
this.whichissue.Curropentime = this.whichissue.Currendtime + int64(v.IntervalTime)
|
|
this.whichissue.Currendtime = this.whichissue.Curropentime + int64(v.DurationTime)
|
|
}
|
|
this.module.ModuleTools.UpdateGlobalData(GlobalWhichIssueKey, this.whichissue)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 更新埋点数据到db中
|
|
func (this *modelComp) getmodel() (model *realArenaModel, err error) {
|
|
var (
|
|
m *db.DBModel
|
|
)
|
|
if m, err = this.module.GetCrossDBModel(this.TableName); err != nil {
|
|
return
|
|
}
|
|
model = &realArenaModel{module: this.module, model: m}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *modelComp) getinfo(session comm.IUserSession) (info *pb.DBRealArena, err error) {
|
|
var (
|
|
model *realArenaModel
|
|
)
|
|
if model, err = this.getmodel(); err != nil {
|
|
return
|
|
}
|
|
info, err = model.getinfo(session)
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) getinfos(uIds []string) (infos []*pb.DBRealArena, err error) {
|
|
var (
|
|
model *realArenaModel
|
|
)
|
|
|
|
if model, err = this.getmodel(); err != nil {
|
|
return
|
|
}
|
|
infos, err = model.getinfos(uIds)
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *modelComp) getinfobyuid(uid string) (info *pb.DBRealArena, err error) {
|
|
var (
|
|
model *realArenaModel
|
|
)
|
|
if model, err = this.getmodel(); err != nil {
|
|
return
|
|
}
|
|
info, err = model.getinfobyuid(uid)
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *modelComp) change(uid string, update map[string]interface{}) (err error) {
|
|
var (
|
|
model *realArenaModel
|
|
)
|
|
if model, err = this.getmodel(); err != nil {
|
|
return
|
|
}
|
|
err = model.change(uid, update)
|
|
return
|
|
}
|
|
|
|
func (this *modelComp) computedan(integral int32) (dan int32, err error) {
|
|
var (
|
|
active *cfg.GameArenarealtimeConfigData
|
|
)
|
|
|
|
if active, err = this.module.configure.getGameArenarealtimeConfigByIntegral(integral); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
dan = active.Id
|
|
return
|
|
}
|
|
|
|
// 积分计算
|
|
func (this *modelComp) integralCompute(red, bule *pb.DBRealArenaMember, winside int32) (err error) {
|
|
var (
|
|
redactive *cfg.GameArenarealtimeConfigData
|
|
buleactive *cfg.GameArenarealtimeConfigData
|
|
)
|
|
if redactive, err = this.module.configure.getGameArenarealtimeConfig(red.Dan); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
if buleactive, err = this.module.configure.getGameArenarealtimeConfig(bule.Dan); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
if winside == 1 {
|
|
red.Addintegral = int32(float64(redactive.Kscore) * float64(1-1/float32(1+math.Pow(10, float64(float64(bule.Integral-red.Integral)/400)))))
|
|
bule.Addintegral = int32(float64(buleactive.Kscore) * float64(0-1/float64(1+math.Pow(10, float64(float64(red.Integral-bule.Integral))/400))))
|
|
} else {
|
|
red.Addintegral = int32(float64(redactive.Kscore) * float64(0-1/float64(1+math.Pow(10, float64(float64(bule.Integral-red.Integral)/400)))))
|
|
bule.Addintegral = int32(float64(redactive.Kscore) * float64(1-1/float64(1+math.Pow(10, float64(float64(red.Integral-bule.Integral)/400)))))
|
|
}
|
|
if red.Integral+red.Addintegral < 0 {
|
|
red.Addintegral = -red.Integral
|
|
red.Integral = 0
|
|
} else {
|
|
red.Integral = red.Integral + red.Addintegral
|
|
}
|
|
if bule.Integral+bule.Addintegral < 0 {
|
|
bule.Addintegral = -bule.Integral
|
|
bule.Integral = 0
|
|
} else {
|
|
bule.Integral = bule.Integral + bule.Addintegral
|
|
}
|
|
if red.Dan, err = this.computedan(red.Integral); err != nil {
|
|
return
|
|
}
|
|
if bule.Dan, err = this.computedan(bule.Integral); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 埋点专属模型 会封装特殊的数据转换接口
|
|
type realArenaModel struct {
|
|
module *RealArena
|
|
model *db.DBModel
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *realArenaModel) getExclusives(uId string) (list []*pb.DB_Exclusive, err error) {
|
|
list = make([]*pb.DB_Exclusive, 0)
|
|
if err = this.model.GetList(uId, &list); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *realArenaModel) getinfo(session comm.IUserSession) (info *pb.DBRealArena, err error) {
|
|
var (
|
|
user *pb.DBUser
|
|
)
|
|
info = &pb.DBRealArena{}
|
|
if err = this.model.Get(session.GetUserId(), info); err != nil && err != mgo.MongodbNil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
if user, err = this.module.GetUserForSession(session); err != nil {
|
|
return
|
|
}
|
|
info = &pb.DBRealArena{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: session.GetUserId(),
|
|
Uinfo: comm.GetUserBaseInfo(user),
|
|
Dan: 1,
|
|
Integral: 0,
|
|
Danaward: make(map[int32]int32),
|
|
}
|
|
err = this.model.Add(session.GetUserId(), info)
|
|
return
|
|
}
|
|
|
|
// 查询用户装备数据
|
|
func (this *realArenaModel) getinfos(uIds []string) (result []*pb.DBRealArena, err error) {
|
|
result = make([]*pb.DBRealArena, 0)
|
|
if _, err = this.model.GetByUids(uIds, &result); err != nil && err != mgo.MongodbNil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *realArenaModel) getinfobyuid(uid string) (info *pb.DBRealArena, err error) {
|
|
info = &pb.DBRealArena{}
|
|
if err = this.model.Get(uid, info); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// /查询用户的武器背包
|
|
func (this *realArenaModel) change(uid string, update map[string]interface{}) (err error) {
|
|
if err = this.model.Change(uid, update); err != nil {
|
|
this.module.Errorf("err:%v", err)
|
|
}
|
|
return
|
|
}
|