From 514d6fdf37e5524b0672cb92d1771c4eed92b5eb Mon Sep 17 00:00:00 2001 From: sk <123456@qq.com> Date: Sun, 18 Aug 2024 00:03:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=AF=94=E8=B5=9B=E5=9C=BA?= =?UTF-8?q?=E5=A5=96=E5=8A=B1=E9=A2=86=E5=8F=96=E6=AC=A1=E6=95=B0=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbproxy/svc/l_matchawardlog.go | 95 ++++++++++++++++++------------- model/matchawardlog.go | 25 ++++---- worldsrv/awardlogmgr.go | 1 + worldsrv/tournament.go | 101 ++++++++++++++++++++------------- 4 files changed, 129 insertions(+), 93 deletions(-) diff --git a/dbproxy/svc/l_matchawardlog.go b/dbproxy/svc/l_matchawardlog.go index 9539fcb..bbc8dfd 100644 --- a/dbproxy/svc/l_matchawardlog.go +++ b/dbproxy/svc/l_matchawardlog.go @@ -2,17 +2,26 @@ package svc import ( "errors" + "net/rpc" + + "github.com/globalsign/mgo" "github.com/globalsign/mgo/bson" + "mongo.games.com/goserver/core/logger" + "mongo.games.com/game/dbproxy/mongo" "mongo.games.com/game/model" - "net/rpc" ) 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) if s != nil { c, _ := s.DB().C(model.MatchAwardLogCollName) @@ -21,47 +30,55 @@ func MatchAwardLogCollection(plt string) *mongo.Collection { return nil } -func InsertOrUpdateMatchAwardLog(logs ...*model.MatchAwardLog) (err error) { - for _, log := range logs { - clog := MatchAwardLogCollection(log.Platform) - if clog == nil { - return - } - _, err = clog.Upsert(nil, log) - if err != nil { - // 处理错误 - return err - } - } - return +type MatchAwardSvc struct { } -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 = 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) + _, err = c.Upsert(nil, req) 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 } -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)) } diff --git a/model/matchawardlog.go b/model/matchawardlog.go index 07dcb94..448e106 100644 --- a/model/matchawardlog.go +++ b/model/matchawardlog.go @@ -4,30 +4,29 @@ import ( "time" ) -// 比赛详情 -type MatchAwardLog struct { - AwardNum map[string]map[int32]int32 // 奖励数量 - Platform string -} - var ( MatchAwardLogDBName = "log" MatchAwardLogCollName = "log_matchawardlog" ) -func NewMatchAwardLog() *MatchAwardLog { - return &MatchAwardLog{} +type MatchAward struct { + Platform string `bson:"-"` + Award map[int32]int32 } -func InsertOrUpdateMatchAwardLog(logs ...*MatchAwardLog) (err error) { +func UpsertMatchAward(data *MatchAward) error { if rpcCli == nil { return ErrRPClientNoConn } 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) { - err = rpcCli.CallWithTimeout("MatchAwardLogSvc.GetMatchAward", platform, &ret, time.Second*30) - return ret, err +func GetMatchAward(platform string) (ret *MatchAward, err error) { + if rpcCli == nil { + return nil, ErrRPClientNoConn + } + ret = new(MatchAward) + err = rpcCli.CallWithTimeout("MatchAwardSvc.GetMatchAward", platform, ret, time.Second*30) + return } diff --git a/worldsrv/awardlogmgr.go b/worldsrv/awardlogmgr.go index 6c9f71d..298f59c 100644 --- a/worldsrv/awardlogmgr.go +++ b/worldsrv/awardlogmgr.go @@ -128,6 +128,7 @@ func (this *AwardLogManager) Update() { func (this *AwardLogManager) Shutdown() { this.Save() + module.UnregisteModule(this) } func (this *AwardLogManager) OnHourTimer() { diff --git a/worldsrv/tournament.go b/worldsrv/tournament.go index 66755db..7ca1cc9 100644 --- a/worldsrv/tournament.go +++ b/worldsrv/tournament.go @@ -24,13 +24,8 @@ import ( "mongo.games.com/game/webapi" ) -func init() { - module.RegisteModule(TournamentMgr, time.Second, 0) - ClockMgrSington.RegisteSinker(TournamentMgr) -} - +// 如果这里比赛类型没有,默认是锦标赛 const ( - // 如果这里比赛类型没有,默认是锦标赛 MatchTypeNormal = iota + 1 // 锦标赛 MatchTypeChampion // 冠军赛/实物赛 MatchTypeVIP // vip比赛 @@ -54,6 +49,11 @@ const ( DaiDing = 2 // 待定 ) +func init() { + module.RegisteModule(TournamentMgr, time.Second, 0) + ClockMgrSington.RegisteSinker(TournamentMgr) +} + var TournamentMgr = NewTournament() type PerRankInfo struct { @@ -78,8 +78,8 @@ type SignupInfo struct { type PlayerRoundInfo struct { players []*PlayerMatchContext // 本轮结算信息 - ranks []*PlayerMatchContext // 本来排名 - num int // 本来完成人数 + ranks []*PlayerMatchContext // 本轮排名 + num int // 本轮完成人数 } type Tournament struct { @@ -91,7 +91,7 @@ type Tournament struct { playerWaitStart map[int32]int64 // 等待时间 玩家Id:等待时长秒 matches map[int32]map[int64]*TmMatch // 开始比赛的数据,比赛配置Id:比赛顺序序号:一场开始的比赛数据 players map[int64]map[int32]*PlayerMatchContext // 比赛中玩家 比赛顺序序号:玩家id:玩家信息 - roundPlayers map[int64]map[int32]*PlayerRoundInfo // 每轮比赛数据 比赛顺序序号:第几轮 + roundPlayers map[int64]map[int32]*PlayerRoundInfo // 每轮比赛数据备份 比赛顺序序号:第几轮 finalPerRank map[int64][]*PerRankInfo // 本场比赛排名,每淘汰一位记录一位,最后记录决赛玩家 比赛顺序序号 MatchAwardNum map[string]map[int32]int32 // 比赛配置Id:比赛奖励次數 } @@ -146,6 +146,7 @@ func (this *Tournament) checkData(cfg *webapiproto.GameMatchDate) bool { return true } +// 记录淘汰人员 func (this *Tournament) addFinalPlayer(sortId int64, p *PerRankInfo) { if this.finalPerRank[sortId] == nil { this.finalPerRank[sortId] = []*PerRankInfo{} @@ -153,6 +154,7 @@ func (this *Tournament) addFinalPlayer(sortId int64, p *PerRankInfo) { this.finalPerRank[sortId] = append(this.finalPerRank[sortId], p) } +// 查询一场比赛某轮的历史数据 func (this *Tournament) getRoundPlayer(sortId int64, round int32) *PlayerRoundInfo { _, ok := this.roundPlayers[sortId] if !ok { @@ -168,6 +170,8 @@ func (this *Tournament) getRoundPlayer(sortId int64, round int32) *PlayerRoundIn return v } +// GetSignUpPlayers 获取报名人员 +// id 比赛配置id func (this *Tournament) GetSignUpPlayers(platform string, id int32) map[int32]*TmPlayer { v, ok := this.signupPlayers[platform] if !ok { @@ -194,7 +198,7 @@ func (this *Tournament) UpdateData(init bool, data *webapiproto.GameMatchDateLis this.FixMatchTimeStamp(v) configs[v.Id] = v } 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 - //拉取数据 - 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 { //todo 优化 @@ -796,6 +792,7 @@ func (this *Tournament) CreatePlayerMatchContext(p *Player, m *TmMatch, seq int) return nil } +// 是否淘汰 func (this *Tournament) isOut(sortId int64, snid int32) bool { if this.finalPerRank[sortId] != nil { for _, info := range this.finalPerRank[sortId] { @@ -1608,6 +1605,20 @@ func (this *Tournament) ModuleName() string { func (this *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() { @@ -1650,35 +1661,42 @@ func (this *Tournament) Update() { } } } + func (this *Tournament) OnDayTimer() { - this.MatchAwardNum = make(map[string]map[int32]int32) - this.Save() - logger.Logger.Trace("比赛场每日库存清理!!!") + for k := range this.MatchAwardNum { + this.MatchAwardNum[k] = make(map[int32]int32) + } + task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { + this.SaveMatchAward() + return nil + }), nil, "save_match_award_times").Start() } + func (this *Tournament) Shutdown() { - // 保存数据 - this.Save() + this.SaveMatchAward() + module.UnregisteModule(this) } -func (this *Tournament) Save() { - logger.Logger.Info("保存比赛场每日奖励数据!!!!", this.MatchAwardNum) + +func (this *Tournament) SaveMatchAward() { if this.MatchAwardNum == nil { return } - for platform, _ := range this.MatchAwardNum { - matchAwardLog := model.NewMatchAwardLog() - matchAwardLog.AwardNum = this.MatchAwardNum - matchAwardLog.Platform = platform - task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { - err := model.InsertOrUpdateMatchAwardLog(matchAwardLog) - if err != nil { - logger.Logger.Error("saveMatchAwardLog error %v", err) - return err - } - return nil - }), task.CompleteNotifyWrapper(func(data interface{}, tt task.Task) { - })).StartByFixExecutor("saveMatchAwardLogTask") + logger.Logger.Tracef("保存比赛场奖励领取次数") + for platform, v := range this.MatchAwardNum { + d := &model.MatchAward{ + Platform: platform, + Award: make(map[int32]int32), + } + for k, vv := range v { + d.Award[k] = vv + } + err := model.UpsertMatchAward(d) + if err != nil { + logger.Logger.Errorf("SaveMatchAward error %v", err) + } } } + func (this *Tournament) getWeekDay() int { getWeekNum := func(t time.Time) int { strWeek := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} @@ -1711,7 +1729,7 @@ func (this *Tournament) FixMatchTimeStamp(d *webapiproto.GameMatchDate) { week := this.getWeekDay() st := time.Unix(d.MatchTimeStamp[0], 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 { if v == int32(week) { @@ -1723,8 +1741,7 @@ func (this *Tournament) FixMatchTimeStamp(d *webapiproto.GameMatchDate) { st = time.Unix(d.MatchTimeStamp[0], 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 } } @@ -1738,6 +1755,8 @@ func (this *Tournament) OnHourTimer() { } } +// GetMatchAwardNum 剩余奖励数量 +// id 比赛配置id func (this *Tournament) GetMatchAwardNum(platform string, id int32) int32 { var num int32 if this.MatchAwardNum != nil && this.MatchAwardNum[platform] != nil {