From c3fa30dcf6fbc8d092a8e2db7d004b8bf03de499 Mon Sep 17 00:00:00 2001
From: mszczepa <marin.szczepanski@inria.fr>
Date: Thu, 19 May 2022 15:48:25 +0200
Subject: [PATCH] Base for the system to save inputs and replay them

---
 .gitignore                     |  2 +-
 levels/interrupteurs/launch.py | 92 ++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 levels/interrupteurs/launch.py

diff --git a/.gitignore b/.gitignore
index fb9ed97c..ffd4f38b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,7 +13,7 @@ levels/tutorial/04_stairs
 levels/tutorial/05_key
 levels/tutorial/05_real_key
 
-levels/interrupteurs/main
+**/main
 
 mediumgame/local_struct
 
diff --git a/levels/interrupteurs/launch.py b/levels/interrupteurs/launch.py
new file mode 100644
index 00000000..19cc49f7
--- /dev/null
+++ b/levels/interrupteurs/launch.py
@@ -0,0 +1,92 @@
+from sys import argv
+from datetime import datetime
+import os
+
+
+out_dir = "saved_inputs"
+
+
+def play_file(filename, display_state=True):
+    print("=== display_state is", display_state)
+    try:
+        with open(filename, "r") as f:
+            for line in f:
+                # give the command to the inferior here and display the result
+                # stop when exit_main_loop becomes true
+                # or maybe give all the file at once?
+                print(line, end="")
+    except OSError:
+        print("=== Can't read file", filename)
+
+
+def record_mode():
+    # We assume students are note able to launch -r twice in the same second
+    filename = out_dir + "/" + datetime.now().strftime("%y-%m-%d_%H:%M:%S") + ".in"
+    print("=== Record mode\n=== Inputs will be saved in", filename)
+
+    try:
+        os.mkdir(out_dir)
+    except FileExistsError:
+        pass
+
+    with open(filename, "w") as f:
+        # just to test we cycle 5 times, in real we will use easytracker to watch exit_main_loop
+        i = 0
+        while i < 5:
+            c = input("=== Enter a command: ")
+            # give the command to the inferior and display the result
+            f.write(c + "\n")
+            i = i + 1
+
+
+def play_mode(filename):
+    print("=== Play mode with", filename)
+    play_file(filename)
+
+
+def all_mode():
+    print("=== All mode\n=== Reading files in", out_dir)
+    empty_folder = True
+
+    try:
+        for e in os.listdir(out_dir):
+            empty_folder = False
+            filename = out_dir + "/" + e
+            if os.path.isfile(filename) and e.endswith(".in"):
+                print("=== Playing", e)
+                play_file(filename, False)
+    except FileNotFoundError:
+        empty_folder = False
+        print("=== The folder", out_dir, "doesn't exist. Use -r mode to create it")
+    
+    if empty_folder:
+        print("=== There is no input file in ", out_dir, ". Use -r mode to create some", sep="")
+            
+
+def main(arg):
+    if len(arg) < 2:
+        print("=== Usage: ", arg[0], " <mode>\n"
+                "=== <mode> can be:\n",
+                "=== -r records manual inputs and displays the game's state,\n",
+                "=== -p FILENAME plays inputs from file FILENAME and display the game's state,\n",
+                "=== -a plays all inputs files in the folder ", out_dir,
+                " and only displays the final result for each",
+                sep="")
+        return
+    
+    if arg[1] == "-r":
+        record_mode()
+    elif arg[1] == "-p":
+        if len(arg) != 3:
+            print("=== Please enter a single FILENAME for play mode.")
+            return
+        else:
+            play_mode(arg[2])
+    elif arg[1] == "-a":
+        all_mode()
+    else:
+        print("=== Unknown mode. Valid modes are -r, -p FILENAME and -a")
+    
+
+if __name__ == "__main__":
+    main(argv)
-- 
GitLab