blob: 2df68820331ed896fc0baaae77bcdf994e69bb4f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
""" Module defining the player class. """
import asyncio
import json
from typing import Any, Dict, Tuple
class Player:
""" Represent a player. """
def __init__(self) -> None:
self.cards: Dict[int, Tuple[int, str]] = {}
self.next_idx = 0
self.score = 0
self.has_played = False
self.name = 'default'
self.notifications: asyncio.Queue = asyncio.Queue()
def pop_card(self, card_id: int) -> Tuple[int, str]:
""" Take and return the card at index card_id. """
return self.cards.pop(card_id)
def inc_score(self) -> None:
""" Increase the score by one. """
self.score += 1
self.register_notification({
'op': 'updated_score',
'content': self.score,
})
def receive_card(self, card: Tuple[int, str]) -> int:
""" Receive a card and return its index. """
self.cards[self.next_idx] = card
self.next_idx += 1
return self.next_idx - 1
def register_notification(self, obj: Any) -> None:
""" Register a notification to be picked up by the client. """
message = json.dumps({'type': 'notification', 'content': obj})
self.notifications.put_nowait(message)
|