Add: Sauvegarde en json + taille de l'inventaire en fonction de la config

master
Thomas Chazot 2 years ago
parent 4f5d854633
commit 6407e4ad0f

@ -28,6 +28,7 @@
<None Remove="Microsoft.Extensions.Logging.Configuration" /> <None Remove="Microsoft.Extensions.Logging.Configuration" />
<None Remove="DevExpress.Blazor.ProjectTemplates" /> <None Remove="DevExpress.Blazor.ProjectTemplates" />
<None Remove="DevExpress.Data" /> <None Remove="DevExpress.Data" />
<None Remove="SavedData\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Services\" /> <Folder Include="Services\" />
@ -35,6 +36,7 @@
<Folder Include="Modals\" /> <Folder Include="Modals\" />
<Folder Include="Controllers\" /> <Folder Include="Controllers\" />
<Folder Include="Components\" /> <Folder Include="Components\" />
<Folder Include="SavedData\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Update="Resources\Pages.List.resx"> <EmbeddedResource Update="Resources\Pages.List.resx">

@ -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; }
}
}

@ -13,7 +13,7 @@
@for(int i=0; i<InventorySize; i++) @for(int i=0; i<InventorySize; i++)
{ {
<InventoryItem Index="@i"/> <InventoryItem Index="@i" Item="@InventoryItems[i].Item" NbItem="@InventoryItems[i].NbItem"/>
} }
</div> </div>

@ -10,12 +10,17 @@ using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using System.Collections.Generic; using System.Collections.Generic;
using BlazorApp.Pages; using BlazorApp.Pages;
using BlazorApp.Data;
using System.Text.Json;
namespace BlazorApp.Components namespace BlazorApp.Components
{ {
public partial class InventoryComp public partial class InventoryComp : IDisposable
{ {
[Inject]
public IConfiguration Configuration { get; set; }
[Parameter] [Parameter]
public int InventorySize { get; set; } public int InventorySize { get; set; }
@ -28,15 +33,21 @@ namespace BlazorApp.Components
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject]
public ISaverInventory SaverInventory { get; set; }
[Inject]
public ILoaderInventory LoaderInventory { get; set; }
[Inject] [Inject]
public IStringLocalizer<InventoryComp> Localizer { get; set; } public IStringLocalizer<InventoryComp> Localizer { get; set; }
[Parameter] [Parameter]
public List<Item> Items { get; set; } public List<Item> Items { get; set; }
public List<Item> InventoryItems { get; set; } public List<SavableItem> InventoryItems { get; set; }
public ObservableCollection<CraftingAction> Actions { get; set; } public ObservableCollection<InventoryAction> Actions { get; set; }
[Inject] [Inject]
internal IJSRuntime JavaScriptRuntime { get; set; } internal IJSRuntime JavaScriptRuntime { get; set; }
@ -54,24 +65,50 @@ namespace BlazorApp.Components
public InventoryComp() public InventoryComp()
{ {
Actions = new ObservableCollection<CraftingAction>(); Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged; 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);
} }
protected override void OnInitialized() for (int i=0; i<InventoryItems.Count; i++)
{ {
this.InventoryItems = new List<Item>(new Item[InventorySize]); if (InventoryItems[i] == null)
{
InventoryItems[i] = new SavableItem(null, -1);
}
}
if (InventoryItems.Count < InventorySize)
{
for (int i = InventoryItems.Count; InventoryItems.Count < InventorySize; i++)
{
InventoryItems.Add(new SavableItem(null, -1));
}
}
}
public void Dispose()
{
SaverInventory.SaveItems(InventoryItems);
} }
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); JavaScriptRuntime.InvokeVoidAsync("Inventory.AddActions", e.NewItems);
} }
/// <summary> /// <summary>

@ -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);
});
}
}

@ -14,6 +14,7 @@ namespace BlazorApp.Components
[Parameter] [Parameter]
public Item Item { get; set; } public Item Item { get; set; }
[Parameter]
public int NbItem { get; set; } public int NbItem { get; set; }
[Parameter] [Parameter]
@ -78,11 +79,11 @@ namespace BlazorApp.Components
} }
Item tmp = this.Item; Item tmp = this.Item;
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
Parent.InventoryItems[this.Index] = this.Item;
Parent.CurrentDragItem = tmp; Parent.CurrentDragItem = tmp;
Parent.CurrentDragIndex = this.Index; 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 //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) if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!= Index && Parent.CurrentDragNbItem!=-1)
{ {
Parent.InventoryItems[this.Index] = this.Item;
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
NbItem = Parent.CurrentDragNbItem; 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.CurrentDragIndex = -1;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;

@ -12,5 +12,15 @@
public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; } public DateTime? UpdatedDate { get; set; }
public string ImageBase64 { get; set; } public string ImageBase64 { get; set; }
public bool Equals(Item obj)
{
if (obj==null)
{
return false;
}
return this.Id == obj.Id;
}
} }
} }

@ -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;
}
}
}

@ -8,6 +8,6 @@
<div> <div>
<InventoryComp Items="Items" InventorySize="18" /> <InventoryComp Items="Items"/>
</div> </div>

@ -27,6 +27,11 @@ builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped<IDataService, DataApiService>(); builder.Services.AddScoped<IDataService, DataApiService>();
builder.Services.AddScoped<ILoaderInventory, LoaderJson>();
builder.Services.AddScoped<ISaverInventory, SaverJson>();
builder.Services.AddBlazoredModal(); builder.Services.AddBlazoredModal();
// Add the controller of the app // Add the controller of the app

@ -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]

@ -0,0 +1,11 @@
using System;
using BlazorApp.Models;
namespace BlazorApp.Services
{
public interface ILoaderInventory
{
List<SavableItem> LoadItems();
}
}

@ -0,0 +1,11 @@
using System;
using BlazorApp.Models;
namespace BlazorApp.Services
{
public interface ISaverInventory
{
void SaveItems(List<SavableItem> items);
}
}

@ -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<SavableItem> LoadItems()
{
using FileStream openStream = File.OpenRead(FileName);
List<SavableItem>? items = JsonSerializer.Deserialize<List<SavableItem>>(openStream);
return items;
}
}
}

@ -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<SavableItem> items)
{
string jsonString = JsonSerializer.Serialize(items);
FileStream fileStream= File.Create(FileName);
JsonSerializer.Serialize(fileStream, items);
fileStream.Dispose();
}
}
}

@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"InventorySize": 18
} }

Loading…
Cancel
Save