def order_dict_to_str(order_dict, phase, map_id=1): """ Converts an order from the dictionary format to the string format :param order_dict: The order in dictionary format from webdiplomacy.net :param phase: The current phase ('Diplomacy', 'Retreats', 'Builds') :return: A tuple consisting of: 1) The power who submitted the order (e.g. 'FRANCE') 2) The order in string format (e.g. 'A PAR H') """ req_fields = ('countryID',) if [1 for field in req_fields if field not in order_dict]: LOGGER.error('The required fields for order dict are %s. Cannot translate %s', req_fields, order_dict) return '', '', '' # Extracting information country_id = int(order_dict['countryID']) # Validating if country_id not in CACHE[map_id]['ix_to_power']: LOGGER.error('Unknown countryID "%s" for mapID "%s".', country_id, map_id) return '', '' # Getting power name and phase_type power_name = CACHE[map_id]['ix_to_power'][country_id] phase_type = {'Diplomacy': 'M', 'Retreats': 'R', 'Builds': 'A'}[phase] # Getting order in string format order = Order(order_dict, map_id=map_id, phase_type=phase_type) if not order: return '', '' # Returning return power_name, order.to_string()
def test_hold_army_002(): """ Tests hold army """ raw_order = 'A ABC H' order_str = '' order_dict = {} order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_move_army_001(): """ Tests move army """ raw_order = 'A YOR - LON' order_str = 'A YOR - LON' order_dict = { 'terrID': 4, 'unitType': 'Army', 'type': 'Move', 'toTerrID': 6, 'fromTerrID': '', 'viaConvoy': 'No' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_hold_fleet_001(): """ Tests hold fleet """ raw_order = 'F LON H' order_str = 'F LON H' order_dict = { 'terrID': 6, 'unitType': 'Fleet', 'type': 'Hold', 'toTerrID': '', 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_disband_fleet_002(): """ Tests disband fleet """ raw_order = 'F BRE D' order_str = 'F BRE D' order_dict = { 'terrID': 46, 'unitType': 'Fleet', 'type': 'Destroy', 'toTerrID': 46, 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order, phase_type='A') order_from_dict = Order(order_dict, phase_type='A') # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_disband_fleet_coast_001(): """ Tests disband fleet (retreats phase) """ raw_order = 'F SPA/NC D' order_str = 'F SPA/NC D' order_dict = { 'terrID': 76, 'unitType': 'Fleet', 'type': 'Disband', 'toTerrID': '', 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order, phase_type='R') order_from_dict = Order(order_dict, phase_type='R') # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_build_army_001(): """ Tests build army """ raw_order = 'A PAR B' order_str = 'A PAR B' order_dict = { 'terrID': 47, 'unitType': 'Army', 'type': 'Build Army', 'toTerrID': 47, 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_retreat_fleet_001(): """ Tests retreat fleet """ raw_order = 'F BRE R SPA/SC' order_str = 'F BRE R SPA/SC' order_dict = { 'terrID': 46, 'unitType': 'Fleet', 'type': 'Retreat', 'toTerrID': 77, 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_disband_army_001(): """ Tests disband army """ raw_order = 'A PAR D' order_str = 'A PAR D' order_dict = { 'terrID': 47, 'unitType': 'Army', 'type': 'Disband', 'toTerrID': '', 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order, phase_type='R') order_from_dict = Order(order_dict, phase_type='R') # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_retreat_army_002(): """ Tests retreat army """ raw_order = 'A PAR - LON' order_str = 'A PAR R LON' order_dict = { 'terrID': 47, 'unitType': 'Army', 'type': 'Retreat', 'toTerrID': 6, 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order, phase_type='R') order_from_dict = Order(order_dict, phase_type='R') # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_convoy_001(): """ Tests convoy """ raw_order = 'F MAO C A PAR - LON' order_str = 'F MAO C A PAR - LON' order_dict = { 'terrID': 61, 'unitType': 'Fleet', 'type': 'Convoy', 'toTerrID': 6, 'fromTerrID': 47, 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_support_move_002(): """ Tests support move """ raw_order = 'F MAO S A PAR - BRE' order_str = 'F MAO S PAR - BRE' order_dict = { 'terrID': 61, 'unitType': 'Fleet', 'type': 'Support move', 'toTerrID': 46, 'fromTerrID': 47, 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_support_hold_001(): """ Tests for support hold """ raw_order = 'A PAR S F BRE' order_str = 'A PAR S BRE' order_dict = { 'terrID': 47, 'unitType': 'Army', 'type': 'Support hold', 'toTerrID': 46, 'fromTerrID': '', 'viaConvoy': '' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def test_move_fleet_001(): """ Tests move fleet """ raw_order = 'F BRE - MAO' order_str = 'F BRE - MAO' order_dict = { 'terrID': 46, 'unitType': 'Fleet', 'type': 'Move', 'toTerrID': 61, 'fromTerrID': '', 'viaConvoy': 'No' } order_from_string = Order(raw_order) order_from_dict = Order(order_dict) # Validating assert order_from_string.to_string() == order_str assert compare_dicts(order_from_string.to_dict(), order_dict) assert order_from_dict.to_string() == order_str assert compare_dicts(order_from_dict.to_dict(), order_dict)
def set_orders(self, game, power_name, orders, wait=None): """ Submits orders back to the server :param game: A diplomacy.Game object representing the current state of the game :param power_name: The name of the power submitting the orders (e.g. 'FRANCE') :param orders: A list of strings representing the orders (e.g. ['A PAR H', 'F BRE - MAO']) :param wait: Optional. If True, sets ready=False, if False sets ready=True. :return: True for success, False for failure :type game: diplomacy.Game """ # Logging orders LOGGER.info('[%s/%s/%s] - Submitting orders: %s', game.game_id, game.get_current_phase(), power_name, orders) # Converting orders to dict orders_dict = [ Order(order, map_name=game.map_name, phase_type=game.phase_type, game=game) for order in orders ] # Recording submitted orders submitted_orders = {} for order in orders_dict: unit = ' '.join(order.to_string().split()[:2]) if order.to_string()[-2:] == ' D': unit = '? ' + unit[2:] submitted_orders[unit] = order.to_norm_string() # Getting other info game_id = int(game.game_id) country_id = CACHE[game.map_name]['power_to_ix'].get(power_name, -1) current_phase = game.get_current_phase() if current_phase != 'COMPLETED': season, current_year, phase_type = current_phase[0], int( current_phase[1:5]), current_phase[5] nb_years = current_year - game.map.first_year turn = 2 * nb_years + (0 if season == 'S' else 1) phase = { 'M': 'Diplomacy', 'R': 'Retreats', 'A': 'Builds' }[phase_type] else: turn = -1 phase = 'Diplomacy' # Sending request route = 'game/orders' url = '%s?%s' % (API_WEBDIPLOMACY_NET, urlencode({'route': route})) body = { 'gameID': game_id, 'turn': turn, 'phase': phase, 'countryID': country_id, 'orders': [order.to_dict() for order in orders_dict if order] } if wait is not None: body['ready'] = 'Yes' if not wait else 'No' body = json.dumps(body).encode('utf-8') # Sending request try: response = yield self._send_post_request(url, body) except HTTP_ERRORS as err: LOGGER.error('Unable to connect to server. Error raised is: "%s"', repr(err)) return False # Error Occurred if response.code != 200: LOGGER.warning('ERROR during "%s". Error code: %d. Body: %s.', route, response.code, response.body) return False # No orders set - Was only setting the ready flag if not orders: return True # No response received from the server - Maybe a connection timeout? if not response.body: LOGGER.warning( 'WARNING during "%s". No response body received. Is the server OK?', route) return False # Otherwise, validating that received orders are the same as submitted orders try: response_body = json.loads(response.body.decode('utf-8')) except (TypeError, ValueError): LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8')) return False orders_dict = [ Order(order, map_name=game.map_name, phase_type=game.phase_type) for order in response_body ] all_orders_set = True # Recording received orders received_orders = {} for order in orders_dict: unit = ' '.join(order.to_string().split()[:2]) if order.to_string()[-2:] == ' D': unit = '? ' + unit[2:] received_orders[unit] = order.to_norm_string() # Logging different orders for unit in submitted_orders: if submitted_orders[unit] != received_orders.get(unit, ''): all_orders_set = False LOGGER.warning( '[%s/%s/%s]. Submitted: "%s" - Server has: "%s".', game.game_id, game.get_current_phase(), power_name, submitted_orders[unit], received_orders.get(unit, '')) # Returning status return all_orders_set