Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions server/src/QRServer/common/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ def __serialize_entry(cls, entry: Optional[GameResultHistory]):
return '#'.join(data)


class ServerRankingThisMonthResponse(ResponseMessage):
prefix = ['<S>', '<SERVER>', '<RANKING(thisMonth)>']
class ServerRankingResponse(ResponseMessage):
prefix = ['<S>', '<SERVER>', '<RANKING>']
argc = [-1]

@classmethod
Expand All @@ -443,6 +443,10 @@ def __serialize_entry(cls, entry: RankingEntry):
]


class ServerRankingThisMonthResponse(ServerRankingResponse):
prefix = ['<S>', '<SERVER>', '<RANKING(thisMonth)>']


class LobbyStateResponse(ResponseMessage):
prefix = ['<L>']
argc = [170]
Expand Down
9 changes: 7 additions & 2 deletions server/src/QRServer/lobby/lobbyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
HelloLobbyRequest, JoinLobbyRequest, ServerRecentRequest, ServerRankingRequest, ServerAliveRequest, \
LobbyStateResponse, LobbyChatMessage, SetCommentRequest, ChallengeMessage, ChallengeAuthMessage, \
DisconnectRequest, PolicyFileRequest, CrossDomainPolicyAllowAllResponse, NameTakenRequest, \
NameTakenResponseYes, NameTakenResponseNo, ChangePasswordRequest, ChangePasswordResponseOk
NameTakenResponseYes, NameTakenResponseNo, ChangePasswordRequest, ChangePasswordResponseOk, \
ServerRankingResponse
from QRServer.discord.webhook import Webhook

log = logging.getLogger('qr.lobby_client_handler')
Expand Down Expand Up @@ -118,6 +119,7 @@ async def _handle_server_recent(self, _: ServerRecentRequest):
await self.send_msg(LastPlayedResponse.new(recent_matches))

async def _handle_server_ranking(self, request: ServerRankingRequest):
now = datetime.now()
start_date, end_date = utils.make_month_dates(request.get_month(), request.get_year())

rankings = await self.connector.get_ranking(
Expand All @@ -126,7 +128,10 @@ async def _handle_server_ranking(self, request: ServerRankingRequest):
ranked_only=self.config.leaderboards_ranked_only.get(),
include_void=self.config.leaderboards_include_void.get()
)
await self.send_msg(ServerRankingThisMonthResponse.new(rankings))
if now.month == request.get_month() and now.year == request.get_year():
await self.send_msg(ServerRankingThisMonthResponse.new(rankings))
else:
await self.send_msg(ServerRankingResponse.new(rankings))

async def _handle_server_alive(self, _: ServerAliveRequest):
await self.send_msg(ServerAliveResponse.new())
Expand Down
27 changes: 26 additions & 1 deletion server/tests/it/test_lobby.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import asyncio
from unittest.mock import patch
from datetime import datetime

from QRServer.common.classes import LobbyPlayer
from QRServer.common.messages import JoinLobbyRequest, LobbyStateResponse, LobbyDuplicateResponse, SetCommentRequest, \
BroadcastCommentResponse, NameTakenRequest, NameTakenResponseYes, NameTakenResponseNo, ChangePasswordRequest, \
ChangePasswordResponseOk, LobbyBadMemberResponse
ChangePasswordResponseOk, LobbyBadMemberResponse, ServerRankingRequest, ServerRankingThisMonthResponse, \
ServerRankingResponse
from QRServer.db.connector import DbConnector
from . import QuadradiusIntegrationTestCase


Expand Down Expand Up @@ -282,3 +285,25 @@ async def test_lobby_password_change_no_login(self):
client = await self.new_lobby_client()
await client.send_message(ChangePasswordRequest.new('912ec803b2ce49e4a541068d495ab570'))
await client.assert_no_more_messages()

@patch('QRServer.lobby.lobbyclient.datetime')
async def test_ranking(self, datetime_mock):
datetime_mock.now.return_value = datetime.fromisoformat('2024-10-01T00:00:00')
datetime_mock.side_effect = lambda *x: datetime(*x)
self.server.connector = DbConnector(':memory:')
await self.server.connector.connect()

client = await self.new_lobby_client()

await client.send_message(
JoinLobbyRequest.new('John', 'cf585d509bf09ce1d2ff5d4226b7dacb'))
await client.assert_received_message(
LobbyStateResponse.new([LobbyPlayer(username='John')]))

await client.send_message(
ServerRankingRequest.new('2024', '10'))
await client.assert_received_message(ServerRankingThisMonthResponse.new([]))

await client.send_message(
ServerRankingRequest.new('2024', '9'))
await client.assert_received_message(ServerRankingResponse.new([]))