def command(cmd, params, **kwargs): if cmd == COMMAND['join']: result = _join(params) elif cmd == COMMAND['reset']: result = _reset() elif cmd == COMMAND['host']: result = _host(params) elif cmd == COMMAND['rejoin']: result = _rejoin(params) elif cmd == COMMAND['start']: result = _start(params) # ACTIONS elif cmd == COMMAND['action']['draw']: result = _draw(params, player_name=kwargs['player_name']) # elif cmd == COMMAND['action']['discard']: # result = _discard(params, player_name=kwargs['player_name']) elif cmd == COMMAND['action']['play']: result = _play(params, player_name=kwargs['player_name']) elif cmd == COMMAND['action']['declare']: result = _declare(params, player_name=kwargs['player_name']) # OTHER elif cmd == COMMAND['other']['gamestate']: return { 'error': False, 'type': "COMMAND", # constant 'message': str(gamestateDao.get_gamestate()) } else: result = {'error': True, 'message': 'Unknown command'} result['type'] = "COMMAND" # constant result['command'] = cmd.upper() return result
def draw(position): gamestate = gamestateDao.get_gamestate() if LETTER_TO_POSITION[position.upper()] != gamestate['draw next']: return {'error': True, 'message': "Not your turn to draw"} else: card = gamestate['deck'].pop(str(len(gamestate['deck']))) # replace with random card choosing gamestate['players'][LETTER_TO_POSITION_KEY[position.upper()]]['cards'].append(card) if len(gamestate['deck']) == 8: if gamestate['trump'] is None: if gamestate['last eight'] is None: gamestate['last eight'] = "N" # hard coded gamestate['players']['north']['cards'].extend(gamestate['deck']) # hard coded gamestate['trump'] = { 'suit': SUIT['heart'], # hard coded 'value': gamestate['team level'][PLAYER_TO_TEAM[gamestate['last eight']]] } gamestate['status'] = GAME_STATE['discarding'] gamestateDao.set_gamestate(gamestate) return {'error': False, 'message': "Now waiting for discarding", 'position': position, 'card': _card_to_string(card), 'cards': [_card_to_string(card) for card in gamestate['deck'].values()]} else: gamestate['draw next'] = DRAW_ORDER[gamestate['draw next']] gamestateDao.set_gamestate(gamestate) return {'error': False, 'position': position, 'card': _card_to_string(card)}
def _draw(params): if len(params) != 1: return {'error': True, 'message': "Incorrect number of arguments"} position = params[0] # short cut if position == "R": gamestate = gamestateDao.get_gamestate() next_draw = gamestate['next draw'] number_left = len(gamestate['deck']) - 8 return actionService.draw(position)
def _start(params): if len(params) != 0: return {'error': True, 'message': "Incorrect number of arguments"} elif not all([ position in playersDao.get_positions() for position in ["N", "E", "S", "W"] ]): return {'error': True, 'message': "Not enough players to start game"} elif gamestateDao.get_gamestate()['status'] != GAME_STATE['not started']: return { 'error': True, 'message': "Cannot start the game during a game" } else: gamestateDao.set_gamestate(INITIAL_GAME_STATE['drawing']) return {'error': False, 'message': "You have started the game"}
def play(position, cards): gamestate = gamestateDao.get_gamestate() player_current_play = gamestate['trick']['current_play'][ LETTER_TO_POSITION_KEY[position.upper()]]['cards'] if len(cards) > 2: return { 'error': True, 'message': "More than two cards cannot be played (currently)" } for card in cards: hand_info = _in_cards(card, gamestate['player'][position]['hand']) if hand_info != -1: player_current_play.append(card) del gamestate['players'][position]['hand'][hand_info] else: return {'error': True, 'message': "Card not in player's hand"} validity = _check_valid(gamestate, position, cards) if validity['error']: gamestate['players'][LETTER_TO_POSITION_KEY[ position.upper()]]['cards'].extend(cards) # need to copy cards? return validity if _trick_over(gamestate): _assign_points(gamestate) gamestateDao.set_gamestate(gamestate) return { 'error': False, 'message': gamestate['trick']['starter'] + " has won the trick!", 'cards': [_card_to_string(card) for card in cards] } if _round_over(gamestate): gamestate['status'] = GAME_STATE['between rounds'] gamestateDao.set_gamestate(gamestate) return { 'error': False, 'message': "Round is over, waiting for players to ready up", 'cards': [_card_to_string(card) for card in cards] } else: gamestateDao.set_gamestate(gamestate) return { 'error': False, 'message': "Card(s) have been played", 'cards': [_card_to_string(card) for card in cards] }
def declare(position, card): gamestate = gamestateDao.get_gamestate() if card['suit'] == "NONE": return {'error': True, 'message': "Cannot declare card with NONE suit"} elif gamestate['team level'][ PLAYER_TO_TEAM[position]] != card['value']['rank']: return { 'error': True, 'message': "Cannot declare card with value your team is not on" } else: gamestate['trump']['suit'] = card['suit'] gamestate['trump']['value'] = gamestate['team level'][ PLAYER_TO_TEAM[position]] gamestateDao.set_gamestate(gamestate) return { 'error': False, 'position': position, 'card': _card_to_string(card) }
def discard(position, cards): gamestate = gamestateDao.get_gamestate() for card in cards: hand_info = _in_cards( card, gamestate['players'][LETTER_TO_POSITION_KEY[ position.upper()]]['cards']) if hand_info != -1: gamestate['deck'].append(card) del gamestate['players'][LETTER_TO_POSITION_KEY[ position.upper()]]['cards'][hand_info] else: return {'error': True, 'message': "Card not in player's hand"} gamestate['status'] = GAME_STATE['in play'] gamestateDao.set_gamestate(gamestate) return { 'error': False, 'position': position, 'cards': [_card_to_string(card) for card in cards] }