This commit is contained in:
aozhiwei 2023-07-25 17:45:57 +08:00
parent b110ef9499
commit 7445379924

View File

@ -8,142 +8,141 @@ const LAST_IDX = 'userLastIdx';
class Rankings { class Rankings {
async start() { async start() {
console.log('Rankings.start'); console.log('Rankings.start');
while (true) { while (true) {
await this.doRanking(); await this.doRanking();
const sleepTime = 60*60*2 const sleepTime = 60*60*2;
console.log('sleepTime:' + sleepTime, new Date(), sleepTime /60); console.log('sleepTime:' + sleepTime, new Date(), sleepTime /60);
await utils.sleep(sleepTime * 1000); await utils.sleep(sleepTime * 1000);
}
}
async doRanking() {
try {
const {err, conn} = await app.getDbConn("GameDb20060");
if (err){
throw err
}
if (await Redis.exists(RANKING_KEY + "account")){
await Redis.del(RANKING_KEY + 'account')
await Redis.del(RANKING_KEY + 'ranking')
await this.calcRanking();
console.log("跟新rank榜单")
}else {
await this.calcRanking();
console.log("生成rank榜单")
}
}catch (err){
console.log(err);
}
}
//计算用户排行榜
async calcRanking() {
console.log("calc ranking...");
const {err, conn} = await app.getDbConn("GameDb20060");
if (err) {
throw err;
}
if (!err && conn) {
// 从user表中遍历所有用户每次取1000个用户逐步加入到排序表中
let sorted = [];
let lastIdx = 0;
while(lastIdx>=0) {
const result = await this.getRecords(conn, lastIdx, 1000);
lastIdx = result.lastIdx;
if (lastIdx != -1) {
this.recordsSort(sorted, result.records)
} }
}
await this.pushResultToRedis(sorted);
// let rankList = await this.pushRankingResult(sorted);
// console.log(rankList);
//存储到redis中
// let listJson = JSON.stringify(rankList);
// Redis.set(RANKING_KEY + "rank",listJson);
console.log("calc ranking end");
} }
}
async doRanking() { //增加排名列
try { async pushResultToRedis(sorted) {
const {err, conn} = await app.getDbConn("GameDb20060"); let rankingList = [];
if (err){ await utils.serial(
throw err sorted,
} (value , index) =>{
if (await Redis.exists(RANKING_KEY + "account")){ value.ranking = index+1
await Redis.del(RANKING_KEY + 'account') Redis.hset(RANKING_KEY + "ranking",value.ranking,value.account_id)
await Redis.del(RANKING_KEY + 'ranking') Redis.hset(RANKING_KEY + "account",value.account_id,value.ranking)
await this.calcRanking(); }
console.log("跟新rank榜单") )
}else { }
await this.calcRanking();
console.log("生成rank榜单") //获取用户记录
} async getRecords(conn, lastIdx, limit) {
}catch (err){ const {err, rows} = await conn.execQuery(
console.log(err); 'select idx,account_id,channel,convert(`name` using utf8) as name,head_id,head_frame,rank,history_best_rank,score,history_best_score,createtime, score_modifytime from t_user where idx > ? order by idx LIMIT ?',
} [
lastIdx,
limit
]
);
if (err) {
throw err;
} }
if (rows.length==0) {
//计算用户排行榜 return {
async calcRanking() { lastIdx: -1,
console.log("calc ranking..."); records: []
const {err, conn} = await app.getDbConn("GameDb20060"); };
if (err) {
throw err;
}
if (!err && conn) {
// 从user表中遍历所有用户每次取1000个用户逐步加入到排序表中
let sorted = [];
let lastIdx = 0;
while(lastIdx>=0) {
const result = await this.getRecords(conn, lastIdx, 1000);
lastIdx = result.lastIdx;
if (lastIdx != -1) {
this.recordsSort(sorted, result.records)
}
}
await this.pushResultToRedis(sorted);
// let rankList = await this.pushRankingResult(sorted);
// console.log(rankList);
//存储到redis中
// let listJson = JSON.stringify(rankList);
// Redis.set(RANKING_KEY + "rank",listJson);
console.log("calc ranking end");
}
} }
// Redis.set(LAST_IDX,rows[rows.length-1].idx)
return {
lastIdx: rows[rows.length-1].idx,
records: rows
};
}
//增加排名列 //给记录按分数排序
async pushResultToRedis(sorted) { recordsSort(sorted, records) {
let rankingList = []; // 根据分数加入到排序表中
await utils.serial( for (let element of records) {
sorted, if (element.score>2800) {
(value , index) =>{ if (sorted.length >= 10000){
value.ranking = index+1 if (element.score >= sorted[sorted.length - 1].score){
Redis.hset(RANKING_KEY + "ranking",value.ranking,value.account_id) this._recordsSort(sorted,element);
Redis.hset(RANKING_KEY + "account",value.account_id,value.ranking) sorted.pop();
} }
) } else {
} this._recordsSort(sorted,element);
//获取用户记录
async getRecords(conn, lastIdx, limit) {
const {err, rows} = await conn.execQuery(
'select idx,account_id,channel,convert(`name` using utf8) as name,head_id,head_frame,rank,history_best_rank,score,history_best_score,createtime, score_modifytime from t_user where idx > ? order by idx LIMIT ?',
[
lastIdx,
limit
]
);
if (err) {
throw err;
} }
if (rows.length==0) { }
return {
lastIdx: -1,
records: []
};
}
// Redis.set(LAST_IDX,rows[rows.length-1].idx)
return {
lastIdx: rows[rows.length-1].idx,
records: rows
};
} }
}
//给记录按分数排序 _recordsSort(sorted,element){
recordsSort(sorted, records) { sorted.push(element);
// 根据分数加入到排序表中 sorted.sort(function(a,b) {
for (let element of records) { let r = b.score - a.score;
if (element.score>2800) { if (r==0) {
if (sorted.length >= 10000){ r = a.score_modifytime - b.score_modifytime;
if (element.score >= sorted[sorted.length - 1].score){ }
this._recordsSort(sorted,element); if (r==0) {
sorted.pop(); r = a.idx - b.idx;
} }
} else { return r;
this._recordsSort(sorted,element); });
} }
}
}
}
_recordsSort(sorted,element){
sorted.push(element);
sorted.sort(function(a,b) {
let r = b.score - a.score;
if (r==0) {
r = a.score_modifytime - b.score_modifytime;
}
if (r==0) {
r = a.idx - b.idx;
}
return r;
});
}
} }
function init() { function init() {
(new Rankings()).start(); (new Rankings()).start();
} }
exports.init = init; exports.init = init;