From 2962d37ae5d1a2590d48cd236d7765bc52158ec0 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 10 May 2020 15:12:24 +0300 Subject: Transferred recipes to MySQL database (database Mundus, table Recipes) using Entity Framework Core. --- Mundus/App.config | 12 ++ Mundus/Data/Crafting/CraftingTableContext.cs | 52 ++++++++ Mundus/Data/Crafting/RI.cs | 41 ------ Mundus/Data/Windows/WI.cs | 2 +- Mundus/Mundus.csproj | 142 ++++++++++++++++++++- Mundus/Program.cs | 9 +- .../Service/Tiles/Crafting/CraftingController.cs | 62 +++------ Mundus/Service/Tiles/Crafting/CraftingRecipe.cs | 125 ++++++++---------- .../Tiles/Items/Presets/StructurePresets.cs | 7 + Mundus/Service/Tiles/Items/Presets/ToolPresets.cs | 16 ++- Mundus/Views/Windows/CraftingWindow.cs | 18 +-- Mundus/packages.config | 39 ++++++ 12 files changed, 348 insertions(+), 177 deletions(-) create mode 100644 Mundus/App.config create mode 100644 Mundus/Data/Crafting/CraftingTableContext.cs delete mode 100644 Mundus/Data/Crafting/RI.cs diff --git a/Mundus/App.config b/Mundus/App.config new file mode 100644 index 0000000..d9ec5bd --- /dev/null +++ b/Mundus/App.config @@ -0,0 +1,12 @@ + + + + +
+ + + + + + + \ No newline at end of file diff --git a/Mundus/Data/Crafting/CraftingTableContext.cs b/Mundus/Data/Crafting/CraftingTableContext.cs new file mode 100644 index 0000000..0a8c6c1 --- /dev/null +++ b/Mundus/Data/Crafting/CraftingTableContext.cs @@ -0,0 +1,52 @@ +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Mundus.Data.Superlayers.Mobs; +using Mundus.Service.Tiles.Crafting; +using Mundus.Service.Tiles.Items.Presets; + +namespace Mundus.Data.Crafting { + public class CraftingTableContext : DbContext { + public DbSet CraftingRecipes { get; set; } + + public CraftingTableContext() : base() + { } + + + public void AddRecipes() { + ResetTable(); + + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetAWoodenShovel().stock_id, 5, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetAWoodenPickaxe().stock_id, 4, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetAWoodenAxe().stock_id, 3, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetAWoodenLongsword().stock_id, 4, MaterialPresets.GetALandStick().stock_id)); + + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetARockShovel().stock_id, 4, MaterialPresets.GetALandRock().stock_id, 2, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetARockPickaxe().stock_id, 4, MaterialPresets.GetALandRock().stock_id, 2, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetARockAxe().stock_id, 3, MaterialPresets.GetALandRock().stock_id, 2, MaterialPresets.GetALandStick().stock_id)); + CraftingRecipes.Add(new CraftingRecipe(ToolPresets.GetARockLongsword().stock_id, 5, MaterialPresets.GetALandRock().stock_id, 2, MaterialPresets.GetALandStick().stock_id)); + + CraftingRecipes.Add(new CraftingRecipe(StructurePresets.GetAWoodenLadder().inventory_stock_id, 6, MaterialPresets.GetALandStick().stock_id)); + + this.SaveChanges(); + } + + private void ResetTable() { + CraftingRecipes.RemoveRange(CraftingRecipes); + } + + public CraftingRecipe[] GetAvalableRecipes() { + var recipes = CraftingRecipes.ToArray(); + return recipes.Where(cr => cr.HasEnoughItems(MI.Player.Inventory.Items)).ToArray(); + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + optionsBuilder.UseMySQL( + "server=localhost;" + + "port=3306;" + + "user id=root; " + + "password=password; " + + "database=Mundus; " + + "SslMode=none"); + } + } +} diff --git a/Mundus/Data/Crafting/RI.cs b/Mundus/Data/Crafting/RI.cs deleted file mode 100644 index fd06dc4..0000000 --- a/Mundus/Data/Crafting/RI.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Collections.Generic; -using Mundus.Service.Tiles.Crafting; -using Mundus.Service.Tiles.Items.Presets; - -namespace Mundus.Data.Crafting { - public static class RI { //short for Recipe Instances - public static List AllRecipies { get; private set; } - - public static CraftingRecipe WoodenShovel { get; private set; } - public static CraftingRecipe WoodenPickaxe { get; private set; } - public static CraftingRecipe WoodenAxe { get; private set; } - public static CraftingRecipe WoodenLongsword { get; private set; } - - public static CraftingRecipe RockShovel { get; private set; } - public static CraftingRecipe RockPickaxe { get; private set; } - public static CraftingRecipe RockAxe { get; private set; } - public static CraftingRecipe RockLongsword { get; private set; } - - public static CraftingRecipe WoodenLadder { get; private set; } - - public static void CreateInstances() { - WoodenShovel = new CraftingRecipe(ToolPresets.GetAWoodenShovel(), 5, MaterialPresets.GetALandStick()); - WoodenPickaxe = new CraftingRecipe(ToolPresets.GetAWoodenPickaxe(), 4, MaterialPresets.GetALandStick()); - WoodenAxe = new CraftingRecipe(ToolPresets.GetAWoodenAxe(), 3, MaterialPresets.GetALandStick()); - WoodenLongsword = new CraftingRecipe(ToolPresets.GetAWoodenLongsword(), 4, MaterialPresets.GetALandStick()); - - RockShovel = new CraftingRecipe(ToolPresets.GetARockShovel(), 4, MaterialPresets.GetALandRock(), 2, MaterialPresets.GetALandStick()); - RockPickaxe = new CraftingRecipe(ToolPresets.GetARockPickaxe(), 4, MaterialPresets.GetALandRock(), 2, MaterialPresets.GetALandStick()); - RockAxe = new CraftingRecipe(ToolPresets.GetARockAxe(), 3, MaterialPresets.GetALandRock(), 2, MaterialPresets.GetALandStick()); - RockLongsword = new CraftingRecipe(ToolPresets.GetARockLongsword(), 5, MaterialPresets.GetALandRock(), 2, MaterialPresets.GetALandStick()); - - WoodenLadder = new CraftingRecipe(StructurePresets.GetAWoodenLadder(), 6, MaterialPresets.GetALandStick()); - - AllRecipies = new List { - WoodenShovel, WoodenPickaxe, WoodenAxe, WoodenLongsword, - RockShovel, RockPickaxe, RockAxe, RockLongsword, - WoodenLadder - }; - } - } -} diff --git a/Mundus/Data/Windows/WI.cs b/Mundus/Data/Windows/WI.cs index 70cb2bd..dbba9a8 100644 --- a/Mundus/Data/Windows/WI.cs +++ b/Mundus/Data/Windows/WI.cs @@ -2,7 +2,7 @@ namespace Mundus.Data.Windows { public static class WI { //stands for Window Instances - public const string BuildName = "Build 01-05-2020 No3"; + public const string BuildName = "Requirements Build"; public static IGameWindow SelWin { get; set; } diff --git a/Mundus/Mundus.csproj b/Mundus/Mundus.csproj index 206e1a9..1a23716 100644 --- a/Mundus/Mundus.csproj +++ b/Mundus/Mundus.csproj @@ -32,6 +32,144 @@ + + ..\packages\System.Data.SqlClient.4.8.1\lib\net461\System.Data.SqlClient.dll + + + + + + + ..\packages\BouncyCastle.1.8.3.1\lib\BouncyCastle.Crypto.dll + + + ..\packages\Google.Protobuf.3.6.1\lib\net45\Google.Protobuf.dll + + + ..\packages\SSH.NET.2016.1.0\lib\net40\Renci.SshNet.dll + + + ..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll + + + ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll + + + ..\packages\K4os.Compression.LZ4.1.1.11\lib\net46\K4os.Compression.LZ4.dll + + + ..\packages\K4os.Hash.xxHash.1.0.6\lib\net46\K4os.Hash.xxHash.dll + + + ..\packages\K4os.Compression.LZ4.Streams.1.1.11\lib\net46\K4os.Compression.LZ4.Streams.dll + + + ..\packages\MySql.Data.8.0.20\lib\net452\MySql.Data.dll + + + ..\packages\MySql.Data.8.0.20\lib\net452\Ubiety.Dns.Core.dll + + + ..\packages\MySql.Data.8.0.20\lib\net452\Zstandard.Net.dll + + + + + + + + + + + + ..\packages\Microsoft.Bcl.HashCode.1.1.0\lib\net461\Microsoft.Bcl.HashCode.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.3.1.3\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Logging.Abstractions.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\System.ComponentModel.Annotations.4.7.0\lib\net461\System.ComponentModel.Annotations.dll + + + ..\packages\Microsoft.Extensions.Primitives.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll + + + ..\packages\Microsoft.Extensions.Caching.Abstractions.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Caching.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Configuration.Abstractions.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Configuration.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll + + + ..\packages\Microsoft.Extensions.Configuration.Binder.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Configuration.Binder.dll + + + ..\packages\Microsoft.Extensions.Options.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Options.dll + + + ..\packages\Microsoft.Extensions.Caching.Memory.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Caching.Memory.dll + + + ..\packages\System.Collections.Immutable.1.7.0\lib\netstandard2.0\System.Collections.Immutable.dll + + + ..\packages\System.Diagnostics.DiagnosticSource.4.7.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + + + ..\packages\Microsoft.Bcl.AsyncInterfaces.1.1.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.3.1.3\lib\net461\Microsoft.Extensions.DependencyInjection.dll + + + ..\packages\Microsoft.Extensions.Logging.3.1.3\lib\netstandard2.0\Microsoft.Extensions.Logging.dll + + + + ..\packages\Microsoft.EntityFrameworkCore.Abstractions.3.1.3\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Abstractions.dll + + + ..\packages\Microsoft.EntityFrameworkCore.3.1.3\lib\netstandard2.0\Microsoft.EntityFrameworkCore.dll + + + ..\packages\Microsoft.Identity.Client.3.0.8\lib\net45\Microsoft.Identity.Client.dll + + + + + + + + + ..\packages\System.Data.Common.4.3.0\lib\net451\System.Data.Common.dll + + + ..\packages\Microsoft.Data.SqlClient.1.0.19269.1\lib\net46\Microsoft.Data.SqlClient.dll + + + ..\packages\Microsoft.EntityFrameworkCore.Relational.3.1.3\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Relational.dll + + + ..\packages\Microsoft.EntityFrameworkCore.SqlServer.3.1.3\lib\netstandard2.0\Microsoft.EntityFrameworkCore.SqlServer.dll + + + ..\packages\MySql.Data.EntityFrameworkCore.8.0.20\lib\netstandard2.0\MySql.Data.EntityFrameworkCore.dll + @@ -121,7 +259,6 @@ - @@ -155,6 +292,7 @@ + @@ -183,6 +321,8 @@ + + \ No newline at end of file diff --git a/Mundus/Program.cs b/Mundus/Program.cs index 7ca908c..48992b5 100644 --- a/Mundus/Program.cs +++ b/Mundus/Program.cs @@ -6,19 +6,26 @@ using Mundus.Data.SuperLayers; using Mundus.Data.Windows; using Mundus.Data; using Mundus.Service; +using Mundus.Service.Tiles.Crafting; +using Mundus.Service.Tiles.Items.Presets; namespace Mundus { public static class MainClass { public static bool runGame = true; + public static CraftingTableContext CTController { get; private set; } public static void Main(string[] args) { + //DBController = new DatabaseController(); + //Data.Crafting.CraftingTableContext.CreateRecipes(); + CTController = new CraftingTableContext(); + CTController.AddRecipes(); + Application.Init(); //All windows and dialogues that are used by user (instances) are saved and created in WindowInstances.cs LogController.Initialize(); WI.CreateInstances(); DI.CreateInstances(); LI.CreateInstances(); - RI.CreateInstances(); WI.WMain.Show(); Application.Run(); diff --git a/Mundus/Service/Tiles/Crafting/CraftingController.cs b/Mundus/Service/Tiles/Crafting/CraftingController.cs index 61bc897..aedaa3a 100644 --- a/Mundus/Service/Tiles/Crafting/CraftingController.cs +++ b/Mundus/Service/Tiles/Crafting/CraftingController.cs @@ -4,38 +4,16 @@ using Mundus.Data.Crafting; using Mundus.Data.Superlayers.Mobs; using Mundus.Service.Tiles.Mobs; using Mundus.Service.Tiles.Items; +using Mundus.Service.Tiles.Items.Presets; namespace Mundus.Service.Tiles.Crafting { public static class CraftingController { - private static Dictionary avalableItems; - /// /// Returns all recipes that can be executed with the current items in the player inventory (Inventory.Items) /// /// All avalable recipies. public static CraftingRecipe[] GetAvalableRecipes() { - FindAvalableItems(); - - List recipes = new List(); - - foreach (var recipe in RI.AllRecipies) { - if (recipe.HasEnoughItems(avalableItems)) { - recipes.Add(recipe); - } - } - - return recipes.ToArray(); - } - - // Sets avalableItems to all items in the player inventory (Inventory.Items) - private static void FindAvalableItems() { - avalableItems = MI.Player.Inventory.Items.Where(x => x != null) - //Can't use distinct on non primative types, beause they also hold their memory location info (I think). - //This is my way of getting only the "unique" item tiles. - .Select(x => x.stock_id).Distinct().Select(x => MI.Player.Inventory.Items.Where(y => y != null).First(y => y.stock_id == x)) - //For each "unique" item tile (key), get how many there are of it in the player inventory (value) - .Select(x => new KeyValuePair(x, MI.Player.Inventory.Items.Where(y => y != null).Count(i => i.stock_id == x.stock_id))) - .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + return MainClass.CTController.GetAvalableRecipes(); } /// @@ -43,31 +21,25 @@ namespace Mundus.Service.Tiles.Crafting { /// /// CraftingRecipie of the item that will be crafted public static void CraftItem(CraftingRecipe itemRecipe, MobTile mob) { - // Removes all items that are used to craft the result item - foreach (var itemAndCount in itemRecipe.GetRequiredItemsAndCounts()) { - for(int i = 0, removedItems = 0; i < mob.Inventory.Items.Length && removedItems < itemAndCount.Value; i++) { - if (MI.Player.Inventory.Items[i] != null) { - if (MI.Player.Inventory.Items[i].stock_id == itemAndCount.Key.stock_id) { - MI.Player.Inventory.Items[i] = null; - removedItems++; - } + //Removes all items that are used to craft the result item + var reqItems = itemRecipe.GetAllRequiredItems(); + var reqCounts = itemRecipe.GetAllCounts(); + var inventoryItems = mob.Inventory.Items.Where(i => i != null).ToArray(); + + for (int item = 0; item < reqItems.Length; item++) + { + for (int i = 0, removed = 0; i < inventoryItems.Length && removed < reqCounts[item]; i++) + { + if (inventoryItems[i].stock_id == reqItems[item]) { + mob.Inventory.DeleteFromItems(i); + removed++; } } } - ItemTile tmp = null; - // Adds the result item to the inventory (in the correct data type) - if (itemRecipe.ResultItem.GetType() == typeof(Material)) { - tmp = new Material((Material)itemRecipe.ResultItem); - } - if (itemRecipe.ResultItem.GetType() == typeof(Tool)) { - tmp = new Tool((Tool)itemRecipe.ResultItem); - } - if (itemRecipe.ResultItem.GetType() == typeof(Gear)) { - tmp = new Gear((Gear)itemRecipe.ResultItem); - } - if (itemRecipe.ResultItem.GetType() == typeof(Structure)) { - tmp = new Structure((Structure)itemRecipe.ResultItem); + ItemTile tmp = ToolPresets.GetFromStock(itemRecipe.ResultItem); + if (tmp == null) { + tmp = StructurePresets.GetFromStock(itemRecipe.ResultItem); } MI.Player.Inventory.AppendToItems(tmp); diff --git a/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs b/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs index af7ec7e..85833e2 100644 --- a/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs +++ b/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs @@ -1,14 +1,19 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using Mundus.Service.Tiles.Items; -namespace Mundus.Service.Tiles.Crafting { +namespace Mundus.Service.Tiles.Crafting { + [Table("Recipes", Schema = "Mundus")] public class CraftingRecipe { + [Key] + public int ID { get; set; } /// /// Item that will be added to the inventory after crafting /// /// The result item. - public ItemTile ResultItem { get; private set; } + public string ResultItem { get; private set; } /// /// Required amount of the first item @@ -17,7 +22,7 @@ namespace Mundus.Service.Tiles.Crafting { /// /// Required first item /// - public ItemTile ReqItem1 { get; private set; } + public string ReqItem1 { get; private set; } /// /// Required amount of the second item @@ -26,7 +31,7 @@ namespace Mundus.Service.Tiles.Crafting { /// /// Required second item /// - public ItemTile ReqItem2 { get; private set; } + public string ReqItem2 { get; private set; } /// /// Required amount of the third item @@ -35,7 +40,7 @@ namespace Mundus.Service.Tiles.Crafting { /// /// Required third item /// - public ItemTile ReqItem3 { get; private set; } + public string ReqItem3 { get; private set; } /// /// Required amount of the fourth item @@ -44,7 +49,7 @@ namespace Mundus.Service.Tiles.Crafting { /// /// Required fourth item /// - public ItemTile ReqItem4 { get; private set; } + public string ReqItem4 { get; private set; } /// /// Required amount of the fifth item @@ -53,37 +58,37 @@ namespace Mundus.Service.Tiles.Crafting { /// /// Required fifth item /// - public ItemTile ReqItem5 { get; private set; } + public string ReqItem5 { get; private set; } - public CraftingRecipe(ItemTile resultItem, int count1, ItemTile reqItem1) :this(resultItem, count1, reqItem1, 0, null, 0, null, 0, null, 0, null) + public CraftingRecipe(string resultItem, int count1, string reqItem1) :this(resultItem, count1, reqItem1, 0, null, 0, null, 0, null, 0, null) { } - public CraftingRecipe(ItemTile resultItem, int count1, ItemTile reqItem1, int count2, ItemTile reqItem2) : this(resultItem, count1, reqItem1, count2, reqItem2, 0, null, 0, null, 0, null) + public CraftingRecipe(string resultItem, int count1, string reqItem1, int count2, string reqItem2) : this(resultItem, count1, reqItem1, count2, reqItem2, 0, null, 0, null, 0, null) { } - public CraftingRecipe(ItemTile resultItem, int count1, ItemTile reqItem1, int count2, ItemTile reqItem2, int count3, ItemTile reqItem3) : this(resultItem, count1, reqItem1, count2, reqItem2, count3, reqItem3, 0, null, 0, null) + public CraftingRecipe(string resultItem, int count1, string reqItem1, int count2, string reqItem2, int count3, string reqItem3) : this(resultItem, count1, reqItem1, count2, reqItem2, count3, reqItem3, 0, null, 0, null) { } - public CraftingRecipe(ItemTile resultItem, int count1, ItemTile reqItem1, int count2, ItemTile reqItem2, int count3, ItemTile reqItem3, int count4, ItemTile reqItem4) : this(resultItem, count1, reqItem1, count2, reqItem2, count3, reqItem3, count4, reqItem4, 0, null) + public CraftingRecipe(string resultItem, int count1, string reqItem1, int count2, string reqItem2, int count3, string reqItem3, int count4, string reqItem4) : this(resultItem, count1, reqItem1, count2, reqItem2, count3, reqItem3, count4, reqItem4, 0, null) { } - public CraftingRecipe(ItemTile resultItem, int count1, ItemTile reqItem1, int count2, ItemTile reqItem2, int count3, ItemTile reqItem3, int count4, ItemTile reqItem4, int count5, ItemTile reqItem5) { + public CraftingRecipe(string resultItem, int count1, string reqItem1, int count2, string reqItem2, int count3, string reqItem3, int count4, string reqItem4, int count5, string reqItem5) { this.ResultItem = resultItem; this.Count1 = count1; this.ReqItem1 = reqItem1; this.Count2 = count2; - this.ReqItem2 = reqItem2; + this.ReqItem2 = reqItem2; //reqItem2.stock_id; this.Count3 = count3; - this.ReqItem3 = reqItem3; + this.ReqItem3 = reqItem3; //reqItem3.stock_id; this.Count4 = count4; - this.ReqItem4 = reqItem4; + this.ReqItem4 = reqItem4; //reqItem4.stock_id; this.Count5 = count5; - this.ReqItem5 = reqItem5; + this.ReqItem5 = reqItem5; //reqItem5.stock_id; } //ugly af, but will rewrite when I imntegrade data bases @@ -91,73 +96,43 @@ namespace Mundus.Service.Tiles.Crafting { /// Checks if the parameter has enough of every requried item /// /// trueIf has enoughfalseotherwise - /// Dictionary that has the items and their respective amounts (that will be checked) - public bool HasEnoughItems(Dictionary itemsAndCounts) { - bool hasEnough = true; + /// Player items inventory (Inventory.Items) + public bool HasEnoughItems(ItemTile[] items) { + bool hasEnough = false; - if (ReqItem1 != null && hasEnough) { - if (itemsAndCounts.Keys.Any(k => k.stock_id == ReqItem1.stock_id)) { - hasEnough = itemsAndCounts.First(x => x.Key.stock_id == ReqItem1.stock_id).Value >= Count1; - } - else hasEnough = false; - } - - if (ReqItem2 != null && hasEnough) { - if (itemsAndCounts.Keys.Any(k => k.stock_id == ReqItem2.stock_id)) { - hasEnough = itemsAndCounts.First(x => x.Key.stock_id == ReqItem2.stock_id).Value >= Count2; + if (items.Any(r => r != null)) { + var allItems = items.Where(x => x != null).Select(x => x.stock_id).ToArray(); + + hasEnough = allItems.Contains(ReqItem1) && + allItems.Count(i => i == ReqItem1) >= Count1; + + if (ReqItem2 != null && hasEnough) { + hasEnough = allItems.Contains(ReqItem2) && + allItems.Count(i => i == ReqItem2) >= Count2; + } + if (ReqItem3 != null && hasEnough) { + hasEnough = allItems.Contains(ReqItem3) && + allItems.Count(i => i == ReqItem3) >= Count3; + } + if (ReqItem4 != null && hasEnough) { + hasEnough = allItems.Contains(ReqItem4) && + allItems.Count(i => i == ReqItem4) >= Count4; + } + if (ReqItem5 != null && hasEnough) { + hasEnough = allItems.Contains(ReqItem5) && + allItems.Count(i => i == ReqItem5) >= Count5; } - else hasEnough = false; - } - - if (ReqItem3 != null && hasEnough) { - if (itemsAndCounts.Keys.Any(k => k.stock_id == ReqItem3.stock_id)) { - hasEnough = itemsAndCounts.First(x => x.Key.stock_id == ReqItem3.stock_id).Value >= Count3; - } - else hasEnough = false; - } - - if (ReqItem4 != null && hasEnough) { - if (itemsAndCounts.Keys.Any(k => k.stock_id == ReqItem4.stock_id)) { - hasEnough = itemsAndCounts.First(x => x.Key.stock_id == ReqItem4.stock_id).Value >= Count4; - } - else hasEnough = false; - } - - if (ReqItem5 != null && hasEnough) { - if (itemsAndCounts.Keys.Any(k => k.stock_id == ReqItem5.stock_id)) { - hasEnough = itemsAndCounts.First(x => x.Key.stock_id == ReqItem5.stock_id).Value >= Count5; - } - else hasEnough = false; } return hasEnough; } - /// - /// Checks if the given item (and amount) is enough for the recipe - /// - public bool HasEnoughOfItem(ItemTile item, int count) { - if (ReqItem1.stock_id == item.stock_id) return count >= Count1; - if (ReqItem2.stock_id == item.stock_id) return count >= Count2; - if (ReqItem3.stock_id == item.stock_id) return count >= Count3; - if (ReqItem4.stock_id == item.stock_id) return count >= Count4; - if (ReqItem5.stock_id == item.stock_id) return count >= Count5; - return false; - } - - /// - /// Returns a dictionary of every required item and their respective amount - /// - public Dictionary GetRequiredItemsAndCounts() { - Dictionary req = new Dictionary(); - - req.Add(ReqItem1, Count1); - if (ReqItem2 != null) req.Add(ReqItem2, Count2); - if (ReqItem3 != null) req.Add(ReqItem3, Count3); - if (ReqItem4 != null) req.Add(ReqItem4, Count4); - if (ReqItem5 != null) req.Add(ReqItem5, Count5); + public string[] GetAllRequiredItems() { + return new string[] { ReqItem1, ReqItem2, ReqItem3, ReqItem4, ReqItem5}; + } - return req; + public int[] GetAllCounts() { + return new int[] { Count1, Count2, Count3, Count4, Count5}; } } } diff --git a/Mundus/Service/Tiles/Items/Presets/StructurePresets.cs b/Mundus/Service/Tiles/Items/Presets/StructurePresets.cs index 1834de4..e71111a 100644 --- a/Mundus/Service/Tiles/Items/Presets/StructurePresets.cs +++ b/Mundus/Service/Tiles/Items/Presets/StructurePresets.cs @@ -26,5 +26,12 @@ namespace Mundus.Service.Tiles.Items.Presets { public static Structure GetAWoodenLadder() { return new Structure("L_wooden_ladder", "L_wooden_ladder_inventory", 1, ToolTypes.Axe, 1, true, true); } + + public static Structure GetFromStock(string stock_id) { + switch(stock_id) { + case "L_wooden_ladder": return GetAWoodenLadder(); + default: return null; + } + } } } diff --git a/Mundus/Service/Tiles/Items/Presets/ToolPresets.cs b/Mundus/Service/Tiles/Items/Presets/ToolPresets.cs index d18f089..a52e027 100644 --- a/Mundus/Service/Tiles/Items/Presets/ToolPresets.cs +++ b/Mundus/Service/Tiles/Items/Presets/ToolPresets.cs @@ -25,7 +25,7 @@ namespace Mundus.Service.Tiles.Items.Presets { public static Tool GetARockAxe() { return new Tool("rock_axe", ToolTypes.Axe, 2); } - + public static Tool GetARockShovel() { return new Tool("rock_shovel", ToolTypes.Shovel, 2); } @@ -33,5 +33,19 @@ namespace Mundus.Service.Tiles.Items.Presets { public static Tool GetARockLongsword() { return new Tool("rock_longsword", ToolTypes.Sword, 4); } + + public static Tool GetFromStock(string stock_id) { + switch(stock_id) { + case "wooden_pickaxe": return GetAWoodenPickaxe(); + case "wooden_axe": return GetAWoodenAxe(); + case "wooden_shovel": return GetAWoodenShovel(); + case "wooden_longsword": return GetAWoodenLongsword(); + case "rock_pickaxe": return GetARockPickaxe(); + case "rock_axe": return GetARockAxe(); + case "rock_shovel": return GetARockShovel(); + case "rock_longsword": return GetARockLongsword(); + default: return null; + } + } } } diff --git a/Mundus/Views/Windows/CraftingWindow.cs b/Mundus/Views/Windows/CraftingWindow.cs index 56d9549..43ba6ff 100644 --- a/Mundus/Views/Windows/CraftingWindow.cs +++ b/Mundus/Views/Windows/CraftingWindow.cs @@ -37,36 +37,30 @@ namespace Mundus.Views.Windows { CraftingRecipe recipe = Recipes[recipeIndex]; btnCraft.Sensitive = true; - if (recipe.ResultItem.GetType() == typeof(Structure)) { - Structure tmp = (Structure)recipe.ResultItem; - imgItem.SetFromStock(tmp.inventory_stock_id, IconSize.Dnd); - } - else { - imgItem.SetFromStock(recipe.ResultItem.stock_id, IconSize.Dnd); - } + imgItem.SetFromStock(recipe.ResultItem, IconSize.Dnd); lblInfo.Text = recipe.ResultItem.ToString(); lblC1.Text = recipe.Count1 + ""; - imgI1.SetFromStock(recipe.ReqItem1.stock_id, IconSize.Dnd); + imgI1.SetFromStock(recipe.ReqItem1, IconSize.Dnd); if (recipe.ReqItem2 != null) { lblC2.Text = recipe.Count2 + ""; - imgI2.SetFromStock(recipe.ReqItem2.stock_id, IconSize.Dnd); + imgI2.SetFromStock(recipe.ReqItem2, IconSize.Dnd); } if (recipe.ReqItem3 != null) { lblC3.Text = recipe.Count3 + ""; - imgI3.SetFromStock(recipe.ReqItem3.stock_id, IconSize.Dnd); + imgI3.SetFromStock(recipe.ReqItem3, IconSize.Dnd); } if (recipe.ReqItem4 != null) { lblC4.Text = recipe.Count4 + ""; - imgI4.SetFromStock(recipe.ReqItem4.stock_id, IconSize.Dnd); + imgI4.SetFromStock(recipe.ReqItem4, IconSize.Dnd); } if (recipe.ReqItem5 != null) { lblC5.Text = recipe.Count5 + ""; - imgI5.SetFromStock(recipe.ReqItem5.stock_id, IconSize.Dnd); + imgI5.SetFromStock(recipe.ReqItem5, IconSize.Dnd); } } } diff --git a/Mundus/packages.config b/Mundus/packages.config index cc1fbe7..cc8415d 100644 --- a/Mundus/packages.config +++ b/Mundus/packages.config @@ -1,4 +1,43 @@  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3