名将擂台:刷新对手

This commit is contained in:
luying
2022-07-19 17:14:11 +08:00
parent 4ee1f25876
commit 7394f14e9d
17 changed files with 48281 additions and 72 deletions
+64 -62
View File
@@ -14,6 +14,7 @@ import { RANK_FIRST_REWARD_STATUS } from '../consts';
import { CounterModel } from "../db/Counter";
import { ActivityTimeLimitRankModel } from "../db/ActivityTimeLimitRank";
import { sendMessageToServerWithSuc } from "./pushService";
import { LadderMatchModel } from "../db/LadderMatch";
/**
@@ -49,6 +50,11 @@ export class Rank {
private setValueConfig(key: REDIS_KEY) {
switch (key) {
case REDIS_KEY.LADDER:
this.valueConfig = [
new ValueConfig(true, 'rank', 0, false, false)
];
break;
case REDIS_KEY.TOP_LINEUP_RANK:
case REDIS_KEY.TOP_HERO_RANK:
case REDIS_KEY.SUM_CE_RANK:
@@ -700,53 +706,67 @@ export class Rank {
return ranks
}
public async getRankByRange(from: number | string = '+inf', to: number | string = '-inf') {
let ranks = new Array<RoleRankInfo | GuildRankInfo>();
// 获取区间里的排行榜,格式 {rank, field, score}[]
public async getRankByRangeRaw(from: number | string = '+inf', to: number | string = '-inf', isReverse = true) {
let ranks: { rank: number, field: string, scores: number[] }[] = [];
let key = this.keyName.getName();
if (this.isUnion) {
key = await this.generateUnionRank();
}
const rankFromDb = await redisClient().zrevrangebyscoreAsync(key, from, to, "WITHSCORES");
const rankFromDb = isReverse?
(await redisClient().zrevrangebyscoreAsync(key, from, to, "WITHSCORES")):
(await redisClient().zrangebyscoreAsync(key, from, to, "WITHSCORES"));
let num = 0;
for (let ii = 0; ii < rankFromDb.length; ii += 2) {
if (num >= this.limit) break;
const field = rankFromDb[ii];
let myId = this.decodeFields(this.key, field);
const scores = this.decodeScore(rankFromDb[ii + 1]);
const info = await redisClient().hgetAsync(this.infoKey, this.composeFields(this.infoKey, myId));
const userInfo = JSON.parse(info);
let param: RoleRankInfo | GuildRankInfo;
if (this.infoKey == REDIS_KEY.USER_INFO) {
if (nowSeconds() - userInfo.updatedAt > 2 * 30 * 24 * 60 * 60) {
continue;
}
param = new RoleRankInfo(userInfo);
param.setInfo(Math.floor(ii / 2) + 1, myId, scores[0], scores[1]);
} else if (this.infoKey == REDIS_KEY.GUILD_INFO) {
if(this.key == REDIS_KEY.GUILD_LV_RANK|| this.key == REDIS_KEY.RACE_ACTIVITY) {
param = new GuildRankInfo(userInfo);
param.setInfo2(Math.floor(ii / 2) + 1, myId, scores[0], scores[1], scores[2]);
} else {
param = new GuildRankInfo(userInfo);
param.setInfo(Math.floor(ii / 2) + 1, myId, scores[0], scores[1]);
}
}
for (let extraKey of this.extraKeys) {
await this.setExInfoToParam(extraKey, param, myId, {});
}
ranks.push(param);
ranks.push({ rank: Math.floor(ii / 2) + 1, field: rankFromDb[ii], scores: this.decodeScore(rankFromDb[ii + 1]) });
num++;
}
return ranks
}
public async getRankByRange(from: number | string = '+inf', to: number | string = '-inf', isReverse = true) {
let raws = await this.getRankByRangeRaw(from, to, isReverse);
let ranks = new Array<RoleRankInfo | GuildRankInfo>();
for (let { rank, field, scores } of raws) {
let param = await this.getParam(rank, field, scores);
if(param) ranks.push(param);
}
return ranks
}
public async getParam(rank: number, field: string, scores: number[]) {
let myId = this.decodeFields(this.key, field);
const info = await redisClient().hgetAsync(this.infoKey, this.composeFields(this.infoKey, myId));
const userInfo = JSON.parse(info);
let param: RoleRankInfo | GuildRankInfo;
if (this.infoKey == REDIS_KEY.USER_INFO) {
if (nowSeconds() - userInfo.updatedAt > 2 * 30 * 24 * 60 * 60) {
return null;
}
param = new RoleRankInfo(userInfo);
param.setInfo(rank, myId, scores[0], scores[1]);
} else if (this.infoKey == REDIS_KEY.GUILD_INFO) {
if(this.key == REDIS_KEY.GUILD_LV_RANK|| this.key == REDIS_KEY.RACE_ACTIVITY) {
param = new GuildRankInfo(userInfo);
param.setInfo2(rank, myId, scores[0], scores[1], scores[2]);
} else {
param = new GuildRankInfo(userInfo);
param.setInfo(rank, myId, scores[0], scores[1]);
}
}
for (let extraKey of this.extraKeys) {
await this.setExInfoToParam(extraKey, param, myId, {});
}
return param
}
public async getRankByRank(start: number, end: number) {
let ranks = new Array<RoleRankInfo | GuildRankInfo>();
@@ -762,35 +782,8 @@ export class Rank {
let num = 0;
for (let ii = 0; ii < rankFromDb.length; ii += 2) {
if (num >= this.limit) break;
const field = rankFromDb[ii];
let myId = this.decodeFields(this.key, field);
const scores = this.decodeScore(rankFromDb[ii + 1]);
const info = await redisClient().hgetAsync(this.infoKey, this.composeFields(this.infoKey, myId));
if(!info) continue;
const userInfo = JSON.parse(info);
let param: RoleRankInfo | GuildRankInfo;
if (this.infoKey == REDIS_KEY.USER_INFO) {
if (nowSeconds() - userInfo.updatedAt > 2 * 30 * 24 * 60 * 60) {
continue;
}
param = new RoleRankInfo(userInfo);
param.setInfo(Math.floor(ii / 2) + 1, myId, scores[0], scores[1]);
} else if (this.infoKey == REDIS_KEY.GUILD_INFO) {
if(this.key == REDIS_KEY.GUILD_LV_RANK) {
param = new GuildRankInfo(userInfo);
param.setInfo2(Math.floor(ii / 2) + 1, myId, scores[0], scores[1], scores[2]);
} else {
param = new GuildRankInfo(userInfo);
param.setInfo(Math.floor(ii / 2) + 1, myId, scores[0], scores[1]);
}
}
for (let extraKey of this.extraKeys) {
await this.setExInfoToParam(extraKey, param, myId, {});
}
ranks.push(param);
let param = await this.getParam(Math.floor(ii / 2) + 1, rankFromDb[ii], this.decodeScore(rankFromDb[ii + 1]));
if(param) ranks.push(param);
num++;
}
return ranks
@@ -1109,6 +1102,15 @@ export async function setRankRedisFromDb(type: string, args?: { serverId?: numbe
await r.setRankWithHeroInfo(hero.roleId, hid, hero.ce, hero.updatedAt.getTime(), hero);
}
}
} else if (type == REDIS_KEY.LADDER) {
let serverId = args.serverId;
let ranks = await LadderMatchModel.findAll(serverId);
let r = new Rank(type, { serverId });
r.setIsInit(true);
for (let rank of ranks) {
let role = <RoleType>rank.role;
await r.setRankWithRoleInfo(rank.roleId, rank.rank, 0, role);
}
}
}