Skip to content

Commit fd5f693

Browse files
committed
CHANGES:
- RestAPI: /leaderboard playtime and credits added
1 parent 3d8bab5 commit fd5f693

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

plugins/restapi/commands.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ async def squadrons(self, limit: int = Query(default=None), offset: int = Query(
350350
}))
351351
return squadrons
352352

353-
async def leaderboard(self, what: Literal['kills', 'kills_pvp', 'deaths', 'kdr', 'deaths_pvp', 'kdr_pvp'],
353+
async def leaderboard(self, what: Literal['kills', 'kills_pvp', 'deaths', 'kdr', 'deaths_pvp', 'kdr_pvp', 'playtime'],
354354
order: Literal['asc', 'desc'] = 'desc', query: Optional[str] = None,
355355
limit: Optional[int] = 10, offset: Optional[int] = 0, server_name: Optional[str] = None):
356356
columns = {
@@ -359,7 +359,9 @@ async def leaderboard(self, what: Literal['kills', 'kills_pvp', 'deaths', 'kdr',
359359
"deaths": 5,
360360
"kdr": 6,
361361
"deaths_pvp": 7,
362-
"kdr_pvp": 8
362+
"kdr_pvp": 8,
363+
"playtime": 9,
364+
"credits": 10
363365
}
364366
order_column = columns[what]
365367
if server_name:
@@ -382,9 +384,13 @@ async def leaderboard(self, what: Literal['kills', 'kills_pvp', 'deaths', 'kdr',
382384
CASE WHEN SUM(s.deaths_pvp) = 0
383385
THEN SUM(s.pvp) ELSE SUM(s.pvp::DECIMAL) / SUM(s.deaths_pvp::DECIMAL)
384386
END AS "kdr_pvp",
387+
ROUND(SUM(EXTRACT(EPOCH FROM(COALESCE(s.hop_off, NOW() AT TIME ZONE 'UTC') - s.hop_on))))::INTEGER AS playtime,
388+
MAX(COALESCE(c.points, 0)) AS "credits",
385389
COUNT(*) OVER() as total_count
386390
FROM statistics s
387391
JOIN players p ON s.player_ucid = p.ucid
392+
LEFT OUTER JOIN credits c ON c.player_ucid = s.player_ucid
393+
LEFT OUTER JOIN campaigns ca ON ca.id = c.campaign_id AND NOW() AT TIME ZONE 'utc' BETWEEN ca.start AND COALESCE(ca.stop, NOW() AT TIME ZONE 'utc')
388394
{join}
389395
GROUP BY 1, 2
390396
ORDER BY {order_column} {order}
@@ -408,7 +414,7 @@ async def leaderboard(self, what: Literal['kills', 'kills_pvp', 'deaths', 'kdr',
408414
del row['total_count']
409415

410416
return LeaderBoard.model_validate({
411-
'items': [row for row in rows if not query or query.casefold() in row['name'].casefold()],
417+
'items': [row for row in rows if not query or query.casefold() in row['nick'].casefold()],
412418
'total_count': total_count,
413419
'offset': offset
414420
})

plugins/restapi/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class TopKill(BaseModel):
175175
kills_pvp: int = Field(..., description="Number of kills in PvP")
176176
deaths_pvp: int = Field(..., description="Number of deaths in PvP")
177177
kdr_pvp: float = Field(..., description="Kill/Death ratio in PvP")
178+
playtime: int = Field(..., description="Total playtime in seconds")
179+
credits: int = Field(..., description="Total credits earned")
178180

179181
model_config = {
180182
"json_schema_extra": {

0 commit comments

Comments
 (0)