From 68f18f228ef27e46eaf3623481977c54972f9d01 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 14 May 2020 20:02:34 +0300 Subject: Did Service/Tiles/Crafting refactorization and documentation. --- .../Service/Tiles/Crafting/CraftingController.cs | 66 +++---- Mundus/Service/Tiles/Crafting/CraftingRecipe.cs | 194 +++++++++++---------- 2 files changed, 142 insertions(+), 118 deletions(-) diff --git a/Mundus/Service/Tiles/Crafting/CraftingController.cs b/Mundus/Service/Tiles/Crafting/CraftingController.cs index 64cdc27..4fe0279 100644 --- a/Mundus/Service/Tiles/Crafting/CraftingController.cs +++ b/Mundus/Service/Tiles/Crafting/CraftingController.cs @@ -1,48 +1,45 @@ -using System.Collections.Generic; -using System.Linq; -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; -using Mundus.Data; - -namespace Mundus.Service.Tiles.Crafting { - public static class CraftingController { - /// - /// Returns all recipes that can be executed with the current items in the player inventory (Inventory.Items) - /// - /// All avalable recipies. - public static CraftingRecipe[] GetAvalableRecipes() { - return DataBaseContexts.CTContext.GetAvalableRecipes(); - } +namespace Mundus.Service.Tiles.Crafting +{ + using System.Linq; + using Mundus.Data; + using Mundus.Data.Superlayers.Mobs; + using Mundus.Service.Tiles.Items; + using Mundus.Service.Tiles.Items.Presets; + using Mundus.Service.Tiles.Mobs; + public static class CraftingController + { /// - /// Removes items, used for crafting and adds the result item to the inventory + /// Removes items and adds the result item to the inventory /// - /// 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 + public static void CraftItem(CraftingRecipe itemRecipe, MobTile mob) + { + // 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(); + + var allInventoryItems = 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++) + for (int i = 0, removed = 0; i < allInventoryItems.Length && removed < reqCounts[item]; i++) { - if (inventoryItems[i].stock_id == reqItems[item]) { + if (allInventoryItems[i].stock_id == reqItems[item]) + { mob.Inventory.DeleteFromItems(i); removed++; } } } - ItemTile tmp = ToolPresets.GetFromStock(itemRecipe.ResultItem); - if (tmp == null) { - tmp = StructurePresets.GetFromStock(itemRecipe.ResultItem); + ItemTile result = ToolPresets.GetFromStock(itemRecipe.ResultItem); + + if (result == null) + { + result = StructurePresets.GetFromStock(itemRecipe.ResultItem); } - MI.Player.Inventory.AppendToItems(tmp); + + MI.Player.Inventory.AppendToItems(result); Data.Windows.WI.SelWin.PrintInventory(); } @@ -51,8 +48,17 @@ namespace Mundus.Service.Tiles.Crafting { /// Does CraftItem method for the player /// /// CraftingRecipie of the item that will be crafted - public static void CraftItemPlayer(CraftingRecipe itemRecipe) { + public static void CraftItemPlayer(CraftingRecipe itemRecipe) + { CraftItem(itemRecipe, MI.Player); } + + /// + /// Returns all recipes that can be executed with the current items in the player inventory (Inventory.Items) + /// + public static CraftingRecipe[] GetAvalableRecipes() + { + return DataBaseContexts.CTContext.GetAvalableRecipes(); + } } } \ No newline at end of file diff --git a/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs b/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs index 148e76b..3d495a3 100644 --- a/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs +++ b/Mundus/Service/Tiles/Crafting/CraftingRecipe.cs @@ -1,137 +1,155 @@ -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 { - [Table("CraftingRecipes", 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 string ResultItem { get; private set; } - - /// - /// Required amount of the first item - /// - public int Count1 { get; private set; } - /// - /// Required first item - /// - public string ReqItem1 { get; private set; } - - /// - /// Required amount of the second item - /// - public int Count2 { get; private set; } - /// - /// Required second item - /// - public string ReqItem2 { get; private set; } - - /// - /// Required amount of the third item - /// - public int Count3 { get; private set; } - /// - /// Required third item - /// - public string ReqItem3 { get; private set; } +namespace Mundus.Service.Tiles.Crafting +{ + using System.ComponentModel.DataAnnotations; + using System.ComponentModel.DataAnnotations.Schema; + using System.Linq; + using Mundus.Service.Tiles.Items; - /// - /// Required amount of the fourth item - /// - public int Count4 { get; private set; } - /// - /// Required fourth item - /// - public string ReqItem4 { get; private set; } - - /// - /// Required amount of the fifth item - /// - public int Count5 { get; private set; } + [Table("CraftingRecipes", Schema = "Mundus")] + public class CraftingRecipe + { /// /// Required fifth item /// public string ReqItem5 { get; private set; } - public CraftingRecipe(string resultItem, int count1, string 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(string resultItem, int count1, string reqItem1, int count2, string 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, int count3, string 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, int count4, string 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, int count5, string 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; //reqItem2.stock_id; + this.ReqItem2 = reqItem2; this.Count3 = count3; - this.ReqItem3 = reqItem3; //reqItem3.stock_id; + this.ReqItem3 = reqItem3; this.Count4 = count4; - this.ReqItem4 = reqItem4; //reqItem4.stock_id; + this.ReqItem4 = reqItem4; this.Count5 = count5; - this.ReqItem5 = reqItem5; //reqItem5.stock_id; + this.ReqItem5 = reqItem5; } + + [Key] + public int ID { get; set; } + + /// + /// Item that will be added to the inventory after crafting + /// + /// The result item. + public string ResultItem { get; private set; } + + /// + /// Required amount of the first item + /// + public int Count1 { get; private set; } + + /// + /// Required first item + /// + public string ReqItem1 { get; private set; } + + /// + /// Required amount of the second item + /// + public int Count2 { get; private set; } + + /// + /// Required second item + /// + public string ReqItem2 { get; private set; } + + /// + /// Required amount of the third item + /// + public int Count3 { get; private set; } + + /// + /// Required third item + /// + public string ReqItem3 { get; private set; } + + /// + /// Required amount of the fourth item + /// + public int Count4 { get; private set; } + + /// + /// Required fourth item + /// + public string ReqItem4 { get; private set; } + + /// + /// Required amount of the fifth item + /// + public int Count5 { get; private set; } - //ugly af, but will rewrite when I imntegrade data bases /// - /// Checks if the parameter has enough of every requried item + /// Checks if the given array of items has enough of every requried item /// /// trueIf has enoughfalseotherwise - /// Player items inventory (Inventory.Items) - public bool HasEnoughItems(ItemTile[] items) { + public bool HasEnoughItems(ItemTile[] items) + { bool hasEnough = false; - if (items.Any(r => r != null)) { - var allItems = items.Where(x => x != null).Select(x => x.stock_id).ToArray(); + if (items.Any(item => item != null)) + { + var allItemStocks = items.Where(x => x != null).Select(x => x.stock_id).ToArray(); - hasEnough = allItems.Contains(ReqItem1) && - allItems.Count(i => i == ReqItem1) >= Count1; + hasEnough = allItemStocks.Contains(ReqItem1) && + allItemStocks.Count(i => i == ReqItem1) >= Count1; - if (ReqItem2 != null && hasEnough) { - hasEnough = allItems.Contains(ReqItem2) && - allItems.Count(i => i == ReqItem2) >= Count2; + if (ReqItem2 != null && hasEnough) + { + hasEnough = allItemStocks.Contains(ReqItem2) && + allItemStocks.Count(i => i == ReqItem2) >= Count2; } - if (ReqItem3 != null && hasEnough) { - hasEnough = allItems.Contains(ReqItem3) && - allItems.Count(i => i == ReqItem3) >= Count3; + if (ReqItem3 != null && hasEnough) + { + hasEnough = allItemStocks.Contains(ReqItem3) && + allItemStocks.Count(i => i == ReqItem3) >= Count3; } - if (ReqItem4 != null && hasEnough) { - hasEnough = allItems.Contains(ReqItem4) && - allItems.Count(i => i == ReqItem4) >= Count4; + if (ReqItem4 != null && hasEnough) + { + hasEnough = allItemStocks.Contains(ReqItem4) && + allItemStocks.Count(i => i == ReqItem4) >= Count4; } - if (ReqItem5 != null && hasEnough) { - hasEnough = allItems.Contains(ReqItem5) && - allItems.Count(i => i == ReqItem5) >= Count5; + if (ReqItem5 != null && hasEnough) + { + hasEnough = allItemStocks.Contains(ReqItem5) && + allItemStocks.Count(i => i == ReqItem5) >= Count5; } } return hasEnough; } - public string[] GetAllRequiredItems() { + public string[] GetAllRequiredItems() + { return new string[] { ReqItem1, ReqItem2, ReqItem3, ReqItem4, ReqItem5}; } - public int[] GetAllCounts() { + public int[] GetAllCounts() + { return new int[] { Count1, Count2, Count3, Count4, Count5}; } } -- cgit v1.2.3