player.py 3.5 KB

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