master
Lilian BRETON 2 years ago
commit e8584747ff

@ -116,7 +116,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <returns>The async task.</returns> /// <returns>The async task.</returns>
[HttpPut] [HttpPut]
[Route("")] [Route("")]
public Task UpdateInventory(InventoryModel item) public Task UpdateInventory(string[] infos)
{ {
var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions); var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
@ -125,15 +125,9 @@ namespace Minecraft.Crafting.Api.Controllers
throw new Exception("Unable to get the inventory."); throw new Exception("Unable to get the inventory.");
} }
var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.Position == item.Position); data.ToArray()[Int32.Parse(infos[1])-1].ItemName = infos[0];
data.ToArray()[Int32.Parse(infos[1])-1].Position = Int32.Parse(infos[1]);
if (inventoryItem == null) data.ToArray()[Int32.Parse(infos[1])-1].Number = Int32.Parse(infos[2]);
{
throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}");
}
inventoryItem.ItemName = item.ItemName;
inventoryItem.Position = item.Position;
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions)); System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions));

@ -0,0 +1,76 @@
[
{
"itemName": "null",
"position": 1
},
{
"itemName": "lili",
"position": 2,
"number": 11
},
{
"itemName": "null",
"position": 3
},
{
"itemName": "null",
"position": 4
},
{
"itemName": "null",
"position": 5
},
{
"itemName": "null",
"position": 6
},
{
"itemName": "null",
"position": 7
},
{
"itemName": "null",
"position": 8
},
{
"itemName": "null",
"position": 9
},
{
"itemName": "lili",
"position": 10,
"number": 11
},
{
"itemName": "null",
"position": 11
},
{
"itemName": "null",
"position": 12
},
{
"itemName": "null",
"position": 13
},
{
"itemName": "null",
"position": 14
},
{
"itemName": "null",
"position": 15
},
{
"itemName": "null",
"position": 16
},
{
"itemName": "null",
"position": 17
},
{
"itemName": "null",
"position": 18
}
]

@ -20,5 +20,7 @@ namespace Minecraft.Crafting.Api.Models
/// Gets or sets the position. /// Gets or sets the position.
/// </summary> /// </summary>
public int Position { get; set; } public int Position { get; set; }
public int Number { get; set; }
} }
} }

@ -6,7 +6,8 @@ namespace myBlazorApp.Components
{ {
public string Action { get; set; } public string Action { get; set; }
public int Index { get; set; } public int Index { get; set; }
public Item Item { get; set; } public string ItemName { get; set; }
public int Number { get; set; }
} }
} }

@ -1,17 +1,16 @@
 
<div <div class="inventory-item"
class="inventory-item" ondragover="event.preventDefault();"
ondragover="event.preventDefault();" draggable="true"
draggable="true" @ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragstart="@OnDragStart" @ondragend="@OnDragEnd"
@ondrop="@OnDrop" @ondragenter="@OnDragEnter"
@ondragenter="@OnDragEnter" @ondragleave="@OnDragLeave">
@ondragleave="@OnDragLeave" @if (Item != "null")
>
@if (Item != null)
{ {
@Item.DisplayName <p>@Item</p>
<p>@Number</p>
} }
else else
{ {

@ -1,5 +1,7 @@
using System; using System;
namespace myBlazorApp.Components; namespace myBlazorApp.Components;
using System.Reflection.Metadata;
using Blazorise; using Blazorise;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using myBlazorApp.Models; using myBlazorApp.Models;
@ -7,11 +9,14 @@ using myBlazorApp.Models;
public partial class InventoryItem public partial class InventoryItem
{ {
[Parameter] [Parameter]
public Item Item { get; set; } public string Item { get; set; }
[Parameter] [Parameter]
public int Index { get; set; } public int Index { get; set; }
[Parameter]
public int Number { get; set; }
[Parameter] [Parameter]
public bool NoDrop { get; set; } public bool NoDrop { get; set; }
@ -24,7 +29,7 @@ public partial class InventoryItem
{ {
return; return;
} }
Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number });
} }
internal void OnDragLeave() internal void OnDragLeave()
@ -33,7 +38,7 @@ public partial class InventoryItem
{ {
return; return;
} }
Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number });
} }
internal void OnDrop() internal void OnDrop()
@ -42,15 +47,60 @@ public partial class InventoryItem
{ {
return; return;
} }
this.Item = Parent.CurrentDragItem; if (Parent.CurrentDragItem != "null")
Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); {
if(this.Item == Parent.CurrentDragItem)
{
this.Number = this.Number + Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Number = 0;
Parent.CurrentInventoryItem.Item = "null";
Parent.ItemsInventory[Parent.CurrentInventoryItem.Index-1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number);
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = Parent.CurrentInventoryItem.Item, Index = Parent.CurrentInventoryItem.Index, Number = Parent.CurrentInventoryItem.Number });
}
else
{
string tmpItem = this.Item;
int tmpNumber = this.Number;
Parent.ItemsInventory[Parent.CurrentDragIndex-1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber);
this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Item = tmpItem;
Parent.CurrentInventoryItem.Number = tmpNumber;
}
}
Parent.Actions.Add(new InventoryAction { Action = "Drop", ItemName = this.Item, Index = this.Index, Number = this.Number });
}
internal void OnDragEnd()
{
if(NoDrop || Parent.CurrentDragItem == "null")
{
return;
}
if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex != this.Index)
{
Parent.ItemsInventory[this.Index-1] = new InventoryListItem(Parent.CurrentDragItem, Parent.CurrentDragIndex, Parent.CurrentDragNumber);
this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
}
Parent.CurrentDragIndex = -1;
Parent.CurrentDragItem = "null";
Parent.CurrentDragNumber = 0;
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = this.Item, Index = this.Index, Number = this.Number });
} }
private void OnDragStart() private void OnDragStart()
{ {
Parent.CurrentInventoryItem = this;
Parent.CurrentDragIndex = this.Index;
Parent.CurrentDragItem = this.Item; Parent.CurrentDragItem = this.Item;
Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); Parent.CurrentDragNumber = this.Number;
this.Item = "null";
this.Number = 0;
Parent.Actions.Add(new InventoryAction { Action = "Drag Start", ItemName = this.Item, Index = this.Index, Number = this.Number});
} }
} }

@ -0,0 +1,22 @@
using System;
using myBlazorApp.Models;
namespace myBlazorApp.Components
{
public class InventoryListItem
{
//public InventoryListItem() { }
public InventoryListItem(string itemName, int position, int number)
{
this.itemName = itemName;
this.position = position;
this.number = number;
}
public string itemName { get; set; }
public int position { get; set; }
public int number { get; set; }
}
}

@ -1,17 +1,13 @@
<CascadingValue Value="@this"> @using myBlazorApp.Models
<CascadingValue Value="@this">
<h3>My Inventory</h3> <h3>My Inventory</h3>
<div class="inventory-items"> <div class="inventory-items">
@foreach(InventoryListItem i in ItemsInventory)
@foreach (var item in ItemsInventory)
{
<InventoryItem Index="@index" Item="item" NoDrop="true" />
index++;
}
@for (int i = @index; i < 18; i++)
{ {
<InventoryItem Index="i" /> <InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" />
} }
</div> </div>
</CascadingValue> </CascadingValue>

@ -10,19 +10,22 @@ namespace myBlazorApp.Components
public partial class MyInventory public partial class MyInventory
{ {
[Parameter] [Parameter]
public List<Item> ItemsInventory { get; set; } = new List<Item>(); public List<InventoryListItem> ItemsInventory { get; set; }
[Parameter]
public int index { get; set; } = 0; public int index { get; set; } = 0;
public ObservableCollection<InventoryAction> Actions { get; set; } public ObservableCollection<InventoryAction> Actions { get; set; }
public Item CurrentDragItem { get; set; } = new Item(); public string CurrentDragItem { get; set; }
public int CurrentDragIndex { get; set; }
public int CurrentDragNumber { get; set; }
public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem();
public MyInventory() public MyInventory()
{ {
Actions = new ObservableCollection<InventoryAction>(); Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged; Actions.CollectionChanged += OnActionsCollectionChanged;
} }
[Inject] [Inject]
@ -37,9 +40,14 @@ namespace myBlazorApp.Components
[Inject] [Inject]
internal IJSRuntime JavaScriptRuntime { get; set; } internal IJSRuntime JavaScriptRuntime { get; set; }
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private async void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems); JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems);
if (this.Actions.Last().Action == "End" || this.Actions.Last().Action == "Drop" || this.Actions.Last().Action == "Start")
{
string[] infos = new string[] { this.Actions.Last().ItemName, this.Actions.Last().Index.ToString(), this.Actions.Last().Number.ToString() };
await DataService.UpdateInventory(infos);
}
} }
} }
} }

@ -6,7 +6,7 @@
<div class="body"> <div class="body">
<div class="inventory"> <div class="inventory">
<MyInventory ItemsInventory="@itemsInv" /> <MyInventory ItemsInventory="@itemsInv"/>
</div> </div>
<div id="ItemList"> <div id="ItemList">

@ -7,6 +7,9 @@ using Blazorise;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Blazorise.Extensions; using Blazorise.Extensions;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using myBlazorApp.Components;
using myBlazorApp.Factories;
using myBlazorApp.Modals; using myBlazorApp.Modals;
using myBlazorApp.Models; using myBlazorApp.Models;
using myBlazorApp.Services; using myBlazorApp.Services;
@ -16,7 +19,7 @@ namespace myBlazorApp.Pages
{ {
public partial class Inventory public partial class Inventory
{ {
public List<Item> itemsInv = new List<Item>(); public List<InventoryListItem> itemsInv = new List<InventoryListItem>();
private List<Item> items = new List<Item>(); private List<Item> items = new List<Item>();
@ -64,7 +67,7 @@ namespace myBlazorApp.Pages
totalItem = await DataService.Count(); totalItem = await DataService.Count();
items = await DataService.List(e.Page, e.PageSize); items = await DataService.List(e.Page, e.PageSize);
full = await DataService.List(e.Page, totalItem); full = await DataService.List(e.Page, totalItem);
itemsInv = await DataService.List(e.Page, e.PageSize); itemsInv = await DataService.GetInventoryItems();
currentPage = e.Page; currentPage = e.Page;
pageSize = e.PageSize; pageSize = e.PageSize;
if(isSorted==false) if(isSorted==false)

@ -58,9 +58,15 @@ namespace myBlazorApp.Services
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:8635/api/Crafting/recipe"); return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:8635/api/Crafting/recipe");
} }
public async Task<List<Item>> FullList(int pageSize) /* TASKS FOR INVENTORY */
public async Task<List<InventoryListItem>> GetInventoryItems()
{ {
return await _http.GetFromJsonAsync<List<Item>>("https://localhost:8635/api/Crafting/all"); return await _http.GetFromJsonAsync<List<InventoryListItem>>("https://localhost:8635/api/Inventory");
}
public async Task UpdateInventory(string[] infos)
{
await _http.PutAsJsonAsync("https://localhost:8635/api/Inventory", infos);
} }
} }
} }

@ -183,19 +183,15 @@ namespace myBlazorApp.Services
return Task.FromResult(items); return Task.FromResult(items);
} }
public async Task<List<Item>> FullList(int pageSize)
public async Task<List<InventoryListItem>> GetInventoryItems()
{
return new List<InventoryListItem>();
}
public async Task UpdateInventory(string[] infos)
{ {
var currentData = await _localStorage.GetItemAsync<Item[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Item[]>($"{_navigationManager.BaseUri}fake-data.json");
await _localStorage.SetItemAsync("data", originalData);
}
return (await _localStorage.GetItemAsync<Item[]>("data")).Take(pageSize).ToList();
} }
} }
} }

@ -9,11 +9,12 @@ namespace myBlazorApp.Services
Task Add(ItemModel model); Task Add(ItemModel model);
Task<int> Count(); Task<int> Count();
Task<List<Item>> List(int currentPage, int pageSize); Task<List<Item>> List(int currentPage, int pageSize);
Task<List<Item>> FullList(int pageSize);
Task<Item> GetById(int id); Task<Item> GetById(int id);
Task Update(int id, ItemModel model); Task Update(int id, ItemModel model);
Task Delete(int id); Task Delete(int id);
Task<List<CraftingRecipe>> GetRecipes(); Task<List<CraftingRecipe>> GetRecipes();
Task<List<InventoryListItem>> GetInventoryItems();
Task UpdateInventory(string[] infos);
} }
} }

Loading…
Cancel
Save