Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save & load preferred recipe #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# import brotlix
import gzip
import sys
import hashlib


################################################################################
Expand Down Expand Up @@ -429,6 +430,11 @@ def create_calculator_page(calculator_name):
# TODO: Add linting for stack sizes here
recipe_type_format_js = uglify_js_string(generate_recipe_type_format_js(calculator_name, recipe_types))

# Give each recipe a unique ID, so that the UI can use it to save state without relying on recipe order
for resource in resources.values():
for recipe in resource['recipes']:
recipe['hash'] = hashlib.md5(f"{ (recipe['recipe_type'], recipe.get('extra_data', None)) }".encode('utf-8')).hexdigest()

recipe_js = mini_js_data(get_recipes_only(resources))

html_resource_data = generate_resource_html_data(resources)
Expand Down
1 change: 1 addition & 0 deletions core/calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
{% for resource in resources %}
<div class="desired_item item item_{{resource['simplename']}}" mc_value="{{ resource['mc_value'] }}">
<input aria-label="{{ resource['mc_value'] }}" class="desired_item_count" type=number id="{{ resource['simplename'] }}" />
<input class="desired_item_recipe" type=hidden />
</div>
{% endfor %}
<div style="clear:both"></div>
Expand Down
47 changes: 34 additions & 13 deletions core/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $(".desired_item").each(function() {

// When doubleclicking open the recipe select menu
item.dblclick( function (event) {
switch_recipe(item.attr("mc_value"), event);
switch_recipe(item, event);
});

// Enable item name hover text
Expand Down Expand Up @@ -149,11 +149,17 @@ function save() {
$(".desired_item").each(function() {
var key = $(this).find(".desired_item_count").attr("id");
// console.log(key);
var value = $(this).find(".desired_item_count").val();
var count = $(this).find(".desired_item_count").val();
var recipe = $(this).find(".desired_item_recipe").val();

if ($.isNumeric(value)) {
if ($.isNumeric(count)) {
// Set the value as negative to indicate they are needed
selected_items[key] = count;
}

if (recipe) {
// Set the value as negative to indicate they are needed
selected_items[key] = value;
selected_items[key + "__recipe"] = recipe;
}

});
Expand All @@ -171,7 +177,7 @@ function save() {
| load() |
| |
| This function is an inverse to save() and reads the state of the resource |
| requirement list from the UIR hash. In addition it will automatically call |
| requirement list from the URI hash. In addition it will automatically call |
| generatelist() to save the user from having to click the button for a saved |
| list. |
\******************************************************************************/
Expand All @@ -183,8 +189,19 @@ function load() {
var split = pairs[i].split("=");
var id = decodeURIComponent(split[0]);
var value = decodeURIComponent(split[1]);
$("#"+id).val(value);
set_textbox_background($("#"+id));
if (id.endsWith("__recipe")) {
id = id.replace("__recipe", "");
var item = $("#"+id).closest(".item");
var item_name = item.attr("mc_value");
var recipe_index = recipe_json[item_name].findIndex(recipe => recipe.hash === value);
if (recipe_index !== -1) {
set_recipe_index(item, item_name, recipe_index);
}
}
else {
$("#"+id).val(value);
set_textbox_background($("#"+id));
}
}
$("#unused_hide_checkbox").prop("checked", true).change();
generatelist();
Expand Down Expand Up @@ -1271,7 +1288,8 @@ $(document).on("mousemove", function(e){
/******************************************************************************\
|
\******************************************************************************/
function switch_recipe(item_name, event) {
function switch_recipe(item, event) {
var item_name = item.attr("mc_value");
var recipe_selector = $("#recipe_select");
var recipe_selector_list = $("#recipe_selector_list");
recipe_selector_list.empty();
Expand All @@ -1282,7 +1300,7 @@ function switch_recipe(item_name, event) {

recipe_item.click( (function(index) {
return function() {
set_recipe_index(item_name, index);
set_recipe_index(item, item_name, index);
find_loop_from_node(item_name);
recipe_selector.css("opacity", 0);
recipe_selector.css("pointer-events", "none");
Expand Down Expand Up @@ -1354,11 +1372,14 @@ $("#recipe_select").mouseleave(function() {

var alternate_recipe_selections = {};

function set_recipe_index(node_name, recipe_index) {
function set_recipe_index(node, node_name, recipe_index) {
alternate_recipe_selections[node_name] = recipe_index;
if (recipe_index === 0) {
delete alternate_recipe_selections[node_name];
}

$(node).find(".desired_item_recipe").val(recipe_json[node_name][recipe_index].hash);
save();
}

function get_recipe_index(node_name) {
Expand All @@ -1369,12 +1390,12 @@ function get_recipe_index(node_name) {
return alternate_recipe_selections[node_name];
}
}
function set_recipe_to_raw(node_name) {
function set_recipe_to_raw(node, node_name) {
// console.log("Setting as raw resource");

for (let i in recipe_json[node_name]){
if (recipe_json[node_name][i].recipe_type === "Raw Resource"){
set_recipe_index(node_name, i);
set_recipe_index(node, node_name, i);
return;
}
}
Expand Down Expand Up @@ -1433,7 +1454,7 @@ function depth_first_search(nodes, node, match) {
// if a requirement is the original node then change this item to a soruce and report back
if (nodes[node][i] === match) {
// Convert to source recipe
set_recipe_to_raw(node);
set_recipe_to_raw(nodes[node]);
// Return this node name as changed
return [node];
}
Expand Down