zturn - More Zork over REST
Hey internet!
Back in 2014 I built a thing called Restful Frotz (still a terrible name) so
people could play Zork inside Slack. It was a pile of PHP that ran the Frotz
interpreter as a child process, fed it the player’s line over stdin, and typed
the game’s own save and restore commands at it to carry state between requests.
It worked, mostly, and teams kept playing Zork in Slack for years after the joke
was over.
unit of work: one Z-machine turn per HTTP request.
and a turn is a pure function. Hand it the VM’s state and a line of input, get
back the new state and whatever the game printed. The game only ever pauses at a
read instruction, so that pause is the turn boundary, and the service never has
to type anything at the game on its own behalf. Disambiguation prompts, yes/no
questions, “restart” with its confirmation, all just turns like any other.
The other change is that randomness comes from a seed. Zork never seeds anything
itself, so zturn hands each session its own generator, and the same seed with
the same commands always produces the same game, thief included. That makes a
session nothing more than a seed and the list of what was typed. The server
keeps the latest state in memory as a cache, and if it’s gone it replays the log
from the start (about half a millisecond a turn). Undo is deleting the last
line. A share link is the seed and the commands in a URL. The transcript is the
log.
and your game stays in your browser so you can come back to it later. There’s a
connecting your own Slack app, one game per channel, everyone in the channel
playing the same game. Which is where this whole thing started.
The games aren’t in the repo. Infocom’s story files are free to download but not
open-licensed, so you bring your own.
– Tim