From 0581d00d74c3572976fbac43ab727554d3dd771f Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:32:11 +0800 Subject: [PATCH 1/8] 1 --- server/game2006service/metadata/GameRedis.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 server/game2006service/metadata/GameRedis.js diff --git a/server/game2006service/metadata/GameRedis.js b/server/game2006service/metadata/GameRedis.js new file mode 100644 index 00000000..239879bd --- /dev/null +++ b/server/game2006service/metadata/GameRedis.js @@ -0,0 +1,8 @@ +const utils = require('j7/utils'); +const basewrap = require('./basewrap'); + +class GameRedis extends basewrap.BaseWrap { + +} + +module.exports = GameRedis; From d9b58760f132353accd661dd06896ac28a06ea79 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:32:57 +0800 Subject: [PATCH 2/8] 1 --- server/game2006service/metadata/factory.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/game2006service/metadata/factory.js b/server/game2006service/metadata/factory.js index c97e13bd..ece70b3d 100644 --- a/server/game2006service/metadata/factory.js +++ b/server/game2006service/metadata/factory.js @@ -84,7 +84,12 @@ function init() { '', 'GameDb2006' ); - + + registerMetaClass(configDir + 'game_redis.json', + '', + 'GameRedis' + ); + registerMetaClass(configDir + 'rankSeason.json', '', 'RankSeason' From 6259f74ec0197439a43f165e3703e96a19a03461 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:37:44 +0800 Subject: [PATCH 3/8] 1 --- server/game2006service/services/redis.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/game2006service/services/redis.js b/server/game2006service/services/redis.js index ce958567..a75be74b 100644 --- a/server/game2006service/services/redis.js +++ b/server/game2006service/services/redis.js @@ -1,7 +1,9 @@ +const metaFactory = require('../metadata/factory'); + const redis = require("redis") const REDIS_CONF = { - port: 6379, - host: '127.0.0.1' + port: metaFactory.getMetaByKey('GameRedis', '0')['port'], + host: metaFactory.getMetaByKey('GameRedis', '0')['host'], } class Redis { From 7474f9a409783eb6d797002ae57e0ccbb9a8884d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:44:29 +0800 Subject: [PATCH 4/8] 1 --- server/game2006service/tasks/rankings.js | 102 +---------------------- 1 file changed, 4 insertions(+), 98 deletions(-) diff --git a/server/game2006service/tasks/rankings.js b/server/game2006service/tasks/rankings.js index 2fba42d5..149bb2a9 100644 --- a/server/game2006service/tasks/rankings.js +++ b/server/game2006service/tasks/rankings.js @@ -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; \ No newline at end of file +exports.init = init; From b110ef949932b436596ca6b15cdb94a85ee6cc6d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:45:16 +0800 Subject: [PATCH 5/8] 1 --- server/game2006service/tasks/rankings.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/server/game2006service/tasks/rankings.js b/server/game2006service/tasks/rankings.js index 149bb2a9..e3300e5f 100644 --- a/server/game2006service/tasks/rankings.js +++ b/server/game2006service/tasks/rankings.js @@ -116,17 +116,14 @@ class Rankings { if (element.score>2800) { if (sorted.length >= 10000){ if (element.score >= sorted[sorted.length - 1].score){ - sorted.pop() - this._recordsSort(sorted,element) + this._recordsSort(sorted,element); + sorted.pop(); } } else { - this._recordsSort(sorted,element) + this._recordsSort(sorted,element); } } } - // if (sorted.length>10000) { - // sorted.length = 10000; - // } } _recordsSort(sorted,element){ From 744537992442086ee3f20fe1fa9baebb379e4916 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:45:57 +0800 Subject: [PATCH 6/8] 1 --- server/game2006service/tasks/rankings.js | 243 +++++++++++------------ 1 file changed, 121 insertions(+), 122 deletions(-) diff --git a/server/game2006service/tasks/rankings.js b/server/game2006service/tasks/rankings.js index e3300e5f..a99fb8e7 100644 --- a/server/game2006service/tasks/rankings.js +++ b/server/game2006service/tasks/rankings.js @@ -8,142 +8,141 @@ const LAST_IDX = 'userLastIdx'; class Rankings { - async start() { - console.log('Rankings.start'); - while (true) { - await this.doRanking(); - const sleepTime = 60*60*2 - console.log('sleepTime:' + sleepTime, new Date(), sleepTime /60); - await utils.sleep(sleepTime * 1000); + async start() { + console.log('Rankings.start'); + while (true) { + await this.doRanking(); + const sleepTime = 60*60*2; + console.log('sleepTime:' + sleepTime, new Date(), sleepTime /60); + 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 { - 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 pushResultToRedis(sorted) { + let rankingList = []; + await utils.serial( + sorted, + (value , index) =>{ + value.ranking = index+1 + Redis.hset(RANKING_KEY + "ranking",value.ranking,value.account_id) + Redis.hset(RANKING_KEY + "account",value.account_id,value.ranking) + } + ) + } + + //获取用户记录 + 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; } - - //计算用户排行榜 - 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"); - - } + 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 + }; + } - //增加排名列 - async pushResultToRedis(sorted) { - let rankingList = []; - await utils.serial( - sorted, - (value , index) =>{ - value.ranking = index+1 - Redis.hset(RANKING_KEY + "ranking",value.ranking,value.account_id) - Redis.hset(RANKING_KEY + "account",value.account_id,value.ranking) - } - ) - } - - //获取用户记录 - 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; + //给记录按分数排序 + recordsSort(sorted, records) { + // 根据分数加入到排序表中 + for (let element of records) { + if (element.score>2800) { + if (sorted.length >= 10000){ + if (element.score >= sorted[sorted.length - 1].score){ + this._recordsSort(sorted,element); + sorted.pop(); + } + } else { + this._recordsSort(sorted,element); } - 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, records) { - // 根据分数加入到排序表中 - for (let element of records) { - if (element.score>2800) { - if (sorted.length >= 10000){ - if (element.score >= sorted[sorted.length - 1].score){ - this._recordsSort(sorted,element); - sorted.pop(); - } - } else { - 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; - }); - } + _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() { - (new Rankings()).start(); + (new Rankings()).start(); } exports.init = init; From 580b5228dd12134b02ddcbcd3a966593e20100a6 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 17:46:53 +0800 Subject: [PATCH 7/8] 1 --- server/game2006service/tasks/rankings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/game2006service/tasks/rankings.js b/server/game2006service/tasks/rankings.js index a99fb8e7..932a6442 100644 --- a/server/game2006service/tasks/rankings.js +++ b/server/game2006service/tasks/rankings.js @@ -33,7 +33,7 @@ class Rankings { await this.calcRanking(); console.log("生成rank榜单") } - }catch (err){ + } catch (err){ console.log(err); } } From ef65397d8a955a1e68ef088f723fc590827b79ad Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jul 2023 19:14:38 +0800 Subject: [PATCH 8/8] 1 --- server/game2006service/tasks/rankings.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/server/game2006service/tasks/rankings.js b/server/game2006service/tasks/rankings.js index 932a6442..9fcd80ca 100644 --- a/server/game2006service/tasks/rankings.js +++ b/server/game2006service/tasks/rankings.js @@ -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);