home · contact · privacy
Server/py: Some refactoring.
[plomrogue] / plomrogue-server.py
index 1792a4a6a12ede5a90e528e5f1d43c568d56d55f..4b2aee189601bd5150730c84c53f615ee6ee0244 100755 (executable)
@@ -119,36 +119,27 @@ def record(command):
 def save_world():
     # Dummy for saving all commands to reconstruct current world state.
     # Misses same optimizations as record() from the original record().
-    ta_string = ""
+    string = ""
+    for key in world_db:
+        if dict != type(world_db[key]):
+            string = string + key + " " + str(world_db[key]) + "\n"
     for id in world_db["ThingActions"]:
-        ta = world_db["ThingActions"][id]
-        ta_string = ta_string + "TA_ID " + str(id) + "\n" + \
-                                "TA_EFFORT " + str(ta["TA_EFFORT"]) + "\n" + \
-                                "TA_NAME '" + ta["TA_NAME"] + "'\n"
-    tt_string = ""
+        string = string + "TA_ID " + str(id) + "\n"
+        for key in world_db["ThingActions"][id]:
+            val = world_db["ThingActions"][id][key]
+            argument = "'" + val + "'" if str == type(val) else str(val)
+            string = string + key + " " + argument + "\n"
     for id in world_db["ThingTypes"]:
-        tt = world_db["ThingTypes"][id]
-        tt_string = tt_string + "TT_ID " + str(id) + "\n" + \
-                                "TT_CONSUMABLE " + \
-                                str(tt["TT_CONSUMABLE"]) + "\n" + \
-                                "TT_CORPSE_ID " + \
-                                str(tt["TT_CORPSE_ID"]) + "\n" + \
-                                "TT_PROLIFERATE " + \
-                                str(tt["TT_PROLIFERATE"]) + "\n" + \
-                                "TT_START_NUMBER " + \
-                                str(tt["TT_START_NUMBER"]) + "\n" + \
-                                "TT_NAME '" + tt["TT_NAME"] + "'\n" + \
-                                "TT_SYMBOL '" + tt["TT_SYMBOL"] + "'\n"
-    atomic_write(io_db["path_save"],
-                 "WORLD_ACTIVE " + str(world_db["WORLD_ACTIVE"]) + "\n" +
-                 "MAP_LENGTH " + str(world_db["MAP_LENGTH"]) + "\n" +
-                 "PLAYER_TYPE " + str(world_db["PLAYER_TYPE"]) + "\n" +
-                 "TURN " + str(world_db["TURN"]) + "\n" +
-                 "SEED_RANDOMNESS " + str(world_db["SEED_RANDOMNESS"]) + "\n" +
-                 "SEED_MAP " + str(world_db["SEED_MAP"]) + "\n" +
-                 ta_string + tt_string)
-    # TODO: If all this ever does is just writing down what's in world_db, some
-    # loop over its entries should be all that's needed.
+        string = string + "TT_ID " + str(id) + "\n"
+        for key in world_db["ThingTypes"][id]:
+            if not "TT_CORPSE_ID" == key:
+                val = world_db["ThingTypes"][id][key]
+                argument = "'" + val + "'" if str == type(val) else str(val)
+                string = string + key + " " + argument + "\n"
+    for id in world_db["ThingTypes"]:
+        string = string + "TT_ID " + str(id) + "\n" + "TT_CORPSE_ID " + \
+                 str(world_db["ThingTypes"][id]["TT_CORPSE_ID"]) + "\n"
+    atomic_write(io_db["path_save"], string)
 
 
 def obey_lines_in_file(path, name, do_record=False):
@@ -277,7 +268,7 @@ def set_world_inactive():
 
 
 def integer_test(val_string, min, max):
-    """Return val_string if possible integer >= min and <= max, else False."""
+    """Return val_string if possible integer >= min and <= max, else None."""
     try:
         val = int(val_string)
         if val < min or val > max:
@@ -286,7 +277,7 @@ def integer_test(val_string, min, max):
     except ValueError:
         print("Ignoring: Please use integer >= " + str(min) + " and <= " +
               str(max) + ".")
-        return False
+        return None
 
 
 def worlddb_value_setter(key, min, max):
@@ -356,7 +347,7 @@ def command_ttid(id_string):
     >255, a new ID is calculated: the lowest unused ID >=0 and <= 255.
     """
     id = integer_test(id_string, -32768, 32767)
-    if id:
+    if None != id:
         if id in world_db["ThingTypes"]:
             command_ttid.id = id
         else:
@@ -374,6 +365,7 @@ def command_ttid(id_string):
             world_db["ThingTypes"][id] = {
                 "TT_NAME": "(none)",
                 "TT_CONSUMABLE": 0,
+                "TT_LIFEPOINTS": 0,
                 "TT_PROLIFERATE": 0,
                 "TT_START_NUMBER": 0,
                 "TT_SYMBOL": "?",
@@ -402,7 +394,7 @@ def ThingType_value_setter(key, min, max):
     @test_ThingType_id
     def f(val_string):
         val = integer_test(val_string, min, max)
-        if val:
+        if None != val:
             world_db["ThingTypes"][command_ttid.id][key] = val
     return f
 
@@ -426,7 +418,7 @@ def command_ttsymbol(char):
 def command_ttcorpseid(str_int):
     """Set to int(str_int) TT_CORPSE_ID of selected ThingType."""
     val = integer_test(str_int, 0, 255)
-    if val:
+    if None != val:
         if val in world_db["ThingTypes"]:
             world_db["ThingTypes"][command_ttid.id]["TT_CORPSE_ID"] = val
         else:
@@ -441,7 +433,7 @@ def command_taid(id_string):
     is calculated: The lowest unused ID >0 and <= 255.
     """
     id = integer_test(id_string, 0, 255)
-    if id:
+    if None != id:
         if id in world_db["ThingActions"]:
             command_taid.id = id
         else:
@@ -468,7 +460,7 @@ test_ThingAction_id = test_for_id_maker(command_taid, "ThingAction")
 def command_taeffort(str_int):
     """Set to int(str_int) TA_EFFORT of selected ThingAction."""
     val = integer_test(str_int, 0, 255)
-    if val:
+    if None != val:
         world_db["ThingActions"][command_taid.id]["TA_EFFORT"] = val
 
 
@@ -525,7 +517,9 @@ commands_db = {
     "TT_START_NUMBER": (1, False, ThingType_value_setter("TT_START_NUMBER",
                                                          0, 255)),
     "TT_PROLIFERATE": (1, False, ThingType_value_setter("TT_PROLIFERATE",
-                                                         0, 255))
+                                                        0, 255)),
+    "TT_LIFEPOINTS": (1, False, ThingType_value_setter("TT_LIFEPOINTS",
+                                                       0, 255))
 }