mirror of
https://github.com/git-learning-game/oh-my-git.git
synced 2024-12-22 20:32:38 +01:00
Use scripts to create levels, add level selector
This commit is contained in:
parent
1b5cbe9cb0
commit
2a0e2e46bb
9 changed files with 124 additions and 38 deletions
28
game.gd
28
game.gd
|
@ -31,8 +31,36 @@ func load_state() -> bool:
|
|||
savegame.close()
|
||||
return true
|
||||
|
||||
# Run a simple command given as a string, blocking, using execute.
|
||||
func run(command):
|
||||
print("run: "+command)
|
||||
var output = []
|
||||
OS.execute(command, [], true, output, true)
|
||||
# Remove trailing newline.
|
||||
return output[0].substr(0,len(output[0])-1)
|
||||
|
||||
func sh(command, wd="/tmp/"):
|
||||
print("sh in "+wd+": "+command)
|
||||
var cwd = game.run("pwd")
|
||||
var output = []
|
||||
|
||||
var hacky_command = command
|
||||
hacky_command = "cd '"+wd+"';"+hacky_command
|
||||
hacky_command = "export EDITOR=fake-editor;"+hacky_command
|
||||
hacky_command = "export PATH=\"$PATH\":"+cwd+"/scripts;"+hacky_command
|
||||
OS.execute("/bin/sh", ["-c", hacky_command], true, output, true)
|
||||
return output[0]
|
||||
|
||||
func read_file(path):
|
||||
var file = File.new()
|
||||
file.open(path, File.READ)
|
||||
var content = file.get_as_text()
|
||||
file.close()
|
||||
return content
|
||||
|
||||
func write_file(path, content):
|
||||
var file = File.new()
|
||||
file.open(path, File.WRITE)
|
||||
file.store_string(content)
|
||||
file.close()
|
||||
return true
|
||||
|
|
6
levels/blobs/goal
Normal file
6
levels/blobs/goal
Normal file
|
@ -0,0 +1,6 @@
|
|||
echo "Hi" > file1
|
||||
echo "Ho" > file1
|
||||
echo "Hu" > file1
|
||||
git hash-object -w file1
|
||||
git hash-object -w file2
|
||||
git hash-object -w file3
|
0
levels/blobs/start
Normal file
0
levels/blobs/start
Normal file
1
levels/first/goal
Normal file
1
levels/first/goal
Normal file
|
@ -0,0 +1 @@
|
|||
git commit --allow-empty -m "Emtpy commit"
|
0
levels/first/start
Normal file
0
levels/first/start
Normal file
79
main.gd
79
main.gd
|
@ -6,21 +6,63 @@ var server
|
|||
var client_connection
|
||||
|
||||
func _ready():
|
||||
var level = "first"
|
||||
var goal_repository_path = "goal"
|
||||
var start_repository_path = "start"
|
||||
var active_repository_path = "active"
|
||||
var active_prefix = "/tmp/"
|
||||
var levels_prefix = game.run("pwd")+"/levels/"+level+"/"
|
||||
|
||||
OS.execute("rm", ["-r", active_prefix+active_repository_path], true)
|
||||
OS.execute("cp", ["-ra", levels_prefix+start_repository_path, active_prefix+active_repository_path], true)
|
||||
$GoalRepository.path = levels_prefix+goal_repository_path
|
||||
$ActiveRepository.path = active_prefix+active_repository_path
|
||||
# Initialize level select.
|
||||
var options = $LevelSelect.get_popup()
|
||||
for level in list_levels():
|
||||
options.add_item(level)
|
||||
options.connect("id_pressed", self, "load_level")
|
||||
|
||||
# Initialize TCP server for fake editor.
|
||||
server = TCP_Server.new()
|
||||
server.listen(1234)
|
||||
|
||||
func list_levels():
|
||||
var levels = []
|
||||
var dir = Directory.new()
|
||||
dir.open("levels")
|
||||
dir.list_dir_begin()
|
||||
|
||||
while true:
|
||||
var file = dir.get_next()
|
||||
if file == "":
|
||||
break
|
||||
elif not file.begins_with("."):
|
||||
levels.append(file)
|
||||
|
||||
dir.list_dir_end()
|
||||
return levels
|
||||
|
||||
func load_level(id):
|
||||
var levels = list_levels()
|
||||
|
||||
var level = levels[id]
|
||||
var cwd = game.run("pwd")
|
||||
var tmp_prefix = "/tmp/"
|
||||
var level_prefix = cwd+"/levels/"
|
||||
|
||||
var goal_repository_path = tmp_prefix+"goal/"
|
||||
var active_repository_path = tmp_prefix+"active/"
|
||||
var goal_script = level_prefix+level+"/goal"
|
||||
var active_script = level_prefix+level+"/start"
|
||||
|
||||
OS.execute("rm", ["-r", active_repository_path], true)
|
||||
OS.execute("rm", ["-r", goal_repository_path], true)
|
||||
construct_repo(goal_script, goal_repository_path)
|
||||
construct_repo(active_script, active_repository_path)
|
||||
|
||||
$GoalRepository.path = goal_repository_path
|
||||
$ActiveRepository.path = active_repository_path
|
||||
|
||||
func construct_repo(script, path):
|
||||
print(path)
|
||||
game.sh("mkdir "+path)
|
||||
game.sh("git init", path)
|
||||
var commands = game.read_file(script).split("\n")
|
||||
print(commands)
|
||||
for command in commands:
|
||||
print(command)
|
||||
game.sh(command, path)
|
||||
|
||||
func _process(delta):
|
||||
if server.is_connection_available():
|
||||
client_connection = server.take_connection()
|
||||
|
@ -50,23 +92,12 @@ func _process(delta):
|
|||
func read_commit_message():
|
||||
$CommitMessage.show()
|
||||
$Terminal/Input.editable = false
|
||||
var file_path = "/tmp/githydragit/.git/COMMIT_EDITMSG"
|
||||
var file = File.new()
|
||||
file.open(file_path, File.READ)
|
||||
var content = file.get_as_text()
|
||||
file.close()
|
||||
$CommitMessage.text = content
|
||||
$CommitMessage.text = game.read_file("/tmp/githydragit/.git/COMMIT_EDITMSG")
|
||||
|
||||
func save_commit_message():
|
||||
var file = File.new()
|
||||
var file_path = "/tmp/githydragit/.git/COMMIT_EDITMSG"
|
||||
file.open(file_path, File.WRITE)
|
||||
var content = $CommitMessage.text
|
||||
file.store_string(content)
|
||||
file.close()
|
||||
game.write_file("/tmp/githydragit/.git/COMMIT_EDITMSG", $CommitMessage.text)
|
||||
print("disconnect")
|
||||
client_connection.disconnect_from_host()
|
||||
$Terminal/Input.editable = true
|
||||
$CommitMessage.text = ""
|
||||
$CommitMessage.hide()
|
||||
|
||||
|
|
11
main.tscn
11
main.tscn
|
@ -57,5 +57,16 @@ size = Vector2( 500, 1000 )
|
|||
[node name="ActiveRepository" parent="." instance=ExtResource( 3 )]
|
||||
position = Vector2( 704.399, 17.2594 )
|
||||
size = Vector2( 500, 1000 )
|
||||
|
||||
[node name="LevelSelect" type="MenuButton" parent="."]
|
||||
margin_left = 11.5584
|
||||
margin_top = 11.1303
|
||||
margin_right = 109.558
|
||||
margin_bottom = 31.1303
|
||||
text = "Select level..."
|
||||
flat = false
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
[connection signal="text_entered" from="Terminal/Input" to="Terminal" method="send_command"]
|
||||
[connection signal="pressed" from="CommitMessage/Button" to="." method="save_commit_message"]
|
||||
|
|
|
@ -10,14 +10,20 @@ func _ready():
|
|||
|
||||
func _process(delta):
|
||||
if path:
|
||||
update_head()
|
||||
update_refs()
|
||||
update_index()
|
||||
update_objects()
|
||||
apply_forces()
|
||||
update_everything()
|
||||
|
||||
func update_everything():
|
||||
update_head()
|
||||
update_refs()
|
||||
update_index()
|
||||
update_objects()
|
||||
apply_forces()
|
||||
|
||||
func set_path(new_path):
|
||||
path = new_path
|
||||
for o in objects.values():
|
||||
o.queue_free()
|
||||
objects = {}
|
||||
|
||||
func get_path():
|
||||
return path
|
||||
|
@ -126,6 +132,16 @@ func update_head():
|
|||
add_child(n)
|
||||
var n = objects["HEAD"]
|
||||
n.children = {symref_target("HEAD"): ""}
|
||||
if not objects.has(symref_target("HEAD")):
|
||||
var n2 = node.instance()
|
||||
var r = symref_target("HEAD")
|
||||
n2.id = r
|
||||
n2.type = "ref"
|
||||
n2.content = ""
|
||||
n2.repository = self
|
||||
n2.position = random_position()
|
||||
objects[r] = n2
|
||||
add_child(n2)
|
||||
|
||||
func all_objects():
|
||||
return git("cat-file --batch-check=%(objectname) --batch-all-objects", true)
|
||||
|
|
11
terminal.gd
11
terminal.gd
|
@ -7,15 +7,8 @@ func send_command(command):
|
|||
thread.start(self, "run_command_in_a_thread", command)
|
||||
|
||||
func run_command_in_a_thread(command):
|
||||
var cwd = game.run("pwd")
|
||||
var output = []
|
||||
|
||||
var hacky_command = command
|
||||
hacky_command = "cd /tmp/githydragit;"+hacky_command
|
||||
hacky_command = "export EDITOR=fake-editor;"+hacky_command
|
||||
hacky_command = "export PATH=\"$PATH\":"+cwd+"/scripts;"+hacky_command
|
||||
OS.execute("/bin/sh", ["-c", hacky_command], true, output, true)
|
||||
var output = game.sh(command, "/tmp/active")
|
||||
|
||||
$Input.text = ""
|
||||
$Output.text = $Output.text + "$ " + command + "\n" + output[0]
|
||||
$Output.text = $Output.text + "$ " + command + "\n" + output
|
||||
$Output.scroll_vertical = 999999
|
||||
|
|
Loading…
Reference in a new issue