Shintolin Crafting Checker

Shows if any crafting tools or ingredients are missing

  1. // ==UserScript==
  2. // @name Shintolin Crafting Checker
  3. // @namespace http://userscripts.org/users/125692
  4. // @description Shows if any crafting tools or ingredients are missing
  5. // @include http://www.shintolin.co.uk/game.cgi
  6. // @include *shintolin.co*game.cgi
  7. // @version 0.0.1.20140406170308
  8. // ==/UserScript==
  9. (function() {
  10.  
  11. function ucfirstletter(str){
  12. return str.replace(/\b[A-Za-z]/,function($0) { return $0.toUpperCase(); })//capitalize first word come across
  13. }
  14. function sortSelect(selElem) {
  15. var tmpAry = new Array();
  16. var sellength=selElem.options.length;
  17. for (var i=0;i<sellength ;i++) {
  18. tmpAry[i] = new Array();
  19. tmpAry[i][0] = ucfirstletter(selElem.options[i].text).replace(/^1 /,"");//Capitalize & strip leading 1
  20. tmpAry[i][1] = selElem.options[i].value;
  21. tmpAry[i][2] = selElem.options[i].selected;
  22. }
  23. tmpAry.sort(function (a,b){//this needed to ignore case and leading numbers
  24. var a=a[0].match(/([A-Za-z]+)/)[1].toUpperCase();
  25. var b=b[0].match(/([A-Za-z]+)/)[1].toUpperCase();
  26. return a<b?-1:b<a?1:0;
  27. });
  28. //while (craftselect.options.length > 0) {
  29. // craftselect.options[0] = null;
  30. //}
  31. //for (var i=sellength-1; i>-1;i--){
  32. // selElem.options[i]=null;
  33. //}
  34. //the abouve null code doesn;t work in practice for some unknown reason. works in shell though :(?
  35.  
  36. for (var i=0;i<tmpAry.length;i++) {
  37. //var op = new Option(tmpAry[i][0], tmpAry[i][1]);
  38. //selElem.options[i] = op;
  39. selElem.options[i].text=tmpAry[i][0];
  40. selElem.options[i].value=tmpAry[i][1];
  41. selElem.options[i].selected=tmpAry[i][2];
  42. }
  43. return;
  44. }
  45.  
  46. //now alter width of crafting dropdown
  47. var craftbutton=document.evaluate( "//input[@value='Craft']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  48. //var craftselect=craftbutton.nextElementSibling.nextElementSibling
  49. var craftselect=craftbutton.parentNode.getElementsByTagName("Select")[0];
  50. craftselect.style.width='20em';//set width to be longer so can read all the things
  51.  
  52. //sort the select
  53. sortSelect(craftselect);
  54.  
  55. var textline= document.createElement("div")//where we will place the results
  56. textline.id="magecomponentlist";
  57. textline.innerHTML=""
  58. textline.style.marginLeft='5px';
  59. var divwewant=craftbutton.parentNode.parentNode;
  60. divwewant.insertBefore(textline,craftbutton.parentNode.nextSibling );
  61. if (!(craftselect.selectedIndex>=0)){
  62. //textline.innerHTML="";
  63. textline.innerHTML='Select Index NAN?:'+craftselect.selectedIndex;
  64. craftselect.selectedIndex=0;
  65. //return;
  66. }
  67. //make an array as repeatedly accessing the select by index makes it fail for reasons I don't get.
  68. var craftselectarray = new Array();
  69. for(i=0;i<craftselect.length;i++){
  70. craftselectarray[i]=craftselect[i].innerHTML.match(/\((.*)\)/)[1];
  71. }
  72.  
  73. var rchange=function(e) {
  74. var textline=document.getElementById("magecomponentlist");
  75. var craftselect=e.target;
  76. if (!(craftselect.selectedIndex>=0)){
  77. //textline.innerHTML="";
  78. textline.innerHTML='Select Index NAN?:'+craftselect.selectedIndex;
  79. craftselect.selectedIndex=0;
  80. //return;
  81. }
  82. var craftselectarray = new Array();
  83. for(i=0;i<craftselect.length;i++){
  84. craftselectarray[i]=craftselect[i].innerHTML.match(/\((.*)\)/)[1];//grab whole ingredient line
  85. }
  86. var index=craftselect.selectedIndex;
  87. textline.innerHTML="index is:"+index;
  88. //textline.innerHTML+=" and:"+craftselect[index];
  89. textline.innerHTML+=craftselectarray[index]
  90. //alert("yah!");
  91. //var crafttextline=craftselect[index].innerHTML.match(/\((.*)\)/)[1];
  92. var crafttextline=craftselectarray[index];
  93. var gib=document.getElementsByClassName("gamebox invbox")[0];
  94. var listofinventory="";
  95. var inventorylines=gib.getElementsByTagName('div');
  96. for (i=0;i<inventorylines.length ;i++ ){
  97. //listofinventory=listofinventory+inventorylines[i].innerHTML+"\n";
  98. var inventorytext=inventorylines[i].innerHTML.replace(/ x /,",").replace(/(s$|(s(?= of)))/,"").replace(/\bknives?\b/,"knife").replace(/\bstaves?\b/,"staff")
  99. +"\n";
  100. //alert(inventorytext);
  101. listofinventory=listofinventory+inventorytext;
  102. }
  103. //list of inventory = long list of inventory, newline delimited
  104. var components=crafttextline.split(',');//components = array of whats needed
  105. var gsb=document.getElementsByClassName("gamebox statsbox")[0];
  106. var searchtextelement=gsb.getElementsByTagName('b');
  107. var a=searchtextelement[1];//second bold thing is ap.
  108. var test=a.innerHTML.match(/(\d+)[^0-9]*(\d+)/) // extract the first 2 numbers
  109. var availableap=test[1];
  110. var outputline;
  111. var craftap=components[0].match(/\d+/);
  112. var canbuildnum;
  113. outputline="<span style='color :blue;' title='AP can go negative'> Costs: </span>";
  114. if (Number(craftap)>Number(availableap)){
  115. //outputline+="<span style='color :red;' title='Need "+ (Number(craftap)-Number(availableap))+
  116. // " more'> "+craftap+" AP </span>";
  117. outputline+="<span style='color :red;' title='AP will go negative to "
  118. +(Number(availableap)-Number(craftap))+"!'> "+craftap+" AP </span>";
  119. }
  120. else{
  121. outputline+="<span> "+craftap+" AP </span>";
  122. }
  123. canbuildnum=Math.ceil(Number(availableap)/Number(craftap));//round up to account for ap can go negative
  124. //alert(canbuildnum)
  125. var toolline=false;
  126. var ingredientline=false;
  127. for (i=1;i<components.length;i++){
  128. if (components[i].match(/^ a /)){//we have a tool requirement
  129. if(!toolline){
  130. outputline+="<span style='color :blue;' title='Tools only occasionally break'> Uses: </span>";
  131. toolline=true;
  132. }
  133. else{
  134. outputline+=","
  135. }
  136. var tool=components[i].match(/^ a (.*)/);//get tool name
  137. var toolname=tool[1].replace(/(s$|(s(?= of)))/,"");//strip plurals;
  138. var toolRE=new RegExp( "(\\d+),"+toolname+"s?(\n|$)");
  139. if (listofinventory.match(toolRE)){
  140. outputline+="<span title='Have "+listofinventory.match(toolRE)[1] +"'> a "+tool[1]+"</span>";
  141. }
  142. else{
  143. outputline+="<span style='color :red;' title='Need 1 more'> a "+tool[1]+"</span>";
  144. // /*debug*/ outputline+="we search for:"+toolname+ " ";
  145. canbuildnum=0;//shockhorror we can't build any!
  146. }
  147. }
  148. else{//we have an ingredient
  149. if(!ingredientline){
  150. outputline+="<span style='color :blue;' title='Ingredients are used up and removed from inventory'> Consumes: </span>";
  151. ingredientline=true;
  152. }
  153. else{
  154. outputline+=","
  155. }
  156. var componentstring=components[i].match(/(.*)/)[1];
  157. var ingredient=componentstring.match(/^ (\d+) x (.*)/);//set the 2 to i again
  158. var ingredientnum=Number(ingredient[1]);
  159. //var ingredientname=ingredient[2].replace(/(.*)(s\b|s$)(.*)/,"$1$2");//strip last s;
  160. var ingredientname=ingredient[2].replace(/(s$|(s(?= of)))/,"");//strip plurals;
  161. //var ingredientname=ingredientname.replace(/\S*\sof/,"of");//special check for "pieces of" etc
  162. var ingredientname=ingredientname.replace(/\bknives?\b/,"knife");//special check for knives
  163. var ingredientname=ingredientname.replace(/\bstaves?\b/,"staff");//special check for staves
  164. var ingRE=new RegExp( "(\\d+),"+ingredientname+"s?(\n|$)");
  165. var inventoryitem=listofinventory.match(ingRE);
  166. if (inventoryitem){
  167. var inventorynum=Number(inventoryitem[1]);//how many particular ingredredient we have
  168. }
  169. //var inventoryname=
  170. if(!inventoryitem){//we have none
  171. outputline+="<span style='color :red;' title='Need "+ingredientnum+
  172. " more'> "+
  173.  
  174. ///*debug line*/"<span style='color :blue;' title="+ingredientname+">Find:"+ ingRE +
  175. ///*debug line*/":from:"+listofinventory+"</span>"+
  176.  
  177. components[i]+"</span>";
  178. canbuildnum=0;//shockhorror we can't build any!
  179. }
  180. else if ((inventoryitem)&&(ingredientnum>inventorynum)){//we don't have enough
  181. //ingredient is what we need. And is bigger than what we have.
  182. //we missing we need-we have. say need 10. we have 3 so we need 7 more
  183. outputline+="<span style='color :red;' title='Need "+
  184. (ingredientnum - inventorynum) +" more'> "+
  185. //"need"+ingredientnum+":have"+inventorynum+" "+//debugline
  186. //"finding"+ componentstring +" in "+ ingredient +" "+//debugline
  187. components[i]+"</span>";
  188. canbuildnum=0;//shockhorror we can't build any!
  189. }
  190. else{//we do have enough
  191. outputline+="<span title='Have "+ inventorynum +"' > "+components[i]+"</span>";
  192. var enuf4=(inventorynum/ingredientnum); //make below line shorter :) lol
  193. canbuildnum=canbuildnum<enuf4?canbuildnum:enuf4;//select lowest
  194. }
  195. }
  196. }//end for
  197. if(canbuildnum>0){//if can build at least on say how many can be built
  198. canbuildnum=Math.floor(canbuildnum);//get rid of fractions
  199. outputline+= "<span style='color :green;' title='You possess sufficient resources to construct "+
  200. canbuildnum+" of these'> Enough for "+canbuildnum+"</span>";
  201. }
  202. textline.innerHTML="<span style='font-size:smaller;'>"+outputline+"</span>";//+" "+listofinventory;
  203. }
  204. craftselect.addEventListener("change",rchange,false);
  205. craftselect.addEventListener("DOMAttrModified",rchange,false);
  206. craftselect.addEventListener("keypress",rchange,false);
  207.  
  208. if (craftselect.selectedIndex>0){
  209. //manuallydoit();
  210. var e = new Object();
  211. var craftbutton=document.evaluate( "//input[@value='Craft']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  212. //e.target=craftbutton.nextElementSibling.nextElementSibling
  213. e.target=craftbutton.parentNode.getElementsByTagName("Select")[0];
  214. rchange(e);
  215. }
  216.  
  217.  
  218. //EOF
  219. })();