-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameRunner.py
86 lines (74 loc) · 2.59 KB
/
GameRunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import os
import subprocess
import sys
# Search for Script.py
sys.path.append("")
sys.dont_write_bytecode = True
try:
import Script
except:
print("Script not found. ")
print("Make sure you're in the script's directory when you open this file.")
print("To do this, navigate to the file with the Script module and then:")
print(" - run the `play.bat` file if one exists")
print(" - run this file using with: python \"" + os.path.abspath(sys.argv[0]) + "\"")
print(" - copy this file into the script's folder")
exit()
# Load the save file, or create a new one if it doesn't exist
saveFilePath = "SaveFile.json"
if os.path.exists(saveFilePath):
with open(saveFilePath) as myfile:
data = json.load(myfile)
else:
data = Script.InitialFile()
with open(saveFilePath, "w") as myfile:
json.dump(data, myfile, indent = 4, separators = (",\n", ": "))
# Create a git repository if it doesn't exist
if not os.path.isdir(".git"):
print("GIT repository not found. I'll make one.")
subprocess.call("git init", shell=True)
subprocess.call("git add .", shell=True)
subprocess.call("git commit -m start", shell=True)
print("Re-run this file to start the game.")
exit()
# The game loop:
while True:
# Tell the user what's going on and get the list of possible actions
print("---------------------------")
print()
actions = Script.DescribeAndGetActions(data)
print()
# If no actions are available then exit
if len(actions) == 0:
print("No actions available")
exit()
# Ask the user to pick an action
print("Available actions:")
for i, action in enumerate(actions):
print(f"{i}. {action[0]}")
print()
try:
chosen = actions[int(input("Pick an action by entering its number: "))]
print()
except:
print("Exiting...")
exit()
# Compare the active save file to the actual save file
with open(saveFilePath) as myfile:
tmpdata = json.load(myfile)
if data != tmpdata:
print("Save file has been edited externally. Please try again.")
data = tmpdata
continue
# Update the save file
if (callable(chosen[1])):
data.update(chosen[1]())
else:
data.update(chosen[1])
# Save the save file
with open(saveFilePath, "w") as myfile:
json.dump(data, myfile, indent = 4, separators = (",\n", ": "))
# git commit the changes
subprocess.call("git add .", shell=True, stdout=subprocess.DEVNULL)
subprocess.call("git commit -m \"" + chosen[0] + "\"", shell=True, stdout=subprocess.DEVNULL)