Example #1
0
 def adjust_ownership(self):
     ''' Lets units take over supply centers they occupy.
         Returns a list of countries that gained supply centers.
         
         >>> then = Message(standard_now[:])
         >>> then[then.index(LON) - 2] = RUS
         >>> then[then.index(STP) - 3] = ENG
         >>> standard_map.handle_NOW(then)
         >>> countries = standard_map.adjust_ownership()
         >>> ENG == standard_map.spaces[STP].owner
         True
         >>> RUS == standard_map.spaces[LON].owner
         True
         
         # Restore original map
         >>> standard_map.restart()
     '''#'''
     net_growth = defaultdict(int)
     for unit in self.units:
         power = unit.nation
         loser = unit.takeover()
         if loser:
             power.eliminated = False
             net_growth[power.key] += 1
             net_growth[loser.key] -= 1
             if not loser.centers:
                 loser.eliminated = self.current_turn.year
     return [token for token,net in net_growth.iteritems() if net > 0]
Example #2
0
 def mdf(self):
     powers = []
     ownership = defaultdict(list)
     for name in sorted(self.homes):
         power = self.rep[name]
         if power is not UNO:
             powers.append(power)
         
         for prov in self.homes[name]:
             ownership[self.rep[prov]].append(power)
     
     neutrals = []
     homes = defaultdict(list)
     for prov in sorted(ownership):
         owners = ownership[prov]
         if owners == [UNO]:
             neutrals.append(prov)
         elif UNO in owners:
             raise ValueError("Supply centers cannot be both home and neutral")
         elif len(owners) == 1:
             homes[owners[0]].append(prov)
         else:
             homes[tuple(owners)].append(prov)
     
     centers = []
     for owners in sorted(homes):
         centers.append([owners] + homes[owners])
     if neutrals:
         centers.append([UNO] + neutrals)
     
     provs = []
     borders = []
     for name in sorted(self.borders):
         prov = self.rep[name]
         if prov not in ownership:
             provs.append(prov)
         
         boundaries = [prov]
         adjacencies = self.borders[name]
         for item in adjacencies:
             boundary = self.rep.translate(adjacencies[item])
             boundaries.append([item] + boundary)
         borders.append(boundaries)
     
     return MDF (powers) (centers, provs) (borders)
Example #3
0
    def __init__(self):
        # Yes, this deliberately skips Configuration.__init__()

        self.names = {
            "str": "string",
            "int": "integer",
            "bool": "boolean",
            "float": "number",
            "intlist": "list of integers",
        }

        self.intro = (
            "Example configuration file for Parlance, a Diplomacy framework",
            "",
            "This file may be saved as parlance.cfg in either $HOME/.config or",
            "the program working directory; settings in the latter will override",
            "those in the former.",
            "",
            "Lines starting with hash marks (#) or semicolons (;) are ignored,",
            "and may be used for comments.  In this sample file, semicolons are",
            "used to show the default setting for each option.",
            "",
            "The sections are ordered in approximate likelihood of customization.",
        )

        # I should probably find a better place to put this information.
        # Perhaps I could simply collect it from parlance.cfg.sample?
        self.headers = headers = self.OrderedDict()
        headers["game"] = "Parameters to be sent in the HLO message."
        headers["server"] = "Options for server operation."
        headers["judge"] = "Options for basic judge operation, including premature endings."
        headers["clients"] = "Options applicable for all Parlance clients."
        headers["network"] = "Settings for the network (DCSP) layer."
        headers["main"] = "Options used by the core program functionality."
        headers["datc"] = (
            "Options from Lucas B. Kruijswijk's Diplomacy Adjudicator Test Cases.",
            "Not all options are supported; some cannot be, within DAIDE.",
            "Some options use letters not used by the DATC; in general,",
            "the syntax disallows the option entirely in these cases.",
        )
        headers["tokens"] = "Minor options for dealing with token conversion."
        headers["syntax"] = (
            "Syntax files and special tokens.",
            "Note: File names are relative to the current working directory,",
            "      *not* the directory of this file.",
        )

        self.modules = defaultdict(self.OrderedDict)
Example #4
0
 def tokens(self):
     cats = defaultdict(list)
     for prov in sorted(self.borders):
         # Inland_non-SC = 0x50
         # Inland_SC = 0x51
         # Sea_non-SC = 0x52
         # Sea_SC = 0x53
         # Coastal_non-SC = 0x54
         # Coastal_SC = 0x55
         # Bicoastal_non-SC = 0x56
         # Bicoastal_SC = 0x57
         num = 0x5000
         keys = list(self.borders[prov])
         if len(keys) > 2:
             # Yes, we should probably look for coastline specs,
             # but this works for anything in the current variants.
             num += 0x0600
         elif AMY not in keys:
             num += 0x0200
         elif FLT in keys:
             num += 0x0400
         
         if any(prov in self.homes[power] for power in self.homes):
             num += 0x0100
         cats[num].append(prov)
     
     numbers = {}
     index = count()
     for cat in sorted(cats):
         for prov in cats[cat]:
             numbers[cat + index.next()] = prov
     
     for index, power in enumerate(sorted(self.homes)):
         if power != "UNO":
             numbers[0x4100 + index] = power
     
     return Representation(numbers, protocol.default_rep)