Syntax

From WarfishWiki

Jump to: navigation, search

Theep's notes on the APIs syntax.

Contents

[edit] Code examples

These are just snippets of code. They are not great or robust, but quick and easy. Hopefully they will be of use to you.

[edit] Requesting the data

Here is the code you would use to call it (this is in javascript, what you'd use for plug-ins).

  /*
  Gets the names of the different countries
  */
  scriptstr = '<scr' + 'ipt src=http://warfish.net/war/services/rest?gid=' + getGameId();
  scriptstr += '&_method=warfish.tables.getDetails&_format=json&_jsonfunc=processdata&sections=map,rules,board></sc';
  scriptstr += 'ript>';
  document.write(scriptstr);


You can also just copy and paste this into a webbrowser

http://warfish.net/war/services/rest?gid=56559282&_method=warfish.tables.getDetails&sections=map,rules,continents


[edit] Processing the data

Here is a short segment of code as to how to parse the return values from the APIs (this is in javascript, which is what you use for plug-ins)

  function processdata(thedata) {
    if ( thedata['stat'] != 'ok' ) {
       document.write("Error " + thedata['_content']['err']['msg']);
       return;
    }
    board = thedata['_content']['board'];
    height = board['height'];
    board = thedata['_content']['map']['_content']['color'];
    for (var j = 0; j < board.length; j++) {
        id = board[j]['id'];
        colorListB[id] = board[j]['blue'];
        colorListG[id] = board[j]['green'];
        colorListR[id] = board[j]['red'];
        }
    board = thedata['_content']['map']['_content']['territory'];
    for (var j = 0; j < board.length; j++) {
        id = board[j]['id'];
        countryList[id] = board[j]['name'];
        countryAttackStr1[id] = "0";
        countryX[id] = parseInt(board[j]['x']);
        countryY[id] = parseInt(board[j]['y']);
        }
  }


[edit] The Data Itself

This is the basic structure for the API calls. If you print the API call, this is what you get. This is just an example of calling the API for basic games. It is not a fully inclusive data dictionary, but it should help. I shortened some entries down because they are long an redundant.

[edit] Details

You can call getDetails with sections= to several things. map, rules, board are the ones I know about. You can neglect things you don't care about to make the request return faster.


[edit] rules

http://warfish.net/war/services/rest?gid=38937559&_method=warfish.tables.getDetails&sections=rules


   "rules" :  {
     "numreserves" : "0"  ,
     "uplace" : "2"  ,
     "tselect" : "2"  ,
     "numtransfers" : "3"  ,
     "numpercountry" : "65535"  ,
     "fog" : "0"  ,
     "baoplay" : "1"  }   ,


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
   <rules 
       numreserves="0" 
       keeppossession="1" 
       uplace="1" 
       allowabandon="1" 
       tselect="1" 
       numtransfers="2" 
       boottime="172800" 
       numpercountry="65535" 
       teamtransfer="1" 
       fog="0" 
       nextcardsworth="10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58" 
       baoplay="0" 
       cardsetstraded="3" 
       teamplaceunits="1" 
       cardscale="4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52" />
   </rsp>

[edit] map

http://warfish.net/war/services/rest?gid=86235552&_method=warfish.tables.getDetails&sections=map

Note - when the rule is set for maxpercountry (e.g. set in the rules), the territories will list the border defined limit. However, the game will disregard the individual one defined in "territory" using the global rule value.

  "map" :  {
     "numterritories" : "42"  ,
     "_content" :  {
        "color" :  [
           {  "blue" : "0"  ,  "green" : "0"  ,  "id" : "1"  ,  "red" : "238"  ,  "name" : "Red"  }   ,
           {  "blue" : "180"  ,  "green" : "130"  ,  "id" : "2"  ,  "red" : "70"  ,  "name" : "Blue"  }   ,
           {  "blue" : "35"  ,  "green" : "142"  ,  "id" : "3"  ,  "red" : "107"  ,  "name" : "Pine"  }   ,
           {  "blue" : "0"  ,  "green" : "238"  ,  "id" : "4"  ,  "red" : "238"  ,  "name" : "Yellow"  }   ,
           {  "blue" : "0"  ,  "green" : "198"  ,  "id" : "16"  ,  "red" : "198"  ,  "name" : "Tan"  }
           ]   ,
        "territory" :  [
           {  "name" : "Alaska"  ,  "y" : "37"  ,  "x" : "53"  ,  "textx" : "-18"  ,  "texty" : "-18"  ,  "id" : "1"  }   ,
           {  "name" : "Northwest Territory"  ,  "y" : "37"  ,  "x" : "92"  ,  "textx" : "-55"  ,  "texty" : "6"  ,  "id" : "2"  }   ,
           {  "name" : "Greenland"  ,  "y" : "37"  ,  "x" : "131"  ,  "textx" : "-33"  ,  "texty" : "-18"  ,  "id" : "3"  }   ,
           {  "name" : "Alberta"  ,  "y" : "75"  ,  "x" : "66"  ,  "textx" : "-12"  ,  "texty" : "-17"  ,  "id" : "4"  }   ,
           {  "name" : "Japan"  ,  "y" : "112"  ,  "x" : "437"  ,  "textx" : "-14"  ,  "texty" : "3"  ,  "id" : "40"  }   ,
           {  "name" : "New Guinea"  ,  "y" : "213"  ,  "x" : "438"  ,  "textx" : "-12"  ,  "texty" : "-17"  ,  "id" : "41"  }   ,
           {  "name" : "Eastern Australia"  ,  "y" : "246"  ,  "x" : "438"  ,  "textx" : "-44"  ,  "texty" : "15"  ,  "id" : "42"  }
           ]
        }   ,
     "fillednumbers" : "0"  ,
     "legendx" : "146"  ,
     "height" : "309"  ,
     "width" : "497"  ,
     "fillmode" : "1"  ,
     "circlemode" : "0"  ,
     "legendy" : "243"
     }   ,
  "continents" :  {
     "numcontinents" : "6"  ,
     "_content" :  {
        "continent" :  [
           {  "units" : "5"  ,  "cids" : "9,8,7,4,5,6,3,2,1"  ,  "id" : "1"  ,  "name" : "North America"  }   ,
           {  "units" : "2"  ,  "cids" : "12,13,11,10"  ,  "id" : "2"  ,  "name" : "South America"  }   ,
           {  "units" : "5"  ,  "cids" : "17,18,19,16,15,14,26"  ,  "id" : "3"  ,  "name" : "Europe"  }   ,
           {  "units" : "3"  ,  "cids" : "20,21,22,25,24,23"  ,  "id" : "4"  ,  "name" : "Africa"  }   ,
           {  "units" : "7"  ,  "cids" : "27,30,29,28,31,32,35,34,33,39,40,36"  ,  "id" : "5"  ,  "name" : "Asia"  }   ,
           {  "units" : "2"  ,  "cids" : "37,38,42,41"  ,  "id" : "6"  ,  "name" : "Australia"  }


<?xml version="1.0" encoding="utf-8" ?>
 <rsp stat="ok">
   <map numterritories="10" fillednumbers="0" legendx="173" height="280" width="377" fillmode="1" circlemode="0" legendy="120">
     <color blue="0" green="0" id="1" red="238" name="Red" />
     <color blue="180" green="130" id="2" red="70" name="Blue" /><color blue="35" green="142" id="3" red="107" name="Pine" />
     <color blue="0" green="238" id="4" red="238" name="Yellow" />
     <color blue="205" green="38" id="5" red="125" name="Purple" />
     <territory name="Haerbin" maxunits="-1" y="50" x="54" textx="-20" texty="0" id="1" />
     <territory name="Shan瑞桐ghai" maxunits="-1" y="50" x="100" textx="-19" texty="0" id="2" />
     <territory name="Al-Jaza’ir" maxunits="-1" y="50" x="150" textx="-13" texty="0" id="3" />
     <territory name="Hong Kong" maxunits="-1" y="100" x="72" textx="-23" texty="0" id="4" />
     <territory name="Chengdu" maxunits="-1" y="100" x="128" textx="-19" texty="0" id="5" />
     <territory name="Hangzhou" maxunits="-1" y="148" x="101" textx="-22" texty="0" id="6" />
  </map>
</rsp>

[edit] board

http://warfish.net/war/services/rest?gid=86235552&_method=warfish.tables.getDetails&sections=board

   "board" :  {
     "boardid" : "8242460"  ,
     "width" : "1000"  ,
     "_content" :  {
        "border" :  [
           {
              "a" : "1"
                "modva" : "0"
                "b" : "69"
                "modvd" : "0"
                "moddd" : "0"
                "moddaf" : "0"
                "modda" : "0"
                "modddf" : "0"  }
           {  "a" : "84"
             "modva" : "0"
             "b" : "82"
             "modvd" : "0"
             "moddd" : "0"
             "moddaf" : "0"
             "modda" : "0"
             "modddf" : "0"  }   ]   }   ,
     "height" : "608"  }   }   }
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<board boardid="7501995" width="377" height="280"> 
   <border a="1" b="2" />
   <border a="1" b="4" />
   <border a="2" b="1" />
   <border a="2" b="3" />
   <border a="2" b="4" />
   <border a="2" b="5" />
   <border a="3" b="2" />
</board>
</rsp>

[edit] continents

<?xml version="1.0" encoding="utf-8" ?> 
<rsp stat="ok">
  <continents numcontinents="3">
      <continent units="2" cids="7,9,8" id="1" name="East Place" />
      <continent units="4" cids="3,5,6,4,2,1" id="2" name="West Place" />
      <continent units="1" cids="10" id="3" name="South Pole" />
  </continents>
</rsp>

[edit] History

{
"stat" : "ok"  ,
"_content" :  {
  "movelog" :  {
     "total" : "124"
     "_content" :  {
        "m" :  [
           {  "a" : "y"  ,  "cid" : "15"  ,  "s" : "-1"  ,  "num" : "1"  ,  "t" : "1170821230"  ,  "id" : "24"  }   ,
           {  "a" : "y"  ,  "cid" : "3"  ,  "s" : "-1"  ,  "num" : "1"  ,  "t" : "1170821230"  ,  "id" : "25"  }   ,
           {  "a" : "y"  ,  "cid" : "25"  ,  "s" : "-1"  ,  "num" : "1"  ,  "t" : "1170821230"  ,  "id" : "26"  }   ,
        }   ]   }   ,
  "numreturned" : "100"  }   }   }


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<movelog total="89" numreturned="89"><m a="n" s="1" logver="3" id="0" t="1205896061" />
  <m a="j" s="1" id="1" t="1205896061" />
  <m a="j" s="0" id="2" t="1205896075" />
  <m a="o" s="0" id="3" t="1205896075" />
  <m a="o" s="1" id="4" t="1205896075" />
  <m a="s" id="5" t="1205896075" />
  <m a="y" cid="8" s="-1" num="3" t="1205896075" id="6" />
  <m a="y" cid="3" s="-1" num="3" t="1205896075" id="7" />
  <m a="y" cid="4" s="-1" num="3" t="1205896075" id="8" />
  <m a="y" cid="7" s="-1" num="3" t="1205896075" id="9" />
  <m a="z" s="0" num="9" id="10" t="1205896075" />
  <m a="z" s="1" num="9" id="11" t="1205896075" />
  <m a="t" s="0" cid="1" id="12" t="1205896075" />
  <m a="t" s="1" cid="10" id="13" t="1205896075" />
  <m a="t" s="0" cid="5" id="14" t="1205896075" />
</movelog>
</rsp>

[edit] getState

You can have 'cards','board','details','players','possibleactions' as arguments into the state request.

[edit] cards

{  "stat" : "ok"  ,
_content" :  {
  "cards" :  {
     "numdiscard" : "0"  ,  "worth" : "5"  ,
     "_content" :  {
        "player" :
          [   {  "num" : "1"  ,  "id" : "0"  }   ,
              {  "num" : "1"  ,  "id" : "1"  }   ]   }   }   ,


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <cards cardsetstraded="0" nextcardsworth="4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52" numdiscard="0" worth="4">
     <player num="2" id="1" />
     <player num="4" id="0" />
     <self numW="0" numA="1" numB="1" numC="0" />
  </cards>
</rsp>

[edit] players

http://warfish.net/war/services/rest?gid=56559282&_method=warfish.tables.getState&sections=players

the <self> XML code provides two useful pieces of information. First, the user id (player id) or seat number of the person making the request. It also gives the state (how far in their turn they are). The code for <self state=?> is as follows:

State Done = -1
State Your turn = 0
State Unit Placement = 1
State Attack = 2
State Free-transfer = 3
State Transfer = 4


  "players" :  {
     "_content" :  {
        "player" :  [
           {  "units" : "11"  ,  "profileid" : "junk20070207040547"  ,  "colorid" : "16"  ,  "id" : "0"  ,  "name" : "DeathDealer"  }   ,
           {  "units" : "15"  ,  "profileid" : "epal20061127005828"  ,  "colorid" : "4"  ,  "id" : "1"  ,  "name" : "Theep"  }   ]   }   }


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
   <players>
       <player name="Junk4" colorid="4" teamid="-1" units="0" profileid="junk20070209161519" id="0" />
       <player name="Theep" colorid="1" teamid="-1" units="0" profileid="epal20061127005828" id="1" />
       <self playerid="1" state="-1" />
   </players>
</rsp>

The new additions, added 20 Jun 2008.

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
   <players>
      <player name="Theep" colorid="1" isturn="0" active="1" teamid="-1" units="0" profileid="epal20061127005828" id="0" />
      <player name="Bob" colorid="2" isturn="1" numfort="0" numattack="9" active="1" teamid="-1" units="0" profileid="bot320070212043546" id="1" />
      <self playerid="1" state="2" />
   </players>
</rsp>

[edit] board

This basically tells you who owns what and how many units they have

 "board" :  {
     "_content" :  {
        "area" :  [
           {  "playerid" : "1"  ,  "units" : "1"  ,  "id" : "1"  }   ,
           {  "playerid" : "1"  ,  "units" : "1"  ,  "id" : "2"  }   ,
           {  "playerid" : "-1"  ,  "units" : "1"  ,  "id" : "3"  }   ,
           {  "playerid" : "-1"  ,  "units" : "1"  ,  "id" : "4"  }   ,
           {  "playerid" : "1"  ,  "units" : "1"  ,  "id" : "84"  }
 ]   }   }   }   }


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
   <board>
      <area playerid="0" units="1" id="1" />
      <area playerid="1" units="1" id="2" />
      <area playerid="-1" units="3" id="3" />
      <area playerid="0" units="3" id="4" />
      <area playerid="1" units="2" id="5" />
      <area playerid="1" units="1" id="6" />
      <area playerid="-1" units="3" id="7" />
      <area playerid="-1" units="3" id="8" />
      <area playerid="0" units="5" id="9" />
      <area playerid="1" units="1" id="10" />
   </board>
</rsp>

[edit] possibleactions

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <possibleactions>
     <action id="attack" />
     <action id="transfer" />
     <action id="placeunits" />
     <action id="usecards" />
     <action id="endturn" />
  </possibleactions>
</rsp>

[edit] details

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
    <details stage="4" />
</rsp>

[edit] XML

This is the data in xml format


<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<rules numreserves="0" uplace="1" tselect="1" numtransfers="1" numpercountry="65535" fog="0" baoplay="0" />
<map numterritories="42" fillednumbers="0" legendx="146" height="309" width="497" fillmode="1" circlemode="0" legendy="243">
<color blue="0" green="0" id="1" red="238" name="Red" />
<color blue="180" green="130" id="2" red="70" name="Blue" /> 
<color blue="0" green="198" id="16" red="198" name="Tan" />
<territory name="Alaska" y="37" x="53" textx="-18" texty="-18" id="1" />
<territory name="Northwest Territory" y="37" x="92" textx="-55" texty="6" id="2" />
<territory name="Eastern Australia" y="246" x="438" textx="-44" texty="15" id="42" />
</map>
<continents numcontinents="6">
<continent units="5" cids="9,8,7,4,5,6,3,2,1" id="1" name="North America" />
<continent units="2" cids="12,13,11,10" id="2" name="South America" />
<continent units="5" cids="17,18,19,16,15,14,26" id="3" name="Europe" />
<continent units="3" cids="20,21,22,25,24,23" id="4" name="Africa" />
<continent units="7" cids="27,30,29,28,31,32,35,34,33,39,40,36" id="5" name="Asia" />
<continent units="2" cids="37,38,42,41" id="6" name="Australia" />
</continents>
</rsp>

http://warfish.net/war/services/rest?gid=56559282&_method=warfish.tables.getDetails&sections=map,rules,continents

Personal tools