1
This commit is contained in:
parent
6259f74ec0
commit
7474f9a409
@ -3,18 +3,15 @@ const utils = require('j7/utils');
|
||||
const constant = require('../constant');
|
||||
const Redis = require('../services/redis');
|
||||
|
||||
/**
|
||||
* redis@3.1.2
|
||||
*/
|
||||
const RANKING_KEY = 'game2006api:'
|
||||
const LAST_IDX = 'userLastIdx'
|
||||
const RANKING_KEY = 'game2006api:';
|
||||
const LAST_IDX = 'userLastIdx';
|
||||
|
||||
class Rankings {
|
||||
|
||||
async start() {
|
||||
console.log('Rankings.start');
|
||||
while (true) {
|
||||
await this.doRanking();
|
||||
// const sleepTime = 60*60*2
|
||||
const sleepTime = 60*60*2
|
||||
console.log('sleepTime:' + sleepTime, new Date(), sleepTime /60);
|
||||
await utils.sleep(sleepTime * 1000);
|
||||
@ -23,8 +20,6 @@ class Rankings {
|
||||
}
|
||||
|
||||
async doRanking() {
|
||||
//redis连接
|
||||
// Redis.connect()
|
||||
try {
|
||||
const {err, conn} = await app.getDbConn("GameDb20060");
|
||||
if (err){
|
||||
@ -42,8 +37,6 @@ class Rankings {
|
||||
}catch (err){
|
||||
console.log(err);
|
||||
}
|
||||
//关闭redis连接
|
||||
// Redis.quit()
|
||||
}
|
||||
|
||||
//计算用户排行榜
|
||||
@ -91,79 +84,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) {
|
||||
const {err, rows} = await conn.execQuery(
|
||||
@ -223,24 +143,10 @@ class Rankings {
|
||||
});
|
||||
}
|
||||
|
||||
//增加排名列
|
||||
async pushRankingResult(sorted) {
|
||||
let rankingList = [];
|
||||
await utils.serial(
|
||||
sorted,
|
||||
(value , index) =>{
|
||||
value.ranking = index+1
|
||||
rankingList.push(value)
|
||||
}
|
||||
)
|
||||
return rankingList;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function init() {
|
||||
(new Rankings()).start();
|
||||
}
|
||||
|
||||
exports.init = init;
|
||||
exports.init = init;
|
||||
|
Loading…
x
Reference in New Issue
Block a user