Plug ins

From WarfishWiki

Jump to: navigation, search

Contents

[edit] Plugins

Programming plugins are not impossible, but they are not too easy either.

Typically, one will use javascript which runs in the player's browser. There are a few gotchas.

[edit] Building Callback Routines

First, to access the warfish data, you must use a callback routine. Here is an example of the code needed to get the first 10 turns.

  scriptstr = '<scr' + 'ipt src=http://warfish.net/war/services/rest?gid=' + getGameId();
  scriptstr += '&_method=warfish.tables.getHistory&_format=json&_jsonfunc=getNum&start=1&num=10></'
  scriptstr += 'script>';
  document.write(scriptstr);

[edit] Receiving and Parsing the Data

Then you will need a function called "getNum" which must match what you put in the jsonfunc. Parsing the data is also a pain. I included an example of what it takes to break apart the function. You can get more detail in the Reference Manual, APIs section to the fields that are there.

  function getNum(thedata) {
    if ( thedata['stat'] != 'ok' ) {
       document.write("Error " + thedata['_content']['err']['msg']);
       return;
    }
  total_num = thedata['_content']['movelog']['total'];
  document.write ("There was a total of " + total_num + " moves in this game
"); b = thedata['_content']['movelog']['_content']['m']; for (var j = 0; j < b.length; j++) { if (b[j] ['a'] == 'a') document.write ("Someone attacked
"); }

[edit] Gotchas

There are two big issues that are non-intuitive.

  • If you notice, the variable scriptstr holds the command to call the warfish server and request information. For some reason, you can't have the full phrase <script> or </script> in the javascript program. Thus, you have to break the word into pieces.
  • The big issue is the scoping of variables. Normally, you can put them at the top of your code with a var, then everyone can use them. However, with the callback functions, none of them can "see" the variables defined in the main part of the javascript (maybe someone knows how to get around this). What is also interesting is all the callback functions know the assignments made by other callback functions. What this means is "total_num" is known by any of the callback functions, but not the main part of the program. It just forces you to do most of your work in the callback functions.
Personal tools