# 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 itertools import permutations
from .utils import get_territory, borders



def assist(T1, o1, T2, o2):
    return get_territory(o2['assist']) == T1


def attack_us(T1, o1, T2, o2):
    return get_territory(o2['target']) == T1


def attack_us_from_target(T1, o1, T2, o2):
    return (get_territory(o2['assist']) == get_territory(o1['target'])
            and get_territory(o2['target']) == 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

# 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/compare.py
from collections import defaultdict
from itertools import permutations
from .utils import get_territory, borders




def assist(T1, o1, T2, o2):
    return get_territory(o2['assist']) == T1


def attack_us(T1, o1, T2, o2):
    return get_territory(o2['target']) == T1


def attack_us_from_target(T1, o1, T2, o2):
    return (get_territory(o2['assist']) == get_territory(o1['target'])
            and get_territory(o2['target']) == 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)):