75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
// engine_logic.js - Reine State- und Datenverarbeitung
|
|||
|
|
const EngineLogic = {
|
||
|
|
storyData: { nodes: {} },
|
||
|
|
state: { reboots: 1, node: "" },
|
||
|
|
|
||
|
|
init: function(rawJsonData) {
|
||
|
|
try {
|
||
|
|
if (!rawJsonData || rawJsonData.trim() === "") throw "Leer";
|
||
|
|
this.storyData = JSON.parse(rawJsonData);
|
||
|
|
} catch(e) {
|
||
|
|
console.warn("[System] Go-Engine Offline. Fallback Modus geladen.");
|
||
|
|
this.storyData = {
|
||
|
|
startNodeId: "err",
|
||
|
|
nodes: {
|
||
|
|
"err": {
|
||
|
|
id: "err", title: "SYSTEM OFFLINE",
|
||
|
|
content: "<p>Konnte <code>[story-data]</code> nicht parsen. Go-Server läuft nicht.</p>",
|
||
|
|
choices: []
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
this.state.node = this.storyData.startNodeId || Object.keys(this.storyData.nodes)[0] || "";
|
||
|
|
},
|
||
|
|
|
||
|
|
getNode: function(id) {
|
||
|
|
return this.storyData.nodes[id];
|
||
|
|
},
|
||
|
|
|
||
|
|
processNodeEntry: function(id) {
|
||
|
|
const node = this.getNode(id);
|
||
|
|
if (!node) return null;
|
||
|
|
|
||
|
|
this.state.node = id;
|
||
|
|
|
||
|
|
// Metadaten anwenden (z.B. [meta: schild=80])
|
||
|
|
if (node.metadata) {
|
||
|
|
Object.keys(node.metadata).forEach(key => {
|
||
|
|
this.state[key] = node.metadata[key];
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Reboot Erkennung
|
||
|
|
if (node.isReboot) {
|
||
|
|
this.state.reboots++;
|
||
|
|
}
|
||
|
|
|
||
|
|
return node;
|
||
|
|
},
|
||
|
|
|
||
|
|
parseRequirement: function(choiceText) {
|
||
|
|
let conditionMatch = choiceText.match(/\[req:\s*([a-zA-Z0-9_]+)\s*=\s*([^\]]+)\]/i);
|
||
|
|
let displayText = choiceText;
|
||
|
|
let meetsCondition = true;
|
||
|
|
let missingReq = "";
|
||
|
|
|
||
|
|
if (conditionMatch) {
|
||
|
|
let reqKey = conditionMatch[1].toLowerCase();
|
||
|
|
let reqVal = conditionMatch[2];
|
||
|
|
if (String(this.state[reqKey]) !== String(reqVal)) {
|
||
|
|
meetsCondition = false;
|
||
|
|
missingReq = `(Benötigt: ${reqKey}=${reqVal})`;
|
||
|
|
}
|
||
|
|
displayText = displayText.replace(conditionMatch[0], "").trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
return { displayText, meetsCondition, missingReq };
|
||
|
|
},
|
||
|
|
|
||
|
|
triggerReboot: function() {
|
||
|
|
const currentReboots = this.state.reboots + 1;
|
||
|
|
this.state = { reboots: currentReboots }; // Resets inventory/state
|
||
|
|
return this.storyData.startNodeId;
|
||
|
|
}
|
||
|
|
};
|