# 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, ())
# 
# def territory_parts(t_token):
#     return standard.subregion_groups.get(t_token, ())
# 
# def has_land(sr_token):
#     return any(is_land(sr) for sr in territory_parts(get_territory(sr_token)))
# 
# def is_land(sr_token):
#     return sr_token.endswith('.l')


from collections import defaultdict, Counter
from itertools import combinations
from . import standard
from .utils import get_territory, borders, territory_parts, has_land, is_land
def actionable_subregions(turn, units, owns):
    """Returns lists of subregions that would be legal actors this turn,
    indexed by government.  These lists are not constrained by the
    number of builds available during Fall Adjustment turns, merely by
    whether the home supply center would fit the criteria.

    """
    if turn['season'] in ('S', 'F'):
        return {
            government: [u['subregion'] for u in units
                         if u['government'] == government]
            for government in standard.powers
        }
    elif turn['season'] in ('SR', 'FR'):
        return {
            government: [u['subregion'] for u in units
                         if u['dislodged'] and u['government'] == government]
            for government in standard.powers
        }
    elif turn['season'] == 'FA':
        builds = builds_available(units, owns)
        actors_index = {}
        for government in standard.powers:
            if builds.get(government, 0) > 0:
                # If we have more supply centers than units, we can build.  If,
                # - we own a supply center
                # - that is one of our original supply centers
                # - and that is not occupied by a unit
                owned_sc = (o['territory'] for o in owns
                            if o['is_supply'] and o['government'] == government)

# 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, ())
# 
# def territory_parts(t_token):
#     return standard.subregion_groups.get(t_token, ())
# 
# def has_land(sr_token):
#     return any(is_land(sr) for sr in territory_parts(get_territory(sr_token)))
# 
# def is_land(sr_token):
#     return sr_token.endswith('.l')


# Path: diplomacy/engine/digest.py
from collections import defaultdict, Counter
from itertools import combinations
from . import standard
from .utils import get_territory, borders, territory_parts, has_land, is_land

def actionable_subregions(turn, units, owns):
    """Returns lists of subregions that would be legal actors this turn,
    indexed by government.  These lists are not constrained by the
    number of builds available during Fall Adjustment turns, merely by
    whether the home supply center would fit the criteria.

    """
    if turn['season'] in ('S', 'F'):
        return {
            government: [u['subregion'] for u in units
                         if u['government'] == government]
            for government in standard.powers
        }
    elif turn['season'] in ('SR', 'FR'):
        return {
            government: [u['subregion'] for u in units
                         if u['dislodged'] and u['government'] == government]
            for government in standard.powers
        }
    elif turn['season'] == 'FA':
        builds = builds_available(units, owns)
        actors_index = {}
        for government in standard.powers:
            if builds.get(government, 0) > 0:
                # If we have more supply centers than units, we can build.  If,
                # - we own a supply center
                # - that is one of our original supply centers
                # - and that is not occupied by a unit
                owned_sc = (o['territory'] for o in owns
                            if o['is_supply'] and o['government'] == government)

                occupied = {get_territory(u['subregion']) for u in units}