fix: 修复排行榜,修复用户获取逻辑。

main
NorthLan 3 years ago
parent 0e7b79a083
commit 993eff94a9

@ -42,8 +42,8 @@ type (
} }
UserAndScore struct { UserAndScore struct {
UserId int64 UserId int64 `gorm:"user_id"`
Score int64 Score int64 `gorm:"score"`
} }
UpdateRecordProps struct { UpdateRecordProps struct {

@ -1,10 +1,10 @@
Name: usercenter.rpc Name: usercenter.rpc
ListenOn: 127.0.0.1:30001 ListenOn: 127.0.0.1:40001
Timeout: 5000 Timeout: 5000
Etcd: Etcd:
Hosts: Hosts:
- 127.0.0.1:2379 - 127.0.0.1:2379
Key: usercenter.rpc.dev Key: usercenter.rpc.v2.dev
Timeout: 5000 # default is 2000 Timeout: 5000 # default is 2000
NonBlock: true NonBlock: true
Log: Log:
@ -27,12 +27,12 @@ UserRetriever:
RoomShortInfoApi: https://api.live.bilibili.com/room/v1/Room/room_init RoomShortInfoApi: https://api.live.bilibili.com/room/v1/Room/room_init
TopListApi: https://api.live.bilibili.com/guard/topList TopListApi: https://api.live.bilibili.com/guard/topList
Rank: Rank:
Enabled: false Enabled: true
Cron: Cron:
Update: "0/10 * * * * ?" # 10s一次 Update: "@every 10s" # 10s一次
Persistence: "0 0/10 * * * ?" # 10min一次 Persistence: "@every 10m" # 10min一次
GiftCollector: GiftCollector:
Enabled: true Enabled: false
Platforms: [ "bilibili" ] Platforms: [ "bilibili" ]
Cron: Cron:
CollectGift: "0 0 0/6 * * ?" # 每隔6小时执行一次 CollectGift: "0 0 0/6 * * ?" # 每隔6小时执行一次

@ -4,18 +4,44 @@ Timeout: 5000
Etcd: Etcd:
Hosts: Hosts:
- 127.0.0.1:2379 - 127.0.0.1:2379
Key: usercenter.rpc Key: usercenter.rpc.v2
Timeout: 5000 # default is 2000 Timeout: 5000 # default is 2000
NonBlock: true NonBlock: true
Log: Log:
Mode: file Mode: file
KeepDays: 7 KeepDays: 7
Level: error Level: info
DB: DB:
#DataSource: root:root@tcp(192.168.1.100:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true #DataSource: root:root@tcp(192.168.1.100:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true
DataSource: root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true DataSource: root:root@tcp(127.0.0.1:3306)/dmgame?charset=utf8mb4&loc=Asia%2FShanghai&parseTime=true
UserRetriever: UserRetriever:
Enabled: true Enabled: true
UpdateDuration: 720 # 720 hours = 30 Day = 1 Month UpdateDuration: 720 # 720 hours = 30 Day = 1 Month
Cron:
PlatformUser: "0 0/5 * * * ?" # 每隔5分钟执行一次
Nobility: "0 0/10 * * * ?" # 每隔10分钟执行一次
NobilityPlatforms: [ "bilibili" ] # 需要获取贵族的平台列表
Bilibili:
RoomId: 8722013
Mid: 6704420
RoomShortInfoApi: https://api.live.bilibili.com/room/v1/Room/room_init
TopListApi: https://api.live.bilibili.com/guard/topList
Rank: Rank:
Enabled: true Enabled: true
Cron:
Update: "@every 10s" # 10s一次
Persistence: "@every 10m" # 10min一次
GiftCollector:
Enabled: true
Platforms: [ "bilibili" ]
Cron:
CollectGift: "0 0 0/6 * * ?" # 每隔6小时执行一次
Integral:
# RMB到积分的转换
RMBToIntegral: 1000
# 平台礼物到RMB的转换
GiftToRMB:
bilibili: 0.001
# 平台免费礼物到积分的转换
FreeToIntegral:
bilibili: 0.0001

@ -46,6 +46,8 @@ func (l *RetrievePlatformUserLogic) RetrievePlatformUser(in *pb.PlatformUserReq)
} }
return nil return nil
} }
// : not found
// insert // insert
newId := uuid.NextId() newId := uuid.NextId()
if err := l.svcCtx.UserModel.InsertTx(l.ctx, tx, &model.User{Id: newId}); err != nil { if err := l.svcCtx.UserModel.InsertTx(l.ctx, tx, &model.User{Id: newId}); err != nil {
@ -61,6 +63,17 @@ func (l *RetrievePlatformUserLogic) RetrievePlatformUser(in *pb.PlatformUserReq)
if err := l.svcCtx.UserPlatformModel.InsertTx(l.ctx, tx, dbPlatformUser); err != nil { if err := l.svcCtx.UserPlatformModel.InsertTx(l.ctx, tx, dbPlatformUser); err != nil {
return errors.Wrap(nerr.NewWithCode(nerr.DBError), "插入平台用户数据失败") return errors.Wrap(nerr.NewWithCode(nerr.DBError), "插入平台用户数据失败")
} }
dbFullUser = &model.FullUser{
UserPlatform: *dbPlatformUser,
UserNobility: model.UserNobility{
UserId: newId,
NobilityLevel: 0,
},
UserIntegral: model.UserIntegral{
UserId: newId,
Integral: 0,
},
}
return nil return nil
}); err != nil { }); err != nil {
return nil, errors.Wrapf(nerr.NewWithCode(nerr.DBError), "查询或创建用户-事务执行失败, err:%+v", err) return nil, errors.Wrapf(nerr.NewWithCode(nerr.DBError), "查询或创建用户-事务执行失败, err:%+v", err)

@ -80,95 +80,51 @@ func InitRankJob(svcCtx *svc.ServiceContext) {
// 3. 开启任务 定时落库 // 3. 开启任务 定时落库
func (j *Job) initJob() { func (j *Job) initJob() {
logx.Info("开启排行榜服务...") logx.Info("开启排行榜服务...")
j.initByType(model.RankTypeDamage) // TODO 读取历史数据功能暂时移除
j.initByType(model.RankTypeDeDamage) //j.initByType(model.RankTypeDamage)
j.initByType(model.RankTypeGeneral) //j.initByType(model.RankTypeDeDamage)
j.initByType(model.RankTypeDeGeneral) //j.initByType(model.RankTypeGeneral)
j.initByType(model.RankTypeKillUnit) //j.initByType(model.RankTypeDeGeneral)
j.initByType(model.RankTypeDeKillUnit) //j.initByType(model.RankTypeKillUnit)
j.initByType(model.RankTypeKillPlayer) //j.initByType(model.RankTypeDeKillUnit)
j.initByType(model.RankTypeDeKillPlayer) //j.initByType(model.RankTypeKillPlayer)
j.initByType(model.RankTypeWin) //j.initByType(model.RankTypeDeKillPlayer)
j.initByType(model.RankTypeLost) //j.initByType(model.RankTypeWin)
j.initByType(model.RankTypeFirstBlood) //j.initByType(model.RankTypeLost)
j.initByType(model.RankTypeDeFirstBlood) //j.initByType(model.RankTypeFirstBlood)
//j.initByType(model.RankTypeDeFirstBlood)
cfg := j.svcCtx.Config.Rank cfg := j.svcCtx.Config.Rank
// job read and update // job read and update
c := cron.New() c := cron.New()
_, _ = c.AddFunc(cfg.Cron.Update, func() { _, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDamage) go j.readAndUpdate(model.RankTypeDamage)
go j.readAndUpdate(model.RankTypeDeDamage)
go j.readAndUpdate(model.RankTypeGeneral)
go j.readAndUpdate(model.RankTypeDeGeneral)
go j.readAndUpdate(model.RankTypeKillUnit)
go j.readAndUpdate(model.RankTypeDeKillUnit)
go j.readAndUpdate(model.RankTypeKillPlayer)
go j.readAndUpdate(model.RankTypeDeKillPlayer)
go j.readAndUpdate(model.RankTypeWin)
go j.readAndUpdate(model.RankTypeLost)
go j.readAndUpdate(model.RankTypeFirstBlood)
go j.readAndUpdate(model.RankTypeDeFirstBlood)
}) })
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDeDamage)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeGeneral)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDeGeneral)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeKillUnit)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDeKillUnit)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeKillPlayer)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDeKillPlayer)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeWin)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeLost)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeFirstBlood)
})
_, _ = c.AddFunc(cfg.Cron.Update, func() {
j.readAndUpdate(model.RankTypeDeFirstBlood)
})
// persistence // persistence
_, _ = c.AddFunc(cfg.Cron.Persistence, func() { _, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeDamage) go j.persistence(model.RankTypeDamage)
}) go j.persistence(model.RankTypeDeDamage)
_, _ = c.AddFunc(cfg.Cron.Persistence, func() { go j.persistence(model.RankTypeGeneral)
j.persistence(model.RankTypeDeDamage) go j.persistence(model.RankTypeDeGeneral)
}) go j.persistence(model.RankTypeKillUnit)
_, _ = c.AddFunc(cfg.Cron.Persistence, func() { go j.persistence(model.RankTypeDeKillUnit)
j.persistence(model.RankTypeGeneral) go j.persistence(model.RankTypeKillPlayer)
}) go j.persistence(model.RankTypeDeKillPlayer)
_, _ = c.AddFunc(cfg.Cron.Persistence, func() { go j.persistence(model.RankTypeWin)
j.persistence(model.RankTypeDeGeneral) go j.persistence(model.RankTypeLost)
}) go j.persistence(model.RankTypeFirstBlood)
_, _ = c.AddFunc(cfg.Cron.Persistence, func() { go j.persistence(model.RankTypeDeFirstBlood)
j.persistence(model.RankTypeKillUnit)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeDeKillUnit)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeKillPlayer)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeDeKillPlayer)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeWin)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeLost)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeFirstBlood)
})
_, _ = c.AddFunc(cfg.Cron.Persistence, func() {
j.persistence(model.RankTypeDeFirstBlood)
}) })
c.Start() c.Start()
} }
@ -251,16 +207,14 @@ func (j *Job) readAndUpdate(rankType int32) {
rankLen := len(rank) rankLen := len(rank)
var score int64 var score int64
if rankLen == 0 { if rankLen > 0 {
score = 0
} else {
// 取当前榜最后一名分数 // 取当前榜最后一名分数
score = rank[rankLen-1][1] score = rank[rankLen-1][1]
} }
// 若榜内数量不够,则直接取 max - len 数量的人 score排序一下 // 若榜内数量不够,则直接取 max - len 数量的人 score排序一下
limit := model.MaxRankN limit := model.MaxRankN
if rankLen < model.MaxRankN { if rankLen < model.MaxRankN {
limit = model.MaxRankN - rankLen + 1 // +1是避免取到自己少取一位 limit = model.MaxRankN - rankLen + 1 // +1是避免取到自己 少取一位
} }
// 末位 score // 末位 score

@ -32,7 +32,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
log.New(os.Stdout, "\r\n", log.LstdFlags), log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{ logger.Config{
SlowThreshold: 5 * time.Second, SlowThreshold: 5 * time.Second,
LogLevel: logger.Info, LogLevel: logger.Warn,
IgnoreRecordNotFoundError: true, IgnoreRecordNotFoundError: true,
Colorful: true, Colorful: true,
}, },

Loading…
Cancel
Save