Skip to content

Commit 2c0ac5b

Browse files
committed
add friendsRank to daily rank
1 parent 56e7130 commit 2c0ac5b

File tree

4 files changed

+9
-11
lines changed

4 files changed

+9
-11
lines changed

backend/__tests__/__integration__/utils/daily-leaderboards.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ describe("Daily Leaderboards", () => {
344344
//WHEN / THEN
345345
expect(
346346
await lb.getRank(user2.uid, dailyLeaderboardsConfig, friends)
347-
).toEqual({ rank: 1, ...user2 });
347+
).toEqual({ rank: 2, friendsRank: 1, ...user2 });
348348

349349
expect(
350350
await lb.getRank(user1.uid, dailyLeaderboardsConfig, friends)
351-
).toEqual({ rank: 2, ...user1 });
351+
).toEqual({ rank: 3, friendsRank: 2, ...user1 });
352352
});
353353
});
354354

backend/redis-scripts/get-rank.lua

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local include_scores = ARGV[2]
1515
local user_ids_csv = ARGV[3]
1616

1717
local rank = nil
18+
local friendsRank = nil
1819
local result = {}
1920
local score = ''
2021

@@ -35,20 +36,16 @@ if user_ids_csv ~= "" then
3536

3637
for i = 1, #scored_users do
3738
if scored_users[i].user_id == user_id then
38-
rank = i - 1
39+
friendsRank = i - 1
3940
end
4041
end
4142

42-
else
43-
-- global leaderboarc
44-
45-
rank = redis_call('ZREVRANK', leaderboard_scores_key, user_id)
46-
4743
end
4844

45+
rank = redis_call('ZREVRANK', leaderboard_scores_key, user_id)
4946
if (include_scores == "true") then
5047
score = redis_call('ZSCORE', leaderboard_scores_key, user_id)
5148
end
5249
result = redis_call('HGET', leaderboard_results_key, user_id)
5350

54-
return {rank, score, result}
51+
return {rank, score, result, friendsRank}

backend/src/init/redis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type RedisConnectionWithCustomMethods = Redis & {
4343
uid: string,
4444
withScores: string,
4545
userIds: string
46-
) => Promise<[number, string, string]>; //rank, score(optional), entry json
46+
) => Promise<[number, string, string, number]>; //rank, score(optional), entry json, friendsRank(optional)
4747
purgeResults: (
4848
keyCount: number,
4949
uid: string,

backend/src/utils/daily-leaderboards.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class DailyLeaderboard {
209209
const { leaderboardScoresKey, leaderboardResultsKey } =
210210
this.getTodaysLeaderboardKeys();
211211

212-
const [rank, _score, result] = await connection.getRank(
212+
const [rank, _score, result, friendsRank] = await connection.getRank(
213213
2,
214214
leaderboardScoresKey,
215215
leaderboardResultsKey,
@@ -229,6 +229,7 @@ export class DailyLeaderboard {
229229
RedisDailyLeaderboardEntrySchema
230230
),
231231
rank: rank + 1,
232+
friendsRank: friendsRank !== undefined ? friendsRank + 1 : undefined,
232233
};
233234
} catch (error) {
234235
throw new Error(

0 commit comments

Comments
 (0)