This commit is contained in:
aozhiwei 2023-07-25 19:16:57 +08:00
commit a5a3eb563c
4 changed files with 138 additions and 225 deletions

View File

@ -0,0 +1,8 @@
const utils = require('j7/utils');
const basewrap = require('./basewrap');
class GameRedis extends basewrap.BaseWrap {
}
module.exports = GameRedis;

View File

@ -85,6 +85,11 @@ function init() {
'GameDb2006' 'GameDb2006'
); );
registerMetaClass(configDir + 'game_redis.json',
'',
'GameRedis'
);
registerMetaClass(configDir + 'rankSeason.json', registerMetaClass(configDir + 'rankSeason.json',
'', '',
'RankSeason' 'RankSeason'

View File

@ -1,7 +1,9 @@
const metaFactory = require('../metadata/factory');
const redis = require("redis") const redis = require("redis")
const REDIS_CONF = { const REDIS_CONF = {
port: 6379, port: metaFactory.getMetaByKey('GameRedis', '0')['port'],
host: '127.0.0.1' host: metaFactory.getMetaByKey('GameRedis', '0')['host'],
} }
class Redis { class Redis {

View File

@ -3,28 +3,24 @@ const utils = require('j7/utils');
const constant = require('../constant'); const constant = require('../constant');
const Redis = require('../services/redis'); const Redis = require('../services/redis');
/** const RANKING_KEY = 'game2006api:';
* redis@3.1.2 const LAST_IDX = 'userLastIdx';
*/
const RANKING_KEY = 'game2006api:' const BASE_SCORE = 2800;
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;
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() { async doRanking() {
//redis连接
// Redis.connect()
try { try {
const {err, conn} = await app.getDbConn("GameDb20060"); const {err, conn} = await app.getDbConn("GameDb20060");
if (err){ if (err){
@ -34,16 +30,14 @@ class Rankings {
await Redis.del(RANKING_KEY + 'account') await Redis.del(RANKING_KEY + 'account')
await Redis.del(RANKING_KEY + 'ranking') await Redis.del(RANKING_KEY + 'ranking')
await this.calcRanking(); await this.calcRanking();
console.log("新rank榜单") console.log("新rank榜单")
}else { }else {
await this.calcRanking(); await this.calcRanking();
console.log("生成rank榜单") console.log("生成rank榜单")
} }
}catch (err){ } catch (err){
console.log(err); console.log(err);
} }
//关闭redis连接
// Redis.quit()
} }
//计算用户排行榜 //计算用户排行榜
@ -57,29 +51,23 @@ class Rankings {
// 从user表中遍历所有用户每次取1000个用户逐步加入到排序表中 // 从user表中遍历所有用户每次取1000个用户逐步加入到排序表中
let sorted = []; let sorted = [];
let lastIdx = 0; let lastIdx = 0;
while(lastIdx>=0) { while (lastIdx>=0) {
const result = await this.getRecords(conn, lastIdx, 1000); const result = await this.getRecords(conn, lastIdx, 1000);
lastIdx = result.lastIdx; lastIdx = result.lastIdx;
if (lastIdx != -1) { if (lastIdx != -1) {
this.recordsSort(sorted, result.records) this.recordsSort(sorted, result.records)
} }
console.log(lastIdx, sorted);
} }
await this.pushResultToRedis(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"); console.log("calc ranking end");
} }
} }
//增加排名列 //增加排名列
async pushResultToRedis(sorted) { async pushResultToRedis(sorted) {
console.log(sorted);
let rankingList = []; let rankingList = [];
await utils.serial( await utils.serial(
sorted, sorted,
@ -91,79 +79,6 @@ class Rankings {
) )
} }
//跟新用户榜单
async updateRankRecords(conn,data){
if (!data || data.length == 0){
return
}
for (let item of data) {
const {err, row} = await conn.execQueryOne(
'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 account_id = ? and score_modifytime > ?',
[
item.account_id,
item.score_modifytime,
]
);
if (err){
throw err
}
if (row){
item.idx = row.idx
item.account_id = row.account_id
item.channel = row.channel
item.name = row.name
item.head_id = row.head_id
item.head_frame = row.head_frame
item.rank = row.rank
item.history_best_rank = row.history_best_rank
item.score = row.score
item.history_best_score = row.history_best_score
item.score_modifytime = row.score_modifytime
}
}
let sorted = [];
this.recordsSort(sorted,data)
let rankList = await this.pushRankingResult(sorted);
//更新redis中榜单
let newListJson = JSON.stringify(rankList);
Redis.set(RANKING_KEY + "rank",newListJson);
}
//获取数据库中最后一个自增id
async getDbLastIdx (conn){
const {err, row} = await conn.execQueryOne(
"select max(idx) as last_idx from t_user"
)
if (err) {
throw err;
}
return row.last_idx;
}
//追加新用户到榜单记录中
async pushNewUserRecords(conn,lastIdx,data){
while(lastIdx>=0) {
const result = await this.getRecords(conn, lastIdx, 1000);
lastIdx = result.lastIdx;
if (lastIdx != -1) {
this.recordsSort(data, result.records)
}
}
console.log("LAST_IDX : ",lastIdx)
let rankList = await this.pushRankingResult(data);
//存储到redis中
let listJson = JSON.stringify(rankList);
Redis.set(RANKING_KEY + "rank",listJson);
}
//获取用户记录 //获取用户记录
async getRecords(conn, lastIdx, limit) { async getRecords(conn, lastIdx, limit) {
const {err, rows} = await conn.execQuery( const {err, rows} = await conn.execQuery(
@ -193,20 +108,17 @@ class Rankings {
recordsSort(sorted, records) { recordsSort(sorted, records) {
// 根据分数加入到排序表中 // 根据分数加入到排序表中
for (let element of records) { for (let element of records) {
if (element.score>2800) { if (element.score>BASE_SCORE) {
if (sorted.length >= 10000){ if (sorted.length >= 10000){
if (element.score >= sorted[sorted.length - 1].score){ if (element.score >= sorted[sorted.length - 1].score){
sorted.pop() this._recordsSort(sorted,element);
this._recordsSort(sorted,element) sorted.pop();
} }
} else { } else {
this._recordsSort(sorted,element) this._recordsSort(sorted,element);
} }
} }
} }
// if (sorted.length>10000) {
// sorted.length = 10000;
// }
} }
_recordsSort(sorted,element){ _recordsSort(sorted,element){
@ -223,22 +135,8 @@ class Rankings {
}); });
} }
//增加排名列
async pushRankingResult(sorted) {
let rankingList = [];
await utils.serial(
sorted,
(value , index) =>{
value.ranking = index+1
rankingList.push(value)
}
)
return rankingList;
}
} }
function init() { function init() {
(new Rankings()).start(); (new Rankings()).start();
} }