forked from asteriskman7/dldtg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dld-min.js
1 lines (1 loc) · 17.4 KB
/
dld-min.js
1
"use strict";var dld={dld_version:"ver_20140506.1",state:{},curTab:"Docs",lastTick:undefined,moving:undefined,init:function(){console.log("dld.init");document.getElementById("div_unsupported").style.display="none";dld.loadDefaultState();dld.loadFromLocalStorage();cycle_sim.initialize();cycle_sim.logElement=document.getElementById("textarea_design_log");cycle_sim.testFinished=dld.testFinished;document.getElementById("button_popup_ok").onclick=dld.popupOk;document.getElementById("button_tab_docs").onclick=function(){dld.changeTab("docs",true)};document.getElementById("button_tab_mine").onclick=function(){dld.changeTab("mine",true)};document.getElementById("button_tab_design").onclick=function(){dld.changeTab("design",true)};document.getElementById("button_tab_business").onclick=function(){dld.changeTab("business",true)};document.getElementById("button_tab_upgrades").onclick=function(){dld.changeTab("upgrades",true)};document.getElementById("button_tab_info").onclick=function(){dld.changeTab("info",true)};dld.changeTab("docs",false);document.getElementById("select_docs").onchange=dld.changeDoc;dld.showDoc("Introduction");dld.changeDoc(true);dld.initPopupButtons();document.getElementById("button_mine").onclick=dld.mineClick;document.getElementById("button_mine").onkeypress=function(a){a.preventDefault()};document.getElementById("button_design_init").onclick=function(){cycle_sim.reset("textarea_design_netlist")};document.getElementById("button_design_run").onclick=cycle_sim.simRun;document.getElementById("button_design_pause").onclick=cycle_sim.simPause;document.getElementById("button_design_step").onclick=cycle_sim.simTick;document.getElementById("button_design_speed+").onclick=function(){cycle_sim.changeSimSpeed("+")};document.getElementById("button_design_speed-").onclick=function(){cycle_sim.changeSimSpeed("-")};document.getElementById("button_design_load_test").onclick=dld.loadTest;document.getElementById("button_design_load_netlist").onclick=dld.loadNetlistFile;document.getElementById("button_design_clear_netlist").onclick=dld.clearNetlist;document.getElementById("checkbox_design_graphs").onchange=function(){cycle_sim.graphsEnabled=this.checked;ga("send","event","design","enable_graphs",this.checked.toString())};document.getElementById("span_design_rDesigns").innerHTML=dld.designListToSelectHTML();document.getElementById("select_design_rDesigns").onchange=dld.loadDesignDesc;document.getElementById("textarea_design_netlist").addEventListener("keydown",dld.netlistKeyHandler,false);dld.initBusinessBlueprints();dld.initBusinessCompletedDesigns();dld.initUpgrades();document.getElementById("button_info_save").onclick=dld.saveClick;document.getElementById("button_info_reset").onclick=dld.reset;setInterval(dld.tick,300);setInterval(dld.saveToLocalStorage,30000);if(dld.state.firstVisit){dld.state.firstVisit=false;dld.showPopup("Welcome! Please read the introduction document to get started and then proceed to Tutorial 1. ")}ga("send","event","init","PASS")},loadDefaultState:function(){dld.state={si:0,autoMineThousandths:0,siPerAttempt:1,siPerNand:10,autoMiners:0,autoMinerRate:0.01,saleScalingFactor:1,cash:0,rDesigns:[],cDesigns:[],upgrades:[],mineClicks:0,sellClicks:0,totalMined:0,totalSold:0,firstVisit:true}},loadFromLocalStorage:function(){var a;var b=JSON.parse(localStorage.getItem("state"));for(a in b){dld.state[a]=b[a]}},saveToLocalStorage:function(){var a=JSON.stringify(dld.state);var b=5*Math.pow(2,20);if(a.length>=b){console.log("Attempting to write "+a.length+"B to localStorage which is probably not large enough to handle it.")}localStorage.clear();localStorage.setItem("state",a)},saveClick:function(){dld.saveToLocalStorage();ga("send","event","info","save","click")},reset:function(){if(window.confirm("Reset all game data?")===true){localStorage.clear();dld.loadDefaultState();dld.init();ga("send","event","info","reset","click")}},showPopup:function(a){document.getElementById("div_popup_text").innerHTML=a;document.getElementById("div_popup").style.visibility="visible"},popupOk:function(){document.getElementById("div_popup").style.visibility="hidden"},createDialog:function(d,h,f){if(document.getElementById(d)!==null){return}ga("send","event","dialog","create",h);var a=document.getElementById("body");var g=document.createElement("div");g.classList.add("div_dialog");g.id=d;var b=document.createElement("div");var c=document.createElement("div");var e;c.classList.add("div_dialogTitle");e=h;e+='<button type="button" class="button_dialog_close" onclick="document.getElementById(\'';e+=d;e+="').parentNode.removeChild(document.getElementById('";e+=d;e+="'));\">X</button>";c.innerHTML=e;dld.moving=undefined;c.onmousedown=function(i){dld.moving=this.parentElement};c.onmouseup=function(i){dld.moving=undefined};document.onmousemove=function(i){if(dld.moving!==undefined){dld.moving.style.top=(i.pageY-20)+"px";dld.moving.style.left=(i.pageX-20)+"px"}};g.appendChild(c);b.classList.add("div_dialogContent");b.insertAdjacentHTML("beforeend",f);g.appendChild(b);a.appendChild(g)},initPopupButtons:function(){var a=document.getElementsByClassName("button_popout");var d;var c;var f;var g;var b;var e;for(d=0;d<a.length;d++){c=a.item(d);f=c.dataset.popout_div;if(f!==undefined){g=document.getElementById(f);if(g!==undefined){b=g.dataset.title;if(b!==undefined){e="div_dialog_"+f;c.onclick=(function(){var k=e;var i=b;var j=g.innerHTML;return function(){dld.createDialog(k,i,j)}}())}}}}},changeTab:function(d,c){var b;var a=document.querySelectorAll(".div_tab");for(b=0;b<a.length;b++){a.item(b).style.display="none"}document.getElementById("div_tab_"+d).style.display="block";dld.curTab=d;if(c){ga("send","event","changeTab","change",d)}},showDoc:function(a){dld.changeTab("docs",false);document.getElementById("select_docs").value=a;dld.changeDoc(true)},changeDoc:function(d){var b;var a=document.getElementById("select_docs").value;var c=document.querySelectorAll(".div_docs");for(b=0;b<c.length;b++){c.item(b).style.display="none"}if(d!==true){ga("send","event","changeDoc","show",a)}document.getElementById("div_docs_"+a).style.display="block"},designListToSelectHTML:function(){var d='<select size="10" id="select_design_rDesigns" class="select_designs">';var c;var e;var a;var b;for(e=0;e<dld_designs_display_order.length;e++){c=dld_designs_display_order[e];if(dld.state.rDesigns[c]!==undefined&&dld.state.rDesigns[c]!==null){a=dld.state.cDesigns[c];if(a===undefined||a===null){a=" (INCOMPLETE)";b=""}else{if(a===dld_designs[c].expectedNands){b=" ="}else{if(a>dld_designs[c].expectedNands){b=" >"}else{b=" < WOW"}}a=" ("+a+")"}d+='<option value="'+c+'">'+dld_designs[c].name+a+b+"</option>\n"}}d+="</select>";return d},loadDesignDesc:function(){var b=document.getElementById("select_design_rDesigns");if(b.value.length>0){var a=document.getElementById("div_design_description");a.innerHTML=dld_designs[parseInt(b.value,10)].desc}},loadTest:function(){var c=document.getElementById("select_design_rDesigns");var b=document.getElementById("textarea_design_netlist");var a;if(c.value.length>0){a=parseInt(c.value,10);b.value=dld_designs[a].netlist;ga("send","event","design","load",dld_designs[a].name)}else{cycle_sim.log("ERROR: You must select a design before you can load a test.");ga("send","event","design","load","FAIL")}},tick:function(){var d=new Date();if(dld.lastTick!==undefined){var a=d.getTime()-dld.lastTick.getTime();var c;var b;dld.state.autoMineThousandths+=Math.round(dld.state.autoMiners*dld.state.autoMinerRate*a);if(dld.state.autoMineThousandths>=10){b=dld.state.autoMineThousandths%10;c=(dld.state.autoMineThousandths-b)/1000;dld.state.autoMineThousandths=b;dld.state.si=dld.roundDecimal(dld.state.si+c,2);dld.state.totalMined=dld.roundDecimal(dld.state.totalMined+c,2)}}dld.lastTick=d;dld.updateDisplay()},roundDecimal:function(c,b){var a=Math.pow(10,b);return parseFloat((Math.round(c*a)/a).toFixed(b))},updateDisplay:function(){var a=dld.roundDecimal(dld.state.si,2).toFixed(2);var b=dld.state.cash;document.getElementById("span_si_count").innerHTML=a;document.getElementById("span_cash_count").innerHTML=b;switch(dld.curTab){case"docs":break;case"mine":document.getElementById("span_mine_si_count").innerHTML=a;document.getElementById("span_mine_skill").innerHTML=dld.state.siPerAttempt;document.getElementById("span_mine_autoMiner_count").innerHTML=dld.state.autoMiners;document.getElementById("span_mine_autoMiner_attempt_rate").innerHTML=dld.state.autoMinerRate;document.getElementById("span_mine_autoMiner_si_rate").innerHTML=dld.roundDecimal(dld.state.autoMiners*dld.state.autoMinerRate,2);break;case"design":break;case"business":dld.updateBusinessBlueprints();dld.updateBusinessCompletedDesigns();break;case"upgrades":dld.updateUpgrades();break;case"info":document.getElementById("span_info_version").innerHTML=dld.dld_version;document.getElementById("span_info_mine_clicks").innerHTML=dld.state.mineClicks;document.getElementById("span_info_sell_clicks").innerHTML=dld.state.sellClicks;document.getElementById("span_info_total_mined").innerHTML=dld.state.totalMined.toFixed(2);document.getElementById("span_info_total_sold").innerHTML=dld.state.totalSold;document.getElementById("span_info_completed_designs").innerHTML=dld.state.cDesigns.reduce(function(d,e){return d+((e!==undefined)&&(e!==null))},0);break}},mineClick:function(){dld.state.si=dld.roundDecimal(dld.state.si+dld.state.siPerAttempt,2);dld.state.mineClicks+=1;dld.state.totalMined=dld.roundDecimal(dld.state.totalMined+dld.state.siPerAttempt,2);dld.updateDisplay();if(dld.state.mineClicks<1000){if(dld.state.mineClicks%100===0){ga("send","event","mine","click","button",100)}}else{if(dld.state.mineClicks%1000===0){ga("send","event","mine","click","button",1000)}}},loadNetlistFile:function(){var c=document.getElementById("input_design_netlist");var b=c.files[0];if(b){var a=new FileReader();a.onload=function(g){var f=g.target.result;var d=document.getElementById("textarea_design_netlist");d.value=f+"\n#/\\---Loaded from "+b.name+" ---\n"+d.value};a.readAsText(b);ga("send","event","design","load_netlist","SUCCESS")}else{cycle_sim.log("ERROR: You must select a file before it can be loaded.");ga("send","event","design","load_netlist","FAIL")}},clearNetlist:function(){document.getElementById("textarea_design_netlist").value="";ga("send","event","design","clear_netlist","SUCCESS")},netlistKeyHandler:function(c){var a=9;var b;if(c.keyCode===a){b=this.selectionStart;this.value=this.value.substr(0,this.selectionStart)+"\t"+this.value.substr(this.selectionEnd,this.value.length);this.selectionStart=b+1;this.selectionEnd=b+1;c.preventDefault()}},testFinished:function(a){if(a>=0){dld.updateDesigns(a,cycle_sim.nandCount)}},updateDesigns:function(b,a){if(dld.state.rDesigns[b]!==true){cycle_sim.log("ERROR: A test passed but hasn't been purchased yet. Are you cheating??");return}if(dld.state.cDesigns[b]===undefined||dld.state.cDesigns[b]===null){dld.state.cDesigns[b]=a}else{if(dld.state.cDesigns[b]>a){dld.state.cDesigns[b]=a}}document.getElementById("span_design_rDesigns").innerHTML=dld.designListToSelectHTML();document.getElementById("select_design_rDesigns").onchange=dld.loadDesignDesc;dld.initBusinessCompletedDesigns()},initBusinessBlueprints:function(){var b;var d=document.getElementById("ul_business_research");var a="";var c;for(c=0;c<dld_designs_display_order.length;c++){b=dld_designs_display_order[c];a+='<li class="li_business_list"><span class="span_business_name">';a+=dld_designs[b].name;a+='</span><button type="button" class="button_business_purchase" id="button_business_research_';a+=b;a+='" onclick="dld.purchaseBlueprints('+b+');" >';a+="</button>Sells for $";a+=dld_designs[b].price;a+="</li>\n"}d.innerHTML=a},updateBusinessBlueprints:function(){var b;var a;var d;var e;var c;for(c=0;c<dld_designs_display_order.length;c++){b=dld_designs_display_order[c];a=document.getElementById("button_business_research_"+b);if(dld.state.rDesigns[b]!==undefined&&dld.state.rDesigns[b]!==null){d="Already purchased";e=true}else{e=dld.state.cash<dld_designs[b].cost;d="Buy for $"+dld_designs[b].cost}if(a.textContent!==d){a.textContent=d}if(a.disabled!==e){a.disabled=e}}},floatToCash:function(a){return Math.round(a)},floatToCash2:function(a){return a.toFixed(2)},initBusinessCompletedDesigns:function(){var c;var f=document.getElementById("ul_business_sale");var b="";var e;var d;var a;for(d=0;d<dld_designs_display_order.length;d++){c=dld_designs_display_order[d];a=dld.state.cDesigns[c];if(a!==undefined&&a!==null){e=a*dld.state.siPerNand;b+='<li class="li_business_list"><span class="span_business_name">';b+=dld_designs[c].name;b+='</span><button type="button" class="button_business_purchase" id="button_business_sale_';b+=c;b+='" onclick="dld.sellDesign('+c+');">Sell for $';b+=dld.floatToCash(dld_designs[c].price*dld.state.saleScalingFactor);b+='</button>Costs <span id="span_business_sale_cost_';b+=c;b+='">';b+=e;b+='</span> Si ($<span id="span_business_sale_cps_';b+=c;b+='">?</span>/Si)</li>\n'}}f.innerHTML=b},updateBusinessCompletedDesigns:function(){var d;var g;var k;var c;var h;var b;var j;var e;var a;var l;var f;for(b=0;b<dld_designs_display_order.length;b++){d=dld_designs_display_order[b];j=dld.state.cDesigns[d];if(j!==undefined&&j!==null){h=j*dld.state.siPerNand;g=document.getElementById("button_business_sale_"+d);k=document.getElementById("span_business_sale_cost_"+d);a=h>dld.state.si;e=dld.floatToCash(dld_designs[d].price*dld.state.saleScalingFactor);l="Sell for $"+e;c=document.getElementById("span_business_sale_cps_"+d);f=dld.floatToCash2(e/h);if(g.disabled!==a){g.disabled=a}if(g.textContent!==l){g.textContent=l}if(k.innerHTML!==h){k.innerHTML=h}if(c.innerHTML!==f){c.innerHTML=f}}}},purchaseBlueprints:function(a){var b=dld_designs[a].cost;if(dld.state.cash>=b){dld.state.cash-=b;dld.state.rDesigns[a]=true;dld.updateBusinessBlueprints();document.getElementById("span_design_rDesigns").innerHTML=dld.designListToSelectHTML();document.getElementById("select_design_rDesigns").onchange=dld.loadDesignDesc;ga("send","event","blueprint","buy",dld_designs[a].name)}},sellDesign:function(a){var c=dld.state.cDesigns[a]*dld.state.siPerNand;var b;if(c<=dld.state.si){dld.state.si=dld.roundDecimal(dld.state.si-c,2);b=dld.floatToCash(dld_designs[a].price*dld.state.saleScalingFactor);dld.state.totalSold+=b;dld.state.sellClicks+=1;dld.state.cash+=b;dld.updateDisplay()}},upgrades:[{initialPrice:100,desc:"Increase Si/mining attempt",stateVar:"siPerAttempt",op:"+",opVal:0.1,scalePrice:1.15,round:2,maxLevel:100},{initialPrice:1000,desc:"Increase auto-miner count",stateVar:"autoMiners",op:"+",opVal:1,scalePrice:1.15,round:0,maxLevel:100},{initialPrice:1000,desc:"Increase auto-miner attempts/s",stateVar:"autoMinerRate",op:"+",opVal:0.01,scalePrice:1.15,round:2,maxLevel:100},{initialPrice:10000,desc:"Decrease Si/NAND",stateVar:"siPerNand",op:"+",opVal:-0.2,scalePrice:1.15,round:2,maxLevel:45},{initialPrice:10000,desc:"Increase completed design sale price",stateVar:"saleScalingFactor",op:"+",opVal:0.1,scalePrice:1.15,round:2,maxLevel:100}],initUpgrades:function(){var e=document.getElementById("ul_upgrades_list");var d="";var c;var b;var a;for(c=0;c<dld.upgrades.length;c++){a=dld.state.upgrades[c];if(a===undefined||a===null){a=0}b=Math.round(dld.upgrades[c].initialPrice*Math.pow(dld.upgrades[c].scalePrice,a));d+='<li class="li_upgrades_list"><button type="button" class="button_upgrades_purchase" id="button_upgrades_purchase_';d+=c;d+='" onclick="dld.buyUpgrade(';d+=c;d+=');">Buy for $';d+=dld.floatToCash(b);d+="</button>";d+=dld.upgrades[c].desc;d+=" (";d+='<span id="span_upgrades_curVal_';d+=c;d+='"></span>';d+=" => ";d+='<span id="span_upgrades_nextVal_';d+=c;d+='"></span>';d+=")";d+="</li>\n"}e.innerHTML=d},updateUpgrades:function(){var f;var e;var b;var a;var h;var g;var d;var c;for(f=0;f<dld.upgrades.length;f++){e=document.getElementById("button_upgrades_purchase_"+f);d=document.getElementById("span_upgrades_curVal_"+f);c=document.getElementById("span_upgrades_nextVal_"+f);d.innerHTML=dld.state[dld.upgrades[f].stateVar];c.innerHTML=dld.getNextUpgradeVal(f);a=dld.state.upgrades[f];if(a===undefined||a===null){a=0}b=Math.round(dld.upgrades[f].initialPrice*Math.pow(dld.upgrades[f].scalePrice,a));if(a<dld.upgrades[f].maxLevel){h=b>dld.state.cash;g="Buy for $"+b}else{h=true;g="At max level"}if(e.disabled!==h){e.disabled=h}if(e.textContent!==g){e.textContent=g}}},getNextUpgradeVal:function(c){var a=dld.state.upgrades[c];var b;if(a===undefined||a===null){a=0}if(dld.upgrades[c].op==="+"){b=dld.state[dld.upgrades[c].stateVar]+dld.upgrades[c].opVal;return parseFloat(b.toFixed(dld.upgrades[c].round))}else{b=dld.state[dld.upgrades[c].stateVar]*dld.upgrades[c].opVal;return parseFloat(b.toFixed(dld.upgrades[c].round))}},buyUpgrade:function(b){var a=dld.state.upgrades[b];var c;if(a===undefined||a===null){a=0}c=Math.round(dld.upgrades[b].initialPrice*Math.pow(dld.upgrades[b].scalePrice,a));if((c<=dld.state.cash)&&(a<dld.upgrades[b].maxLevel)){dld.state.cash=dld.state.cash-c;dld.state.upgrades[b]=a+1;dld.state[dld.upgrades[b].stateVar]=dld.getNextUpgradeVal(b);dld.updateUpgrades();ga("send","event","upgrades","buy",b.toString())}}};var gaErrorReportsRemaining=5;window.onerror=function(e,d,a){console.log("ERROR\nMSG: "+e+"\nURL: "+d+"\nLINE: "+a);var b;if((window.dld==="undefined")||(window.dld.dld_version==="undefined")){b="ver_ERR:"}else{b=dld.dld_version+":"}var f=b+(d+":"+a).substr(-500+b.length);var c=e.substr(0,500);if(gaErrorReportsRemaining>0){ga("send","event","error",f,c);console.log('Sent as "'+f.substr(0,500)+'", "'+c.substr(0,500)+'"');gaErrorReportsRemaining-=1}else{console.log("Max GA reports reached. No further reports will be sent.")}return false};if(window.addEventListener===undefined){ga("send","event","init","FAIL")}else{window.addEventListener("load",dld.init,false)};