Advertisement
harveyy

arimaa shortcuts

Jan 30th, 2014
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Arimaa shortcuts
  3. // @namespace  harvestsnow.arimaa
  4. // @description Keyboard shortcuts for the arimaa game window
  5. // @match        http://arimaa.com/arimaa/java/ys/ms4/v5/*
  6. // @match        http://arimaa.com/arimaa/gameroom/opengamewin.cgi?*
  7. // @match        http://arimaa.com/arimaa/games/jsShowGame.cgi?*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. function userscript_main()
  12. {    
  13.     // keyboard codes
  14.     var SPACE = 32,
  15.         TAB = 9,
  16.         LEFTARROW = 37,
  17.         UPARROW = 38,
  18.         RIGHTARROW = 39,
  19.         DOWNARROW = 40,
  20.         PAGEUP = 33,
  21.         PAGEDOWN = 34,
  22.         ENTER = 13,
  23.         BACKSPACE = 8,
  24.         F8 = 119;
  25.    
  26.    
  27.     // let's not pollute the environment too much
  28.     window.us_env = new Object();
  29.    
  30.     // default options:
  31.     // set to true to disable the sounds
  32.     var BE_MUTE = false;
  33.     // set to true to control the pieces with the WASD keys
  34.     var WASD_MOVES = false;
  35.    
  36.     // correspondance key->function
  37.     var shortcuts = {};
  38.     shortcuts[SPACE] = arimaa.sendMove;
  39.    
  40.     shortcuts[UPARROW] = arimaa.showPrevMove;
  41.     shortcuts[DOWNARROW]= arimaa.showNextMove;
  42.     shortcuts[PAGEUP] = arimaa.showFirstMove;
  43.     shortcuts[PAGEDOWN] = arimaa.showLastMove;
  44.     shortcuts[ENTER] = arimaa.showMoveSlow;
  45.     shortcuts[BACKSPACE] = arimaa.eventUndoMove;
  46.    
  47.     shortcuts[ord("U")] = arimaa.eventUndoStep;
  48.     shortcuts[ord("R")] = arimaa.eventRedoStep;
  49.     shortcuts[ord("P")] = arimaa.openPlanWindow;
  50.     shortcuts[ord("M")] = arimaa.openMoveListWindow;
  51.     shortcuts[ord("G")] = arimaa.goToMove;
  52.     shortcuts[ord("A")] = arimaa.changeAnim;
  53.     //shortcuts[ord("L")] = switchWL;
  54.    
  55.     shortcuts[ord("1")] = setup99of9cats;
  56.     shortcuts[ord("2")] = setup99of9dogs;
  57.     shortcuts[ord("3")] = setupBrowni3141;
  58.     shortcuts[ord("4")] = setupFritzlein;
  59.     shortcuts[ord("5")] = setupBoo;
  60.     shortcuts[ord("6")] = setupEHH;
  61.     shortcuts[ord("0")] = setupRandom;
  62.     shortcuts[TAB] = flipSetup;
  63.    
  64.     shortcuts[F8] = replayGame;
  65.    
  66.     us_env.us_shortcuts = shortcuts;
  67.    
  68.    
  69.     arimaa.addButton = function(buttonId, jsCode, buttonVal)
  70.     {
  71.         node = document.getElementById("buttonGroupNet").parentNode.childNodes[4];
  72.         html =  '<br><input id="ID" type="button" value="VALUE" onclick="javascript:CODE" style="padding:0">';
  73.         html = html.replace("ID", buttonId).replace("VALUE", buttonVal).replace("CODE", jsCode);
  74.         node.innerHTML += html;
  75.     }
  76.    
  77.     //  predefined setups:
  78.     function setup99of9cats ()
  79.     {
  80.         return us_env.automateSetupMove(  
  81.             "1g Ra1 Rb1 Rc1 Dd1 De1 Rf1 Rg1 Rh1 Ra2 Hb2 Cc2 Ed2 Me2 Cf2 Hg2 Rh2",
  82.             "1s ra8 rb8 rc8 dd8 de8 rf8 rg8 rh8 ra7 hb7 cc7 ed7 me7 cf7 hg7 rh7" );
  83.     }
  84.    
  85.     function setup99of9dogs ()
  86.     {
  87.         return us_env.automateSetupMove(  
  88.             "1g Ra1 Rb1 Rc1 Cd1 Ce1 Rf1 Rg1 Rh1 Ra2 Hb2 Dc2 Ed2 Me2 Df2 Hg2 Rh2",
  89.             "1s ra8 rb8 rc8 cd8 ce8 rf8 rg8 rh8 ra7 hb7 dc7 ed7 me7 df7 hg7 rh7");
  90.     }
  91.    
  92.     function setupBrowni3141()
  93.     {
  94.         return us_env.automateSetupMove(  
  95.             "1g Ra1 Rb1 Cc1 Rd1 Re1 Rf1 Rg1 Rh1 Ha2 Mb2 Dc2 Ed2 De2 Cf2 Hg2 Rh2",
  96.             "1s ha7 mb7 dc7 ed7 de7 cf7 hg7 rh7 ra8 rb8 cc8 rd8 re8 rf8 rg8 rh8");
  97.     }
  98.    
  99.     function setupFritzlein()
  100.     {
  101.         return us_env.automateSetupMove(
  102.             "1g Ra1 Cb1 Rc1 Dd1 De1 Rf1 Cg1 Rh1 Ra2 Hb2 Rc2 Ed2 Me2 Rf2 Hg2 Rh2",
  103.             "1s ra7 hb7 rc7 ed7 me7 rf7 hg7 rh7 ra8 cb8 rc8 dd8 de8 rf8 cg8 rh8");
  104.     }
  105.    
  106.     function setupBoo()
  107.     {
  108.         return us_env.automateSetupMove(
  109.             "1g Ra1 Rb1 Cc1 Rd1 Re1 Rf1 Rg1 Rh1 Ha2 Hb2 Ec2 Cd2 De2 Df2 Mg2 Rh2",
  110.             "1s ha7 hb7 ec7 cd7 de7 df7 mg7 rh7 ra8 rb8 cc8 rd8 re8 rf8 rg8 rh8");
  111.     }
  112.    
  113.     function setupEHH()
  114.     {
  115.         return us_env.automateSetupMove(
  116.             "1g Ra1 Rb1 Rc1 Dd1 Re1 Rf1 Rg1 Rh1 Ha2 Hb2 Cc2 Ed2 De2 Rf2 Mg2 Rh2",
  117.             "1s ha7 hb7 cc7 ed7 de7 rf7 mg7 rh7 ra8 rb8 rc8 rd8 de8 rf8 rg8 rh8");
  118.     }
  119.    
  120.    
  121.     function setupRandom()
  122.     {
  123.         var squares = us_env.getHomeSquares();
  124.         for(var i=0;i<16;i++)
  125.         {
  126.             var j = i + (Math.floor(Math.random()*(16-i)));
  127.             if(i!=j)
  128.             {
  129.                 var id1 = us_env.nameToId(squares[i]);
  130.                 var id2 = us_env.nameToId(squares[j]);
  131.                
  132.                 document.getElementById(id1).click();
  133.                 document.getElementById(id2).click();
  134.             }
  135.         }
  136.     }  
  137.    
  138.     // horizontally flip the setup
  139.     function flipSetup()
  140.     {
  141.         var homeSquares = us_env.getHomeSquares();
  142.         for(var i=0;i<16;i++)
  143.             if(i%8<4)
  144.             {
  145.                 var id1 = us_env.nameToId(homeSquares[i]);
  146.                 var id2 = us_env.nameToId(homeSquares[i^7]);
  147.                 document.getElementById(id1).click();
  148.                 document.getElementById(id2).click();
  149.             }
  150.     }
  151.     //////////////////////////
  152.    
  153.    
  154.  
  155.    
  156.     if(BE_MUTE)
  157.     {
  158.         function mute()
  159.         {
  160.             arimaa.changeMute();
  161.             var b = document.getElementById("buttonM");
  162.             if((b==null) || (b.value!="<)) off"))
  163.             {
  164.                     setTimeout(mute,300);
  165.             }
  166.         }
  167.         mute();
  168.     }
  169.    
  170.    
  171.     function ord(letter)
  172.     {
  173.         return letter.charCodeAt(0);
  174.     }
  175.    
  176.     arimaa.getMoveIndex = function(move)
  177.     {
  178.         var moveregex = /^(\d+)([gswb]?)/;
  179.         var match = moveregex.exec(move);
  180.         if(match)
  181.         {
  182.             var moveindex = (parseInt(match[1])-1)*2;
  183.             if((match[2]=="s") || (match[2]=="b"))
  184.             {
  185.                 moveindex++;
  186.             }
  187.             return moveindex;
  188.         }
  189.     }
  190.     arimaa.getMoveName = function(n)
  191.     {
  192.         return "" + ((n>>1)+1) + "gs"[n&1];
  193.     }
  194.        
  195.    
  196.     arimaa.goToMove = function()
  197.     {
  198.         var move = prompt("Go before move:");
  199.         var moveIndex = arimaa.getMoveIndex(move);
  200.         if(moveIndex)
  201.             arimaa.goPosList(moveIndex);
  202.     }
  203.    
  204.    
  205.     arimaa.changeAnim = function changeAnim()
  206.     {
  207.         button = document.getElementById("buttonA");
  208.         if(button.value=="Anim on")
  209.         {
  210.             arimaa.backup_showMoveSlowDo=arimaa.showMoveSlowDo;
  211.             arimaa.showMoveSlowDo=arimaa.showNextMove;
  212.             button.value = "Anim off";
  213.         }
  214.         else
  215.         {
  216.             arimaa.showMoveSlowDo = arimaa.backup_showMoveSlowDo;
  217.             button.value = "Anim on";
  218.         }
  219.     }
  220.    
  221.     // unfortunately the variable that contains this is private
  222.     us_env.getView = function getView()
  223.     {
  224.         var view = document.getElementById("buttonV").value;
  225.         return view[1];
  226.     };
  227.    
  228.     // convert the square representation (view-dependant id to standard name)
  229.     us_env.idToName = function (squareId)
  230.     {
  231.         var view = us_env.getView();
  232.         var x = parseInt(squareId[1]);
  233.         var y = parseInt(squareId[2]);
  234.         var row, col;
  235.         if(view=="b"){
  236.             x = 7-x;
  237.             y = 7-y;
  238.         }
  239.         else if(view=="n")
  240.         {
  241.             var tmp = x;
  242.             x = 7-y;
  243.             y = x;
  244.         }
  245.         col = "abcdefgh"[x];
  246.         row = "12345678"[y];
  247.         return col+row;
  248.     };
  249.     // convert the square representation (standard name to view-dependant id)
  250.     us_env.nameToId = function (squareName)
  251.     {
  252.         var view = us_env.getView()
  253.         var x = "abcdefgh".indexOf(squareName[0]);
  254.         var y = parseInt(squareName[1])-1;
  255.         if(view=="b")
  256.         {
  257.             x = 7-x;
  258.             y = 7-y;
  259.         }
  260.         else if(view=="n")
  261.         {
  262.             var tmp = x;
  263.             x = y;
  264.             y = 7-tmp;
  265.         }
  266.         return "s"+x+y;
  267.     };
  268.    
  269.     ////////////////
  270.     // Prompt moves
  271.     ////////////////
  272.     arimaa.promptCurrent = function()
  273.     {
  274.         var ml = document.getElementById("movelist");
  275.         var index = ml.selectedIndex;
  276.         if(index==ml.length-1)
  277.            index-=1;
  278.         var move = ml[index].text;
  279.         prompt("Current move", move);
  280.     }
  281.     //arimaa.addButton("buttonPC", "arimaa.promptCurrent();", "Current");
  282.    
  283.     arimaa.promptRange = function()
  284.     {
  285.         var ml = document.getElementById("movelist");
  286.         var range = prompt("What range, love? (first-last)");
  287.         var rangetab = range.split("-");
  288.         var first = arimaa.getMoveIndex(rangetab[0]) || 0;
  289.         var last = arimaa.getMoveIndex(rangetab[1]) || (ml.length-2);
  290.         var line = ml[first].text;
  291.         for(var i=first+1;i<=last;i++)
  292.         {
  293.             line += " / " + ml[i].text;
  294.         }
  295.         prompt("Line "+arimaa.getMoveName(first)+"-"+arimaa.getMoveName(last)+":",
  296.                line);
  297.     }
  298.     //arimaa.addButton("buttonPR", "arimaa.promptRange();", "Range");
  299.    
  300.     /////////////////
  301.     // setups moves
  302.     /////////////////
  303.    
  304.    
  305.     // swap two pieces during the setup
  306.     // the second argument must be an array of the previously filled squares
  307.     us_env.automateSetupStep = function (setupStep, alreadyFilled)
  308.     {
  309.         var stepArray = setupStep.match( /^(\w)([a-h][1-8])$/ );
  310.         if(!stepArray)
  311.             return;
  312.        
  313.         var piece = stepArray[1];
  314.         var squareName = stepArray[2];
  315.         var squareId = us_env.nameToId(squareName);
  316.        
  317.         var squareImg = document.getElementById(squareId);
  318.         var filename = arimaa.vars.myrole + piece.toLowerCase() + ".gif";
  319.         // the right piece is already there?
  320.         if(squareImg.src.indexOf(filename)!=-1)
  321.             return squareName;
  322.        
  323.         var homeSquares = us_env.getHomeSquares();
  324.         for(var i in homeSquares)
  325.         {
  326.             var squareName2 = homeSquares[i];
  327.             if(alreadyFilled.indexOf(squareName2)!=-1)
  328.                 continue;
  329.             var squareId2 = us_env.nameToId(squareName2);
  330.             var squareImg2 = document.getElementById(squareId2);
  331.             if(squareImg2.src.indexOf(filename)!=-1)
  332.             {
  333.                 squareImg.click();
  334.                 squareImg2.click();
  335.                 return squareName;
  336.             }
  337.         }
  338.     };
  339.    
  340.     us_env.automateSetupMove = function (setupMoveGold, setupMoveSilver)
  341.     {
  342.         if(arimaa.vars.myrole!=arimaa.vars.turn)
  343.             return;
  344.         if(! ( (arimaa.vars.myrole=="w" && arimaa.vars.plycount==undefined)
  345.             || (arimaa.vars.myrole=="b") && (arimaa.vars.plycount==1) ) )
  346.             return;
  347.        
  348.         var setupMove;
  349.         if(setupMoveSilver==undefined)
  350.             setupMove = setupMoveGold;
  351.         else
  352.         {
  353.             if(arimaa.vars.myrole=="w")
  354.                 setupMove = setupMoveGold;
  355.             else if(arimaa.vars.myrole=="b")
  356.                 setupMove = setupMoveSilver;
  357.         }
  358.         setupMove = setupMove.replace( /^\d+\w /, " ");
  359.         var setupMoveArray = setupMove.split(" ");
  360.         var alreadyFilled = [];
  361.         for(var i in setupMoveArray)
  362.         {
  363.             var step = setupMoveArray[i];
  364.             var result = us_env.automateSetupStep(step, alreadyFilled);
  365.             if(result)
  366.             {
  367.                 alreadyFilled.push(result);
  368.             }
  369.         }
  370.     };
  371.    
  372.     us_env.getHomeSquares = function ()
  373.     {
  374.         if(arimaa.vars.myrole=="w")
  375.             return "a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 c2 d2 e2 f2 g2 h2".split(" ");
  376.         else if(arimaa.vars.myrole=="b")
  377.             return "a7 b7 c7 d7 e7 f7 g7 h7 a8 b8 c8 d8 e8 f8 g8 h8".split(" ");
  378.         else
  379.             return;
  380.     };
  381.    
  382.    
  383.    
  384.     //////////////
  385.     // replay
  386.     //////////////
  387.    
  388.     us_env.automateGameStep = function (step)
  389.     {
  390.         var stepArray = step.match( /^(\w)([a-h][1-8])([nsewx])$/ );
  391.         var squareName = stepArray[2];
  392.         var squareId = us_env.nameToId(squareName);
  393.         var col = squareName[0];
  394.         var row = squareName[1];
  395.         var direction = stepArray[3];
  396.         var dx=0,dy=0;
  397.         switch(direction)
  398.         {
  399.             case "x": return true;
  400.             case "n": dy=1;break;
  401.             case "s": dy=-1;break;
  402.             case "e": dx=1;break;
  403.             case "w": dx=-1;break;
  404.         }
  405.         col = String.fromCharCode(col.charCodeAt()+dx);
  406.         row = String.fromCharCode(row.charCodeAt()+dy);
  407.         var squareName2 = col+row;
  408.         if(!squareName2.match( /[a-h][1-8]/ ))
  409.         {
  410.             alert("invalid step: " + step);
  411.             return false;
  412.         }
  413.         var squareId2 = us_env.nameToId(squareName2);
  414.         arimaa.eventOverSquare(parseInt(squareId[1]),parseInt(squareId[2]));
  415.         if(! document.getElementById(squareId2).src.match(/a[gs]([nsew])2\.png/))
  416.             return false;
  417.         document.getElementById(squareId2).click();
  418.         return true;
  419.     };
  420.    
  421.    
  422.     us_env.automateGameMove = function(move)
  423.     {
  424.         if(parseInt(move)==1)
  425.         {
  426.             us_env.automateSetupMove(move);
  427.             return true;
  428.         }
  429.         move = move.replace( /^\d+[gs] /, "");
  430.         move = move.replace( / +$/, "");
  431.         var stepArray = move.split(" ");
  432.         for(i in stepArray)
  433.         {
  434.             if(!us_env.automateGameStep(stepArray[i]))
  435.             {
  436.                 //alert("step "+stepArray[i] + "failed");
  437.                 arimaa.eventUndoMove();
  438.                 return false;
  439.             }
  440.         }
  441.         return true;
  442.     };
  443.  
  444.    
  445.     us_env.sendNextMove = function()
  446.     {
  447.         if(us_env.moveIndex >= us_env.moveArray.length)
  448.             return; // the replay is over
  449.         if( (arimaa.vars.myrole!=arimaa.vars.turn)
  450.            || document.getElementById("sendBtn").value!="Send" )
  451.         {
  452.             setTimeout(us_env.sendNextMove, 1000);
  453.             return;
  454.         }
  455.         move = us_env.moveArray[us_env.moveIndex];
  456.         if(us_env.automateGameMove(move))
  457.         {
  458.             us_env.moveIndex+=1;
  459.             us_env.nbfail = 0;
  460.             arimaa.sendMove();
  461.         }
  462.         else
  463.         {
  464.             us_env.nbfail+=1;
  465.             if(us_env.nbfail>=5)
  466.             {
  467.                 alert("Problem with move " + move +". Aborting.");
  468.                 return;
  469.             }
  470.         }
  471.        
  472.         setTimeout(us_env.sendNextMove,1000);
  473.         return;
  474.     };
  475.    
  476.     function replayGame(gameHistory)
  477.     {
  478.         if(arimaa.vars.rated!="U")
  479.         {
  480.             if(confirm("This game is rated. Carry on?")==false)
  481.                 return;
  482.         }
  483.         us_env.nbfail = 0;
  484.         if(gameHistory==undefined)
  485.             gameHistory = prompt("Please paste the game history:").split(/[\r\n]+/);
  486.         //var moveregex = /\d+[gs]( [EMHDCRemhdcr][abcdefgh][12345678][nsewx]?){1,16}/g;
  487.         // move = movelist.match(moveregex);
  488.         if(arimaa.vars.myrole == "w")
  489.             var moveRegex = /^\d+g( [EMHDCRemhdcr][a-h][1-8][nsewx]?){1,16}/g;
  490.         else if(arimaa.vars.myrole == "b")
  491.             var moveRegex = /^\d+s( [EMHDCRemhdcr][a-h][1-8][nsewx]?){1,16}/g;
  492.         var moveArray = new Array();
  493.         for(var line in gameHistory)
  494.         {
  495.             if(gameHistory[line].match(moveRegex))
  496.                 moveArray.push( gameHistory[line] );
  497.         }
  498.         // alert(moveArray);
  499.         us_env.moveArray = moveArray;
  500.         us_env.moveIndex = 0;
  501.         us_env.sendNextMove();
  502.     }
  503.    
  504.    
  505.     ////////////////////
  506.     // keystroke input
  507.     ////////////////////
  508.    
  509.     window.onkeyup = function(event)
  510.     {
  511.         // behave normally if the focus is on the chat form
  512.         if(document.activeElement.id=="chat")
  513.         {
  514.             return true;
  515.         }
  516.        
  517.         if(event.keyCode in us_env.us_shortcuts)
  518.         {
  519.             us_env.us_shortcuts[event.keyCode]();
  520.         }
  521.         return false;
  522.     };
  523.    
  524.     window.onkeypress = function(event)
  525.     {
  526.         if(document.activeElement.id=="chat")
  527.         {
  528.             return true;
  529.         }
  530.         // this will disable the space scrolling
  531.         return false;
  532.     };
  533. };
  534.  
  535. // inject the script into the original page
  536. var script = document.createElement("script");
  537. script.textContent = "(" + userscript_main.toString() + ")();";
  538. document.body.appendChild(script);
  539.  
  540. arimaa.addButtons = function(){
  541.     arimaa.addButton("buttonA", "arimaa.changeAnim();", "Anim on");
  542.     arimaa.addButton("buttonPC", "arimaa.promptCurrent();", "Current");
  543.     arimaa.addButton("buttonPR", "arimaa.promptRange();", "Range");
  544. }
  545. setTimeout("arimaa.addButtons()" ,2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement