Skip to content

Commit 81f4628

Browse files
committed
use setWindowFields
1 parent 0ff2ac6 commit 81f4628

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

backend/src/dal/leaderboards.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,7 @@ export async function update(
135135
timeTyping: { $gt: isDevEnvironment() ? 0 : minTimeTyping },
136136
},
137137
},
138-
{
139-
$sort: {
140-
[`${key}.wpm`]: -1,
141-
[`${key}.acc`]: -1,
142-
[`${key}.timestamp`]: -1,
143-
},
144-
},
138+
145139
{
146140
$project: {
147141
_id: 0,
@@ -158,6 +152,20 @@ export async function update(
158152
discordAvatar: 1,
159153
inventory: 1,
160154
premium: 1,
155+
sortKey: {
156+
$add: [
157+
{ $multiply: [`${key}.wpm`, 1e12] }, //maybe use $convert: {input: `${key}.wpm`,to: "double",onError: null,},
158+
{ $multiply: [`${key}.acc`, 1e9] },
159+
[`${key}.timestamp`],
160+
],
161+
},
162+
},
163+
},
164+
//sort by wpm, acc, timestamp descending, add rank number
165+
{
166+
$setWindowFields: {
167+
sortBy: { sortKey: -1 },
168+
output: { "user.rank": { $documentNumber: {} } },
161169
},
162170
},
163171
{
@@ -200,17 +208,6 @@ export async function update(
200208
else: "$$REMOVE",
201209
},
202210
},
203-
204-
"user.rank": {
205-
$function: {
206-
lang: "js",
207-
args: [],
208-
body: `function() {
209-
try {row_number+= 1;} catch (e) {row_number= 1;}
210-
return row_number;
211-
}`,
212-
},
213-
},
214211
},
215212
},
216213
{

0 commit comments

Comments
 (0)