Skip to content

Commit

Permalink
fixed parsing args issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Denifia committed Apr 30, 2017
1 parent 3a51f63 commit 32d9425
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
67 changes: 61 additions & 6 deletions BuyRecipes/BuyRecipes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Denifia.Stardew.BuyRecipes.Domain;
using Denifia.Stardew.BuyRecipes.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
Expand Down Expand Up @@ -30,11 +31,51 @@ public override void Entry(IModHelper helper)
new LevelBasedRecipeAquisition()
};

helper.ConsoleCommands.Add("buy", $"temp", HandleCommand);
helper.ConsoleCommands
.Add("buyrecipe", $"Buy a recipe. \n\nUsage: buyrecipe \"<name of recipe>\" \n\nNote: This is case sensitive!", HandleCommand)
.Add("showrecipes", $"Lists this weeks available recipes. \n\nUsage: showrecipes", HandleCommand);
}

private void HandleCommand(string command, string[] args)
{
var newArgs = new List<string>();
var quote = "\"";
var temp = string.Empty;
var tempInt = -1;
for (int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith(quote))
{
temp = args[i].TrimStart(quote.ToArray());
tempInt = i;
}
if (args[i].EndsWith(quote))
{
if (tempInt != i)
{
temp += " " + args[i];
}
temp = temp.TrimEnd(quote.ToArray());
newArgs.Add(temp);
temp = string.Empty;
tempInt = -1;
continue;
}
if (tempInt == (i - 1))
{
temp += " " + args[i];
tempInt = i;
continue;
}

if (temp.Equals(string.Empty))
{
newArgs.Add(args[i]);
}
}

args = newArgs.ToArray();

if (!_savedGameLoaded)
{
Monitor.Log("Please load up a saved game first, then try again.", LogLevel.Warn);
Expand All @@ -43,17 +84,19 @@ private void HandleCommand(string command, string[] args)

switch (command)
{
case "buy":
case "buyrecipe":
BuyRecipe(args);
break;
case "showrecipes":
ShowWeeklyRecipes();
break;
default:
throw new NotImplementedException($"Send Items received unknown command '{command}'.");
}
}

private void BuyRecipe(string[] args)
{

if (args.Length == 1)
{
var recipeName = args[0];
Expand All @@ -74,11 +117,18 @@ private void BuyRecipe(string[] args)
if (!_thisWeeksRecipes.Any(x => x.Name.Equals(recipeName)))
{
Monitor.Log("Recipe is not availble to buy this week", LogLevel.Info);
return;
}

if (Game1.player.Money < recipe.AquisitionConditions.Cost)
{
Monitor.Log("You can't affort this recipe", LogLevel.Info);
return;
}

Game1.player.cookingRecipes.Add(recipeName, 0);
Game1.player.Money -= recipe.AquisitionConditions.Cost;
Monitor.Log($"{recipeName} bought for ${recipe.AquisitionConditions.Cost}!", LogLevel.Alert);
Monitor.Log($"{recipeName} bought for {ModHelper.GetMoneyAsString(recipe.AquisitionConditions.Cost)}!", LogLevel.Alert);
}
else
{
Expand Down Expand Up @@ -121,10 +171,15 @@ private void SaveEvents_AfterLoad(object sender, EventArgs e)
}
}

Monitor.Log($"This weeks recipes are:", LogLevel.Info);
ShowWeeklyRecipes();
}

private void ShowWeeklyRecipes()
{
Monitor.Log($"This weeks recipes are:", LogLevel.Alert);
foreach (var item in _thisWeeksRecipes)
{
Monitor.Log($"{item.AquisitionConditions.Cost} - {item.Name}", LogLevel.Info);
Monitor.Log($"{ModHelper.GetMoneyAsString(item.AquisitionConditions.Cost)} - {item.Name}", LogLevel.Info);
}
}

Expand Down
5 changes: 5 additions & 0 deletions BuyRecipes/Framework/ModHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ private static void DeserializeGameObjects()
});
}
}

public static string GetMoneyAsString(int money)
{
return $"G{money.ToString("#,##0")}";
}
}
}
4 changes: 2 additions & 2 deletions BuyRecipes/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"MajorVersion": 0,
"MinorVersion": 1,
"PatchVersion": 0,
"Build": null
"Build": "beta"
},
"MinimumApiVersion": "1.10",
"Description": "Buy recipes you don't know from a magazine rack in Pierre's shop",
"Description": "Buy recipes you don't know from the SMAPI console. Coming soon to a magazine rack in Pierre's shop.",
"UniqueID": "Denifia.BuyRecipes",
"EntryDll": "Denifia.Stardew.BuyRecipes.dll"
}

0 comments on commit 32d9425

Please sign in to comment.