|  | 
|  | 1 | +using Denifia.Stardew.BuyRecipes.Domain; | 
|  | 2 | +using Denifia.Stardew.BuyRecipes.Framework; | 
|  | 3 | +using Denifia.Stardew.BuyRecipes.Services; | 
|  | 4 | +using StardewModdingAPI; | 
|  | 5 | +using StardewModdingAPI.Events; | 
|  | 6 | +using StardewValley; | 
|  | 7 | +using System; | 
|  | 8 | +using System.Collections.Generic; | 
|  | 9 | +using System.Linq; | 
|  | 10 | +using System.Text; | 
|  | 11 | +using System.Threading.Tasks; | 
|  | 12 | + | 
|  | 13 | +namespace Denifia.Stardew.BuyRecipes | 
|  | 14 | +{ | 
|  | 15 | +    public class BuyRecipes : Mod | 
|  | 16 | +    { | 
|  | 17 | +        private bool _savedGameLoaded = false; | 
|  | 18 | +        private List<CookingRecipe> _cookingRecipes; | 
|  | 19 | +        private List<CookingRecipe> _thisWeeksRecipes; | 
|  | 20 | +        private int _seed; | 
|  | 21 | +         | 
|  | 22 | +        public static List<IRecipeAquisitionConditions> RecipeAquisitionConditions; | 
|  | 23 | + | 
|  | 24 | +        public override void Entry(IModHelper helper) | 
|  | 25 | +        { | 
|  | 26 | +            SaveEvents.AfterLoad += SaveEvents_AfterLoad; | 
|  | 27 | +            SaveEvents.AfterReturnToTitle += SaveEvents_AfterReturnToTitle; | 
|  | 28 | +            TimeEvents.DayOfMonthChanged += TimeEvents_DayOfMonthChanged; | 
|  | 29 | + | 
|  | 30 | +            RecipeAquisitionConditions = new List<IRecipeAquisitionConditions>() | 
|  | 31 | +            { | 
|  | 32 | +                new FriendBasedRecipeAquisition(), | 
|  | 33 | +                new SkillBasedRecipeAquisition(), | 
|  | 34 | +                new LevelBasedRecipeAquisition() | 
|  | 35 | +            }; | 
|  | 36 | + | 
|  | 37 | +            helper.ConsoleCommands | 
|  | 38 | +                .Add("buyrecipe", $"Buy a recipe. \n\nUsage: buyrecipe \"<name of recipe>\" \n\nNote: This is case sensitive!", HandleCommand) | 
|  | 39 | +                .Add("showrecipes", $"Lists this weeks available recipes. \n\nUsage: showrecipes", HandleCommand); | 
|  | 40 | + | 
|  | 41 | +            // Instance the Version Check Service | 
|  | 42 | +            new VersionCheckService(this); | 
|  | 43 | +        } | 
|  | 44 | + | 
|  | 45 | +        private void HandleCommand(string command, string[] args) | 
|  | 46 | +        { | 
|  | 47 | +            var newArgs = new List<string>(); | 
|  | 48 | +            var quote = "\""; | 
|  | 49 | +            var temp = string.Empty; | 
|  | 50 | +            var tempInt = -1; | 
|  | 51 | +            for (int i = 0; i < args.Length; i++) | 
|  | 52 | +            { | 
|  | 53 | +                if (args[i].StartsWith(quote)) | 
|  | 54 | +                { | 
|  | 55 | +                    temp = args[i].TrimStart(quote.ToArray()); | 
|  | 56 | +                    tempInt = i; | 
|  | 57 | +                } | 
|  | 58 | +                if (args[i].EndsWith(quote)) | 
|  | 59 | +                { | 
|  | 60 | +                    if (tempInt != i) | 
|  | 61 | +                    { | 
|  | 62 | +                        temp += " " + args[i]; | 
|  | 63 | +                    } | 
|  | 64 | +                    temp = temp.TrimEnd(quote.ToArray()); | 
|  | 65 | +                    newArgs.Add(temp); | 
|  | 66 | +                    temp = string.Empty; | 
|  | 67 | +                    tempInt = -1; | 
|  | 68 | +                    continue; | 
|  | 69 | +                } | 
|  | 70 | +                if (tempInt == (i - 1)) | 
|  | 71 | +                { | 
|  | 72 | +                    temp += " " + args[i]; | 
|  | 73 | +                    tempInt = i; | 
|  | 74 | +                    continue; | 
|  | 75 | +                } | 
|  | 76 | + | 
|  | 77 | +                if (temp.Equals(string.Empty)) | 
|  | 78 | +                { | 
|  | 79 | +                    newArgs.Add(args[i]); | 
|  | 80 | +                } | 
|  | 81 | +            } | 
|  | 82 | + | 
|  | 83 | +            args = newArgs.ToArray(); | 
|  | 84 | + | 
|  | 85 | +            if (!_savedGameLoaded) | 
|  | 86 | +            { | 
|  | 87 | +                Monitor.Log("Please load up a saved game first, then try again.", LogLevel.Warn); | 
|  | 88 | +                return; | 
|  | 89 | +            } | 
|  | 90 | + | 
|  | 91 | +            switch (command) | 
|  | 92 | +            { | 
|  | 93 | +                case "buyrecipe": | 
|  | 94 | +                    BuyRecipe(args); | 
|  | 95 | +                    break; | 
|  | 96 | +                case "showrecipes": | 
|  | 97 | +                    ShowWeeklyRecipes(); | 
|  | 98 | +                    break; | 
|  | 99 | +                default: | 
|  | 100 | +                    throw new NotImplementedException($"Send Items received unknown command '{command}'."); | 
|  | 101 | +            } | 
|  | 102 | +        } | 
|  | 103 | + | 
|  | 104 | +        private void BuyRecipe(string[] args) | 
|  | 105 | +        { | 
|  | 106 | +            if (args.Length == 1) | 
|  | 107 | +            { | 
|  | 108 | +                var recipeName = args[0]; | 
|  | 109 | +                var recipe = _cookingRecipes.FirstOrDefault(x => x.Name == recipeName); | 
|  | 110 | +                if (recipe == null) | 
|  | 111 | +                { | 
|  | 112 | +                    Monitor.Log("Recipe not found", LogLevel.Info); | 
|  | 113 | +                    return; | 
|  | 114 | +                } | 
|  | 115 | + | 
|  | 116 | +                if (recipe.IsKnown || Game1.player.cookingRecipes.ContainsKey(recipeName)) | 
|  | 117 | +                { | 
|  | 118 | +                    recipe.IsKnown = true; | 
|  | 119 | +                    Monitor.Log("Recipe already known", LogLevel.Info); | 
|  | 120 | +                    return; | 
|  | 121 | +                } | 
|  | 122 | + | 
|  | 123 | +                if (!_thisWeeksRecipes.Any(x => x.Name.Equals(recipeName))) | 
|  | 124 | +                { | 
|  | 125 | +                    Monitor.Log("Recipe is not availble to buy this week", LogLevel.Info); | 
|  | 126 | +                    return; | 
|  | 127 | +                } | 
|  | 128 | + | 
|  | 129 | +                if (Game1.player.Money < recipe.AquisitionConditions.Cost) | 
|  | 130 | +                { | 
|  | 131 | +                    Monitor.Log("You can't affort this recipe", LogLevel.Info); | 
|  | 132 | +                    return; | 
|  | 133 | +                } | 
|  | 134 | + | 
|  | 135 | +                Game1.player.cookingRecipes.Add(recipeName, 0); | 
|  | 136 | +                Game1.player.Money -= recipe.AquisitionConditions.Cost; | 
|  | 137 | +                Monitor.Log($"{recipeName} bought for {ModHelper.GetMoneyAsString(recipe.AquisitionConditions.Cost)}!", LogLevel.Alert); | 
|  | 138 | +            } | 
|  | 139 | +            else | 
|  | 140 | +            { | 
|  | 141 | +                LogArgumentsInvalid("buy"); | 
|  | 142 | +            } | 
|  | 143 | +        } | 
|  | 144 | + | 
|  | 145 | +        private void SaveEvents_AfterReturnToTitle(object sender, EventArgs e) | 
|  | 146 | +        { | 
|  | 147 | +            _savedGameLoaded = false; | 
|  | 148 | +            _cookingRecipes = null; | 
|  | 149 | +            _thisWeeksRecipes = null; | 
|  | 150 | +        } | 
|  | 151 | +         | 
|  | 152 | +        private void SaveEvents_AfterLoad(object sender, EventArgs e) | 
|  | 153 | +        { | 
|  | 154 | +            _savedGameLoaded = true; | 
|  | 155 | +            DiscoverRecipes(); | 
|  | 156 | +            GenerateWeeklyRecipes(); | 
|  | 157 | +        } | 
|  | 158 | + | 
|  | 159 | +        private void TimeEvents_DayOfMonthChanged(object sender, EventArgsIntChanged e) | 
|  | 160 | +        { | 
|  | 161 | +            GenerateWeeklyRecipes(); | 
|  | 162 | +        } | 
|  | 163 | + | 
|  | 164 | +        private void GenerateWeeklyRecipes() | 
|  | 165 | +        { | 
|  | 166 | +            var gameDateTime = new GameDateTime(Game1.timeOfDay, Game1.dayOfMonth, Game1.currentSeason, Game1.year); | 
|  | 167 | +            var startDayOfWeek = (((gameDateTime.DayOfMonth / 7) + 1) * 7) - 6; | 
|  | 168 | +            var seed = int.Parse($"{startDayOfWeek}{gameDateTime.Season}{gameDateTime.Year}"); | 
|  | 169 | +            var random = new Random(seed); | 
|  | 170 | + | 
|  | 171 | +            if (_seed == seed) return; | 
|  | 172 | +            _seed = seed; | 
|  | 173 | + | 
|  | 174 | +            _thisWeeksRecipes = new List<CookingRecipe>(); | 
|  | 175 | +            var maxNumberOfRecipesPerWeek = 5; | 
|  | 176 | +            var unknownRecipes = _cookingRecipes.Where(x => !x.IsKnown).ToList(); | 
|  | 177 | +            var unknownRecipesCount = unknownRecipes.Count; | 
|  | 178 | +            for (int i = 0; i < maxNumberOfRecipesPerWeek; i++) | 
|  | 179 | +            { | 
|  | 180 | +                var recipe = unknownRecipes[random.Next(unknownRecipesCount)]; | 
|  | 181 | +                if (!_thisWeeksRecipes.Any(x => x.Name.Equals(recipe.Name))) | 
|  | 182 | +                { | 
|  | 183 | +                    _thisWeeksRecipes.Add(recipe); | 
|  | 184 | +                } | 
|  | 185 | +            } | 
|  | 186 | + | 
|  | 187 | +            ShowWeeklyRecipes(); | 
|  | 188 | +        } | 
|  | 189 | + | 
|  | 190 | +        private void ShowWeeklyRecipes() | 
|  | 191 | +        { | 
|  | 192 | +            Monitor.Log($"This weeks recipes are:", LogLevel.Alert); | 
|  | 193 | +            foreach (var item in _thisWeeksRecipes) | 
|  | 194 | +            { | 
|  | 195 | +                Monitor.Log($"{ModHelper.GetMoneyAsString(item.AquisitionConditions.Cost)} - {item.Name}", LogLevel.Info); | 
|  | 196 | +            } | 
|  | 197 | +        } | 
|  | 198 | + | 
|  | 199 | +        private void DiscoverRecipes() | 
|  | 200 | +        { | 
|  | 201 | +            var knownRecipes = Game1.player.cookingRecipes.Keys; | 
|  | 202 | +            _cookingRecipes = new List<CookingRecipe>(); | 
|  | 203 | +            foreach (var recipe in CraftingRecipe.cookingRecipes) | 
|  | 204 | +            { | 
|  | 205 | +                var cookingRecipe = new CookingRecipe(recipe.Key, recipe.Value); | 
|  | 206 | +                if (Game1.player.cookingRecipes.ContainsKey(cookingRecipe.Name)) | 
|  | 207 | +                { | 
|  | 208 | +                    cookingRecipe.IsKnown = true; | 
|  | 209 | +                } | 
|  | 210 | +                _cookingRecipes.Add(cookingRecipe); | 
|  | 211 | +            } | 
|  | 212 | + | 
|  | 213 | +            var unknownRecipeCount = _cookingRecipes.Where(x => !x.IsKnown).Count(); | 
|  | 214 | +        } | 
|  | 215 | + | 
|  | 216 | +        private void LogUsageError(string error, string command) | 
|  | 217 | +        { | 
|  | 218 | +            Monitor.Log($"{error} Type 'help {command}' for usage.", LogLevel.Error); | 
|  | 219 | +        } | 
|  | 220 | + | 
|  | 221 | +        private void LogArgumentsInvalid(string command) | 
|  | 222 | +        { | 
|  | 223 | +            LogUsageError("The arguments are invalid.", command); | 
|  | 224 | +        } | 
|  | 225 | +    } | 
|  | 226 | +} | 
0 commit comments