"""The page the story writes on: its text and its HTML, put on the page as the release put them. The release ran on Vorple, whose Haven interface puts a story's text on the page and lets the story add HTML around it. This does what the story asked of them, the same way, so the page comes out with the same elements in the same places and the release's stylesheet lays it out the same: * Text goes into s: each line, and each run in one style, gets its own (`class=""`, `"font-italic"` or `"font-bold"`), and the stylesheet keeps the line breaks (`white-space: pre-wrap`). * `open_tag` adds an element where the text is going and sends the text into it, until `close_tag`. `focus` sends the text into the last element with a class instead. The other calls find the last element with a class (or all of them) and remove it, hide it, empty it or add a class to it. * The story waits for a key, or for a command, which the page's links send. It works on anything with the browser's document API: web.py hands it the page's, the tests a small stand-in. """ from __future__ import annotations import asyncio STYLE_CLASS = {"roman": "", "italic": "font-italic", "bold": "font-bold"} class Screen: def __init__(self, document): self.document = document self.window = document.getElementById("window0") self.container = self.window # where text and new elements go self.line = "" # text not yet on the page: it goes on at the end of a line, or before anything else self.current_style = "roman" # Haven's command line. The release hides it, but hidden it still takes a line at the end of the page while # the game waits for a command, so it comes and goes here too; it's out of the page the rest of the time. self.prompt = document.getElementById("lineinput") if self.prompt is not None: self.prompt.remove() self.waiting = None # "key" or "command" while the story waits for one self.commands: list[str] = [] # commands sent while the story wasn't waiting for one self._reply = None # -- Text ------------------------------------------------------------------------------------------------------ def print(self, text: str) -> None: while text: line, newline, text = text.partition("\n") self.line += line + newline if newline: self.flush() def flush(self) -> None: if self.line: span = self.document.createElement("span") span.className = STYLE_CLASS[self.current_style] span.textContent = self.line self.container.appendChild(span) self.line = "" def style(self, name: str) -> None: """Roman, italic or bold, for the text that follows.""" if name != self.current_style: self.flush() self.current_style = name # -- Elements -------------------------------------------------------------------------------------------------- def open_tag(self, tag: str, classes: str = ""): """`open HTML tag`: a new element where the text is going; the text goes into it from now on.""" element = self.document.createElement(tag) element.className = classes self.flush() self.container.appendChild(element) self.container = element return element def close_tag(self) -> None: """`close HTML tag`: the text goes back to where it went before the element was opened.""" if self.container.id != "window0": self.flush() self.container = self.container.parentNode def place_block(self, classes: str) -> None: """`place a block level element called ...`: a
, which the text goes into.""" self.open_tag("div", classes) def focus(self, classes: str) -> None: """`set output focus to the element called ...`: the text goes into the last such element.""" element = self.last(classes) if element is not None: self.flush() self.container = element def focus_main(self) -> None: """`set output focus to the main window`.""" self.flush() self.container = self.window def every(self, classes: str) -> list: return list(self.document.getElementsByClassName(classes)) def last(self, classes: str): found = self.every(classes) return found[-1] if found else None def remove(self, classes: str) -> None: """`remove the element called ...`: the last one.""" element = self.last(classes) if element is not None: element.remove() def remove_all(self, classes: str) -> None: """`remove all elements called ...`.""" for element in self.every(classes): element.remove() def hide(self, classes: str) -> None: """`hide the element called ...`.""" element = self.last(classes) if element is not None: element.style.display = "none" def add_class(self, classes: str, name: str) -> None: """`add name ... to the element called ...`: the stylesheet animates some of these (`fadein`).""" element = self.last(classes) if element is not None: element.classList.add(name) def clear(self, classes: str) -> None: """`clear the element called ...`: empty it.""" element = self.last(classes) if element is not None: element.replaceChildren() def image(self, source: str, description: str, centered: bool = False) -> None: """`place an image ... with the description ...`: a
holding an .""" holder = self.open_tag("div", "vorple-image centered" if centered else "vorple-image") self.close_tag() picture = self.document.createElement("img") picture.setAttribute("src", source) picture.setAttribute("alt", description) holder.appendChild(picture) def open_command_link(self, command: str): """Opens a link that sends `command` to the story when it's clicked; close it with close_tag.""" link = self.open_tag("a", "vorple-link vorple-commandlink") link.setAttribute("href", "#") link.setAttribute("data-command", command) return link def open_web_link(self, url: str, new_window: bool = True): """Opens a link to a page; close it with close_tag.""" link = self.open_tag("a", "vorple-link vorple-weblink") link.setAttribute("href", url) if new_window: link.setAttribute("target", "_blank") return link def preload(self, sources: list[str]) -> None: """`preload image`: the browser's version fetches them now, so they show at once later.""" def scroll_to_end(self) -> None: """`scroll to the end of the page`: only a browser scrolls.""" # -- Waiting for the player ------------------------------------------------------------------------------------ async def wait_for_key(self) -> None: """`wait for any key`: until a key is pressed or the page is clicked.""" self.flush() self.waiting = "key" self._reply = asyncio.get_running_loop().create_future() await self._reply async def read_command(self) -> str: """The next command: one already sent, or the next a link sends. The prompt goes up either way, as in the release, where a command sent ahead runs as soon as it's up. """ self.flush() if self.prompt is not None: self.window.appendChild(self.prompt) self.showing_prompt() if self.commands: command = self.commands.pop(0) else: self.waiting = "command" self._reply = asyncio.get_running_loop().create_future() command = await self._reply if self.prompt is not None: self.prompt.remove() return command def showing_prompt(self) -> None: """Called as the game starts waiting for a command; the browser's version may scroll.""" def press_key(self) -> None: """A key was pressed (or the page clicked): it ends a `wait for any key`.""" if self.waiting == "key": self._answer(None) def send_command(self, command: str) -> None: """A command link was clicked: the story gets the command now, or when it next asks for one.""" if self.waiting == "command": self._answer(command) else: self.commands.append(command) def _answer(self, value) -> None: self.waiting = None self._reply.set_result(value)