StonehearthEditor.ModuleFile.RecommendNetWorth C# (CSharp) Method

RecommendNetWorth() private method

private RecommendNetWorth ( ) : void
return void
        private void RecommendNetWorth()
        {
            JsonFileData jsonFileData = FileData as JsonFileData;
            if (jsonFileData == null)
            {
                return;
            }

            foreach (FileData reference in jsonFileData.ReferencedByFileData.Values)
            {
                JsonFileData refJson = reference as JsonFileData;
                if (refJson != null && refJson.JsonType == JSONTYPE.RECIPE)
                {
                    JArray produces = refJson.Json["produces"] as JArray;
                    int productCount = 0;
                    foreach (JToken product in produces)
                    {
                        JToken item = product["item"];
                        if (item != null && item.ToString().Equals(FullAlias))
                        {
                            productCount++;
                        }

                        JToken fine = product["fine"];
                        if (fine != null && fine.ToString().Equals(FullAlias))
                        {
                            productCount++;
                        }
                    }

                    if (productCount <= 0)
                    {
                        continue;
                    }

                    int totalCost = 0;

                    // If we are created by a recipe, look at the ingredients for the recipe to calculate net worth of all ingredients ...
                    JArray ingredients = refJson.Json["ingredients"] as JArray;
                    if (ingredients != null)
                    {
                        foreach (JToken ingredient in ingredients)
                        {
                            int costPer = 0;
                            JToken material = ingredient["material"];
                            if (material != null)
                            {
                                // try get cost of material
                                string materialString = material.ToString();
                                costPer = ModuleDataManager.GetInstance().GetAverageMaterialCost(materialString);
                            }

                            JToken uri = ingredient["uri"];
                            if (uri != null)
                            {
                                // try get cost of material
                                string uriString = uri.ToString();
                                ModuleFile file = ModuleDataManager.GetInstance().GetModuleFile(uriString);
                                if (file != null)
                                {
                                    costPer = (file.FileData as JsonFileData).NetWorth;
                                }
                            }

                            int count = int.Parse(ingredient["count"].ToString());
                            totalCost = totalCost + (costPer * count);
                        }
                    }

                    jsonFileData.RecommendedMinNetWorth = totalCost / productCount;

                    JToken workUnits = refJson.Json["work_units"];
                    if (workUnits != null)
                    {
                        int units = int.Parse(workUnits.ToString());
                        totalCost = totalCost + (units * kWorkUnitsWorth);
                    }

                    jsonFileData.RecommendedMaxNetWorth = totalCost / productCount;
                }
            }
        }