October 10, 2008

For the D&D JavaScript Code Monkeys...

OK.. this post is sorta about D&D.. well, its more about how I've been messing around with ways to make my Core Lists more useable AND so something for myself: learn a new programming tongue. My programming experience (10+ years) is about 90% with Perl. Before anyone starts carp()ing about how it sucks, or my-favorite-language is better - just understand that I fell into Perl becuase one of my fields of expertise was dominated by this language for so many years (less now than it used to be). Perl rules for text processing, and I would argue that it still does. But I digress...

Some of you may know that I have a list I maintain that couples all the existing "official" monsters from 4th Edition Dungeons & Dragons with all the Terrain and Climate data that was present in every previous edition. This list was born from my frustration with the existing 4E Monster Manual and that the designers decided that Terrain and Climate data should be stripped from the source books as unneeded fluff. OK - for some of the monsters the data is there in the Lore sections, but its not consistently present. So, I made a big list of all the monsters and research all their terrain and climate info from every previous edition. I then presented the list to rest of you - and it seems people are downloading it (from my server stats at least). But, for me.. this does not have enough kkool. So...

What I'm basically interested in eventually doing is making a series of widgets that can be dropped into any blog or webpage, that serve to dynamically link back to my Core Lists. To do this I'll need to
  1. Learn JavaScript (can't be too hard, seems simple enough)
  2. Learn how to use the Google Documents API
  3. Learn how to use JSON to pull my Google Spreadsheets data as an RSS feed.
  4. Learn how to use JavaScript to parse the data and display it on a page.
Its not that tall of an order. I have no doubt I'll be able to pull it off, but my weaknesses are in layout and design. I may be calling for some CSS guru's to climb on board with me to build a sweet set of widgets for the community to use, but in the meantime I'll work on getting the data out and clobbered so that it behaves well.

Two hours of messing around with JavaScript for the first time, and reading through several Google API documents and I was able to come up with the following SIMPLE static script result that spits out the Rituals Core List as a unordering list (I'll add interactivity later)

Here's the silly little scriptlet code i came up with:
<script>
function listRows(root) {
 var feed = root.feed;
 var entries = feed.entry || [];
 var html = ['<ul>'];
 for (var i = 0; i < entries.length; ++i) {
  var entry = entries[i];
  var title = entry.title.$t;
  var content = entry.content.$t.replace(/(,.*?:)/gi,
',&nbsp;');
  content = content.replace(/^.*?:/,"");    
  html.push('<li>', title, ' ', content, '</li>');
 }
 html.push('</ul>');
 document.getElementById("TCMdata").innerHTML = html.join("");
}
</script>
<hr/>
<h3>20 Rituals in 4E</h3>
<div id="TCMdata"></div>
<script
src="http://spreadsheets.google.com/feeds/list/p6th3bqNYPflsFyxmZsAEcw/od6/public/basic?alt=json-in-script&callback=listRows"></script>

And here's what it does... i know.. it looks like I typed it out, but this is what it looks like once rendered:

http://spreadsheets.google.com/feeds/list/p6th3bqNYPflsFyxmZsAEcw/od6/public/basic

Rituals in 4E



What is significant, to me, is that in one evening I've figured out how to make calls to my Google Spreadsheets from inside a JavaScript without anything too too fancy. In short, I learned something - and that is a good thing.

I'll keep you all posted on how this little project progresses.

2 comments:

  1. Let me be the first to offer encouragement and praise!

    I sometimes sit down and want to start programming some game related stuff, but after coding all day at the office, I just want to do something else when I get home.

    Keep up the outstanding work!

    ReplyDelete
  2. And when I think about programming something game related, and want to slit my wrists because of how boring I find programming :)

    But I'm always happy to have other people program useful stuff that I can use!

    ReplyDelete

By submitting your comment below, you agree to the blog's Terms of Service.