add Player class
This commit is contained in:
85
BearOnline/game/_player.py
Normal file
85
BearOnline/game/_player.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from collections.abc import Iterable
|
||||
from functools import singledispatch
|
||||
|
||||
from ..cards.collections import Hand
|
||||
from ..cards import Card
|
||||
|
||||
class Player(object):
|
||||
|
||||
valid_roles = {"dealer","host","guest"}
|
||||
|
||||
def __init__(self, name, roles={}):
|
||||
self._name = name
|
||||
self._hand = Hand()
|
||||
self._score = 0
|
||||
if not roles:
|
||||
roles = {}
|
||||
elif isinstance(roles, str):
|
||||
if roles in self.valid_roles:
|
||||
roles = {roles}
|
||||
else:
|
||||
raise ValueError(f"{roles} is not a valid role.")
|
||||
elif isinstance(roles, Iterable):
|
||||
if all([role in self.valid_roles for role in roles]):
|
||||
self._roles = set(roles)
|
||||
else:
|
||||
raise ValueError(f"{', '.join(set(roles) - self.valid_roles)} are not valid roles")
|
||||
else:
|
||||
raise TypeError("Role should be a string or iterable")
|
||||
|
||||
@singledispatch
|
||||
def __add__(self, other):
|
||||
pass
|
||||
|
||||
@__add__.register
|
||||
def _(self, other: int):
|
||||
self.score += other
|
||||
|
||||
@__add__.register
|
||||
def _(self, other: Card):
|
||||
self._hand.append(other)
|
||||
|
||||
@property
|
||||
def hand(self):
|
||||
return self._hand
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value):
|
||||
self._name = value
|
||||
|
||||
@property
|
||||
def score(self):
|
||||
return self._score
|
||||
|
||||
@score.setter
|
||||
def score(self, value):
|
||||
self._score = value
|
||||
|
||||
@property
|
||||
def roles(self):
|
||||
return self._roles
|
||||
|
||||
@roles.setter
|
||||
def roles(self, value):
|
||||
if not value in self.valid_roles:
|
||||
raise ValueError(f"Invalid Player role {value}")
|
||||
|
||||
if (value == "host" or "host" in value) and "host" in self._roles:
|
||||
raise RuntimeError("Once a player is the host, they are the host forever")
|
||||
|
||||
if isinstance(value, str):
|
||||
self._roles ^= set((value,))
|
||||
elif isinstance(Iterable, value):
|
||||
self._roles ^= set(value)
|
||||
else:
|
||||
raise TypeError("Invalid role type")
|
||||
|
||||
@property
|
||||
def cards_in_hand(self):
|
||||
return len(self._hand)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user