option.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from enum import Enum
  2. from color import Color
  3. from cards import Card
  4. import json
  5. class OptionCode(str, Enum):
  6. NEW_GAME = "new_game"
  7. JOIN_GAME = "join_game"
  8. PICK_COLOR = "pick_color"
  9. CHANGE_NAME = "change_name"
  10. DEAL = "deal"
  11. SWAP_CARD = "swap_card"
  12. PLAY_CARD = "play_card"
  13. READY = "ready"
  14. UNDO_CARD = "undo_card"
  15. SKIP_TURN = "skip_turn"
  16. class Option(object):
  17. def __init__(self, code, text, game_code=None, color=None, card=None, user_name=None):
  18. self.code = OptionCode(code)
  19. self.text = text
  20. self.game_code = int(game_code) if game_code != None else None
  21. self.color = Color(color) if color != None else None
  22. self.card = card if isinstance(card, Card) else Card(card['suit'], card['denom']) if isinstance(card, dict) else None
  23. self.user_name = user_name
  24. def isOption(self, other):
  25. # ignore text and game_code
  26. return \
  27. self.code == other.code and \
  28. self.color in [None, other.color] and \
  29. self.card in [None, other.card]
  30. if __name__ == "__main__":
  31. option = Option(OptionCode.NEW_GAME, "Nieuw spel", game_code=3)
  32. print(option)
  33. print(option.__dict__)
  34. print(json.dumps(option.__dict__))
  35. other = Option(OptionCode.NEW_GAME, "Start nog een spel", game_code=4)
  36. print(option.isOption(other))
  37. other = Option(OptionCode.NEW_GAME, "Doe maar gek", color='red')
  38. print(option.isOption(other))
  39. other = Option(OptionCode.SWAP_CARD, "Wissel kaart", card=Card("spades", "10"))