Skip to content

Commit 499a464

Browse files
authored
Merge pull request #29 from Denifia/develop
Develop
2 parents 21fcc41 + 9dba859 commit 499a464

28 files changed

+898
-17
lines changed

BuyRecipes/BuyRecipes.cs

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
}

BuyRecipes/BuyRecipes.csproj

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
<ProjectGuid>{7DACA8E6-D69F-43BB-8442-CDB25F7C863B}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>BuyRecipes</RootNamespace>
11-
<AssemblyName>BuyRecipes</AssemblyName>
12-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
10+
<RootNamespace>Denifia.Stardew.BuyRecipes</RootNamespace>
11+
<AssemblyName>Denifia.Stardew.BuyRecipes</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<NuGetPackageImportStamp>
15+
</NuGetPackageImportStamp>
16+
<TargetFrameworkProfile />
1417
</PropertyGroup>
1518
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1619
<DebugSymbols>true</DebugSymbols>
@@ -30,6 +33,9 @@
3033
<WarningLevel>4</WarningLevel>
3134
</PropertyGroup>
3235
<ItemGroup>
36+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
38+
</Reference>
3339
<Reference Include="System" />
3440
<Reference Include="System.Core" />
3541
<Reference Include="System.Xml.Linq" />
@@ -40,14 +46,52 @@
4046
<Reference Include="System.Xml" />
4147
</ItemGroup>
4248
<ItemGroup>
49+
<Compile Include="BuyRecipes.cs" />
50+
<Compile Include="Domain\BaseRecipeAquisition.cs" />
51+
<Compile Include="Domain\CookingRecipe.cs" />
52+
<Compile Include="Domain\DefaultRecipeAquisition.cs" />
53+
<Compile Include="Domain\FriendBasedRecipeAquisition.cs" />
54+
<Compile Include="Domain\GameDateTime.cs" />
55+
<Compile Include="Domain\GameItem.cs" />
56+
<Compile Include="Domain\GameItemWithQuantity.cs" />
57+
<Compile Include="Domain\IRecipeAquisitionConditions.cs" />
58+
<Compile Include="Domain\LevelBasedRecipeAquisition.cs" />
59+
<Compile Include="Domain\SkillBasedRecipeAquisition.cs" />
60+
<Compile Include="Framework\ModConstants.cs" />
61+
<Compile Include="Framework\ModHelper.cs" />
62+
<Compile Include="Framework\UpdateHelper.cs" />
63+
<Compile Include="ModConfig.cs" />
4364
<Compile Include="Properties\AssemblyInfo.cs" />
65+
<Compile Include="Services\VersionCheckService.cs" />
4466
</ItemGroup>
4567
<ItemGroup>
68+
<None Include="config.json">
69+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
70+
</None>
4671
<None Include="manifest.json">
4772
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4873
</None>
74+
<None Include="packages.config" />
4975
<None Include="readme.md" />
5076
<None Include="release-notes.md" />
5177
</ItemGroup>
5278
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
79+
<Import Project="..\packages\Pathoschild.Stardew.ModBuildConfig.1.5.0\build\Pathoschild.Stardew.ModBuildConfig.targets" Condition="Exists('..\packages\Pathoschild.Stardew.ModBuildConfig.1.5.0\build\Pathoschild.Stardew.ModBuildConfig.targets')" />
80+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
81+
<PropertyGroup>
82+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
83+
</PropertyGroup>
84+
<Error Condition="!Exists('..\packages\Pathoschild.Stardew.ModBuildConfig.1.5.0\build\Pathoschild.Stardew.ModBuildConfig.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Pathoschild.Stardew.ModBuildConfig.1.5.0\build\Pathoschild.Stardew.ModBuildConfig.targets'))" />
85+
</Target>
86+
<Target Name="AfterBuild">
87+
<PropertyGroup>
88+
<ModPath>$(GamePath)\Mods\BuyRecipes</ModPath>
89+
</PropertyGroup>
90+
<Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(ModPath)" />
91+
<Copy SourceFiles="$(TargetDir)\$(TargetName).pdb" DestinationFolder="$(ModPath)" Condition="Exists('$(TargetDir)\$(TargetName).pdb')" />
92+
<Copy SourceFiles="$(TargetDir)\$(TargetName).dll.mdb" DestinationFolder="$(ModPath)" Condition="Exists('$(TargetDir)\$(TargetName).dll.mdb')" />
93+
<Copy SourceFiles="$(ProjectDir)manifest.json" DestinationFolder="$(ModPath)" />
94+
<Copy SourceFiles="$(TargetDir)\Newtonsoft.Json.dll" DestinationFolder="$(ModPath)" />
95+
<Copy SourceFiles="$(TargetDir)\config.json" DestinationFolder="$(ModPath)" />
96+
</Target>
5397
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Denifia.Stardew.BuyRecipes.Domain
2+
{
3+
public abstract class BaseRecipeAquisition
4+
{
5+
public BaseRecipeAquisition()
6+
{
7+
}
8+
9+
public BaseRecipeAquisition(string data)
10+
{
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)