oh-my-git/scenes/game.gd

163 lines
4.2 KiB
GDScript3
Raw Normal View History

2020-01-29 20:25:13 +01:00
extends Node
2023-09-07 11:43:51 +02:00
var tmp_prefix = get_tmp_prefix()
var global_shell
var fake_editor
2020-01-29 20:25:13 +01:00
var dragged_object
2020-10-15 15:22:38 +02:00
var energy = 2
2021-01-21 15:08:59 +01:00
var used_cards = false
var current_chapter = 0
var current_level = 0
var skipped_title = false
2020-09-28 16:18:06 +02:00
var _file = "user://savegame.json"
var state = {}
var mutex
2023-09-07 11:43:51 +02:00
func get_tmp_prefix():
if OS.get_name() == "Web":
return "/tmp/"
else:
2023-09-07 14:45:52 +02:00
return OS.get_user_data_dir() + "/tmp/"
2023-09-07 11:43:51 +02:00
2020-01-29 20:25:13 +01:00
func _ready():
mutex = Mutex.new()
load_state()
if OS.has_feature("standalone"):
get_tree().set_auto_accept_quit(false)
else:
game.toggle_music()
if OS.get_name() == "Windows":
start_remote_shell()
2023-09-07 14:45:52 +02:00
global_shell = await new_shell()
if false:
2023-09-07 14:45:52 +02:00
if (await global_shell.run("command -v git &>/dev/null && echo yes || echo no")) == "no\n":
game.skipped_title = true
get_tree().change_scene_to_file("res://scenes/no_git.tscn")
else:
2023-09-07 14:45:52 +02:00
await create_file_in_game_env(".gitconfig", helpers.read_file("res://scripts/gitconfig"))
2023-09-07 14:45:52 +02:00
await copy_script_to_game_env("fake-editor")
await copy_script_to_game_env("hint")
func start_remote_shell():
var user_dir = ProjectSettings.globalize_path("user://")
var script_content = helpers.read_file("res://scripts/net-test")
var target_filename = user_dir + "net-test"
2023-09-06 16:04:23 +02:00
var target_file = FileAccess.open(target_filename,FileAccess.WRITE)
#target_file.open(target_filename, File.WRITE)
target_file.store_string(script_content)
target_file.close()
helpers.exec_async(_perl_executable(), [target_filename, _bash_executable()])
func _perl_executable():
if OS.get_name() == "Windows":
return "dependencies/windows/git/usr/bin/perl.exe"
else:
return "perl"
func _bash_executable():
if OS.get_name() == "Windows":
return "dependencies/windows/git/usr/bin/bash.exe"
else:
return "bash"
func shell_received(text):
print(text)
func _notification(what):
2023-09-06 16:04:23 +02:00
if what == NOTIFICATION_WM_CLOSE_REQUEST:
#get_tree().quit() # default behavio
2023-09-06 16:04:23 +02:00
get_tree().change_scene_to_file("res://scenes/survey.tscn")
func copy_script_to_game_env(name):
2023-09-07 14:45:52 +02:00
await create_file_in_game_env(name, helpers.read_file("res://scripts/%s" % name))
await global_shell.run("chmod u+x '%s'" % (tmp_prefix + name))
2020-09-28 16:18:06 +02:00
func _initial_state():
return {"history": [], "solved_levels": [], "received_hints": [], "cli_badge": [], "played_cards": []}
2020-09-28 16:18:06 +02:00
func save_state():
2023-09-06 16:04:23 +02:00
var savegame = FileAccess.open(_file,FileAccess.WRITE)
2020-09-28 16:18:06 +02:00
2023-09-06 16:04:23 +02:00
#savegame.open(_file, File.WRITE)
savegame.store_line(JSON.new().stringify(state))
2020-09-28 16:18:06 +02:00
savegame.close()
func load_state():
2023-09-06 16:04:23 +02:00
var savegame = FileAccess.open(_file,FileAccess.READ)
2023-09-06 18:41:36 +02:00
if not savegame:
2020-09-28 16:18:06 +02:00
save_state()
2023-09-06 16:04:23 +02:00
#savegame.open(_file, File.READ)
2020-09-28 16:18:06 +02:00
state = _initial_state()
2023-09-06 16:04:23 +02:00
var test_json_conv = JSON.new()
test_json_conv.parse(savegame.get_line())
var new_state = test_json_conv.get_data()
2020-09-28 16:18:06 +02:00
for key in new_state:
state[key] = new_state[key]
savegame.close()
# filename is relative to the tmp directory!
func create_file_in_game_env(filename, content):
2023-09-07 14:45:52 +02:00
await global_shell.cd(tmp_prefix)
# Quoted HERE doc doesn't do any substitutions inside.
2023-09-07 14:45:52 +02:00
await global_shell.run("cat > '%s' <<'HEREHEREHERE'\n%s\nHEREHEREHERE" % [filename, content])
2020-12-22 15:38:43 +01:00
func notify(text, target=null, hint_slug=null):
if hint_slug:
if not state.has("received_hints"):
state["received_hints"] = []
if hint_slug in state["received_hints"]:
return
2023-09-06 16:04:23 +02:00
var notification = preload("res://scenes/notification.tscn").instantiate()
2020-12-22 15:38:43 +01:00
notification.text = text
if not target:
target = get_tree().root
target.call_deferred("add_child", notification)
if hint_slug:
state["received_hints"].push_back(hint_slug)
save_state()
func open_survey():
OS.shell_open("https://docs.google.com/forms/d/e/1FAIpQLSehHVcYfELT59h6plcn2ilbuqBcmDX3TH0qzB4jCgFIFOy_qg/viewform")
2021-02-11 10:46:08 +01:00
func toggle_music():
2023-09-06 16:04:23 +02:00
var music = game.find_child("Music")
2021-02-11 10:46:08 +01:00
if music.volume_db > -20:
music.volume_db -= 100
else:
music.volume_db += 100
func shell_test(command):
mutex.lock()
2021-02-27 15:07:35 +01:00
#print("go")
#print(command)
2023-09-06 16:04:23 +02:00
var before = Time.get_ticks_msec()
while not $ShellServer._connected:
$ShellServer._process(0.1)
var response = $ShellServer.send(command)
2023-09-06 16:04:23 +02:00
var after = Time.get_ticks_msec()
#print("took " + str(after-before)+" ms")
2021-02-27 15:07:35 +01:00
#print("stop")
mutex.unlock()
return response
2021-03-04 14:49:16 +01:00
func new_shell():
if OS.get_name() == "Windows":
return BetterShell.new()
else:
2023-09-07 14:45:52 +02:00
return await Shell.new()