API changes and initial save file processing
authorJakob Cornell <jakob@jcornell.net>
Sat, 22 Jun 2019 23:13:46 +0000 (18:13 -0500)
committerJakob Cornell <jakob@jcornell.net>
Sat, 22 Jun 2019 23:13:46 +0000 (18:13 -0500)
common.py
savefiles/get_troupe.py [new file with mode: 0644]
troupe.py

index 8195c0a47c741dcdebf549367792b474a4302838..f618b16ffb7183fc1d0544e939311059b5999798 100644 (file)
--- a/common.py
+++ b/common.py
@@ -5,15 +5,15 @@ class Zoombini:
                pass # marker class
 
        class Hair(Property):
-               PINK = 'pink'; GREEN = 'green'; TUFT = 'tuft'; SPIKY = 'spiky'; FLAT = 'flat'
+               SHAGGY = 'shaggy'; PONYTAIL = 'ponytail'; FLAT_TOP = 'flat top'; TUFT = 'tuft'; HAT = 'hat';
        class Eyes(Property):
-               ONE = 'one'; WIDE = 'wide'; SLEEPY = 'sleepy'; SHADES = 'shades'; GLASSES = 'glasses'
+               WIDE = 'wide'; ONE = 'one'; SLEEPY = 'sleepy'; GLASSES = 'glasses'; SUNGLASSES = 'sunglasses';
        class Nose(Property):
-               RED = 'red'; ORANGE = 'orange'; GREEN = 'green'; BLUE = 'blue'; PURPLE = 'purple'
+               GREEN = 'green'; ORANGE = 'orange'; RED = 'red'; PURPLE = 'purple'; BLUE = 'blue';
        class Feet(Property):
-               SHOES = 'shoes'; PROPELLER = 'propeller'; SPRING = 'spring'; WHEELS = 'wheels'; SKATES = 'skates'
+               SHOES = 'shoes'; BLADES = 'blades'; SPRING = 'spring'; WHEELS = 'wheels'; PROPELLER = 'propeller'
 
-       PROPERTIES = frozenset([Hair, Eyes, Nose, Feet])
+       PROPERTIES = [Hair, Eyes, Nose, Feet]
 
        def __init__(self, attrs):
                assert (
@@ -23,8 +23,8 @@ class Zoombini:
                self.by_prop = {type(attr): attr for attr in attrs}
                self.attrs = frozenset(attrs)
 
-       def __str__(self):
-               return str({type(a).__name__: a.value for a in self.attrs})
+       def __repr__(self):
+               return '({})'.format('/'.join(self.by_prop[p].value for p in Zoombini.PROPERTIES))
 
 ALL_ATTRS = frozenset(attr for prop in Zoombini.PROPERTIES for attr in prop)
 
diff --git a/savefiles/get_troupe.py b/savefiles/get_troupe.py
new file mode 100644 (file)
index 0000000..75697ee
--- /dev/null
@@ -0,0 +1,20 @@
+import sys
+import pickle
+
+from zoombinis import common
+
+file_name = sys.argv[1]
+
+def from_bytes(bytes_):
+       attrs = {list(prop)[byte - 1] for (prop, byte) in zip(common.Zoombini.PROPERTIES, bytes_[:4])}
+       return common.Zoombini(attrs)
+
+def get_zoombinis():
+       with open(file_name, 'rb') as f:
+               if sys.argv[2] == 'LastFull':
+                       f.seek(45226)
+                       return {from_bytes(f.read(20)) for _ in range(16)}
+               else:
+                       raise ValueError("Invalid selector: {}".format(sys.argv[2]))
+
+pickle.dump(get_zoombinis(), sys.stdout.buffer)
index b79b62dabc937661c1a6877da9e2804c0a50c2c7..eb62033eabe7a307af0638e665efc8b33c74890d 100644 (file)
--- a/troupe.py
+++ b/troupe.py
@@ -7,34 +7,34 @@ Z = Zoombini
 def get_options(type_):
        return '/'.join(variant.value for variant in type_)
 
-zs = []
-try:
-       while True:
-               template = "\tchoose {} ({}): "
-               print(template.format('hair', get_options(Z.Hair)), file = sys.stderr, end = '')
-               hair = input()
-               print(template.format('eyes', get_options(Z.Eyes)), file = sys.stderr, end = '')
-               eyes = input()
-               print(template.format('nose', get_options(Z.Nose)), file = sys.stderr, end = '')
-               nose = input()
-               print(template.format('feet', get_options(Z.Feet)), file = sys.stderr, end = '')
-               feet = input()
+def get_interactive():
+       zs = []
+       try:
+               while True:
+                       template = "\tchoose {} ({}): "
+                       print(template.format('hair', get_options(Z.Hair)), file = sys.stderr, end = '')
+                       hair = input()
+                       print(template.format('eyes', get_options(Z.Eyes)), file = sys.stderr, end = '')
+                       eyes = input()
+                       print(template.format('nose', get_options(Z.Nose)), file = sys.stderr, end = '')
+                       nose = input()
+                       print(template.format('feet', get_options(Z.Feet)), file = sys.stderr, end = '')
+                       feet = input()
 
-               try:
-                       zs.append(
-                               Zoombini({
-                                       Z.Hair[hair.upper()],
-                                       Z.Eyes[eyes.upper()],
-                                       Z.Nose[nose.upper()],
-                                       Z.Feet[feet.upper()],
-                               })
-                       )
-               except KeyError:
-                       print("not added!", file = sys.stderr)
-               else:
-                       print("added", file = sys.stderr)
-except EOFError:
-       pass
+                       try:
+                               zs.append(
+                                       Zoombini({
+                                               Z.Hair[hair.upper()],
+                                               Z.Eyes[eyes.upper()],
+                                               Z.Nose[nose.upper()],
+                                               Z.Feet[feet.upper()],
+                                       })
+                               )
+                       except KeyError:
+                               print("not added!", file = sys.stderr)
+                       else:
+                               print("added", file = sys.stderr)
+       except EOFError:
+               return zs
 
-pickle.dump(zs, sys.stdout.buffer)
-print()
+pickle.dump(get_interactive(), sys.stdout.buffer)