For about three years already I’ve been working on and off on a server/bot that runs Interactive Fiction games on web forums, effectively turning the single-player text adventure games into cooperative. The beta version launched at Idle Thumbs more than a year ago, but unfortunately it had some problems that made it crash regularly, and I didn’t find the time to fix them.
We’ll, a couple of weeks ago I decided to take the time and rewrote the main server code in Scala. That took only about a week and a half, the bot is now live again and running Hitchhiker’s Guide to the Galaxy. There are still several Java libraries in use of course: Z-Machine from Zinc, Bulletin Board API and ECF, Velocity, HTTPClient etc. But the main server code is now written in Scala.
My main goal for this rewrite, besides switching to Scala, was to simplify the architecture (back to the roots, I guess): the previous architecture was becoming too enterprisey for such a small project. It had too many layers of abstraction, some multi-threading issues made the code hard to debug, and using an OSGi runtime proved to be not that useful in my context. I still think OSGi is great, but sometimes simplicity is even better :)
Thankfully, ECF still works outside of OSGi and only requires two libraries from Equinox. By dropping OSGi, I could simplify the build process (or at least move it to what I’m more comfortable with – Maven). On the server I’m simply launching a Scala object with a main method instead of an OSGi runtime and that is good enough for this project.
Another simplification was dropping some layers of abstraction and removing code that was there only to support hypothetical future features (such as games on IRC and IM, many Interactive Fiction VM implementations etc.). I may be adding some of those layers back in the future, but for now the code should focus on making the core functionality (running a Z-machine game in a vBulletin or phpBB forum thread) as good as it can be.
Perhaps the most improvement that came from this rewrite was that Scala code tends to be about two times shorter than equivalent Java code, as I aslo experienced with my Scala port of Box2D, which I mentioned in a previous post.
But I would say that using Actors for communication between different threads has also gained me some simplicity and I was now better able to understand and fix some of the concurrency issues that used to make the server hang. However, I wasn’t able to move to a completely Actor-based communication model yet, as some of aspects of the messaging between the Z-machine and the forum bot still seemed easier done using other methods, such as blocking queues. Maybe I just don’t know enough about actors yet.
Overall, I’m happy with the result so far and since I find programming in Scala to be more enjoyable than Java, I think I will be more motivated to further develop Ziggy and keep it running smoothly from now on.