优化比赛场奖励领取次数限制
This commit is contained in:
parent
28921649c5
commit
514d6fdf37
|
@ -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
|
||||
type MatchAwardSvc struct {
|
||||
}
|
||||
_, err = clog.Upsert(nil, log)
|
||||
|
||||
func (svc *MatchAwardSvc) UpsertMatchAward(req *model.MatchAward, ret *bool) (err error) {
|
||||
c := MatchAwardCollection(req.Platform)
|
||||
if c == nil {
|
||||
return MatchAwardDBErr
|
||||
}
|
||||
|
||||
_, err = c.Upsert(nil, req)
|
||||
if err != nil {
|
||||
// 处理错误
|
||||
logger.Logger.Errorf("UpsertMatchAward err:%v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
func (svc *MatchAwardSvc) GetMatchAward(plt string, ret *model.MatchAward) (err error) {
|
||||
c := MatchAwardCollection(plt)
|
||||
if c == nil {
|
||||
return MatchAwardDBErr
|
||||
}
|
||||
|
||||
type MatchAwardLogSvc struct {
|
||||
}
|
||||
|
||||
func (svc *MatchAwardLogSvc) InsertOrUpdateMatchAwardLog(args []*model.MatchAwardLog, ret *bool) (err error) {
|
||||
err = InsertOrUpdateMatchAwardLog(args...)
|
||||
// 旧数据
|
||||
old := &MatchAwardLog{}
|
||||
err = c.Find(bson.M{"platform": "1"}).One(old)
|
||||
if err == nil {
|
||||
*ret = true
|
||||
for k, v := range old.AwardNum {
|
||||
d := &model.MatchAward{
|
||||
Platform: k,
|
||||
Award: make(map[int32]int32),
|
||||
}
|
||||
return
|
||||
for kk, vv := range v {
|
||||
d.Award[kk] = vv
|
||||
}
|
||||
func GetMatchAward(plt string, ret *model.MatchAwardLog) (err error) {
|
||||
clog := MatchAwardLogCollection(plt)
|
||||
if clog == nil {
|
||||
return nil
|
||||
var b bool
|
||||
svc.UpsertMatchAward(d, &b)
|
||||
}
|
||||
selecter := bson.M{"platform": plt}
|
||||
err = clog.Find(selecter).One(&ret)
|
||||
if err != nil {
|
||||
return nil
|
||||
c.Remove(bson.M{"platform": "1"})
|
||||
}
|
||||
return
|
||||
// 旧数据
|
||||
|
||||
err = nil
|
||||
err = c.Find(nil).One(ret)
|
||||
if err != nil && err != mgo.ErrNotFound {
|
||||
logger.Logger.Errorf("GetMatchAward err:%v", err)
|
||||
}
|
||||
func (svc *MatchAwardLogSvc) GetMatchAward(Plt string, ret *model.MatchAwardLog) (err error) {
|
||||
err = GetMatchAward(Plt, ret)
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
rpc.Register(new(MatchAwardLogSvc))
|
||||
rpc.Register(new(MatchAwardSvc))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -128,6 +128,7 @@ func (this *AwardLogManager) Update() {
|
|||
|
||||
func (this *AwardLogManager) Shutdown() {
|
||||
this.Save()
|
||||
module.UnregisteModule(this)
|
||||
}
|
||||
|
||||
func (this *AwardLogManager) OnHourTimer() {
|
||||
|
|
|
@ -24,13 +24,8 @@ import (
|
|||
"mongo.games.com/game/webapi"
|
||||
)
|
||||
|
||||
func init() {
|
||||
module.RegisteModule(TournamentMgr, time.Second, 0)
|
||||
ClockMgrSington.RegisteSinker(TournamentMgr)
|
||||
}
|
||||
|
||||
const (
|
||||
// 如果这里比赛类型没有,默认是锦标赛
|
||||
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)
|
||||
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.Error("saveMatchAwardLog error %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}), task.CompleteNotifyWrapper(func(data interface{}, tt task.Task) {
|
||||
})).StartByFixExecutor("saveMatchAwardLogTask")
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue