[Twisted-Python] Transporting pb.Copyable classes?

Samuel Reynolds sam at SpinwardStars.com
Wed Apr 21 11:54:03 EDT 2004

At 2004-04-20 06:44 PM -0700, you wrote:
>Oh no, no mutable state!  Purely static data and static methods.
>What I have coils down to something like this:
>class Terrain( pb.Copyable, pb.RemoteCopy ):
>     def moveCost( cls, unit ):
>         return cls.cost
>     moveCost = classmethod( moveCost )
>class Mountains( Terrain ):
>     cost = 6
>A bunch of hexes then have their .terrain = Mountains (etc.)  I could easily
>change this to instantiation the various Terrains, but in my mind that
>implies that an instance's state could differ.  I'm beginning to think
>the distinction is more trouble than it's worth however, and potentially
>I have a dictionary of hexes keyed by their grid location.
>map.cells = {(1,1):hexCellInstance, ...}
>Each of these cells has a .terrain, which is a reference to a Terrain
>subclass.  These have static data and methods, for use by various game
>algorithms, e.g. how far can unit Foo move through Mountains.
>On each turn a (filtered) version of this state is passed to game clients,
>so that they can view it, validate their orders against it, etc.

How about making Mountains, Plains, etc. Singletons?
Then, instead of assigning hexCellInstance.terrain=Mountain,
you assign hexCellInstance.terrain=Mountain() (note the
parentheses!). At that point, each cell.terrain is an object
reference instead of a class reference and (IIUC) should
remote-copy properly.

Caveat: I've not used pb; this is based on my *very* weak
understanding (misunderstanding?) of it. In particular,
I don't know how pb/copyable would interact with a singleton.
I would *expect* that the remote unserialization would
(eventually) invoke Mountains.__call__ and get a reference
to the corresponding singleton on the client side.

class Singleton(type):
     def __init__( cls, name, bases, classdict ):
         super( Singleton, cls ).__init__( name, bases, classdict )
         cls.instance = None
     def __call__( cls, *args, **kw ):
         if cls.instance is None:
             cls.instance = super( Singleton, cls ).__call__( *args, **kw )
         return cls.instance

class Mountains( Terrain ):
     __metaclass__ = Singleton
     cost = 6

- Sam

Spinward Stars, LLC                        Samuel Reynolds
Software Consulting and Development           303-805-1446
http://SpinwardStars.com/            sam at SpinwardStars.com 

More information about the Twisted-Python mailing list