Skip to content

Commit

Permalink
Merge pull request #8 from The-Galley/fix/lobby-fix
Browse files Browse the repository at this point in the history
Fix lobby
  • Loading branch information
andy-takker authored Mar 2, 2024
2 parents 42f9193 + fe5e3c6 commit 3bb2004
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
16 changes: 11 additions & 5 deletions industry_game/handlers/games/lobby/read_lobby.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aiohttp.web import HTTPNotFound, Response, View
from aiohttp.web import Response, View

from industry_game.utils.http.auth.base import (
AuthMixin,
Expand All @@ -7,14 +7,20 @@
from industry_game.utils.http.deps import DependenciesMixin
from industry_game.utils.http.params import parse_path_param
from industry_game.utils.http.response import msgspec_json_response
from industry_game.utils.lobby.models import LobbyStatus, LobbyStatusType


class ReadGameUserLobbyHandler(View, DependenciesMixin, AuthMixin):
@require_player_authorization
async def get(self) -> Response:
game_id = parse_path_param(self.request, "game_id", int)

game = await self.game_storage.read_by_id(game_id=game_id)
if game is None:
raise HTTPNotFound
return msgspec_json_response(game)
lobby = await self.lobby_storage.read_by_id(
game_id=game_id,
user_id=self.user.id,
)
if lobby is None:
status = LobbyStatusType.NOT_CHECKED_IN
else:
status = LobbyStatusType.CHECKED_IN
return msgspec_json_response(LobbyStatus(status=status))
12 changes: 12 additions & 0 deletions industry_game/utils/lobby/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import StrEnum, unique

import msgspec

from industry_game.db.models import UserGameLobby as UserGameLobbyDb
Expand All @@ -20,3 +22,13 @@ def from_model(cls, obj: UserGameLobbyDb) -> "Lobby":
class LobbyPagination(msgspec.Struct, frozen=True):
meta: MetaPagination
items: list[ShortUser]


@unique
class LobbyStatusType(StrEnum):
CHECKED_IN = "CHECKED_IN"
NOT_CHECKED_IN = "NOT_CHECKED_IN"


class LobbyStatus(msgspec.Struct, frozen=True):
status: StrEnum
5 changes: 1 addition & 4 deletions industry_game/utils/users/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ async def read_by_username(
session: AsyncSession,
username: str,
) -> FullUser | None:
stmt = select(UserDb).where(
UserDb.type == UserType.PLAYER,
UserDb.username == username,
)
stmt = select(UserDb).where(UserDb.username == username)
obj = (await session.scalars(stmt)).first()
return FullUser.from_model(obj) if obj else None

Expand Down
23 changes: 22 additions & 1 deletion tests/api/players/test_player_register.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from http import HTTPStatus

import pytest
from aiohttp.test_utils import TestClient
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
Expand All @@ -21,7 +24,7 @@ async def test_player_register_successful_status_created(
"name": "your name",
},
)
assert response.status == 201
assert response.status == HTTPStatus.CREATED


async def test_player_register_successful_check_db(
Expand All @@ -43,3 +46,21 @@ async def test_player_register_successful_check_db(

assert user.username == "username"
assert user.type == UserType.PLAYER


@pytest.mark.parametrize("user_type", (UserType.ADMIN, UserType.PLAYER))
async def test_player_register_same_username_error_conflict(
api_client: TestClient, create_user, user_type
):
user = await create_user(type=user_type)

response = await api_client.post(
API_URL,
json={
"username": user.username,
"password": "password",
"telegram": "telegram",
"name": "your name",
},
)
assert response.status == HTTPStatus.BAD_REQUEST

0 comments on commit 3bb2004

Please sign in to comment.