# Path: diplomacy/engine/compare.py
# def assist(T1, o1, T2, o2):
#     return get_territory(o2['assist']) == T1
# 
# def head_to_head(T1, o1, T2, o2, c1=False, c2=False):
#     actor = o2['assist'] if o2['assist'] else o2['actor']
#     T2 = get_territory(actor)
#     if not any(get_territory(S) == T2 for S in borders(o1['actor'])):
#         return False
#     if not any(get_territory(S) == T1 for S in borders(actor)):
#         return False
#     if c1 or c2:
#         return False
#     return get_territory(o2['target']) == T1 and get_territory(o1['target']) == T2
# 
# Path: diplomacy/engine/digest.py
# def find_convoys(units, fleets):
#     """
#     Generates pairs consisting of a cluster of adjacent non-coastal
#     fleets, and the coastal territories that are reachable via convoy
#     from that cluster.  This is necessary to determine legal orders.
# 
#     """
#     fleets = [f for f in fleets if not has_land(f)]
#     index = {f: {f} for f in fleets}
# 
#     # Calculate the connected sets of fleets
#     for f1, f2 in combinations(fleets, 2):
#         # If this pair is not adjacent, ignore it
#         if f2 not in borders(f1):
#             continue
# 
#         # If the sets for each of these fleets are not already the same set,
#         # merge them and update the index
#         if index[f1] != index[f2]:
#             index[f1] |= index[f2]
#             index.update((x, index[f1]) for x in index[f2])
# 
#     groups = {frozenset(S) for S in index.values()}
# 
#     armies = {u['subregion'] for u in units if u['u_type'] == 'A'}
# 
#     convoyable = []
#     for gset in groups:
#         coasts = {
#             sr for f in gset
#             for b in borders(f)
#             for sr in territory_parts(get_territory(b))
#             if is_land(sr)
#         }
#         if coasts & armies:
#             convoyable.append((set(gset), coasts))
# 
#     return convoyable
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
#     if not sr_token:
#         return u''
#     return sr_token.split(u'.')[0]
# 
# def borders(sr_token):
#     return standard.connectivity.get(sr_token, ())


from collections import defaultdict
from .compare import assist, head_to_head
from .digest import find_convoys
from .utils import get_territory, borders
            # matching successful convoy orders
            matching = [
                orders[T2]['actor'] for T2, d2 in state.items()
                if d2
                and orders[T2]['action'] == 'C'
                and assist(T, order, T2, orders[T2])
                and (order['government'] == orders[T2]['government']
                     or order['convoy'])
            ]
            # matching successful and paradoxical convoy orders
            p_matching = matching + [
                orders[T2]['actor'] for T2 in paradox
                if assist(T, order, T2, orders[T2])
                and order['convoy']
            ]

            if any(order['actor'] in L and order['target'] in L
                   for F, L in find_convoys(units, matching)):
                # We have a valid convoy path if there is a chain
                # of successful convoys between our endpoints.
                path[T] = True

            if (not path[T] and
                any(order['actor'] in L and order['target'] in L
                    for F, L in find_convoys(units, p_matching))):
                # But if there is a path when paradoxical convoys
                # are included, we have a paradox.
                P = True

            convoy[T] = path[T]

# Path: diplomacy/engine/compare.py
# def assist(T1, o1, T2, o2):
#     return get_territory(o2['assist']) == T1
# 
# def head_to_head(T1, o1, T2, o2, c1=False, c2=False):
#     actor = o2['assist'] if o2['assist'] else o2['actor']
#     T2 = get_territory(actor)
#     if not any(get_territory(S) == T2 for S in borders(o1['actor'])):
#         return False
#     if not any(get_territory(S) == T1 for S in borders(actor)):
#         return False
#     if c1 or c2:
#         return False
#     return get_territory(o2['target']) == T1 and get_territory(o1['target']) == T2
# 
# Path: diplomacy/engine/digest.py
# def find_convoys(units, fleets):
#     """
#     Generates pairs consisting of a cluster of adjacent non-coastal
#     fleets, and the coastal territories that are reachable via convoy
#     from that cluster.  This is necessary to determine legal orders.
# 
#     """
#     fleets = [f for f in fleets if not has_land(f)]
#     index = {f: {f} for f in fleets}
# 
#     # Calculate the connected sets of fleets
#     for f1, f2 in combinations(fleets, 2):
#         # If this pair is not adjacent, ignore it
#         if f2 not in borders(f1):
#             continue
# 
#         # If the sets for each of these fleets are not already the same set,
#         # merge them and update the index
#         if index[f1] != index[f2]:
#             index[f1] |= index[f2]
#             index.update((x, index[f1]) for x in index[f2])
# 
#     groups = {frozenset(S) for S in index.values()}
# 
#     armies = {u['subregion'] for u in units if u['u_type'] == 'A'}
# 
#     convoyable = []
#     for gset in groups:
#         coasts = {
#             sr for f in gset
#             for b in borders(f)
#             for sr in territory_parts(get_territory(b))
#             if is_land(sr)
#         }
#         if coasts & armies:
#             convoyable.append((set(gset), coasts))
# 
#     return convoyable
# 
# Path: diplomacy/engine/utils.py
# def get_territory(sr_token):
#     if not sr_token:
#         return u''
#     return sr_token.split(u'.')[0]
# 
# def borders(sr_token):
#     return standard.connectivity.get(sr_token, ())


# Path: diplomacy/engine/resolver.py
from collections import defaultdict
from .compare import assist, head_to_head
from .digest import find_convoys
from .utils import get_territory, borders

            # matching successful convoy orders
            matching = [
                orders[T2]['actor'] for T2, d2 in state.items()
                if d2
                and orders[T2]['action'] == 'C'
                and assist(T, order, T2, orders[T2])
                and (order['government'] == orders[T2]['government']
                     or order['convoy'])
            ]
            # matching successful and paradoxical convoy orders
            p_matching = matching + [
                orders[T2]['actor'] for T2 in paradox
                if assist(T, order, T2, orders[T2])
                and order['convoy']
            ]

            if any(order['actor'] in L and order['target'] in L
                   for F, L in find_convoys(units, matching)):
                # We have a valid convoy path if there is a chain
                # of successful convoys between our endpoints.
                path[T] = True

            if (not path[T] and
                any(order['actor'] in L and order['target'] in L
                    for F, L in find_convoys(units, p_matching))):
                # But if there is a path when paradoxical convoys
                # are included, we have a paradox.
                P = True

            convoy[T] = path[T]

            if not order['convoy'] and order['target'] in borders(order['actor']):