Skip to content

Instantly share code, notes, and snippets.

@davidcorry
Last active August 29, 2015 14:06
Show Gist options
  • Save davidcorry/f24f591519991387924a to your computer and use it in GitHub Desktop.
Save davidcorry/f24f591519991387924a to your computer and use it in GitHub Desktop.
// Copy and paste this entire page into the console to add information about the raw cost of each building.
// If you'd like to stop it, paste this into the console: rawCost.disconnect();
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var rawCost = new MutationObserver(function(mutations) {
var building = gamePage.selectedBuilding;
if (building) {
var windowConsole = window.console;
try {
window.console = {
log: function() {},
warn: function() {},
error: function() {}
};
var craftRatio = gamePage.bld.getEffect("craftRatio");
function combine(foo, bar) {
for (var item in bar) {
if(!foo[item]) {
foo[item] = 0;
}
foo[item] += bar[item];
}
}
function decraftResource(name, amount, remaining, depth) {
var prices = new Object(),
resource = gamePage.workshop.getCraft(name),
decraftManuscript = true,
decraftable = (resource && name != "wood" && decraftManuscript);
if (name == "manuscript" && gamePage.workshop.get("printingPress").researched && gamePage.bld.get("steamworks").on > 0) {
decraftManuscript = false;
}
if ((!decraftable || (remaining && depth > 1)) && amount > 0) {
prices[name] = amount;
}
if (decraftable) {
var craftAmount = 1 + craftRatio;
if (name == "blueprint") {
var bpRatio = gamePage.workshop.getEffect("blueprintCraftRatio");
var scienceBldAmt = gamePage.bld.get("library").val + gamePage.bld.get("academy").val +
gamePage.bld.get("observatory").val + gamePage.bld.get("biolab").val;
craftAmount += scienceBldAmt * bpRatio; //~2x refine rate with 200 buildings
}
var priceRatio = 1 / craftAmount;
for (var i = 0; i < resource.prices.length; i++) {
if (remaining) {
// Thanks to cecilpl for this block of code!
var currentAmount = gamePage.resPool.get(name).value;
if (currentAmount > amount) {
amount = 0;
} else {
amount -= currentAmount;
amount = Math.ceil(amount / craftAmount) * craftAmount;
}
}
var priceName = resource.prices[i].name,
priceAmount = resource.prices[i].val * amount * priceRatio,
decraftedPrice = decraftResource(priceName, priceAmount, remaining, (depth+1));
combine(prices, decraftedPrice);
}
}
return prices;
}
prices = gamePage.bld.getPrices(building.name);
pricesNames = new Array();
decraftedPrices = new Object();
neededToCraft = new Object();
for (var i = 0; i < prices.length; i++) {
pricesNames.push(prices[i].name);
var decraftedPrice = decraftResource(prices[i].name, prices[i].val, false, 1);
combine(decraftedPrices, decraftedPrice);
var amountInInventory = gamePage.resPool.get(prices[i].name).value;
if(amountInInventory < prices[i].val) {
var decraftedRemaining = decraftResource(prices[i].name, prices[i].val, true, 1);
combine(neededToCraft, decraftedRemaining);
}
}
var decraftedPricesNames = Object.keys(decraftedPrices);
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
a.sort(); b.sort();
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function header(text) {
return "<div style='clear: both; text-align: center; width: 100%; border-bottom-width: 1px; " +
"border-bottom-style: solid; border-bottom-color: gray; padding-bottom: 4px; " +
"margin-bottom: 8px;'>" + text + ":</div>"
}
if(!arraysEqual(pricesNames, decraftedPricesNames)) {
var flavorText = $("#tooltip .flavor"),
rawCostDiv = $("<div id='dc-rawcost'></div>"),
n2cDiv = $("<div id='dc-neededtocraft'></div>");
rawCostDiv.append(header("Raw cost"));
for (var item in decraftedPrices) {
var amount = decraftedPrices[item];
rawCostDiv.append("<div style='overflow: hidden;'><span style='float: left;'>" + item +
"</span><span style='float: right;'>" + gamePage.getDisplayValueExt(decraftedPrices[item])
+ "</span></div>");
}
flavorText.before(rawCostDiv);
if (Object.keys(neededToCraft).length > 0) {
n2cDiv.append(header("Resources needed to craft"));
for (var item in neededToCraft) {
var amountInInventory = gamePage.resPool.get(item).value,
perTickUI = gamePage.resPool.get(item).perTickUI;
if (item == "manuscript") {
if (gamePage.workshop.get("printingPress").researched){
perTickUI = 0.0005 * gamePage.bld.get('steamworks').on;
if (gamePage.workshop.get("offsetPress").researched){
perTickUI *= 4;
}
}
}
n2cDiv.append("<div style='overflow: hidden;'><span style='float: left;'>" + item +
"</span><span style='float: right;'" + (
(amountInInventory < neededToCraft[item]) ?
(
" class='noRes'>" + gamePage.getDisplayValueExt(amountInInventory) +
" / " + gamePage.getDisplayValueExt(neededToCraft[item]) +
((perTickUI > 0) ? (
" (" + gamePage.toDisplaySeconds((neededToCraft[item]-amountInInventory) / (perTickUI * gamePage.rate)) + ")"
) : "")
) :
(
">" + gamePage.getDisplayValueExt(neededToCraft[item])
)
)
+ "</span></div>")
}
flavorText.before(n2cDiv);
}
}
var tooltip = $("#tooltip"),
offset = tooltip.offset();
//prevent tooltip from leaving the window area
var scrollBottom = $(window).scrollTop() + $(window).height() - 50; //50px padding-bottom
if (offset.top + $(tooltip).height() >= scrollBottom){
offset.top = scrollBottom - $(tooltip).height();
tooltip.css("top", (offset.top) + "px");
}
} catch (e) {
windowConsole.warn(e);
} finally {
window.console = windowConsole;
}
}
});
rawCost.observe(document.querySelector("#tooltip"), { childList: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment