152 lines
3.8 KiB
Go
152 lines
3.8 KiB
Go
package task
|
|
|
|
import (
|
|
"f5"
|
|
"fmt"
|
|
"main/constant"
|
|
"math"
|
|
"mt"
|
|
"q5"
|
|
"sort"
|
|
)
|
|
|
|
type userDb struct {
|
|
idx int64
|
|
account_id string
|
|
address string
|
|
channel int32
|
|
rank int32
|
|
score int32
|
|
score_modifytime int64
|
|
}
|
|
|
|
type sortUserDb []userDb
|
|
|
|
func (this sortUserDb) Len() int {
|
|
return len(this)
|
|
}
|
|
|
|
func (this sortUserDb) Swap(i, j int) {
|
|
this[i], this[j] = this[j], this[i]
|
|
}
|
|
|
|
func (this sortUserDb) Less(i, j int) bool {
|
|
if this[i].score == this[j].score {
|
|
if this[i].score_modifytime == this[j].score_modifytime {
|
|
return this[i].idx < this[j].idx
|
|
} else {
|
|
return this[i].score_modifytime < this[j].score_modifytime
|
|
}
|
|
} else {
|
|
return this[i].score > this[j].score
|
|
}
|
|
}
|
|
|
|
type SeasonRankMgr struct {
|
|
}
|
|
|
|
func (this *SeasonRankMgr) CalcRanking() {
|
|
seasonMt := mt.Table.RankSeason.GetCurrentSeason()
|
|
season := mt.Table.RankSeason.GetLastSeason()
|
|
f5.GetJsStyleDb().OrmSelectOne(
|
|
constant.GAME_DB,
|
|
"t_season_ranking",
|
|
[][]string{
|
|
{"season", q5.ToString(season.GetId())},
|
|
},
|
|
func(err error, row *f5.DataSet) {
|
|
if err != nil {
|
|
f5.GetSysLog().Error("SeasonRankMgr Error1:%v \n", err)
|
|
return
|
|
}
|
|
row.Next()
|
|
if row.NumOfReaded() <= 0 && seasonMt == nil {
|
|
fmt.Println("OK")
|
|
this.goGetUsersRecords()
|
|
}
|
|
})
|
|
}
|
|
|
|
func (this *SeasonRankMgr) goGetUsersRecords() {
|
|
userHash := make(map[int64]*userDb)
|
|
userList := []userDb{}
|
|
var lastIdx int64 = 0
|
|
f5.NewAsyncTask(
|
|
func(task *f5.AsyncTask) {
|
|
sql := fmt.Sprintf("SELECT * FROM t_user WHERE idx > %d AND score >= %d LIMIT %d",
|
|
lastIdx,
|
|
constant.BASE_SCORE,
|
|
1000)
|
|
f5.GetJsStyleDb().SelectCustomQuery(
|
|
constant.GAME_DB,
|
|
sql,
|
|
func(err error, rows *f5.DataSet) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
f5.GetSysLog().Error("SeasonRankMgr Error2:%v \n", err)
|
|
return
|
|
}
|
|
for rows.Next() {
|
|
user := userDb{}
|
|
user.idx = q5.ToInt64(rows.GetByName("idx"))
|
|
user.account_id = q5.ToString(rows.GetByName("account_id"))
|
|
user.address = q5.ToString(rows.GetByName("address"))
|
|
user.channel = q5.ToInt32(rows.GetByName("channel"))
|
|
user.rank = q5.ToInt32(rows.GetByName("rank"))
|
|
user.score = q5.ToInt32(rows.GetByName("score"))
|
|
user.score_modifytime = q5.ToInt64(rows.GetByName("score_modifytime"))
|
|
userHash[user.idx] = &user
|
|
userList = append(userList, user)
|
|
if user.idx > lastIdx {
|
|
lastIdx = user.idx
|
|
}
|
|
f5.GetSysLog().Info("get User:%s \n", user.idx)
|
|
}
|
|
task.SetSucc()
|
|
},
|
|
)
|
|
}).OnSucc(
|
|
func(task *f5.AsyncTask) {
|
|
if userList != nil {
|
|
sort.Sort(sortUserDb(userList))
|
|
this.pushRankingResult(userList)
|
|
}
|
|
}).OnFail(
|
|
func(task *f5.AsyncTask) {
|
|
f5.GetSysLog().Error("SeasonRankMgr Error3 \n")
|
|
return
|
|
})
|
|
}
|
|
|
|
func (this *SeasonRankMgr) pushRankingResult(record []userDb) {
|
|
for i, v := range record {
|
|
ranking := i + 1
|
|
topX := celTopX(q5.ToInt64(ranking))
|
|
point := math.Round(celUserRankingPoint(q5.ToInt64(ranking), topX))
|
|
fields := [][]string{
|
|
{"ranking", q5.ToString(ranking)},
|
|
{"ranking_point", q5.ToString(point)},
|
|
{"account_id", v.account_id},
|
|
q5.GenFieldKvEmptyAsNull("address", v.address),
|
|
{"channel", q5.ToString(v.channel)},
|
|
{"rank", q5.ToString(v.rank)},
|
|
{"score", q5.ToString(v.score)},
|
|
{"season", q5.ToString(2)},
|
|
{"createtime", q5.ToString(f5.GetApp().GetNowSeconds())},
|
|
{"modifytime", q5.ToString(f5.GetApp().GetNowSeconds())},
|
|
}
|
|
//f5.GetJsStyleDb().Insert(
|
|
// constant.GAME_DB,
|
|
// "t_season_ranking",
|
|
// fields,
|
|
// func(err error, id int64, affectedRows int64) {
|
|
// if err != nil || affectedRows != 1 {
|
|
// // 插入失败,处理错误逻辑
|
|
// fmt.Printf("Failed to insert t_season_ranking: %v", err)
|
|
// }
|
|
// },
|
|
//)
|
|
f5.GetSysLog().Info("ranking:%v", fields)
|
|
}
|
|
}
|