Ajout de la sérialisation

master
Félix MIELCAREK 2 years ago
parent 8c1f6eb638
commit 47d16b3e0d

@ -7,27 +7,13 @@
<div>
<div class="content">
<div class="css-inv">
<div class="css-inv">
<div class="slotSpace">
<h1>@Localizer["Inventory"]</h1>
<InventoryItem Index="0" />
<InventoryItem Index="1" />
<InventoryItem Index="2" />
<InventoryItem Index="3" />
<InventoryItem Index="4" />
<InventoryItem Index="5" />
<InventoryItem Index="6" />
<InventoryItem Index="7" />
<InventoryItem Index="8" />
<InventoryItem Index="9" />
<InventoryItem Index="10" />
<InventoryItem Index="11" />
<InventoryItem Index="12" />
<InventoryItem Index="13" />
<InventoryItem Index="14" />
<InventoryItem Index="15" />
<InventoryItem Index="16" />
<InventoryItem Index="17" />
<h1>@Localizer["Inventory"]</h1>
@for(int i = 0 ; i<18 ; i++)
{
<InventoryItem Index=i Item=@this.RecipeItems[i] />
}
</div>
</div>
</div>

@ -1,4 +1,5 @@
using BlazorApp.Models;
using BlazorApp.Data;
using BlazorApp.Models;
using BlazorApp.Pages;
using BlazorApp.Services;
using Blazorise;
@ -8,6 +9,7 @@ using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Text.Json;
namespace BlazorApp.Components
{
@ -38,7 +40,10 @@ namespace BlazorApp.Components
{
Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged;
this.RecipeItems = new List<Item> { null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null };
string fileName = "Inventory.json";
string jsonString = File.ReadAllText(fileName);
this.RecipeItems = JsonSerializer.Deserialize<List<Item>>(jsonString)!;
}
/// <summary>
@ -49,6 +54,9 @@ namespace BlazorApp.Components
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems);
}
/// <summary>

@ -2,6 +2,7 @@
using BlazorApp.Pages;
using Blazorise;
using Microsoft.AspNetCore.Components;
using System.Text.Json;
namespace BlazorApp.Components
{
@ -13,10 +14,6 @@ namespace BlazorApp.Components
[Parameter]
public Item Item { get; set; }
[Parameter]
public bool NoDrop { get; set; }
@ -69,9 +66,15 @@ namespace BlazorApp.Components
}
Parent.RecipeItems[this.Index] = this.Item;
string fileName = "Inventory.json";
string jsonString = JsonSerializer.Serialize(Parent.RecipeItems);
File.WriteAllText(fileName, jsonString);
Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index });
Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index });
}
/// <summary>
/// method call when darg start and send an action
@ -94,6 +97,11 @@ namespace BlazorApp.Components
this.Item = null;
}
Parent.Actions.Add(new InventoryAction { Action = "Delete", Item = this.Item, Index = this.Index });
Parent.RecipeItems[this.Index] = null;
string fileName = "Inventory.json";
string jsonString = JsonSerializer.Serialize(Parent.RecipeItems);
File.WriteAllText(fileName, jsonString);
}
}
}

@ -0,0 +1 @@
[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]

@ -424,3 +424,4 @@ C:\Users\felix\Desktop\Plazor\Sources\BlazorApp\BlazorApp\obj\Debug\net6.0\Blazo
C:\Users\felix\Desktop\Plazor\Sources\BlazorApp\BlazorApp\obj\Debug\net6.0\ref\BlazorApp.dll
C:\Users\felix\Desktop\Plazor\Sources\BlazorApp\BlazorApp\obj\Debug\net6.0\BlazorApp.Resources.Pages.List.fr-FR.resources
C:\Users\felix\Desktop\Plazor\Sources\BlazorApp\BlazorApp\obj\Debug\net6.0\BlazorApp.Resources.Pages.List.resources
C:\Users\felix\Desktop\Plazor\Sources\BlazorApp\BlazorApp\bin\Debug\net6.0\Inventory.json

Loading…
Cancel
Save