Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Arimaa shortcuts
- // @namespace harvestsnow.arimaa
- // @description Keyboard shortcuts for the arimaa game window
- // @match http://arimaa.com/arimaa/java/ys/ms4/v5/*
- // @match http://arimaa.com/arimaa/gameroom/opengamewin.cgi?*
- // @match http://arimaa.com/arimaa/games/jsShowGame.cgi?*
- // @grant none
- // ==/UserScript==
- function userscript_main()
- {
- // keyboard codes
- var SPACE = 32,
- TAB = 9,
- LEFTARROW = 37,
- UPARROW = 38,
- RIGHTARROW = 39,
- DOWNARROW = 40,
- PAGEUP = 33,
- PAGEDOWN = 34,
- ENTER = 13,
- BACKSPACE = 8,
- F8 = 119;
- // let's not pollute the environment too much
- window.us_env = new Object();
- // default options:
- // set to true to disable the sounds
- var BE_MUTE = false;
- // set to true to control the pieces with the WASD keys
- var WASD_MOVES = false;
- // correspondance key->function
- var shortcuts = {};
- shortcuts[SPACE] = arimaa.sendMove;
- shortcuts[UPARROW] = arimaa.showPrevMove;
- shortcuts[DOWNARROW]= arimaa.showNextMove;
- shortcuts[PAGEUP] = arimaa.showFirstMove;
- shortcuts[PAGEDOWN] = arimaa.showLastMove;
- shortcuts[ENTER] = arimaa.showMoveSlow;
- shortcuts[BACKSPACE] = arimaa.eventUndoMove;
- shortcuts[ord("U")] = arimaa.eventUndoStep;
- shortcuts[ord("R")] = arimaa.eventRedoStep;
- shortcuts[ord("P")] = arimaa.openPlanWindow;
- shortcuts[ord("M")] = arimaa.openMoveListWindow;
- shortcuts[ord("G")] = arimaa.goToMove;
- shortcuts[ord("A")] = arimaa.changeAnim;
- //shortcuts[ord("L")] = switchWL;
- shortcuts[ord("1")] = setup99of9cats;
- shortcuts[ord("2")] = setup99of9dogs;
- shortcuts[ord("3")] = setupBrowni3141;
- shortcuts[ord("4")] = setupFritzlein;
- shortcuts[ord("5")] = setupBoo;
- shortcuts[ord("6")] = setupEHH;
- shortcuts[ord("0")] = setupRandom;
- shortcuts[TAB] = flipSetup;
- shortcuts[F8] = replayGame;
- us_env.us_shortcuts = shortcuts;
- arimaa.addButton = function(buttonId, jsCode, buttonVal)
- {
- node = document.getElementById("buttonGroupNet").parentNode.childNodes[4];
- html = '<br><input id="ID" type="button" value="VALUE" onclick="javascript:CODE" style="padding:0">';
- html = html.replace("ID", buttonId).replace("VALUE", buttonVal).replace("CODE", jsCode);
- node.innerHTML += html;
- }
- // predefined setups:
- function setup99of9cats ()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Rb1 Rc1 Dd1 De1 Rf1 Rg1 Rh1 Ra2 Hb2 Cc2 Ed2 Me2 Cf2 Hg2 Rh2",
- "1s ra8 rb8 rc8 dd8 de8 rf8 rg8 rh8 ra7 hb7 cc7 ed7 me7 cf7 hg7 rh7" );
- }
- function setup99of9dogs ()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Rb1 Rc1 Cd1 Ce1 Rf1 Rg1 Rh1 Ra2 Hb2 Dc2 Ed2 Me2 Df2 Hg2 Rh2",
- "1s ra8 rb8 rc8 cd8 ce8 rf8 rg8 rh8 ra7 hb7 dc7 ed7 me7 df7 hg7 rh7");
- }
- function setupBrowni3141()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Rb1 Cc1 Rd1 Re1 Rf1 Rg1 Rh1 Ha2 Mb2 Dc2 Ed2 De2 Cf2 Hg2 Rh2",
- "1s ha7 mb7 dc7 ed7 de7 cf7 hg7 rh7 ra8 rb8 cc8 rd8 re8 rf8 rg8 rh8");
- }
- function setupFritzlein()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Cb1 Rc1 Dd1 De1 Rf1 Cg1 Rh1 Ra2 Hb2 Rc2 Ed2 Me2 Rf2 Hg2 Rh2",
- "1s ra7 hb7 rc7 ed7 me7 rf7 hg7 rh7 ra8 cb8 rc8 dd8 de8 rf8 cg8 rh8");
- }
- function setupBoo()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Rb1 Cc1 Rd1 Re1 Rf1 Rg1 Rh1 Ha2 Hb2 Ec2 Cd2 De2 Df2 Mg2 Rh2",
- "1s ha7 hb7 ec7 cd7 de7 df7 mg7 rh7 ra8 rb8 cc8 rd8 re8 rf8 rg8 rh8");
- }
- function setupEHH()
- {
- return us_env.automateSetupMove(
- "1g Ra1 Rb1 Rc1 Dd1 Re1 Rf1 Rg1 Rh1 Ha2 Hb2 Cc2 Ed2 De2 Rf2 Mg2 Rh2",
- "1s ha7 hb7 cc7 ed7 de7 rf7 mg7 rh7 ra8 rb8 rc8 rd8 de8 rf8 rg8 rh8");
- }
- function setupRandom()
- {
- var squares = us_env.getHomeSquares();
- for(var i=0;i<16;i++)
- {
- var j = i + (Math.floor(Math.random()*(16-i)));
- if(i!=j)
- {
- var id1 = us_env.nameToId(squares[i]);
- var id2 = us_env.nameToId(squares[j]);
- document.getElementById(id1).click();
- document.getElementById(id2).click();
- }
- }
- }
- // horizontally flip the setup
- function flipSetup()
- {
- var homeSquares = us_env.getHomeSquares();
- for(var i=0;i<16;i++)
- if(i%8<4)
- {
- var id1 = us_env.nameToId(homeSquares[i]);
- var id2 = us_env.nameToId(homeSquares[i^7]);
- document.getElementById(id1).click();
- document.getElementById(id2).click();
- }
- }
- //////////////////////////
- if(BE_MUTE)
- {
- function mute()
- {
- arimaa.changeMute();
- var b = document.getElementById("buttonM");
- if((b==null) || (b.value!="<)) off"))
- {
- setTimeout(mute,300);
- }
- }
- mute();
- }
- function ord(letter)
- {
- return letter.charCodeAt(0);
- }
- arimaa.getMoveIndex = function(move)
- {
- var moveregex = /^(\d+)([gswb]?)/;
- var match = moveregex.exec(move);
- if(match)
- {
- var moveindex = (parseInt(match[1])-1)*2;
- if((match[2]=="s") || (match[2]=="b"))
- {
- moveindex++;
- }
- return moveindex;
- }
- }
- arimaa.getMoveName = function(n)
- {
- return "" + ((n>>1)+1) + "gs"[n&1];
- }
- arimaa.goToMove = function()
- {
- var move = prompt("Go before move:");
- var moveIndex = arimaa.getMoveIndex(move);
- if(moveIndex)
- arimaa.goPosList(moveIndex);
- }
- arimaa.changeAnim = function changeAnim()
- {
- button = document.getElementById("buttonA");
- if(button.value=="Anim on")
- {
- arimaa.backup_showMoveSlowDo=arimaa.showMoveSlowDo;
- arimaa.showMoveSlowDo=arimaa.showNextMove;
- button.value = "Anim off";
- }
- else
- {
- arimaa.showMoveSlowDo = arimaa.backup_showMoveSlowDo;
- button.value = "Anim on";
- }
- }
- // unfortunately the variable that contains this is private
- us_env.getView = function getView()
- {
- var view = document.getElementById("buttonV").value;
- return view[1];
- };
- // convert the square representation (view-dependant id to standard name)
- us_env.idToName = function (squareId)
- {
- var view = us_env.getView();
- var x = parseInt(squareId[1]);
- var y = parseInt(squareId[2]);
- var row, col;
- if(view=="b"){
- x = 7-x;
- y = 7-y;
- }
- else if(view=="n")
- {
- var tmp = x;
- x = 7-y;
- y = x;
- }
- col = "abcdefgh"[x];
- row = "12345678"[y];
- return col+row;
- };
- // convert the square representation (standard name to view-dependant id)
- us_env.nameToId = function (squareName)
- {
- var view = us_env.getView()
- var x = "abcdefgh".indexOf(squareName[0]);
- var y = parseInt(squareName[1])-1;
- if(view=="b")
- {
- x = 7-x;
- y = 7-y;
- }
- else if(view=="n")
- {
- var tmp = x;
- x = y;
- y = 7-tmp;
- }
- return "s"+x+y;
- };
- ////////////////
- // Prompt moves
- ////////////////
- arimaa.promptCurrent = function()
- {
- var ml = document.getElementById("movelist");
- var index = ml.selectedIndex;
- if(index==ml.length-1)
- index-=1;
- var move = ml[index].text;
- prompt("Current move", move);
- }
- //arimaa.addButton("buttonPC", "arimaa.promptCurrent();", "Current");
- arimaa.promptRange = function()
- {
- var ml = document.getElementById("movelist");
- var range = prompt("What range, love? (first-last)");
- var rangetab = range.split("-");
- var first = arimaa.getMoveIndex(rangetab[0]) || 0;
- var last = arimaa.getMoveIndex(rangetab[1]) || (ml.length-2);
- var line = ml[first].text;
- for(var i=first+1;i<=last;i++)
- {
- line += " / " + ml[i].text;
- }
- prompt("Line "+arimaa.getMoveName(first)+"-"+arimaa.getMoveName(last)+":",
- line);
- }
- //arimaa.addButton("buttonPR", "arimaa.promptRange();", "Range");
- /////////////////
- // setups moves
- /////////////////
- // swap two pieces during the setup
- // the second argument must be an array of the previously filled squares
- us_env.automateSetupStep = function (setupStep, alreadyFilled)
- {
- var stepArray = setupStep.match( /^(\w)([a-h][1-8])$/ );
- if(!stepArray)
- return;
- var piece = stepArray[1];
- var squareName = stepArray[2];
- var squareId = us_env.nameToId(squareName);
- var squareImg = document.getElementById(squareId);
- var filename = arimaa.vars.myrole + piece.toLowerCase() + ".gif";
- // the right piece is already there?
- if(squareImg.src.indexOf(filename)!=-1)
- return squareName;
- var homeSquares = us_env.getHomeSquares();
- for(var i in homeSquares)
- {
- var squareName2 = homeSquares[i];
- if(alreadyFilled.indexOf(squareName2)!=-1)
- continue;
- var squareId2 = us_env.nameToId(squareName2);
- var squareImg2 = document.getElementById(squareId2);
- if(squareImg2.src.indexOf(filename)!=-1)
- {
- squareImg.click();
- squareImg2.click();
- return squareName;
- }
- }
- };
- us_env.automateSetupMove = function (setupMoveGold, setupMoveSilver)
- {
- if(arimaa.vars.myrole!=arimaa.vars.turn)
- return;
- if(! ( (arimaa.vars.myrole=="w" && arimaa.vars.plycount==undefined)
- || (arimaa.vars.myrole=="b") && (arimaa.vars.plycount==1) ) )
- return;
- var setupMove;
- if(setupMoveSilver==undefined)
- setupMove = setupMoveGold;
- else
- {
- if(arimaa.vars.myrole=="w")
- setupMove = setupMoveGold;
- else if(arimaa.vars.myrole=="b")
- setupMove = setupMoveSilver;
- }
- setupMove = setupMove.replace( /^\d+\w /, " ");
- var setupMoveArray = setupMove.split(" ");
- var alreadyFilled = [];
- for(var i in setupMoveArray)
- {
- var step = setupMoveArray[i];
- var result = us_env.automateSetupStep(step, alreadyFilled);
- if(result)
- {
- alreadyFilled.push(result);
- }
- }
- };
- us_env.getHomeSquares = function ()
- {
- if(arimaa.vars.myrole=="w")
- return "a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 c2 d2 e2 f2 g2 h2".split(" ");
- else if(arimaa.vars.myrole=="b")
- return "a7 b7 c7 d7 e7 f7 g7 h7 a8 b8 c8 d8 e8 f8 g8 h8".split(" ");
- else
- return;
- };
- //////////////
- // replay
- //////////////
- us_env.automateGameStep = function (step)
- {
- var stepArray = step.match( /^(\w)([a-h][1-8])([nsewx])$/ );
- var squareName = stepArray[2];
- var squareId = us_env.nameToId(squareName);
- var col = squareName[0];
- var row = squareName[1];
- var direction = stepArray[3];
- var dx=0,dy=0;
- switch(direction)
- {
- case "x": return true;
- case "n": dy=1;break;
- case "s": dy=-1;break;
- case "e": dx=1;break;
- case "w": dx=-1;break;
- }
- col = String.fromCharCode(col.charCodeAt()+dx);
- row = String.fromCharCode(row.charCodeAt()+dy);
- var squareName2 = col+row;
- if(!squareName2.match( /[a-h][1-8]/ ))
- {
- alert("invalid step: " + step);
- return false;
- }
- var squareId2 = us_env.nameToId(squareName2);
- arimaa.eventOverSquare(parseInt(squareId[1]),parseInt(squareId[2]));
- if(! document.getElementById(squareId2).src.match(/a[gs]([nsew])2\.png/))
- return false;
- document.getElementById(squareId2).click();
- return true;
- };
- us_env.automateGameMove = function(move)
- {
- if(parseInt(move)==1)
- {
- us_env.automateSetupMove(move);
- return true;
- }
- move = move.replace( /^\d+[gs] /, "");
- move = move.replace( / +$/, "");
- var stepArray = move.split(" ");
- for(i in stepArray)
- {
- if(!us_env.automateGameStep(stepArray[i]))
- {
- //alert("step "+stepArray[i] + "failed");
- arimaa.eventUndoMove();
- return false;
- }
- }
- return true;
- };
- us_env.sendNextMove = function()
- {
- if(us_env.moveIndex >= us_env.moveArray.length)
- return; // the replay is over
- if( (arimaa.vars.myrole!=arimaa.vars.turn)
- || document.getElementById("sendBtn").value!="Send" )
- {
- setTimeout(us_env.sendNextMove, 1000);
- return;
- }
- move = us_env.moveArray[us_env.moveIndex];
- if(us_env.automateGameMove(move))
- {
- us_env.moveIndex+=1;
- us_env.nbfail = 0;
- arimaa.sendMove();
- }
- else
- {
- us_env.nbfail+=1;
- if(us_env.nbfail>=5)
- {
- alert("Problem with move " + move +". Aborting.");
- return;
- }
- }
- setTimeout(us_env.sendNextMove,1000);
- return;
- };
- function replayGame(gameHistory)
- {
- if(arimaa.vars.rated!="U")
- {
- if(confirm("This game is rated. Carry on?")==false)
- return;
- }
- us_env.nbfail = 0;
- if(gameHistory==undefined)
- gameHistory = prompt("Please paste the game history:").split(/[\r\n]+/);
- //var moveregex = /\d+[gs]( [EMHDCRemhdcr][abcdefgh][12345678][nsewx]?){1,16}/g;
- // move = movelist.match(moveregex);
- if(arimaa.vars.myrole == "w")
- var moveRegex = /^\d+g( [EMHDCRemhdcr][a-h][1-8][nsewx]?){1,16}/g;
- else if(arimaa.vars.myrole == "b")
- var moveRegex = /^\d+s( [EMHDCRemhdcr][a-h][1-8][nsewx]?){1,16}/g;
- var moveArray = new Array();
- for(var line in gameHistory)
- {
- if(gameHistory[line].match(moveRegex))
- moveArray.push( gameHistory[line] );
- }
- // alert(moveArray);
- us_env.moveArray = moveArray;
- us_env.moveIndex = 0;
- us_env.sendNextMove();
- }
- ////////////////////
- // keystroke input
- ////////////////////
- window.onkeyup = function(event)
- {
- // behave normally if the focus is on the chat form
- if(document.activeElement.id=="chat")
- {
- return true;
- }
- if(event.keyCode in us_env.us_shortcuts)
- {
- us_env.us_shortcuts[event.keyCode]();
- }
- return false;
- };
- window.onkeypress = function(event)
- {
- if(document.activeElement.id=="chat")
- {
- return true;
- }
- // this will disable the space scrolling
- return false;
- };
- };
- // inject the script into the original page
- var script = document.createElement("script");
- script.textContent = "(" + userscript_main.toString() + ")();";
- document.body.appendChild(script);
- arimaa.addButtons = function(){
- arimaa.addButton("buttonA", "arimaa.changeAnim();", "Anim on");
- arimaa.addButton("buttonPC", "arimaa.promptCurrent();", "Current");
- arimaa.addButton("buttonPR", "arimaa.promptRange();", "Range");
- }
- setTimeout("arimaa.addButtons()" ,2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement