This commit is contained in:
aozhiwei 2023-07-25 19:14:38 +08:00
parent 580b5228dd
commit ef65397d8a

View File

@ -6,6 +6,8 @@ const Redis = require('../services/redis');
const RANKING_KEY = 'game2006api:';
const LAST_IDX = 'userLastIdx';
const BASE_SCORE = 2800;
class Rankings {
async start() {
@ -28,7 +30,7 @@ class Rankings {
await Redis.del(RANKING_KEY + 'account')
await Redis.del(RANKING_KEY + 'ranking')
await this.calcRanking();
console.log("新rank榜单")
console.log("新rank榜单")
}else {
await this.calcRanking();
console.log("生成rank榜单")
@ -49,29 +51,23 @@ class Rankings {
// 从user表中遍历所有用户每次取1000个用户逐步加入到排序表中
let sorted = [];
let lastIdx = 0;
while(lastIdx>=0) {
while (lastIdx>=0) {
const result = await this.getRecords(conn, lastIdx, 1000);
lastIdx = result.lastIdx;
if (lastIdx != -1) {
this.recordsSort(sorted, result.records)
}
console.log(lastIdx, sorted);
}
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 pushResultToRedis(sorted) {
console.log(sorted);
let rankingList = [];
await utils.serial(
sorted,
@ -112,7 +108,7 @@ class Rankings {
recordsSort(sorted, records) {
// 根据分数加入到排序表中
for (let element of records) {
if (element.score>2800) {
if (element.score>BASE_SCORE) {
if (sorted.length >= 10000){
if (element.score >= sorted[sorted.length - 1].score){
this._recordsSort(sorted,element);