player.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from enum import Enum
  2. from cards import Card
  3. from color import Color
  4. from option import Option, OptionCode
  5. from typing import List
  6. import json
  7. class StateCode(str, Enum):
  8. START = "start"
  9. PICK_COLOR = "pick_color"
  10. DEAL = "deal"
  11. DEAL_OTHER = "deal_other"
  12. SWAP_CARD = "swap_card"
  13. SWAP_CARD_OTHERS = "swap_card_others"
  14. PLAY_CARD = "play_card"
  15. PLAY_CARD_OTHER = "play_card_other"
  16. class ErrorCode(str, Enum):
  17. NO_GAME = "no_game"
  18. UNKNOWN_CODE = "unknown_code"
  19. COLOR_ALREADY_CHOSEN = "color_already_chosen"
  20. OPTION_NOT_ALLOWED = "option_not_allowed"
  21. class PlayerState(object):
  22. def __init__(self, code, **kwargs):
  23. self.code = StateCode(code)
  24. self.args = kwargs
  25. class PlayerError(object):
  26. def __init__(self, code, **kwargs):
  27. self.code = ErrorCode(code)
  28. self.args = kwargs
  29. class OtherPlayer(object):
  30. def __init__(self, color, name):
  31. self.color = color
  32. self.name = name
  33. class DictEncoder(json.JSONEncoder):
  34. def default(self, obj):
  35. if hasattr(obj, '__dict__'):
  36. return obj.__dict__
  37. return json.JSONEncoder.default(self, obj)
  38. class Player(object):
  39. color: Color
  40. name: str
  41. game_code: int
  42. connected: bool
  43. hand: List[Card]
  44. options: List[Option]
  45. message: str
  46. state: PlayerState
  47. error: PlayerError
  48. swap_card: Card
  49. card_is_swapped: bool
  50. passed: bool
  51. others: List[OtherPlayer]
  52. current_player: OtherPlayer
  53. play_card: Card
  54. def __init__(self, color = None, name = None):
  55. self.color = color
  56. self.name = name
  57. self.game_code = None
  58. self.connected = True
  59. self.hand = []
  60. self.options = []
  61. self.message = "Even wachten"
  62. self.state = StateCode.START
  63. self.error = None
  64. self.swap_card = None
  65. self.card_is_swapped = False
  66. self.passed = False
  67. self.others = []
  68. self.current_player = None
  69. self.play_card = None
  70. def check_option(self, option):
  71. found_option = any(o.isOption(option) for o in self.options)
  72. if not found_option:
  73. self.message = f"{option.text} is niet toegestaan. " + self.message
  74. self.set_error(ErrorCode.OPTION_NOT_ALLOWED, option=option)
  75. return found_option
  76. def set_error(self, error_code, **kwargs):
  77. if error_code is not None:
  78. self.error = PlayerError(error_code, **kwargs)
  79. else:
  80. self.error = None
  81. def set_others(self, all_players):
  82. self.others = [OtherPlayer(p.color, p.name) for p in all_players if p.name != self.name or p.color != self.color]
  83. def set_current(self, current_player):
  84. self.current_player = OtherPlayer(current_player.color, current_player.name)
  85. def merge_from(self, other):
  86. self.color = other.color
  87. self.hand = other.hand
  88. self.options = other.options
  89. self.message = other.message
  90. self.state = other.state
  91. self.error = other.error
  92. self.swap_card = other.swap_card
  93. self.card_is_swapped = other.card_is_swapped
  94. self.passed = other.passed
  95. self.current_player = other.current_player
  96. self.play_card = other.play_card
  97. def get_json(self):
  98. return json.dumps(self.__dict__, cls=DictEncoder)
  99. if __name__ == "__main__":
  100. player = Player(Color.BLUE, "Groen")
  101. player.options = [Option(OptionCode.NEW_GAME, "Nieuw spel", color='green'), Option(OptionCode.JOIN_GAME, "doe mee met een spel")]
  102. player.state = StateCode.PICK_COLOR
  103. print(player.__dict__)
  104. print(player.options[0].__dict__)
  105. print(player.get_json())