Light Rising Crafting Checker

Shows if any crafting tools or ingredients are missing

  1. // ==UserScript==
  2. // @name Light Rising Crafting Checker
  3. // @namespace http://userscripts.org/users/125692
  4. // @description Shows if any crafting tools or ingredients are missing
  5. // @include *lightrising.com*game.cgi
  6. // @grant none
  7. // @version 1.1
  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. craftbutton.id="CraftButton";//name so easily found later
  49. var craftselect=craftbutton.nextElementSibling.nextElementSibling
  50. craftselect.style.width='27em';//set width to be longer so can read all the things
  51.  
  52. //sort craft select
  53. /*
  54. //strip first digit if it is 1
  55. function ucfirstletter(str){
  56. return str.replace(/^1 /,"")
  57. }
  58. */
  59.  
  60. //this function from stackoverflow then modified to work here.
  61. sortSelect(craftselect);
  62.  
  63. var textline= document.createElement("div")//where we will place the results
  64. textline.id="magecomponentlist";
  65. textline.innerHTML=""
  66. textline.style.marginLeft='5px';
  67. var divwewant=craftbutton.parentNode.parentNode;
  68. divwewant.insertBefore(textline,craftbutton.parentNode.nextSibling );
  69. if (!(craftselect.selectedIndex>=0)){
  70. //textline.innerHTML="";
  71. textline.innerHTML='Select Index NAN?:'+craftselect.selectedIndex;
  72. craftselect.selectedIndex=0;
  73. //return;
  74. }
  75. //make an array as repeatedly accessing the select by index makes it fail for reasons I don't get.
  76. var craftselectarray = new Array();
  77. for(i=0;i<craftselect.length;i++){
  78. craftselectarray[i]=craftselect[i].innerHTML.match(/\((.*)\)/)[1];
  79. }
  80.  
  81. var rchange=function(e) {
  82. var craftbutton=document.getElementById("CraftButton");
  83. var textline=document.getElementById("magecomponentlist");
  84. var craftselect=e.target;
  85. if (!(craftselect.selectedIndex>=0)){
  86. //textline.innerHTML="";
  87. textline.innerHTML='Select Index NAN?:'+craftselect.selectedIndex;
  88. craftselect.selectedIndex=0;
  89. //return;
  90. }
  91. var craftselectarray = new Array();
  92. for(i=0;i<craftselect.length;i++){
  93. craftselectarray[i]=craftselect[i].innerHTML.match(/\((.*)\)/)[1];//grab whole ingredient line
  94. }
  95. var index=craftselect.selectedIndex;
  96. textline.innerHTML="index is:"+index;
  97. //textline.innerHTML+=" and:"+craftselect[index];
  98. textline.innerHTML+=craftselectarray[index]
  99. //alert("yah!");
  100. //var crafttextline=craftselect[index].innerHTML.match(/\((.*)\)/)[1];
  101. var crafttextline=craftselectarray[index];
  102. var gib=document.getElementsByClassName("gamebox invbox")[0];
  103. var listofinventory="";
  104. var inventorylines=gib.getElementsByTagName('div');
  105. for (i=0;i<inventorylines.length ;i++ ){
  106. //listofinventory=listofinventory+inventorylines[i].innerHTML+"\n";
  107. listofinventory=listofinventory+
  108. inventorylines[i].innerHTML.replace(/ x /,",").replace(/(s$|(s(?= of)))/,"").replace(/\bknives?\b/,"knife").replace(/\bstaves?\b/,"staff").replace(/\bglasses?\b/,"glass")
  109. +"\n";
  110. }
  111. //list of inventory = long list of inventory, newline delimited
  112. var components=crafttextline.split(',');//components = array of whats needed
  113. var gsb=document.getElementsByClassName("gamebox statsbox")[0];
  114. var searchtextelement=gsb.getElementsByTagName('b');
  115. var a=searchtextelement[1];//second bold thing is ap.
  116. var test=a.innerHTML.match(/(\d+)[^0-9]*(\d+)/) // extract the first 2 numbers
  117. var availableap=test[1];
  118. var outputline;
  119. var craftap=components[0].match(/\d+/);
  120. var canbuildnum;
  121. outputline="<span style='color :blue;' title='AP can go negative'> Costs: </span>";
  122. canbuildnum=Math.ceil(Number(availableap)/Number(craftap));//round up to account for ap can go negative
  123. if (Number(craftap)>Number(availableap)){
  124. //outputline+="<span style='color :red;' title='Need "+ (Number(craftap)-Number(availableap))+
  125. // " more'> "+craftap+" AP </span>";
  126. outputline+="<span style='color :red;' title='AP will go negative to "
  127. +(Number(availableap)-Number(craftap))+"!'> "+craftap+" AP </span>";
  128. craftbutton.style.color="red";
  129. }
  130. else{//can build at least one
  131. if(canbuildnum==2){//we have ap for 2(though may have low ap afterwards so orange warning
  132. outputline+="<span style='color :darkorange;' title='AP will go to "
  133. +(Number(availableap)-Number(craftap))+"!'> "+craftap+" AP </span>";
  134. craftbutton.style.color="darkorange";
  135. }
  136. else{
  137. outputline+="<span> "+craftap+" AP </span>";
  138. craftbutton.style.color="black";
  139. }
  140. }
  141. //alert(canbuildnum)
  142. var toolline=false;
  143. var ingredientline=false;
  144. for (i=1;i<components.length;i++){
  145. if (components[i].match(/^ a /)){//we have a tool requirement
  146. if(!toolline){
  147. outputline+="<span style='color :blue;' title='Tools only occasionally break'> Uses: </span>";
  148. toolline=true;
  149. }
  150. else{
  151. outputline+=","
  152. }
  153. var tool=components[i].match(/^ a (.*)/);//get tool name
  154. var toolname=tool[1].replace(/(s$|(s(?= of)))/,"");//strip plurals;
  155. var toolRE=new RegExp( "\\d+,"+toolname+"s?(\n|$)");
  156. if (listofinventory.match(toolRE)){
  157. outputline+="<span> a "+tool[1]+"</span>";
  158. }
  159. else{
  160. outputline+="<span style='color :red;' title='Need 1 more'> a "+tool[1]+"</span>";
  161. // /*debug*/ outputline+="we search for:"+toolname+ " ";
  162. canbuildnum=0;//shockhorror we can't build any!
  163. }
  164. }
  165. else{//we have an ingredient
  166. if(!ingredientline){
  167. outputline+="<span style='color :blue;' title='Ingredients are used up and removed from inventory'> Consumes: </span>";
  168. ingredientline=true;
  169. }
  170. else{
  171. outputline+=","
  172. }
  173. var componentstring=components[i].match(/(.*)/)[1];
  174. var ingredient=componentstring.match(/^ (\d+) x (.*)/);//set the 2 to i again
  175. var ingredientnum=Number(ingredient[1]);
  176. //var ingredientname=ingredient[2].replace(/(.*)(s\b|s$)(.*)/,"$1$2");//strip last s;
  177. var ingredientname=ingredient[2].replace(/(s$|(s(?= of)))/,"");//strip plurals;
  178. //var ingredientname=ingredientname.replace(/\S*\sof/,"of");//special check for "pieces of" etc
  179. var ingredientname=ingredientname.replace(/\bknives?\b/,"knife");//special check for knives
  180. var ingredientname=ingredientname.replace(/\bstaves?\b/,"staff");//special check for staves
  181. var ingredientname=ingredientname.replace(/\bglasses?\b/,"glass");
  182. var ingRE=new RegExp( "(\\d+),"+ingredientname+"s?(\n|$)");
  183. var inventoryitem=listofinventory.match(ingRE);
  184. if (inventoryitem){
  185. var inventorynum=Number(inventoryitem[1]);//how many particular ingredredient we have
  186. }
  187. //var inventoryname=
  188. if(!inventoryitem){//we have none
  189. outputline+="<span style='color :red;' title='Need "+ingredientnum+
  190. " more'> "+
  191.  
  192. ///*debug line*/"<span style='color :blue;' title="+ingredientname+">Find:"+ ingRE +
  193. ///*debug line*/":from:"+listofinventory+"</span>"+
  194.  
  195. components[i]+"</span>";
  196. canbuildnum=0;//shockhorror we can't build any!
  197. }
  198. else if ((inventoryitem)&&(ingredientnum>inventorynum)){//we don't have enough
  199. //ingredient is what we need. And is bigger than what we have.
  200. //we missing we need-we have. say need 10. we have 3 so we need 7 more
  201. outputline+="<span style='color :red;' title='Need "+
  202. (ingredientnum - inventorynum) +" more'> "+
  203. //"need"+ingredientnum+":have"+inventorynum+" "+//debugline
  204. //"finding"+ componentstring +" in "+ ingredient +" "+//debugline
  205. components[i]+"</span>";
  206. canbuildnum=0;//shockhorror we can't build any!
  207. }
  208. else{//we do have enough
  209. outputline+="<span> "+components[i]+"</span>";
  210. var enuf4=(inventorynum/ingredientnum); //make below line shorter :) lol
  211. canbuildnum=canbuildnum<enuf4?canbuildnum:enuf4;//select lowest
  212. }
  213. }
  214. }//end for
  215. if(canbuildnum>0){//if can build at least on say how many can be built
  216. canbuildnum=Math.floor(canbuildnum);//get rid of fractions
  217. outputline+= "<span style='color :green;' title='You possess sufficient resources to construct "+
  218. canbuildnum+" of these'> Enough for "+canbuildnum+"</span>";
  219. craftbutton.style.removeProperty('background-color');
  220. //craftbutton.style.setProperty('color',"black",null);
  221. }
  222. else{
  223. craftbutton.style.setProperty('background-color',"whitesmoke",null);
  224. craftbutton.style.setProperty('color',"darkgrey ",null);
  225. }
  226. textline.innerHTML="<span style='font-size:smaller;'>"+outputline+"</span>";//+" "+listofinventory;
  227. }
  228. craftselect.addEventListener("change",rchange,false);
  229. //craftselect.addEventListener("DOMAttrModified",rchange,false);
  230. craftselect.addEventListener("keypress",rchange,false);
  231.  
  232. if (craftselect.selectedIndex>0){
  233. //manuallydoit();
  234. var e = new Object();
  235. var craftbutton=document.evaluate( "//input[@value='Craft']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  236. e.target=craftbutton.nextElementSibling.nextElementSibling
  237. rchange(e);
  238. }
  239.  
  240.  
  241. //EOF
  242. })();