From 6407e4ad0f8a11ef1ad2c3cb9dc473e6058daa3a Mon Sep 17 00:00:00 2001 From: Thomas Chazot Date: Tue, 20 Dec 2022 10:24:54 +0100 Subject: [PATCH] Add: Sauvegarde en json + taille de l'inventaire en fonction de la config --- BlazorApp/BlazorApp/BlazorApp.csproj | 2 + .../BlazorApp/Components/InventoryAction.cs | 13 +++++ .../BlazorApp/Components/InventoryComp.razor | 2 +- .../Components/InventoryComp.razor.cs | 53 ++++++++++++++++--- .../Components/InventoryComp.razor.js | 16 ++++++ .../Components/InventoryItem.razor.cs | 11 ++-- BlazorApp/BlazorApp/Models/Item.cs | 30 +++++++---- BlazorApp/BlazorApp/Models/SavableItem.cs | 18 +++++++ BlazorApp/BlazorApp/Pages/Inventory.razor | 2 +- BlazorApp/BlazorApp/Program.cs | 5 ++ .../BlazorApp/SavedData/InventoryItems.json | 1 + .../BlazorApp/Services/ILoaderInventory.cs | 11 ++++ .../BlazorApp/Services/ISaverInventory.cs | 11 ++++ BlazorApp/BlazorApp/Services/LoaderJson.cs | 27 ++++++++++ BlazorApp/BlazorApp/Services/SaverJson.cs | 26 +++++++++ BlazorApp/BlazorApp/appsettings.json | 3 +- 16 files changed, 205 insertions(+), 26 deletions(-) create mode 100644 BlazorApp/BlazorApp/Components/InventoryAction.cs create mode 100644 BlazorApp/BlazorApp/Components/InventoryComp.razor.js create mode 100644 BlazorApp/BlazorApp/Models/SavableItem.cs create mode 100644 BlazorApp/BlazorApp/SavedData/InventoryItems.json create mode 100644 BlazorApp/BlazorApp/Services/ILoaderInventory.cs create mode 100644 BlazorApp/BlazorApp/Services/ISaverInventory.cs create mode 100644 BlazorApp/BlazorApp/Services/LoaderJson.cs create mode 100644 BlazorApp/BlazorApp/Services/SaverJson.cs diff --git a/BlazorApp/BlazorApp/BlazorApp.csproj b/BlazorApp/BlazorApp/BlazorApp.csproj index d45543f..bc4b8fc 100644 --- a/BlazorApp/BlazorApp/BlazorApp.csproj +++ b/BlazorApp/BlazorApp/BlazorApp.csproj @@ -28,6 +28,7 @@ + @@ -35,6 +36,7 @@ + diff --git a/BlazorApp/BlazorApp/Components/InventoryAction.cs b/BlazorApp/BlazorApp/Components/InventoryAction.cs new file mode 100644 index 0000000..1e9f1f2 --- /dev/null +++ b/BlazorApp/BlazorApp/Components/InventoryAction.cs @@ -0,0 +1,13 @@ +using System; +using BlazorApp.Models; + +namespace BlazorApp.Components +{ + public class InventoryAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} + diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor b/BlazorApp/BlazorApp/Components/InventoryComp.razor index 9742a4b..46f0d91 100644 --- a/BlazorApp/BlazorApp/Components/InventoryComp.razor +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor @@ -13,7 +13,7 @@ @for(int i=0; i + } diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs b/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs index 6d9dcd4..e4572f5 100644 --- a/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor.cs @@ -10,12 +10,17 @@ using Microsoft.AspNetCore.Components.Web; using Microsoft.Extensions.Localization; using System.Collections.Generic; using BlazorApp.Pages; +using BlazorApp.Data; +using System.Text.Json; namespace BlazorApp.Components { - public partial class InventoryComp + public partial class InventoryComp : IDisposable { + [Inject] + public IConfiguration Configuration { get; set; } + [Parameter] public int InventorySize { get; set; } @@ -28,15 +33,21 @@ namespace BlazorApp.Components [Inject] public IDataService DataService { get; set; } + [Inject] + public ISaverInventory SaverInventory { get; set; } + + [Inject] + public ILoaderInventory LoaderInventory { get; set; } + [Inject] public IStringLocalizer Localizer { get; set; } [Parameter] public List Items { get; set; } - public List InventoryItems { get; set; } + public List InventoryItems { get; set; } - public ObservableCollection Actions { get; set; } + public ObservableCollection Actions { get; set; } [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } @@ -54,24 +65,50 @@ namespace BlazorApp.Components public InventoryComp() { - Actions = new ObservableCollection(); + Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; + } + + protected override void OnInitialized() + { + + InventorySize = int.Parse(Configuration["InventorySize"]); + + InventoryItems = LoaderInventory.LoadItems(); - + if (InventorySize < InventoryItems.Count) + { + InventoryItems.RemoveRange(InventorySize, InventoryItems.Count - InventorySize); + } + for (int i=0; i(new Item[InventorySize]); + SaverInventory.SaveItems(InventoryItems); + } private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { - JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + JavaScriptRuntime.InvokeVoidAsync("Inventory.AddActions", e.NewItems); } /// diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor.js b/BlazorApp/BlazorApp/Components/InventoryComp.razor.js new file mode 100644 index 0000000..263a9bf --- /dev/null +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor.js @@ -0,0 +1,16 @@ +window.Inventory = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs index d721555..b13fcaa 100644 --- a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs +++ b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs @@ -14,6 +14,7 @@ namespace BlazorApp.Components [Parameter] public Item Item { get; set; } + [Parameter] public int NbItem { get; set; } [Parameter] @@ -78,11 +79,11 @@ namespace BlazorApp.Components } Item tmp = this.Item; this.Item = Parent.CurrentDragItem; - Parent.InventoryItems[this.Index] = this.Item; Parent.CurrentDragItem = tmp; Parent.CurrentDragIndex = this.Index; } - Parent.Actions.Add(new CraftingAction { Action = "On Drop", Item = this.Item, Index = this.Index }); + Parent.InventoryItems[this.Index] = new SavableItem(this.Item, NbItem); + Parent.Actions.Add(new InventoryAction { Action = "On Drop", Item = this.Item, Index = this.Index }); } @@ -129,7 +130,7 @@ namespace BlazorApp.Components } } - Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); } @@ -148,12 +149,12 @@ namespace BlazorApp.Components //Test if the InventoryItem is from the list with the CurrentDragIndex != -1 or if the InventoryItem is being dropped onto itself or if the item was equal to the other and the sum of their nbItem was < than the stackSize if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!= Index && Parent.CurrentDragNbItem!=-1) { - Parent.InventoryItems[this.Index] = this.Item; this.Item = Parent.CurrentDragItem; NbItem = Parent.CurrentDragNbItem; + Parent.InventoryItems[this.Index] = new SavableItem(this.Item, NbItem); } - Parent.Actions.Add(new CraftingAction { Action = "Drag End", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag End", Item = this.Item, Index = this.Index }); Parent.CurrentDragIndex = -1; Parent.CurrentDragItem = null; diff --git a/BlazorApp/BlazorApp/Models/Item.cs b/BlazorApp/BlazorApp/Models/Item.cs index 9d26617..35a1176 100644 --- a/BlazorApp/BlazorApp/Models/Item.cs +++ b/BlazorApp/BlazorApp/Models/Item.cs @@ -2,15 +2,25 @@ { public class Item { - public int Id { get; set; } - public string DisplayName { get; set; } - public string Name { get; set; } - public int StackSize { get; set; } - public int MaxDurability { get; set; } - public List EnchantCategories { get; set; } - public List RepairWith { get; set; } - public DateTime CreatedDate { get; set; } - public DateTime? UpdatedDate { get; set; } - public string ImageBase64 { get; set; } + public int Id { get; set; } + public string DisplayName { get; set; } + public string Name { get; set; } + public int StackSize { get; set; } + public int MaxDurability { get; set; } + public List EnchantCategories { get; set; } + public List RepairWith { get; set; } + public DateTime CreatedDate { get; set; } + public DateTime? UpdatedDate { get; set; } + public string ImageBase64 { get; set; } + + + public bool Equals(Item obj) + { + if (obj==null) + { + return false; + } + return this.Id == obj.Id; + } } } diff --git a/BlazorApp/BlazorApp/Models/SavableItem.cs b/BlazorApp/BlazorApp/Models/SavableItem.cs new file mode 100644 index 0000000..7e2ae09 --- /dev/null +++ b/BlazorApp/BlazorApp/Models/SavableItem.cs @@ -0,0 +1,18 @@ +using System; +namespace BlazorApp.Models +{ + public class SavableItem + { + + public Item Item { get; set; } + + public int NbItem { get; set; } + + public SavableItem(Item item, int nbItem) + { + Item = item; + NbItem = nbItem; + } + } +} + diff --git a/BlazorApp/BlazorApp/Pages/Inventory.razor b/BlazorApp/BlazorApp/Pages/Inventory.razor index 4dcd083..157adb5 100644 --- a/BlazorApp/BlazorApp/Pages/Inventory.razor +++ b/BlazorApp/BlazorApp/Pages/Inventory.razor @@ -8,6 +8,6 @@
- +
diff --git a/BlazorApp/BlazorApp/Program.cs b/BlazorApp/BlazorApp/Program.cs index 77f260e..6248320 100644 --- a/BlazorApp/BlazorApp/Program.cs +++ b/BlazorApp/BlazorApp/Program.cs @@ -27,6 +27,11 @@ builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + + + builder.Services.AddBlazoredModal(); // Add the controller of the app diff --git a/BlazorApp/BlazorApp/SavedData/InventoryItems.json b/BlazorApp/BlazorApp/SavedData/InventoryItems.json new file mode 100644 index 0000000..d99cb0b --- /dev/null +++ b/BlazorApp/BlazorApp/SavedData/InventoryItems.json @@ -0,0 +1 @@ +[null,{"Item":{"Id":329,"DisplayName":"Saddle","Name":"saddle","StackSize":1,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAkklEQVQ4y2NgGAVEgUBFnv/1lpJwTJbmj90xYHwrTYc4Q0AaYZpBmkB4uZ8i2BAQjdUQZGeCFME0ImOYOIYB2JyJzRCYuKk413\u002BsToY5E6QYxke2HWQASByrATBJmCYQDePDMFbN6GEA04TOBmkE8UmOe5BGmK1EGwDThO5csMH5yf/JTpUbZnb8D/Vw/k\u002B1ZA4Ab5Gzh5CvSJcAAAAASUVORK5CYII="},"NbItem":1},{"Item":null,"NbItem":-1},{"Item":null,"NbItem":-1},{"Item":{"Id":1,"DisplayName":"Stone","Name":"stone","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAnklEQVQ4y4VTQRIAMQTzXA9w7xM8uXti0jS6B8OYEiK1zNxoa60dETsiOnb3I4dmFWABPypz925Wbw1REImRp8Y9Aa5QnvM4bZlhZ0YrzzxhjTGiMl6pfGaeEzAKxuMVsDsjvNbrBqwBReC0Yp9R8aCKlejM3S8h8UossOOMarTX2LyuKYLUn2Apl7fpE03MY\u002B6a4CWmiUj7U5/iBME\u002BeeR3wyunqoUAAAAASUVORK5CYII="},"NbItem":5},null,{"Item":{"Id":294,"DisplayName":"Golden Hoe","Name":"golden_hoe","StackSize":1,"MaxDurability":32,"EnchantCategories":["digger","breakable","vanishable"],"RepairWith":["gold_ingot"],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAHlBMVEUAAABJNhUoHguCXRY/Lg6JZydoTh7q7lfpsRX9/3arrxltAAAAAXRSTlMAQObYZgAAADdJREFUGNOtirkRACAMw5yHOOy/MLVzBxXqJBu4EBHqm9SS2QwrSd3L5WTsFK9Un/8a/nmHO14cgEIAxb/xarsAAAAASUVORK5CYII="},"NbItem":1},{"Item":{"Id":4,"DisplayName":"Cobblestone","Name":"cobblestone","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA0ElEQVQ4y22S0Q2FMAwDMyMbMABvjcck3YD5QKl01WHxUVDcxElc1\u002B847n3f7zHGPNu2zRjsuq75P//nxPpvrJzYQJ8m6diJ4JB04xdBB/33RBQaT5JiXICMjUPGOk1W/XECYxNDCM7hrvqiCVIHjwxmTZYGqbB390o0Md6E5WRYc2\u002BvRQ53ZTbUzmJIrQnalYtTeU9Ed686Rcyi7EKhReUllgZO8J7e3S\u002BCS5cGTk5N0okWeVk5PW8rf73My8p2Vlr3q9i\u002BmSKmZb9I3DVf7AEDw33Q7E89pwAAAABJRU5ErkJggg=="},"NbItem":1},{"Item":null,"NbItem":-1},{"Item":{"Id":39,"DisplayName":"Brown Mushroom","Name":"brown_mushroom","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAUklEQVQ4y2NgGAWjAA84M7PiPwxPzA39T7ZmmAFFYc7EGQJSjKwRGRNlSJafNVwxiA3CIDaMT5JXUkO9/vvYGv8nOyC3Tqn9DzKEopgAGUJTAwC0DFIPHLDd5AAAAABJRU5ErkJggg=="},"NbItem":2},null,null,{"Item":null,"NbItem":-1},{"Item":null,"NbItem":-1},{"Item":null,"NbItem":-1},{"Item":{"Id":329,"DisplayName":"Saddle","Name":"saddle","StackSize":1,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAkklEQVQ4y2NgGAVEgUBFnv/1lpJwTJbmj90xYHwrTYc4Q0AaYZpBmkB4uZ8i2BAQjdUQZGeCFME0ImOYOIYB2JyJzRCYuKk413\u002BsToY5E6QYxke2HWQASByrATBJmCYQDePDMFbN6GEA04TOBmkE8UmOe5BGmK1EGwDThO5csMH5yf/JTpUbZnb8D/Vw/k\u002B1ZA4Ab5Gzh5CvSJcAAAAASUVORK5CYII="},"NbItem":1},{"Item":{"Id":3,"DisplayName":"Dirt","Name":"dirt","StackSize":64,"MaxDurability":0,"EnchantCategories":[],"RepairWith":[],"CreatedDate":"0001-01-01T00:00:00","UpdatedDate":null,"ImageBase64":"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA0UlEQVR42nWSsQ1CMQxEvQ4rINFR0SOBxAgpaRgA/Q3YhelAF\u002Bmi9\u002B/nF1YS2zn7zq7v\u002B/H7tEu31/XYbea7nw6ruM/SRYlM0HtZluEzIO\u002BKycporsI3qzGHeSW01tpwsJukkl0MAAcV0GdyJ6AKpa/cVlZyq9SG9yFicpR4TKDipOmzMjnH525m/g6QTp5uOT8zNt2D5J68SaVyPORsYPtJxVrVjBcraHSkwDH3TUxnguyZu1pRkD1v541wpjHb0MokKkz\u002BWWgDkDtgkfbm7wJ/9oUoq3qXqFkAAAAASUVORK5CYII="},"NbItem":1},null] \ No newline at end of file diff --git a/BlazorApp/BlazorApp/Services/ILoaderInventory.cs b/BlazorApp/BlazorApp/Services/ILoaderInventory.cs new file mode 100644 index 0000000..69f2b5e --- /dev/null +++ b/BlazorApp/BlazorApp/Services/ILoaderInventory.cs @@ -0,0 +1,11 @@ +using System; +using BlazorApp.Models; + +namespace BlazorApp.Services +{ + public interface ILoaderInventory + { + List LoadItems(); + } +} + diff --git a/BlazorApp/BlazorApp/Services/ISaverInventory.cs b/BlazorApp/BlazorApp/Services/ISaverInventory.cs new file mode 100644 index 0000000..cdc6d86 --- /dev/null +++ b/BlazorApp/BlazorApp/Services/ISaverInventory.cs @@ -0,0 +1,11 @@ +using System; +using BlazorApp.Models; + +namespace BlazorApp.Services +{ + public interface ISaverInventory + { + void SaveItems(List items); + } +} + diff --git a/BlazorApp/BlazorApp/Services/LoaderJson.cs b/BlazorApp/BlazorApp/Services/LoaderJson.cs new file mode 100644 index 0000000..9e91dee --- /dev/null +++ b/BlazorApp/BlazorApp/Services/LoaderJson.cs @@ -0,0 +1,27 @@ +using System; +using BlazorApp.Data; +using System.Text.Json; +using BlazorApp.Models; + +namespace BlazorApp.Services +{ + public class LoaderJson : ILoaderInventory + { + + private string FileName { get; set; } + + public LoaderJson() + { + FileName="SavedData/InventoryItems.json"; + } + + public List LoadItems() + { + using FileStream openStream = File.OpenRead(FileName); + List? items = JsonSerializer.Deserialize>(openStream); + return items; + + } + } +} + diff --git a/BlazorApp/BlazorApp/Services/SaverJson.cs b/BlazorApp/BlazorApp/Services/SaverJson.cs new file mode 100644 index 0000000..e61ee13 --- /dev/null +++ b/BlazorApp/BlazorApp/Services/SaverJson.cs @@ -0,0 +1,26 @@ +using System; +using BlazorApp.Data; +using System.Text.Json; +using BlazorApp.Models; + +namespace BlazorApp.Services +{ + public class SaverJson : ISaverInventory + { + private string FileName { get; set; } + + public SaverJson() + { + FileName = "SavedData/InventoryItems.json"; + } + + public void SaveItems(List items) + { + string jsonString = JsonSerializer.Serialize(items); + FileStream fileStream= File.Create(FileName); + JsonSerializer.Serialize(fileStream, items); + fileStream.Dispose(); + } + } +} + diff --git a/BlazorApp/BlazorApp/appsettings.json b/BlazorApp/BlazorApp/appsettings.json index 4d56694..9a586c9 100644 --- a/BlazorApp/BlazorApp/appsettings.json +++ b/BlazorApp/BlazorApp/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "InventorySize": 18 }