Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: Hand Notification #338

Merged
merged 15 commits into from
Sep 1, 2022
22 changes: 22 additions & 0 deletions tests/whist_server/api/room/test_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import MagicMock

from whist_core.game.errors import HandNotDoneError

from tests.whist_server.api.room.base_created_case import BaseCreateGameTestCase


class GameTestCase(BaseCreateGameTestCase):
def setUp(self) -> None:
super().setUp()
self.hand_mock = MagicMock()
self.room_mock.next_hand = MagicMock(return_value=self.hand_mock)

def test_next_hand_success(self):
response = self.client.post(url=f'/room/next_hand/{self.room_mock.id}')
self.assertEqual('Success', response.json()['status'])
self.assertEqual(200, response.status_code)

def test_next_hand_not_done(self):
self.room_mock.next_hand = MagicMock(side_effect=HandNotDoneError())
response = self.client.post(url=f'/room/next_hand/{self.room_mock.id}')
self.assertEqual(400, response.status_code)
2 changes: 2 additions & 0 deletions whist_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from whist_server.api.ranking.leaderboard import router as leaderboard
from whist_server.api.room.action import router as game_action
from whist_server.api.room.create import router as game_creation
from whist_server.api.room.game import router as game_room
from whist_server.api.room.info import router as game_info
from whist_server.api.room.join import router as game_join
from whist_server.api.room.trick import router as game_trick
Expand All @@ -24,6 +25,7 @@
app.include_router(github)
app.include_router(game_action)
app.include_router(game_creation)
app.include_router(game_room)
app.include_router(game_info)
app.include_router(game_join)
app.include_router(game_trick)
Expand Down
36 changes: 36 additions & 0 deletions whist_server/api/room/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Route of /room/game"""
from fastapi import APIRouter, BackgroundTasks, Depends, status
from whist_core.game.errors import HandNotDoneError

from whist_server.api.util import create_http_error
from whist_server.database.room import RoomInDb
from whist_server.services.channel_service import ChannelService
from whist_server.services.room_db_service import RoomDatabaseService
from whist_server.web_socket.events.event import NextHandEvent

router = APIRouter(prefix='/room')


@router.post('/next_hand/{room_id}', status_code=200)
def next_hand(room_id: str, background_tasks: BackgroundTasks,
channel_service: ChannelService = Depends(ChannelService),
room_service=Depends(RoomDatabaseService)) -> dict:
"""
Request to start the next hand.
:param room_id: at which table the card is requested to be played
:param background_tasks: asynchronous handler
:param channel_service: Injection of the websocket channel manager.
:param room_service: Injection of the room database service. Requires to interact with the
database.
:return: Status: 'Success' if next hand is created else raises error.
"""
room: RoomInDb = room_service.get(room_id)

try:
_ = room.next_hand()
room_service.save(room)
background_tasks.add_task(channel_service.notify(room_id, NextHandEvent()))
except HandNotDoneError as ready_error:
message = 'The hand is not done yet.'
raise create_http_error(message, status.HTTP_400_BAD_REQUEST) from ready_error
return {'status': 'Success'}
7 changes: 7 additions & 0 deletions whist_server/database/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional

from pydantic import BaseModel, Field
from whist_core.game.hand import Hand
from whist_core.game.player_at_table import PlayerAtTable
from whist_core.game.rubber import Rubber
from whist_core.game.trick import Trick
Expand Down Expand Up @@ -69,6 +70,12 @@ def current_trick(self) -> Trick:
"""
return self._current_hand().current_trick

def next_hand(self) -> Hand:
"""
Creates the next hand.
"""
return self._current_game().next_hand()

def next_trick(self) -> Trick:
"""
Creates the next trick.
Expand Down
6 changes: 6 additions & 0 deletions whist_server/web_socket/events/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class CardPlayedEvent(Event):
player: Player


class NextHandEvent(Event):
"""
It is send when the next hand has been started.
"""


class PlayerJoinedEvent(Event):
"""
It is sent when a player joins a room.
Expand Down