From 32e2a8a1ee63c00a99b00866bb2e5ba62cf9cfb6 Mon Sep 17 00:00:00 2001
From: Andrew Miner
Date: Wed, 21 Aug 2013 22:21:30 -0700
Subject: [PATCH] [#1] Add inventory and update calc's to use it
---
html/crafting.js | 34 ++++++++++++++++++++++++++++++++--
html/css/main.css | 2 +-
html/index.html | 8 +++++---
3 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/html/crafting.js b/html/crafting.js
index 1e0af7eb5..df13f52f2 100644
--- a/html/crafting.js
+++ b/html/crafting.js
@@ -6,6 +6,7 @@ var SLIDE_DURATION = 600; // ms
// Global Variables ///////////////////////////////////////////////////////////////////////////////////////////////////
+var __inventory = createManifest();
var __loadingCount = 0;
var __recipebooks = [];
var __toolsIncluded = false;
@@ -17,6 +18,7 @@ $(function() {
$("#crafting_count").change(onCraftingSelectorChanged);
$("#tools_included").change(onIncludeToolsChanged);
$("#crafting_selector").focus(onCraftingSelectorFocused);
+ $("#inventory").keyup(onInventoryChanged);
loadRecipebook("data/vanilla.json");
loadRecipebook("data/buildcraft.json");
@@ -33,7 +35,7 @@ function onCraftingSelectorChanged() {
if (recipe === undefined) {
$("#crafting_output").slideUp(SLIDE_DURATION);
} else {
- var inventory = createManifest();
+ var inventory = __inventory.copy();
var craftedItems = createManifest();
var missingMaterials = createManifest();
for (var i = 0; i < count; i++) {
@@ -59,6 +61,27 @@ function onIncludeToolsChanged() {
onCraftingSelectorChanged()
}
+function onInventoryChanged() {
+ __inventory.removeAll();
+
+ var inventoryText = $("#inventory").val();
+ if (inventoryText) {
+ var inventoryLines = inventoryText.split("\n");
+ for (var i = 0; i < inventoryLines.length; i++) {
+ var line = inventoryLines[i].trim();
+ var match = /^([0-9]+)(.*)$/.exec(line);
+
+ var count = (match) ? parseInt(match[1]) : 1;
+ var itemName = (match) ? match[2].trim() : line;
+
+ if (itemName.length > 0) {
+ __inventory.add(count, itemName);
+ }
+ }
+ }
+ onCraftingSelectorChanged();
+}
+
function onRecipeBookLoaded() {
if (__loadingCount > 0 || __recipebooks.length === 0) {
$("#crafting").fadeOut(FADE_DURATION);
@@ -160,7 +183,6 @@ function updatePageState(count, recipeName) {
newLink = newLink.replace(/%20/g, "+");
state = {count: count, name: recipeName};
}
- console.log("newTitle: " + newTitle + ", newLink: " + newLink);
document.title = newTitle;
history.pushState(state, newTitle, newLink);
@@ -185,6 +207,14 @@ function createManifest() {
return (object.materials[name] >= count);
};
+ object.copy = function(count, name) {
+ var result = createManifest();
+ for (var name in object.materials) {
+ result.add(object.materials[name], name);
+ }
+ return result;
+ }
+
object.remove = function(count, name) {
if (object.materials[name] !== undefined) {
if (object.materials[name] <= count) {
diff --git a/html/css/main.css b/html/css/main.css
index 94731e199..0af20e9b9 100644
--- a/html/css/main.css
+++ b/html/css/main.css
@@ -16,7 +16,7 @@ p { margin-left: 10px; margin-right: 10px; }
#border_bedrock { background: url("../images/bedrock.png"); height: 32px; box-shadow: 2px 0px 4px #444; }
#crafting { margin-bottom: 64px; }
#crafting_error { margin-left: 10px; }
-#crafting_output { margin-left: 10px; width: 920px; }
+#crafting_output { margin-left: 10px; min-height: 400px; width: 920px; }
#crafting_output th { background: #f8f8f8; border-left: 1px solid #bbb; font-weight: bold; padding: 10px; }
#crafting_output td { background: #f8f8f8; border-left: 1px solid #bbb; padding: 10px; }
#crafting_selector { width: 400px; }
diff --git a/html/index.html b/html/index.html
index 79a84ef0c..d5fa73922 100644
--- a/html/index.html
+++ b/html/index.html
@@ -49,10 +49,12 @@
- | You need to find: |
- You need to make: |
- You'll end up with: |
+ I already have: |
+ You'll need to find: |
+ You'll need to make: |
+ You'll end up with: |
+ |
|
|
|