Changeset - 5e28ba3945f7
[Not reviewed]
default
0 1 0
Jason Maltzen - 3 years ago 2021-09-10 00:03:21
jason@hiddenachievement.com
Recipe count is now a ulong instead of an int so it can show values > 2.47 billion. Also, don't allow clearing the recipe list while the recipe generator is running.
1 file changed with 12 insertions and 6 deletions:
0 comments (0 inline, 0 general)
ViewModels/RecipeGeneratorViewModel.cs
Show inline comments
...
 
@@ -81,25 +81,25 @@ namespace DesertPaintCodex.ViewModels
 
        public ObservableCollection<Reagent> Reagents { get; } = new();
 
        public ObservableCollection<GeneratorRecipe> AllRecipes { get; } = new();
 
        
 
        #endregion // Collections
 
        
 
        private readonly RecipeGenerator _generator;
 

	
 
        private DateTime _lastSave = DateTime.Now;
 
        private readonly PlayerProfile _profile;
 
        private uint _minConcentration;
 

	
 
        private readonly ConcurrentQueue<PaintRecipe> _pendingNewRecipes = new();
 
        private volatile int _newRecipeCount;
 
        private ulong _newRecipeCount;
 
        private volatile bool _updatesAvailable;
 
        private readonly DispatcherTimer _updateTimer;
 
        
 
        private bool _ribbonMode;
 
        private bool _unsavedRecipes;
 
        private bool _saving;
 

	
 
        
 
        public RecipeGeneratorViewModel()
 
        {
 
            List<string> reagentNames = ReagentService.Names;
 
            foreach (string name in reagentNames)
...
 
@@ -146,24 +146,26 @@ namespace DesertPaintCodex.ViewModels
 
            SaveSettings(_ribbonMode ? "Ribbon" : "Paint");
 
            _updateTimer.Start();
 
            SelectedView = 1;
 

	
 
            _generator.CloseLog();
 
            SettingsService.Get("Generator.Logging", out bool logGenerator, false);
 
            if (logGenerator)
 
            {
 
                string logDir = DesertPaintCodex.Util.FileUtils.AppDataPath;
 
                _generator.Log = System.IO.Path.Combine(logDir, "recipe_generator_log.txt");
 
            }
 

	
 
            CanClear = false;
 

	
 
            _generator.BeginRecipeGeneration(
 
                _minConcentration, 
 
                (uint)MaxConcentration, 
 
                1, 
 
                (uint)MaxReagents, 
 
                (uint)FullQuantityDepth, 
 
                (uint)FullQuantity);
 
        }
 

	
 
        
 
        public void End()
 
        {
...
 
@@ -192,24 +194,25 @@ namespace DesertPaintCodex.ViewModels
 
            IsRunning = true;
 
            _updateTimer.Start();
 
            SelectedView = 1;
 

	
 
            _generator.CloseLog();
 
            SettingsService.Get("Generator.Logging", out bool logGenerator, false);
 
            if (logGenerator)
 
            {
 
                string logDir = DesertPaintCodex.Util.FileUtils.AppDataPath;
 
                _generator.Log = System.IO.Path.Combine(logDir, "recipe_generator_log.txt");
 
            }
 

	
 
            CanClear = false;
 
            _generator.ResumeRecipeGeneration();
 

	
 
        }
 

	
 
        public async Task Clear()
 
        {
 
            bool result = await ShowYesNoBox("Clear Recipes", "Are you sure you want to clear your recipes?");
 
            if (result)
 
            {
 
                CanClear = false;
 
                if (_ribbonMode)
 
                {
...
 
@@ -290,38 +293,39 @@ namespace DesertPaintCodex.ViewModels
 
                GeneratorRecipe genRecipe = new(color);
 
                if (recipes.TryGetValue(color.Name, out PaintRecipe? recipe)) //  && recipe.IsValidForConcentration(minConcentration)) // This check is redundant, since they're checked when loading.
 
                {
 
                    genRecipe.DraftRecipe(recipe);
 
                }
 
                AllRecipes.Add(genRecipe);
 
            }
 
        }
 

	
 
        #region Event Handlers
 
        private void OnProgress(object? sender, EventArgs args)
 
        {
 
            _newRecipeCount = (int)_generator.RecipeCount;
 
            _newRecipeCount = _generator.RecipeCount;
 
            _updatesAvailable = true;
 
        }
 

	
 
        private void OnNewRecipe(object? sender, NewRecipeEventArgs args)
 
        {
 
            PaintRecipe recipe = new(args.Recipe);
 
            _pendingNewRecipes.Enqueue(recipe);
 
            _updatesAvailable = true;
 
        }
 
        
 
        
 
        private void OnGeneratorStopped(object? sender, EventArgs args)
 
        {
 
            Debug.WriteLine($"Generator stopped. Saving = {_saving}");
 
            if (_saving)
 
            {
 
                SaveState();
 

	
 
                _generator.ResumeRecipeGeneration();
 
                _lastSave = DateTime.Now;
 
                _saving = false;
 
                return;
 
            }
 

	
 
            _generator.FlushLog();
 

	
...
 
@@ -337,25 +341,26 @@ namespace DesertPaintCodex.ViewModels
 
        }
 

	
 
        private void Update(object? sender, EventArgs e)
 
        {
 
            if (!_updatesAvailable) return;
 
            if (_saving) return;
 

	
 
            _updatesAvailable = false;
 

	
 
            DateTime now = DateTime.Now;
 

	
 
            // Update test count.
 
            PermutationCount = $"{_newRecipeCount:n0}";
 
            ulong newRecipeCount = _newRecipeCount;
 
            PermutationCount = $"{newRecipeCount:n0}";
 
            
 
            // Pull in new recipes.
 
            if (!_pendingNewRecipes.IsEmpty)
 
            {
 
                GeneratorRecipe? lastRecipe = null;
 
                while (_pendingNewRecipes.TryDequeue(out PaintRecipe? newRecipe))
 
                {
 
                    string recipeColor = PaletteService.FindNearest(newRecipe.ReactedColor);
 
                    foreach (GeneratorRecipe recipe in AllRecipes)
 
                    {
 
                        if (recipe.Color.Name != recipeColor) continue;
 
                        recipe.DraftRecipe(newRecipe);
...
 
@@ -378,30 +383,31 @@ namespace DesertPaintCodex.ViewModels
 
                {
 
                    _unsavedRecipes = true;
 
                    MostRecentRecipe = lastRecipe;
 
                    MostRecentTime = now;
 
                }
 
            }
 
            
 
            // Save if it is time.
 
            if (!((now - _lastSave).TotalMilliseconds > SaveInterval)) return;
 
            
 
            if (_unsavedRecipes)
 
            {
 
                Debug.WriteLine("Saving generator state.");
 
                _saving = true;
 
                _generator.Stop();
 

	
 
                _profile.SaveRecipes();
 
                _unsavedRecipes = false;
 
            }
 

	
 
            _saving = true;
 
            _generator.Stop();
 
        }
 
        
 
        #endregion
 

	
 

	
 
        private void SavePaintSettings()
 
        {
 
            SaveSettings("Paint");
 
        }
 

	
 
        private void LoadPaintSettings()
 
        {
0 comments (0 inline, 0 general)