优化比赛场奖励领取次数限制

This commit is contained in:
sk 2024-08-18 00:03:03 +08:00
parent 28921649c5
commit 514d6fdf37
4 changed files with 129 additions and 93 deletions

View File

@ -2,17 +2,26 @@ package svc
import ( import (
"errors" "errors"
"net/rpc"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson" "github.com/globalsign/mgo/bson"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/game/dbproxy/mongo" "mongo.games.com/game/dbproxy/mongo"
"mongo.games.com/game/model" "mongo.games.com/game/model"
"net/rpc"
) )
var ( var (
MatchAwardLogDBErr = errors.New("log_matchawardlog db open failed.") MatchAwardDBErr = errors.New("log_matchawardlog db open failed")
) )
func MatchAwardLogCollection(plt string) *mongo.Collection { type MatchAwardLog struct {
AwardNum map[string]map[int32]int32 // 奖励数量
Platform string
}
func MatchAwardCollection(plt string) *mongo.Collection {
s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.MatchAwardLogDBName) s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.MatchAwardLogDBName)
if s != nil { if s != nil {
c, _ := s.DB().C(model.MatchAwardLogCollName) c, _ := s.DB().C(model.MatchAwardLogCollName)
@ -21,47 +30,55 @@ func MatchAwardLogCollection(plt string) *mongo.Collection {
return nil return nil
} }
func InsertOrUpdateMatchAwardLog(logs ...*model.MatchAwardLog) (err error) { type MatchAwardSvc struct {
for _, log := range logs {
clog := MatchAwardLogCollection(log.Platform)
if clog == nil {
return
}
_, err = clog.Upsert(nil, log)
if err != nil {
// 处理错误
return err
}
}
return
} }
type MatchAwardLogSvc struct { func (svc *MatchAwardSvc) UpsertMatchAward(req *model.MatchAward, ret *bool) (err error) {
} c := MatchAwardCollection(req.Platform)
if c == nil {
return MatchAwardDBErr
}
func (svc *MatchAwardLogSvc) InsertOrUpdateMatchAwardLog(args []*model.MatchAwardLog, ret *bool) (err error) { _, err = c.Upsert(nil, req)
err = InsertOrUpdateMatchAwardLog(args...)
if err == nil {
*ret = true
}
return
}
func GetMatchAward(plt string, ret *model.MatchAwardLog) (err error) {
clog := MatchAwardLogCollection(plt)
if clog == nil {
return nil
}
selecter := bson.M{"platform": plt}
err = clog.Find(selecter).One(&ret)
if err != nil { if err != nil {
return nil logger.Logger.Errorf("UpsertMatchAward err:%v", err)
} }
return
}
func (svc *MatchAwardLogSvc) GetMatchAward(Plt string, ret *model.MatchAwardLog) (err error) {
err = GetMatchAward(Plt, ret)
return err return err
} }
func init() {
rpc.Register(new(MatchAwardLogSvc)) func (svc *MatchAwardSvc) GetMatchAward(plt string, ret *model.MatchAward) (err error) {
c := MatchAwardCollection(plt)
if c == nil {
return MatchAwardDBErr
}
// 旧数据
old := &MatchAwardLog{}
err = c.Find(bson.M{"platform": "1"}).One(old)
if err == nil {
for k, v := range old.AwardNum {
d := &model.MatchAward{
Platform: k,
Award: make(map[int32]int32),
}
for kk, vv := range v {
d.Award[kk] = vv
}
var b bool
svc.UpsertMatchAward(d, &b)
}
c.Remove(bson.M{"platform": "1"})
}
// 旧数据
err = nil
err = c.Find(nil).One(ret)
if err != nil && err != mgo.ErrNotFound {
logger.Logger.Errorf("GetMatchAward err:%v", err)
}
return err
}
func init() {
rpc.Register(new(MatchAwardSvc))
} }

View File

@ -4,30 +4,29 @@ import (
"time" "time"
) )
// 比赛详情
type MatchAwardLog struct {
AwardNum map[string]map[int32]int32 // 奖励数量
Platform string
}
var ( var (
MatchAwardLogDBName = "log" MatchAwardLogDBName = "log"
MatchAwardLogCollName = "log_matchawardlog" MatchAwardLogCollName = "log_matchawardlog"
) )
func NewMatchAwardLog() *MatchAwardLog { type MatchAward struct {
return &MatchAwardLog{} Platform string `bson:"-"`
Award map[int32]int32
} }
func InsertOrUpdateMatchAwardLog(logs ...*MatchAwardLog) (err error) { func UpsertMatchAward(data *MatchAward) error {
if rpcCli == nil { if rpcCli == nil {
return ErrRPClientNoConn return ErrRPClientNoConn
} }
var ret bool var ret bool
return rpcCli.CallWithTimeout("MatchAwardLogSvc.InsertOrUpdateMatchAwardLog", logs, &ret, time.Second*30) return rpcCli.CallWithTimeout("MatchAwardSvc.UpsertMatchAward", data, &ret, time.Second*30)
} }
func GetMatchAwardLog(platform string) (ret MatchAwardLog, err error) { func GetMatchAward(platform string) (ret *MatchAward, err error) {
err = rpcCli.CallWithTimeout("MatchAwardLogSvc.GetMatchAward", platform, &ret, time.Second*30) if rpcCli == nil {
return ret, err return nil, ErrRPClientNoConn
}
ret = new(MatchAward)
err = rpcCli.CallWithTimeout("MatchAwardSvc.GetMatchAward", platform, ret, time.Second*30)
return
} }

View File

@ -128,6 +128,7 @@ func (this *AwardLogManager) Update() {
func (this *AwardLogManager) Shutdown() { func (this *AwardLogManager) Shutdown() {
this.Save() this.Save()
module.UnregisteModule(this)
} }
func (this *AwardLogManager) OnHourTimer() { func (this *AwardLogManager) OnHourTimer() {

View File

@ -24,13 +24,8 @@ import (
"mongo.games.com/game/webapi" "mongo.games.com/game/webapi"
) )
func init() { // 如果这里比赛类型没有,默认是锦标赛
module.RegisteModule(TournamentMgr, time.Second, 0)
ClockMgrSington.RegisteSinker(TournamentMgr)
}
const ( const (
// 如果这里比赛类型没有,默认是锦标赛
MatchTypeNormal = iota + 1 // 锦标赛 MatchTypeNormal = iota + 1 // 锦标赛
MatchTypeChampion // 冠军赛/实物赛 MatchTypeChampion // 冠军赛/实物赛
MatchTypeVIP // vip比赛 MatchTypeVIP // vip比赛
@ -54,6 +49,11 @@ const (
DaiDing = 2 // 待定 DaiDing = 2 // 待定
) )
func init() {
module.RegisteModule(TournamentMgr, time.Second, 0)
ClockMgrSington.RegisteSinker(TournamentMgr)
}
var TournamentMgr = NewTournament() var TournamentMgr = NewTournament()
type PerRankInfo struct { type PerRankInfo struct {
@ -78,8 +78,8 @@ type SignupInfo struct {
type PlayerRoundInfo struct { type PlayerRoundInfo struct {
players []*PlayerMatchContext // 本轮结算信息 players []*PlayerMatchContext // 本轮结算信息
ranks []*PlayerMatchContext // 本排名 ranks []*PlayerMatchContext // 本排名
num int // 本完成人数 num int // 本完成人数
} }
type Tournament struct { type Tournament struct {
@ -91,7 +91,7 @@ type Tournament struct {
playerWaitStart map[int32]int64 // 等待时间 玩家Id:等待时长秒 playerWaitStart map[int32]int64 // 等待时间 玩家Id:等待时长秒
matches map[int32]map[int64]*TmMatch // 开始比赛的数据比赛配置Id:比赛顺序序号:一场开始的比赛数据 matches map[int32]map[int64]*TmMatch // 开始比赛的数据比赛配置Id:比赛顺序序号:一场开始的比赛数据
players map[int64]map[int32]*PlayerMatchContext // 比赛中玩家 比赛顺序序号:玩家id:玩家信息 players map[int64]map[int32]*PlayerMatchContext // 比赛中玩家 比赛顺序序号:玩家id:玩家信息
roundPlayers map[int64]map[int32]*PlayerRoundInfo // 每轮比赛数据 比赛顺序序号:第几轮 roundPlayers map[int64]map[int32]*PlayerRoundInfo // 每轮比赛数据备份 比赛顺序序号:第几轮
finalPerRank map[int64][]*PerRankInfo // 本场比赛排名,每淘汰一位记录一位,最后记录决赛玩家 比赛顺序序号 finalPerRank map[int64][]*PerRankInfo // 本场比赛排名,每淘汰一位记录一位,最后记录决赛玩家 比赛顺序序号
MatchAwardNum map[string]map[int32]int32 // 比赛配置Id:比赛奖励次數 MatchAwardNum map[string]map[int32]int32 // 比赛配置Id:比赛奖励次數
} }
@ -146,6 +146,7 @@ func (this *Tournament) checkData(cfg *webapiproto.GameMatchDate) bool {
return true return true
} }
// 记录淘汰人员
func (this *Tournament) addFinalPlayer(sortId int64, p *PerRankInfo) { func (this *Tournament) addFinalPlayer(sortId int64, p *PerRankInfo) {
if this.finalPerRank[sortId] == nil { if this.finalPerRank[sortId] == nil {
this.finalPerRank[sortId] = []*PerRankInfo{} this.finalPerRank[sortId] = []*PerRankInfo{}
@ -153,6 +154,7 @@ func (this *Tournament) addFinalPlayer(sortId int64, p *PerRankInfo) {
this.finalPerRank[sortId] = append(this.finalPerRank[sortId], p) this.finalPerRank[sortId] = append(this.finalPerRank[sortId], p)
} }
// 查询一场比赛某轮的历史数据
func (this *Tournament) getRoundPlayer(sortId int64, round int32) *PlayerRoundInfo { func (this *Tournament) getRoundPlayer(sortId int64, round int32) *PlayerRoundInfo {
_, ok := this.roundPlayers[sortId] _, ok := this.roundPlayers[sortId]
if !ok { if !ok {
@ -168,6 +170,8 @@ func (this *Tournament) getRoundPlayer(sortId int64, round int32) *PlayerRoundIn
return v return v
} }
// GetSignUpPlayers 获取报名人员
// id 比赛配置id
func (this *Tournament) GetSignUpPlayers(platform string, id int32) map[int32]*TmPlayer { func (this *Tournament) GetSignUpPlayers(platform string, id int32) map[int32]*TmPlayer {
v, ok := this.signupPlayers[platform] v, ok := this.signupPlayers[platform]
if !ok { if !ok {
@ -194,7 +198,7 @@ func (this *Tournament) UpdateData(init bool, data *webapiproto.GameMatchDateLis
this.FixMatchTimeStamp(v) this.FixMatchTimeStamp(v)
configs[v.Id] = v configs[v.Id] = v
} else { } else {
logger.Logger.Error("GameMatchDate error: ", v) logger.Logger.Errorf("GameMatchDate check error: %v", v)
} }
} }
@ -239,14 +243,6 @@ func (this *Tournament) UpdateData(init bool, data *webapiproto.GameMatchDateLis
} }
} }
this.GameMatchDateList[data.Platform] = configs this.GameMatchDateList[data.Platform] = configs
//拉取数据
if this.MatchAwardNum == nil {
ret, err := model.GetMatchAwardLog(data.Platform)
logger.Logger.Tracef("ret = %v,err = %v", ret, err)
if err == nil {
this.MatchAwardNum = ret.AwardNum
}
}
// 通知平台玩家数据更新 // 通知平台玩家数据更新
if !init { if !init {
//todo 优化 //todo 优化
@ -796,6 +792,7 @@ func (this *Tournament) CreatePlayerMatchContext(p *Player, m *TmMatch, seq int)
return nil return nil
} }
// 是否淘汰
func (this *Tournament) isOut(sortId int64, snid int32) bool { func (this *Tournament) isOut(sortId int64, snid int32) bool {
if this.finalPerRank[sortId] != nil { if this.finalPerRank[sortId] != nil {
for _, info := range this.finalPerRank[sortId] { for _, info := range this.finalPerRank[sortId] {
@ -1608,6 +1605,20 @@ func (this *Tournament) ModuleName() string {
func (this *Tournament) Init() { func (this *Tournament) Init() {
logger.Logger.Trace("Tournament Init") logger.Logger.Trace("Tournament Init")
for _, v := range PlatformMgrSingleton.GetPlatforms() {
if v == nil || v.IdStr == "0" {
continue
}
ret, err := model.GetMatchAward(v.IdStr)
if err != nil {
logger.Logger.Tracef("GetMatchAward error %v", err)
continue
}
if this.MatchAwardNum == nil {
this.MatchAwardNum = make(map[string]map[int32]int32)
}
this.MatchAwardNum[v.IdStr] = ret.Award
}
} }
func (this *Tournament) Update() { func (this *Tournament) Update() {
@ -1650,35 +1661,42 @@ func (this *Tournament) Update() {
} }
} }
} }
func (this *Tournament) OnDayTimer() { func (this *Tournament) OnDayTimer() {
this.MatchAwardNum = make(map[string]map[int32]int32) for k := range this.MatchAwardNum {
this.Save() this.MatchAwardNum[k] = make(map[int32]int32)
logger.Logger.Trace("比赛场每日库存清理!!!") }
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
this.SaveMatchAward()
return nil
}), nil, "save_match_award_times").Start()
} }
func (this *Tournament) Shutdown() { func (this *Tournament) Shutdown() {
// 保存数据 this.SaveMatchAward()
this.Save() module.UnregisteModule(this)
} }
func (this *Tournament) Save() {
logger.Logger.Info("保存比赛场每日奖励数据!!!!", this.MatchAwardNum) func (this *Tournament) SaveMatchAward() {
if this.MatchAwardNum == nil { if this.MatchAwardNum == nil {
return return
} }
for platform, _ := range this.MatchAwardNum { logger.Logger.Tracef("保存比赛场奖励领取次数")
matchAwardLog := model.NewMatchAwardLog() for platform, v := range this.MatchAwardNum {
matchAwardLog.AwardNum = this.MatchAwardNum d := &model.MatchAward{
matchAwardLog.Platform = platform Platform: platform,
task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { Award: make(map[int32]int32),
err := model.InsertOrUpdateMatchAwardLog(matchAwardLog) }
if err != nil { for k, vv := range v {
logger.Logger.Error("saveMatchAwardLog error %v", err) d.Award[k] = vv
return err }
} err := model.UpsertMatchAward(d)
return nil if err != nil {
}), task.CompleteNotifyWrapper(func(data interface{}, tt task.Task) { logger.Logger.Errorf("SaveMatchAward error %v", err)
})).StartByFixExecutor("saveMatchAwardLogTask") }
} }
} }
func (this *Tournament) getWeekDay() int { func (this *Tournament) getWeekDay() int {
getWeekNum := func(t time.Time) int { getWeekNum := func(t time.Time) int {
strWeek := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} strWeek := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
@ -1711,7 +1729,7 @@ func (this *Tournament) FixMatchTimeStamp(d *webapiproto.GameMatchDate) {
week := this.getWeekDay() week := this.getWeekDay()
st := time.Unix(d.MatchTimeStamp[0], 0).In(l) st := time.Unix(d.MatchTimeStamp[0], 0).In(l)
et := time.Unix(d.MatchTimeStamp[1], 0).In(l) et := time.Unix(d.MatchTimeStamp[1], 0).In(l)
logger.Logger.Tracef("FixMatchTimeStamp 1 id:%v now:%v week:%v start:%v end:%v", d.Id, bTs, bTs.Weekday(), st, et) //logger.Logger.Tracef("FixMatchTimeStamp 1 id:%v now:%v week:%v start:%v end:%v", d.Id, bTs, bTs.Weekday(), st, et)
// 重复时间段比赛时间 // 重复时间段比赛时间
for _, v := range d.MatchTimeWeek { for _, v := range d.MatchTimeWeek {
if v == int32(week) { if v == int32(week) {
@ -1723,8 +1741,7 @@ func (this *Tournament) FixMatchTimeStamp(d *webapiproto.GameMatchDate) {
st = time.Unix(d.MatchTimeStamp[0], 0).In(l) st = time.Unix(d.MatchTimeStamp[0], 0).In(l)
et = time.Unix(d.MatchTimeStamp[1], 0).In(l) et = time.Unix(d.MatchTimeStamp[1], 0).In(l)
logger.Logger.Tracef("FixMatchTimeStamp 2 id:%v now:%v week:%v start:%v end:%v", d.Id, bTs, bTs.Weekday(), st, et) //logger.Logger.Tracef("FixMatchTimeStamp 2 id:%v now:%v week:%v start:%v end:%v", d.Id, bTs, bTs.Weekday(), st, et)
break break
} }
} }
@ -1738,6 +1755,8 @@ func (this *Tournament) OnHourTimer() {
} }
} }
// GetMatchAwardNum 剩余奖励数量
// id 比赛配置id
func (this *Tournament) GetMatchAwardNum(platform string, id int32) int32 { func (this *Tournament) GetMatchAwardNum(platform string, id int32) int32 {
var num int32 var num int32
if this.MatchAwardNum != nil && this.MatchAwardNum[platform] != nil { if this.MatchAwardNum != nil && this.MatchAwardNum[platform] != nil {