From de9ca011dc4e8753310421d04217e1276b0ed5d0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 3 Jul 2022 01:17:45 +1200 Subject: [PATCH] Refactor global variable --- processor.js | 51 +++++++++++++++++++++++++++++++-------------------- start.js | 6 +++--- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/processor.js b/processor.js index 0e720f4..d7e4718 100644 --- a/processor.js +++ b/processor.js @@ -2,8 +2,6 @@ import {Parser} from "./parser.js" -const interchanges = [] - /* .dia file format: @@ -80,6 +78,27 @@ class MSStream { } } +class Dialog { + constructor() { + this.interchanges = [] + this.askaboutLine = "Created with PE-DIA." + } + + writeTo(stream) { + // interchange and reply data + const msstream = new MSStream(stream) + msstream.writeInt(this.interchanges.length) + for (const interchange of this.interchanges) { + interchange.writeTo(msstream) + } + // askabout data + msstream.writeInt(0) + msstream.writeString(this.askaboutLine) + // write + msstream.writeOut() + } +} + class Interchange { /** * @param {number} index @@ -87,7 +106,9 @@ class Interchange { * @param {string} text * @param {{effect: string, start: number, end: number}[]} effects */ - constructor(index, label, text, effects) { + constructor(index, dialog, label, text, effects) { + /** @type {Dialog} */ + this.dialog = dialog this.index = index this.label = label this.text = text @@ -163,7 +184,7 @@ class Interchange { return {text: noEffectText, effects: closedEffects} } - static CreateFromParser(index, parser) { + static CreateFromParser(parser, dialog) { let label = null let text = "" while (true) { @@ -194,7 +215,8 @@ class Interchange { } } - const interchange = new Interchange(index, label, text, effects) + const interchange = new Interchange(dialog.interchanges.length, dialog, label, text, effects) + dialog.interchanges.push(interchange) let structuredChoices = choices.map((choice, index) => Choice.CreateFromLine(interchange, index, choice)) interchange.choices = structuredChoices return interchange @@ -340,7 +362,7 @@ class Choice { } else if (this.attributes.gotoType === "absolute") { return [fnc, this.attributes.gotoLocation] } else if (this.attributes.gotoType === "label") { - const location = interchanges.findIndex(interchange => interchange.label === this.attributes.gotoLocation) + const location = this.interchange.dialog.interchanges.findIndex(interchange => interchange.label === this.attributes.gotoLocation) if (location === -1) throw new Error(`Supposed to go to label '${this.attributes.gotoLocation}', but no such label exists`) return [fnc, location] } else { @@ -364,21 +386,10 @@ class Choice { export function loadDialog(fileContents) { fileContents = fileContents.split("\n").filter(line => !line.startsWith("~")).join("\n") + const dialog = new Dialog() const parser = new Parser(fileContents) while (parser.hasRemaining()) { - interchanges.push(Interchange.CreateFromParser(interchanges.length, parser)) - } -} - -export function saveDialog(stream) { - // interchange and reply data - const msstream = new MSStream(stream) - msstream.writeInt(interchanges.length) - for (const interchange of interchanges) { - interchange.writeTo(msstream) + Interchange.CreateFromParser(parser, dialog) } - // askabout data - msstream.writeInt(0) - msstream.writeString("Created with PE-DIA.") - msstream.writeOut() + return dialog } diff --git a/start.js b/start.js index 460befd..67525b2 100644 --- a/start.js +++ b/start.js @@ -1,6 +1,6 @@ // here's where it does the thing -import {loadDialog, saveDialog} from "./processor.js" +import {loadDialog} from "./processor.js" function die(message) { console.error("Error: " + message) @@ -24,11 +24,11 @@ if (status.state === "denied") { // load the file let fileContents = await Deno.readTextFile(filename) fileContents = fileContents.replace(/\r\n/g, "\n") -loadDialog(fileContents) +const dialog = loadDialog(fileContents) // save the file // const stream = fs.createWriteStream("1.dia", {encoding: null}) const stream = Deno.stdout -saveDialog(stream) +dialog.writeTo(stream) console.error("Success! All done.") -- 2.45.2