public static Recipe LoadFromeJuice(string fn) { string[] recipe = File.ReadAllLines(fn); Recipe r = new Recipe(); r.nicotine_base = Convert.ToSingle(recipe[0]); r.nicotine_target = Convert.ToSingle(recipe[1]); r.amount = Convert.ToSingle(recipe[2]); r.target_pg = Convert.ToSingle(recipe[14]); r.target_vg = Convert.ToSingle(recipe[15]); r.base_pg_percent = Convert.ToSingle(recipe[18]); r.base_vg_percent = Convert.ToSingle(recipe[19]); r.notes = ""; r.mlNotes = null; if (recipe.Length >= 47) { r.mlNotes = recipe.Slice(46, recipe.Length); } else if (recipe.Length == 47) { r.notes = recipe[46]; } r.flavors = new List<Flavor>(); r.flavors.Add(new Flavor(recipe[9], recipe[4], recipe[20], recipe[25], recipe[31])); r.flavors.Add(new Flavor(recipe[10], recipe[5], recipe[21], recipe[26], recipe[32])); r.flavors.Add(new Flavor(recipe[11], recipe[6], recipe[22], recipe[27], recipe[33])); r.flavors.Add(new Flavor(recipe[12], recipe[7], recipe[23], recipe[28], recipe[34])); r.flavors.Add(new Flavor(recipe[13], recipe[8], recipe[24], recipe[29], recipe[35])); r.flavors.Add(new Flavor(recipe[37], recipe[36], recipe[38], recipe[39], recipe[40])); r.flavors.Add(new Flavor(recipe[42], recipe[41], recipe[43], recipe[44], recipe[45])); return r; }
/* * void newRecipe() * Clears the form and starts a new recipe. Queries the * user to save if required. */ public void newRecipe() { if (saveRecipeQuery()) { // Clear the recipe recipe = null; clearRecipe(); saveFilename = "Untitled Recipe.json"; saveFullPath = null; removeEdited(); } }
/* * void openRecipe() * Queries this user to `Save As` the current recipe, then * to open a new one. */ public void openRecipe() { if (saveRecipeQuery()) { if (openFileDialog.ShowDialog() == DialogResult.OK) { switch (openFileDialog.FilterIndex) { case 1: default: saveType = SaveType.JSON; recipe = Recipe.LoadFromJSON(openFileDialog.FileName); break; case 2: saveType = SaveType.EJUICE; recipe = Recipe.LoadFromeJuice(openFileDialog.FileName); break; } // Convert the opened filename to a more friendly one saveFullPath = openFileDialog.FileName; saveFilename = Path.GetFileName(saveFullPath); // Update the title of the form this.Text = saveFilename + title; populateRecipe(); if (!buildRecipe()) { return; } // This recipe is fresh and unedited removeEdited(); } } }
/* * bool buildRecipe() * Returns true if recipe is successfully built. * * Calculates a recipe from the form if possible and stores * the result in a `Recipe` object named `recipe`. */ public bool buildRecipe() { if (!checkForm()) return false; // not implemented //MessageBox.Show("Building recipes has not been implemented.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); /* Implementation details: * Target nic level is calculated thusly, if target and base nic levels differ: * * Let tN be target nic level, let bN be base nic level, let tA be target amount, * let bNV be base nic volume, let bZV be base zero nic volume, and let fP be flavor * percentage. * * bNV = ((tN / bN) * tA) * bZV = tA - bNV - (tA * fP) * */ string notes = ""; if (!string.IsNullOrWhiteSpace(recipeNotes.Text) || recipeNotes.Text != "Notes") notes = recipeNotes.Text; recipe = new Recipe(baseStrength.Text, targetStrength.Text, targetAmount.Text, "100", "0", "100", "0", notes); // Add flavors and populate display recipe.flavors = new List<Flavor>(); for (int i = 0; i < 7; i++) { // Skip unnamed flavors if (!string.IsNullOrWhiteSpace(flavorNames[i].Text)) { recipe.flavors.Add(new Flavor(flavorNames[i].Text, flavorPGs[i].Text, flavorVGs[i].Text, flavorPercents[i].Text, false)); outputFlavorNames[i].Text = recipe.flavors[i].name; outputFlavorAmounts[i].Text = (recipe.amount * (recipe.flavors[i].percent / 100)).ToString(); } } /* Calculate concentrations * This shit is pretty sweet, check it out. */ float fP = 0f; int c = recipe.flavors.Count; // Add up the total percentage of final solution that is flavor for (int i=0; i < c; i++) { float x = recipe.flavors[i].percent / 100; fP += recipe.flavors[i].percent / 100; } // Determine total volume of flavor in the final solution float fA = recipe.amount * (fP / 100); // Calculate volumes of nic and nic-free bases to reach target nic level float bNV = (recipe.nicotine_target / recipe.nicotine_base) * recipe.amount; float bZV = recipe.amount - bNV - (recipe.amount * fP); outputBaseAmount.Text = bNV.ToString(); // Sometimes the cut amount is negative when loading legacy recipes, so just ignore it. if (bZV > 0) outputCutAmount.Text = bZV.ToString(); else outputCutAmount.Text = "0"; return false; }