player.py 3.5 KB

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