OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / game / save_banner.gd

73 lines · gd ref:
extends Label
## SAYS WHEN THE GAME IS NOT SAVING (docs/home-world-design.md §4, gate H8).
##
## Without the keet-daemon there is no durable state: GameState keeps every edit
## in its in-memory mirror and the next quit loses it, and nothing on screen said
## so. Home is a keet room and the daemon always runs, so its absence is a fault
## worth a line across the top of the screen:
##
##   - the daemon is gone while a room (Home included) is selected -> stays up
##     until the daemon is back;
##   - a durable write was REFUSED (GameState.write_rejected) -> names the key
##     for a few seconds.
##
## No room selected = nothing to save to, so no banner for the daemon alone.

const REJECT_SHOW_S := 6.0

var _daemon_down := false
var _reject_text := ""
var _reject_timer: Timer = null


func _ready() -> void:
	name = "SaveBanner"
	visible = false
	horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	set_anchors_and_offsets_preset(Control.PRESET_TOP_WIDE)
	offset_top = 8
	add_theme_color_override("font_color", Color(1.0, 0.45, 0.35))
	add_theme_color_override("font_outline_color", Color.BLACK)
	add_theme_constant_override("outline_size", 6)
	mouse_filter = Control.MOUSE_FILTER_IGNORE
	_reject_timer = Timer.new()
	_reject_timer.one_shot = true
	_reject_timer.timeout.connect(func() -> void:
		_reject_text = ""
		_refresh())
	add_child(_reject_timer)
	var ks := get_node_or_null("/root/KeetService")
	if ks != null:
		ks.daemon_status_changed.connect(_on_daemon_status)
		ks.room_selected.connect(func(_id: String, _t: String) -> void: _refresh())
	var gs := get_node_or_null("/root/GameState")
	if gs != null:
		gs.write_rejected.connect(_on_write_rejected)


func _on_daemon_status(status: String) -> void:
	_daemon_down = status == "disconnected"
	_refresh()


func _on_write_rejected(key: String, reason: String) -> void:
	_reject_text = "Not saved: %s — %s" % [key, reason]
	_reject_timer.start(REJECT_SHOW_S)
	_refresh()


func _in_room() -> bool:
	var ks := get_node_or_null("/root/KeetService")
	return ks != null and String(ks.get_current_room()) != ""


func _refresh() -> void:
	if _daemon_down and _in_room():
		text = "NOT SAVING — keet-daemon is not running. Edits are not saved and are lost on quit."
		visible = true
	elif _reject_text != "":
		text = _reject_text
		visible = true
	else:
		visible = false