Showing posts with label web programming. Show all posts
Showing posts with label web programming. Show all posts

Wednesday, 29 January 2014

Roguelike MUD progress #7 - Web-based client

Previous post: Roguelike MUD progress #6

I set a mini-goal for myself, to display a curses-based program on a web page.

The first step was to find a no frills, easy to adopt and extend Python websocket solution. One I've stumbled on  few times, is the simple websockets client/server gist. I used a slightly fixed version of it as a base. The next step was to find decent web-based terminal emulation. There are frameworks available which ditch the terminal window, but they come with larger problems.  The best candidate is term.js.

So taking the simple websockets simple_websocket_client.html and adding term.js, then modifying simple_websocket_server.py, it was easy to get a simple echo working with embedded ANSI control sequences.

First, I tried to proxy the input and output from the nano editor. Unfortunately, doing this with the subprocess module resulted in nano outputting a message about not supporting redirection.  There's probably a way to do this, but too much bother and it is not really my goal anyway.

Next, I went for something simpler.  I did a simple proxying of input and output to a telnet connection. Specifically my Roguelike MUD codebase.  There were some teething problems.  Text incoming from the browser-based terminal through the websocket, is UTF-8 encoded.  And similarly, outgoing text has to be as well.  But responses from the MUD contain both ANSI escape sequences and telnet negotiation sequences.  In order to work around this, I stripped the telnet negotiation out of outgoing text, and modified the relatively basic websocket code to also send binary frames.

A working connection:


In-game display:


As the picture above shows, the ANSI sequences are working perfectly.  But the character set does not seem to contain the line drawing characters.  My game interface already has debugging tools which display accessible character sets.

The main character sets:


As can be seen above, the only character set with even a hint of line drawing characters, is the one exposed through the ESC(0 ANSI sequence.  Most of these sequences are faked by term.js, where known character sets are mocked up from the underlying unicode-supporting font.

One solution is to do what I do with Putty, which is to identify the connected client, and if it's one which supports unicode, then switch unicode on using terminal control sequences.  Modifying term.js to respond to the ENQ character (0x05), and send it's terminal name (xterm) was easy enough.

This is what I should be seeing now:


This is what I am actually seeing:


The correct unicode characters are being sent, but not being displayed correctly by term.js.  I don't expect this will be that difficult to correct when I get some more time to work on this.

Monday, 4 February 2013

pysrd

One of my long-term projects is to programmatically generate game logic from the D&D 3.5 SRD.  The idea is that ideally if the required legwork was done, any time I wanted to prototype something around a game experience, it should be possible to plug in the processed data and be 98% of the way to having stock D&D gameplay.  There's a lot of hand-waving there around the phrase "required legwork", but that's not really important for the purpose of this post.


I've extracted the low level scripts I use to both extend the database and introspect it's contents, and published them on github under the project name pysrd.




The SRD is available from Wizards of the Coast as a set of RTF documents.  This of course, is not ideal, but fortunately it is possible to get the data from andargor.com in different database formats, including SQLite.  Unfortunately however, not all SRD data is included in these databases.  Fortunately, the OpenSRD project includes generated HTML versions of the RTF documents, which can more easily be processed.

Tuesday, 29 November 2011

Open source web-based consoles

I'm looking to bring both my MUD and networked roguelike code-base into the 90's, erm.. that should be 00's these days.  A web-based console with connectivity to the backend server through the web server is the way to go.

I've been collecting candidate "already written wheels" and wanted to collect them in one place.

Forum thread: HTML+JS Tech Demo (demo).

A GPL licensed code-base which uses an array of character cells to emulate a curses-like display.

Advantages:
  • Cleanly written code.
Disadvantages:
  • Restrictively licensed.
  • Code bound to existing game implementation.
Forum thread: js-like beta (first version!) released (demo).
A BSD licensed code-base which implements three different display mechanisms, canvas, array of character cells and image based.

Advantages:
  • Liberally licensed.
  • More library like, than game specific.
Disadvantages:
  • Code is somewhat hard to follow.
Forum thread: DecafMUD (demo)
A unknown licensed code-base which provides a complete out of the box web-based MUD client. Supports different code pages, and able to interpret the complete telnet stream from negotiation to escape codes. Tries to use web sockets for connectivity, but can fall back on flash if they are not usable.

Advantages:
  • Complete solution
  • Straight-forward code.
Disadvantages:
  • Windows-incompatible repository contents, errors when cloned.
  • Official web-site down.
  • Telnet protocol support is superfluous, a modern MUD server should be have a direct connection and not be encumbered by these complications.
While the GPL license is indeed restrictive, and reason enough to avoid using a project.  In this case, given a client is kept separated from the server, it shouldn't encumber the server-side in any way that matters.

Saturday, 19 June 2010

MUDBytes and post bloat

The most active MUD-related forum is currently MUDBytes. One of the good things is that the team there maintain their own forum software, and as the idea or need arises, it gets extended to support new features. However this is both good and bad, as the amount of feature "cruft" that decorates each displayed post increases.


Finding the "cruft" distracting, I decided to write a greasemonkey script to remove as much as possible. Here's the result:


And the greasemonkey script:

// ==UserScript==
// @name MUDBytes Page Cleaner
// @namespace http://disinterest.org
// @description Makes the pages less cluttered.
// @include http://www.mudbytes.net/topic-*
// @include http://www.mudbytes.net/index.php*
// ==/UserScript==

var tds = document.getElementsByTagName("td");
var posterInfoTD = null;
var classNames = "";
for(var i = 0; i < tds.length; i++){
classNames = tds[i].className;
if (classNames == "posterinfo") {
posterInfoTD = tds[i];
} else if (classNames == "posttop") {
for (var j = 0; j < tds[i].childNodes.length; j++) {
var topElement = tds[i].childNodes[j];
if (topElement.className == "button butright") {
topElement.className = "butright";
topElement.innerHTML = " ["+ topElement.innerHTML +"]";
}
}

// Remove the arrows.
var divs = tds[i].getElementsByTagName("div");
divs[0].innerHTML = "";
// Inject the poster name.
var posterText = document.createTextNode(" -- ");
var posterNameB = document.createElement("b");
var as = posterInfoTD.getElementsByTagName("a");
if (as.length > 0) {
posterNameB.appendChild(as[0].cloneNode(true));
} else {
var posterNameText = document.createTextNode("Guest");
posterNameB.appendChild(posterNameText);
}
divs[divs.length-1].appendChild(posterText);
divs[divs.length-1].appendChild(posterNameB);
// Remove the LHS poster info area.
posterInfoTD.parentNode.removeChild(posterInfoTD);
} else if (classNames == "postbottom") {
tds[i].innerHTML = "";
}
}

// Reduce the reading area width to make it more readable.
var tableElements = document.getElementsByTagName("table");
for (var i = 0; i < tableElements.length; i++) {
if (tableElements[i].className == "stand")
tableElements[i].style.width = '700px';
}

As cluttered as MUDBytes posts are getting, at least they haven't added header and footer frame areas like The MUD Connector.

Friday, 21 May 2010

Web development and Internet Explorer 7

For the past six weeks, I've been doing contracting work involving working on an enterprise web application using ASP.NET. In the final week of the project, one of the problems raised by the tester was that the web site did not display correctly in IE7. I first did web development back in 2000, before I moved overseas for the first time, and even then the worst part of the process was getting web pages to display in the then current versions of IE.

My coworkers mentioned tools that would allow installation of multiple concurrent versions of Internet Explorer, and the ability to switch between them. Searching for them, I stumbled across this blog post. It warned about the reliability of having multiple versions of IE installed at the same time. A quick search found the location of the current set of Virtual PC disk images, that Microsoft have made available.

I was developing in a virtual machine over Remote Desktop, using Visual Studio. The built-in ASP.NET Development Server that comes with Visual Studio, does not allow remote connections. And attempts to install Virtual PC in the virtual machine so that the version of IE within it could access the local web server, which were a dodgy endeavour at best, failed anyway. So I had to install Virtual PC on my desktop machine, and find a way to allow it to access the development web server.

The simplest way would have been to tunnel ports over the Remote Desktop connection, I currently do something similar to this with OpenSSH and VNC for my home computer. But Remote Desktop does not support tunneling. However, another solution is to use Internet Information Services (IIS). IIS comes with Windows, at least since Vista as far as I know, as a feature which can be optionally turned on or off. It can concurrently serve the same web site that the development web server, with a minimal amount of configuration and pointing it at the projects web.config file.

Tuesday, 9 February 2010

Mailman-style mailing list archives

I have the posts made to several mailing lists in a variety of non-standard formats. Converting them to a standard mbox file is a matter of parsing and is a different process for each. Once I have each parsed, what I would like to do is generate Mailman-style list archives.

I've downloaded Mailman and tried to get it to take my mbox file, and output the list archives. But the process is to some degree tied to Unix-style platforms, relying on functionality that is not supported on Windows. But to a larger degree, it is tied into the quality of being a proper Mailman hosted mailing list. Even changing the code to address or work around these things is not the cleanest of processes. There must be a better way.

Any suggestions?

Have some hacky code while I am at it:


WORKING_PATH = r"D:\MailingList"
MAILMAN_PATH = os.path.join(WORKING_PATH, "mailman-2.1.13")

class MailList:
def __init__(self, basePath, fileName):
self.basePath = basePath
self.fileName = fileName
self.SetVars()

def SetVars(self):
self._internal_name = "mud-dev"
self._fullpath = "/resource/MUD-Dev/"
self.host_name = "localhost"
self.subject_prefix = "[MUD-Dev] "
self.real_name = "MUD-Dev"

def fullpath(self):
return self._fullpath

def archive_dir(self):
return self.basePath

def internal_name(self):
return self.fileName

def ArchiveFileName(self):
return os.path.join(self.basePath, self.internal_name() + ".mbox")

def GetScriptURL(self, *args, **kwargs):
return args[0]

def GetListEmail(self):
return "no-list-email"

class SuperDuperArchive(HyperArchive):
def GetArchLock(self): return 1

def DropArchLock(self): pass

def fake_symlink(src, dst):
if os.path.exists(src):
open(dst, "w").write(open(src, "r").read())

os.symlink = fake_symlink
os.link = fake_symlink
Mailman.mm_cfg.TEMPLATE_DIR = os.path.join(MAILMAN_PATH, "templates")
Mailman.mm_cfg.LIST_DATA_DIR = WORKING_PATH
Mailman.mm_cfg.PUBLIC_ARCHIVE_FILE_DIR = WORKING_PATH
Mailman.mm_cfg.PRIVATE_ARCHIVE_FILE_DIR = WORKING_PATH

mlist = MailList(os.path.join(filePath, "archives"), "mud-dev")
mlist.preferred_language = 'en'

listPath = os.path.join(Mailman.mm_cfg.LIST_DATA_DIR, mlist.internal_name())
if not os.path.exists(listPath):
os.makedirs(listPath)

class DummyClass:
def internal_name(self):
return "mud-dev"

def archive_dir(self):
return Site.get_archpath(self.internal_name())

def GetScriptURL(self, *args, **kwargs):
return args[0]

def GetListEmail(self):
return "no-list-email"

instance = DummyClass()

Mailman.MailList.MailList.InitVars.im_func(instance, "mud-dev")
for baseclass in Mailman.MailList.MailList.__bases__:
if hasattr(baseclass, 'InitVars'):
baseclass.InitVars.im_func(instance)

MailList.SetVars.im_func(instance)

listConfigPath = os.path.join(listPath, "config.pck")
if not os.path.exists(listConfigPath):
cPickle.dump(instance.__dict__, open(listConfigPath, "wb"))

archive = SuperDuperArchive(mlist)
archive.processListArch()
archive.close()
Next post: Abusing mailman on Windows

Wednesday, 23 December 2009

DOM key event confusion

There are two standard sets of events that can be used to detect key presses, the first is onkeypress and the second is both onkeyup and onkeydown. Initially I went with onkeypress for my terminal, just to keep it simple.

When you are wanting to receive presses of what are called special keys, using onkeypress becomes a problem. In Firefox you can intercept the pressing of the backspace key, but in Internet Explorer this event never receives it and instead the browser goes back to the previously viewed page. Other keys that onkeypress does not handle in IE, include the cursor keys.

In order to work around this, I switched to onkeyup and onkeydown. Now all key presses, normal or special, work perfectly well in both browsers. However, there is another problem. All keys received are upper case characters, and various keys on the keyboard give symbols rather than their expected characters. The ',' key for instance gives the '¼' character.

Typing the following:

This is a test..,,,.M,,,,
Gives the following output:
THIS IS A TEST¾¾¼¼¼¾M¼¼¼¼
I made countless web searches trying to work out why this was. I even tried manually searching around Stack Overflow. I found web pages where someone was asking how to make the character received from onkeydown upper case, which implied that the problem was on my end. I was pretty much resigned to the existing behaviour, and was planning to use onkeypress to get the entered characters and onkeydown/onkeyup to get the special key presses. However it seemed like a hack, to work around the fact I didn't know what the problem was.

A last ditch question in ##javascript on the FreeNode IRC server yielded the following information:
[16:59] <deltab> chglpnts: keydown and keyup are lower-level, just identifying which *keys* have been pressed; keypress comes later, after the specific combination of keys has been converted into a character
[17:00] <chglpnts> deltab: Ah, I wondered if that was the case, but could find no references. How do I convert for instance 1/4 into , or is the standard way to use keydown/keyup for special keys but still use keypress for normal ones?
[17:00] <deltab> chglpnts: use both
I wonder where this this difference is clearly stated. Searching w3.org for onkeydown does not yield any relevant pages. It is stated in the Detecting keystrokes article on the quirksmode web site, although I was unable to distinguish the meaning until I was already aware of it.

In any case, I am now back on track..

CSS attribute matching and browsers

The goal is to have a blinking cursor. One approach I tried out, was through the use of attribute selectors, where making the cursor visible or not would be a matter of changing an attribute value.

Here's the HTML element:

<font id="cursor" class="cursor" shown="yes">
And the javascript to change the custom "shown" attribute:
if (cursorElement.getAttribute("shown") == "yes")
cursorElement.setAttribute("shown", "no")
else
cursorElement.setAttribute("shown", "yes")
With the following style sheet entries:
.cursor[shown="yes"] {
color: #7799CC;
background-color: rgb(241,128,22);
}

.cursor[shown="no"] {
color: white;
background-color: #7799CC;
}
This works perfectly in Firefox 3.5.6, but Internet Explorer 8.0 completely refuses to recognise the custom attribute. I'm not sure if there is a way in which this can be made to work in IE without defining custom DTD entries, but for now, I've gone back to an approach where I change the element class to make it blink.