# HG changeset patch # User Gerard Krijgsman # Date 1347881339 -7200 # Mon Sep 17 13:28:59 2012 +0200 # Node ID 642680d13e9c28734b61e0fb3a5ed1bcb6675605 # Parent 0de4777fc130d689ddae953a6e6f3a1276db7884 Reverted class "constants" to uppercase diff --git a/common.py b/common.py --- a/common.py +++ b/common.py @@ -9,16 +9,16 @@ class PlanetsData(object): """Base class for all Planets data objects""" - filename = None # Default filename - count = None # Default number of objects in a data file - pack_length = None # Length of binary data for single object - pack_format = None # Format of binary data for single object - fields = () # Fields for single object + FILENAME = None # Default filename + COUNT = None # Default number of objects in a data file + PACK_LENGTH = None # Length of binary data for single object + PACK_FORMAT = None # Format of binary data for single object + FIELDS = () # Fields for single object def __init__(self): # Expand field list where necessary (in case of dictionary fields) fields = [] - for field in self.fields: + for field in self.FIELDS: # tuple format: ('name', start, end,) start/end must be 'int' if isinstance(field, tuple) and (len(field) == 3): for i in range(field[1], field[1]+field[2]): @@ -26,10 +26,10 @@ else: fields.append(field) fields = tuple(fields) - if self.__class__.fields is not fields: - self.__class__.fields = fields + if self.__class__.FIELDS is not fields: + self.__class__.FIELDS = fields # Initialize fields - for field in self.fields: + for field in self.FIELDS: # Dictionary field: see EngSpec class in fixed.py for an example if isinstance(field, tuple): setattr(self, str(field[0])+'['+str(field[1])+']', None) @@ -39,12 +39,12 @@ def unpack(self, data): """Unpacks binary data into class instance variables""" - if len(data) is not self.pack_length: + if len(data) is not self.PACK_LENGTH: raise ValueError('Binary data passed has incorrect length') - values = struct.unpack(self.pack_format, data) - if len(self.fields) is not len(values): + values = struct.unpack(self.PACK_FORMAT, data) + if len(self.FIELDS) is not len(values): raise ValueError('Incorrect number of variables in pack format') - for name, value in zip(self.fields, values): + for name, value in zip(self.FIELDS, values): # Dictionary field: see EngSpec class in fixed.py for an example if isinstance(name, tuple): setattr(self, str(name[0])+'['+str(name[1])+']', value) @@ -64,14 +64,14 @@ """Unpacks binary data into a list of object instances""" result = {} if count is None: - if cls.count is not None: - count = cls.count - elif len(data) >= cls.pack_length: - count = int(len(data) / cls.pack_length) + if cls.COUNT is not None: + count = cls.COUNT + elif len(data) >= cls.PACK_LENGTH: + count = int(len(data) / cls.PACK_LENGTH) for i in range(1, count+1): # For compatibility, count from 1 and not 0 instance = cls() - start = cls.pack_length*(i-1) - end = cls.pack_length*i + start = cls.PACK_LENGTH*(i-1) + end = cls.PACK_LENGTH*i instance.unpack(data[start:end]) if instance.id is None: instance.id = i @@ -91,9 +91,9 @@ (count,) = struct.unpack('< h', f.read(2)) # Load "count" objects or predetermined "count" objects or whole file if count is not None: - data = f.read(cls.pack_length * count) - elif cls.count is not None: - data = f.read(cls.pack_length * cls.count) + data = f.read(cls.PACK_LENGTH * count) + elif cls.COUNT is not None: + data = f.read(cls.PACK_LENGTH * cls.COUNT) else: data = f.read() result = cls.load(data, count) diff --git a/fixed.py b/fixed.py --- a/fixed.py +++ b/fixed.py @@ -7,11 +7,11 @@ class BeamSpec(PlanetsData): """List of starship beam weapons""" - filename = 'BEAMSPEC.DAT' - count = 10 - pack_length = 36 - pack_format = '< 20s h h h h h h h h' - fields = ('name', + FILENAME = 'BEAMSPEC.DAT' + COUNT = 10 + PACK_LENGTH = 36 + PACK_FORMAT = '< 20s h h h h h h h h' + FIELDS = ('name', 'cost', 'tritanium', 'duranium', @@ -23,11 +23,11 @@ class EngSpec(PlanetsData): """List of starship engines""" - filename = 'ENGSPEC.DAT' - count = 9 - pack_length = 66 - pack_format = '< 20s h h h h h 9i' - fields = ('name', + FILENAME = 'ENGSPEC.DAT' + COUNT = 9 + PACK_LENGTH = 66 + PACK_FORMAT = '< 20s h h h h h 9i' + FIELDS = ('name', 'cost', 'tritanium', 'duranium', @@ -37,11 +37,11 @@ class HullSpec(PlanetsData): """List of starship hulls""" - filename = 'HULLSPEC.DAT' - # No count, but typically 105 objects in default HULLSPEC.DAT - pack_length = 60 - pack_format = '< 30s h h h h h h h h h h h h h h h' - fields = ('name', + FILENAME = 'HULLSPEC.DAT' + # No COUNT, but typically 105 objects in default HULLSPEC.DAT + PACK_LENGTH = 60 + PACK_FORMAT = '< 30s h h h h h h h h h h h h h h h' + FIELDS = ('name', 'picture', 'damaged', # Unused field for picture of damaged ship 'tritanium', @@ -60,11 +60,11 @@ class TorpSpec(PlanetsData): """List of starship torpedo weapons""" - filename = 'TORPSPEC.DAT' - count = 10 - pack_length = 38 - pack_format = '< 20s h h h h h h h h h' - fields = ('name', + FILENAME = 'TORPSPEC.DAT' + COUNT = 10 + PACK_LENGTH = 38 + PACK_FORMAT = '< 20s h h h h h h h h h' + FIELDS = ('name', 'torpedo_cost', 'launcher_cost', 'tritanium', diff --git a/player.py b/player.py --- a/player.py +++ b/player.py @@ -11,11 +11,11 @@ class Base(PlanetsData): """List of player's starbases""" - # No filename, but typically: BDATAx.DAT or BDATAx.DIS (x=player) - # No count, but obviously limited by number of planets - pack_length = 156 - pack_format = '< h h h h h h h h 9h 20h 10h 10h 10h h h h h h h h h h h h' - fields = ('id', + # No FILENAME, but typically: BDATAx.DAT or BDATAx.DIS (x=player) + # No COUNT, but obviously limited by number of planets + PACK_LENGTH = 156 + PACK_FORMAT = '< h h h h h h h h 9h 20h 10h 10h 10h h h h h h h h h h h h' + FIELDS = ('id', 'owner', 'defense', 'damage', @@ -42,11 +42,11 @@ class Planet(PlanetsData): """List of player's planets""" - # No filename, but typically: PDATAx.DAT or PDATAx.DIS (x=player) - # No count, but maximum 500 - pack_length = 85 - pack_format = '