RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
codex_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var discoveries := {}
8var failed_reactions := {}
9var notes := []
10var reaction_history := []
11
12
13
14func record_discovery(reaction:Dictionary) -> void:
15 var result_id = str(reaction.get("result", ""))
16 if result_id == "":
17 return
18 var entry = discoveries.get(result_id, {}).duplicate(true)
19 entry["id"] = result_id
20 entry["state"] = "discovered"
21 entry["reaction_key"] = str(reaction.get("key", ""))
22 entry["inputs"] = reaction.get("inputs", []).duplicate(true)
23 entry["description"] = str(reaction.get("description", entry.get("description", "")))
24 entry["uses"] = int(entry.get("uses", 0)) + 1
25 discoveries[result_id] = entry
26 _record_history({
27 "type": "discovery",
28 "id": result_id,
29 "inputs": entry.get("inputs", []).duplicate(true),
30 "catalysts": reaction.get("catalysts", []).duplicate(true),
31 "message": str(entry.get("description", "")),
32 "success": true
33 })
34
35
36
37func record_failure(reaction_attempt:Dictionary) -> void:
38 var key = str(reaction_attempt.get("key", ""))
39 if key == "":
40 return
41 failed_reactions[key] = {
42 "key": key,
43 "inputs": reaction_attempt.get("inputs", []).duplicate(true),
44 "catalysts": reaction_attempt.get("catalysts", []).duplicate(true),
45 "message": str(reaction_attempt.get("message", "No stable reaction."))
46 }
47 _record_history({
48 "type": "failure",
49 "id": key,
50 "inputs": reaction_attempt.get("inputs", []).duplicate(true),
51 "catalysts": reaction_attempt.get("catalysts", []).duplicate(true),
52 "message": str(reaction_attempt.get("message", "No stable reaction.")),
53 "success": false
54 })
55
56
57func add_note(category:String, text:String) -> void:
58 notes.append({
59 "category": category,
60 "text": text,
61 "created_at": Time.get_unix_time_from_system()
62 })
63
64
65func has_discovery(id:String) -> bool:
66 return discoveries.has(id)
67
68
69func get_discoveries() -> Array:
70 return discoveries.values()
71
72
73func get_failures() -> Array:
74 return failed_reactions.values()
75
76
77func get_reaction_history(filters := {}) -> Array:
78 var entries := []
79 for entry in reaction_history:
80 if _matches_filters(entry, filters):
81 entries.append(entry.duplicate(true))
82 return entries
83
84
85func get_collection_entries(filters := {}) -> Array:
86 var entries := []
87 for discovery in get_discoveries():
88 var row = discovery.duplicate(true)
89 row["type"] = "discovery"
90 row["title"] = str(row.get("id", "")).capitalize()
91 row["subtitle"] = " + ".join(row.get("inputs", []))
92 row["locked"] = false
93 if _matches_filters(row, filters):
94 entries.append(row)
95 for failure in get_failures():
96 var row = failure.duplicate(true)
97 row["id"] = str(row.get("key", ""))
98 row["type"] = "failure"
99 row["title"] = "Failed: %s" % " + ".join(row.get("inputs", []))
100 row["subtitle"] = str(row.get("message", "No stable reaction."))
101 row["locked"] = false
102 if _matches_filters(row, filters):
103 entries.append(row)
104 for note in notes:
105 var row = note.duplicate(true)
106 row["id"] = "%s:%s" % [str(row.get("category", "note")), str(row.get("created_at", ""))]
107 row["type"] = "note"
108 row["title"] = str(row.get("category", "Note")).capitalize()
109 row["subtitle"] = str(row.get("text", ""))
110 row["locked"] = false
111 if _matches_filters(row, filters):
112 entries.append(row)
113 return entries
114
115
116func get_state() -> Dictionary:
117 return {
118 "discoveries": discoveries.duplicate(true),
119 "failed_reactions": failed_reactions.duplicate(true),
120 "notes": notes.duplicate(true),
121 "reaction_history": reaction_history.duplicate(true)
122 }
123
124
125func apply_state(state:Dictionary) -> void:
126 discoveries = state.get("discoveries", {}).duplicate(true)
127 failed_reactions = state.get("failed_reactions", {}).duplicate(true)
128 notes = state.get("notes", []).duplicate(true)
129 reaction_history = state.get("reaction_history", []).duplicate(true)
130
131
132func reset() -> void:
133 discoveries.clear()
134 failed_reactions.clear()
135 notes.clear()
136 reaction_history.clear()
137
138
139func _record_history(entry:Dictionary) -> void:
140 var row = entry.duplicate(true)
141 row["created_at"] = Time.get_unix_time_from_system()
142 reaction_history.push_front(row)
143
144
145func _matches_filters(entry:Dictionary, filters:Dictionary) -> bool:
146 var type_filter = str(filters.get("type", "all"))
147 if type_filter != "all" and str(entry.get("type", "")) != type_filter:
148 return false
149 var query = str(filters.get("query", "")).strip_edges().to_lower()
150 if query == "":
151 return true
152 var haystack = [
153 str(entry.get("id", "")),
154 str(entry.get("key", "")),
155 str(entry.get("title", "")),
156 str(entry.get("subtitle", "")),
157 str(entry.get("message", "")),
158 " ".join(entry.get("inputs", [])),
159 " ".join(entry.get("catalysts", []))
160 ]
161 return " ".join(haystack).to_lower().contains(query)