public static void ParseJsonFileToXMLFile() { //load all json recipes var root = JsonConvert.DeserializeObject <Root>(File.ReadAllText(@"C:\Users\tmi\Downloads\f.txt")); List <Recipee> rec = new List <Recipee>(); foreach (Recipe node in root.recipes) { var r = new Recipee(); if ( node.name.IndexOf("void") >= 0 || node.name.IndexOf("crate") >= 0 || node.name.IndexOf("stack") >= 0 || node.name.IndexOf("barrel") >= 0 || node.name.IndexOf("delivery") >= 0 || node.name.IndexOf("pack") >= 0 || node.name.IndexOf("blackhole") >= 0 ) { continue; } r.Name = node.name; foreach (Ingredient child in node.ingredients) { var m = new ResourceChunk() { Name = child.name, Quantity = child.amount, }; r.Inputs.Add(m); } foreach (Product child in node.products) { var m = new ResourceChunk() { Name = child.name, Quantity = child.amount, Probability = child.probability }; r.Outputs.Add(m); } rec.Add(r); } //rec. Distinct(a => a.) XmlSerializer writer = new XmlSerializer(rec.GetType()); var path = @"C:\Users\tmi\Downloads\f_xml.xml"; using (FileStream file = File.Create(path)) { writer.Serialize(file, rec); } }
public static Tuple <decimal, string> GetQuantityv2(List <RecipeNode> pathRecipes, List <RecipeNode> allAvailableRecipes, ResourceChunk startingResource, string endingResource) { var simulateChanceBasedRecipes = false; var currentResources = new List <ResourceChunk>(); currentResources.Add(startingResource); Func <double, int, decimal> getRes = (recipeQuantity, timesRecipeUsage) => { if (simulateChanceBasedRecipes) { var r = new Random(); var retVal = 0; for (int i = 0; i < timesRecipeUsage; i++) { if (r.Next(1, 101) <= recipeQuantity * 100) { retVal++; } } return(retVal); } return(Convert.ToDecimal(recipeQuantity * timesRecipeUsage)); }; var sb = new StringBuilder(); int j = 0; while (true) { j++; if (j > 20) { var x = 3; } var recipesICanUse = new List <RecipeNode>(); foreach (var availableRecipe in pathRecipes) { if (availableRecipe.Recipe.Inputs.Any(a => a.Name == endingResource)) { continue; } //ak aspon jeden input mam a mam ho dost if (availableRecipe.Recipe.Inputs .Any(input => currentResources .Any(cr => cr.Name == input.Name && cr.Quantity >= input.Quantity)) ) { if (availableRecipe.Recipe.Inputs.All(input => { //ak aspon jeden input mam a mam ho dost if (currentResources.FirstOrDefault(a => a.Name == input.Name && a.Quantity >= input.Quantity) != null) { return(true); } //ak je to zdroj co mozem ziskat inym receptom //if (pathRecipes.Any(a => a.Recipe.Outputs.Any(b => b.Name == input.Name))) return false; if (IgnoredInputs.Contains(input.Name)) { return(true); } else { //ak app uz informovala ze nemam surku a dal som ok, je to ocakavana chybajuca surka vramci pouzivanych receptov if (MissingNotIgnoredResourcesReportedThisSession.Contains(input.Name)) { return(false); } //MessageBox.Show($"nemam {input.Name}"); MissingNotIgnoredResourcesReportedThisSession.Add(input.Name); return(false); } //return true; })) { recipesICanUse.Add(availableRecipe); break; } } } var recipeToUse = recipesICanUse.FirstOrDefault(); if (recipeToUse == null) { break; } var resourceChunkToUseAsInput = currentResources.First(a => recipeToUse.Recipe.Inputs.Any(b => a.Name == b.Name)); var recipeInputChunk = recipeToUse.Recipe.Inputs.First(a => a.Name == resourceChunkToUseAsInput.Name); int timesRecipeUsage = (int)Math.Floor(resourceChunkToUseAsInput.Quantity / recipeInputChunk.Quantity); sb.AppendLine($"{timesRecipeUsage}[{string.Join(",", recipeToUse.Recipe.Inputs)} to {string.Join(",", recipeToUse.Recipe.Outputs)}]"); var totalInputUsed = timesRecipeUsage * recipeInputChunk.Quantity; resourceChunkToUseAsInput.Quantity -= totalInputUsed; foreach (var item in recipeToUse.Recipe.Outputs) { var addedResource = item.Probability == 1 ? item.Quantity * timesRecipeUsage : getRes(item.Probability, timesRecipeUsage); var q = currentResources.FirstOrDefault(_ => _.Name == item.Name); if (q != null) { q.Quantity += item.Quantity * timesRecipeUsage; } else { currentResources.Add(new ResourceChunk() { Name = item.Name, Quantity = item.Quantity * timesRecipeUsage }); } } foreach (var item in currentResources) { item.Quantity = Math.Round(item.Quantity, 3); } currentResources.RemoveAll(_ => _.Quantity == 0); } sb.AppendLine(); foreach (var item in currentResources) { sb.AppendLine($"{item.Quantity} {item.Name}"); } sb.AppendLine(); sb.AppendLine("Ending resources:"); currentResources.ForEach(_ => sb.AppendLine($"{_.Quantity}x {_.Name}")); if (!currentResources.Any(a => a.Name == endingResource)) { var q = 2; } var endingQuantity = currentResources.FirstOrDefault(a => a.Name == endingResource)?.Quantity ?? 0; File.WriteAllText($@"{SaveFolder}\{startingResource.Name}_{endingResource}_{endingQuantity}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.txt", sb.ToString()); return(new Tuple <decimal, string>(endingQuantity, sb.ToString())); }
private static List <RecipeNode> InitRecipes(string fileName) { XmlDocument doc = new XmlDocument(); doc.Load(Path.GetFullPath(Path.Combine(Application.StartupPath, $@"..\..\{fileName}.xml"))); var recipes = new List <Recipee>(); foreach (XmlNode node in doc.SelectNodes("//recipe")) { var r = new Recipee(); r.Name = node.Attributes["name"]?.Value; foreach (XmlNode child in node.ChildNodes) { var m = new ResourceChunk() { Name = child.Attributes["name"].Value, Quantity = decimal.Parse(child.Attributes["q"].Value.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)) }; if (child.Name == "input") { r.Inputs.Add(m); } else { r.Outputs.Add(m); } } recipes.Add(r); } List <RecipeNode> labels = new List <RecipeNode>(); foreach (Recipee rec in recipes) { var r = new RecipeNode() { Recipe = rec }; labels.Add(r); } foreach (var item in labels) { foreach (var input in item.Recipe.Inputs) { foreach (var matchedRecipeLabel in labels.Where(_ => _.Recipe.Outputs.Any(__ => __.Name == input.Name))) { item.IncomingRecipes.Add(matchedRecipeLabel); } } foreach (var output in item.Recipe.Outputs) { foreach (var matchedRecipeLabel in labels.Where(_ => _.Recipe.Inputs.Any(__ => __.Name == output.Name))) { item.OutGoingRecipes.Add(matchedRecipeLabel); } } } return(labels); }
private void be() { Class1.Start(); return; var root = JsonConvert.DeserializeObject <Root>(File.ReadAllText(@"C:\Users\tmi\Downloads\f.txt")); //var root = JsonConvert.DeserializeObject<Root>(File.ReadAllText(@"C:\Downloads\f.txt")); List <Recipee> rec = new List <Recipee>(); foreach (Recipe node in root.recipes) { var r = new Recipee(); if ( node.name.IndexOf("void") >= 0 || node.name.IndexOf("crate") >= 0 || node.name.IndexOf("stack") >= 0 || node.name.IndexOf("barrel") >= 0 || node.name.IndexOf("delivery") >= 0 || node.name.IndexOf("pack") >= 0 || node.name.IndexOf("blackhole") >= 0 ) { continue; } r.Name = node.name; foreach (Ingredient child in node.ingredients) { var m = new ResourceChunk() { Name = child.name, Quantity = child.amount, }; r.Inputs.Add(m); } foreach (Product child in node.products) { var m = new ResourceChunk() { Name = child.name, Quantity = child.amount, Probability = child.probability }; r.Outputs.Add(m); } rec.Add(r); } List <RecipeNode> labels = new List <RecipeNode>(); foreach (Recipee re in rec) { var r = new RecipeNode() { Recipe = re }; labels.Add(r); } labels = getMostEfficientPath(labels, "titanium", "ore-titanium", "titanium-plate", 1000); //labels = getMostEfficientPath(labels); }