master
Lilian BRETON 2 years ago
commit e8584747ff

@ -116,7 +116,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <returns>The async task.</returns>
[HttpPut]
[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);
@ -125,15 +125,9 @@ namespace Minecraft.Crafting.Api.Controllers
throw new Exception("Unable to get the inventory.");
}
var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.Position == item.Position);
if (inventoryItem == null)
{
throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}");
}
inventoryItem.ItemName = item.ItemName;
inventoryItem.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]);
data.ToArray()[Int32.Parse(infos[1])-1].Number = Int32.Parse(infos[2]);
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.
/// </summary>
public int Position { get; set; }
public int Number { get; set; }
}
}

@ -6,7 +6,8 @@ namespace myBlazorApp.Components
{
public string Action { 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
class="inventory-item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave"
>
@if (Item != null)
<div class="inventory-item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragend="@OnDragEnd"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave">
@if (Item != "null")
{
@Item.DisplayName
<p>@Item</p>
<p>@Number</p>
}
else
{

@ -1,5 +1,7 @@
using System;
namespace myBlazorApp.Components;
using System.Reflection.Metadata;
using Blazorise;
using Microsoft.AspNetCore.Components;
using myBlazorApp.Models;
@ -7,11 +9,14 @@ using myBlazorApp.Models;
public partial class InventoryItem
{
[Parameter]
public Item Item { get; set; }
public string Item { get; set; }
[Parameter]
public int Index { get; set; }
[Parameter]
public int Number { get; set; }
[Parameter]
public bool NoDrop { get; set; }
@ -24,7 +29,7 @@ public partial class InventoryItem
{
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()
@ -33,7 +38,7 @@ public partial class InventoryItem
{
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()
@ -42,15 +47,60 @@ public partial class InventoryItem
{
return;
}
this.Item = Parent.CurrentDragItem;
Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index });
if (Parent.CurrentDragItem != "null")
{
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()
{
Parent.CurrentInventoryItem = this;
Parent.CurrentDragIndex = this.Index;
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>
<div class="inventory-items">
@foreach (var item in ItemsInventory)
{
<InventoryItem Index="@index" Item="item" NoDrop="true" />
index++;
}
@for (int i = @index; i < 18; i++)
@foreach(InventoryListItem i in ItemsInventory)
{
<InventoryItem Index="i" />
<InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" />
}
</div>
</CascadingValue>

@ -10,19 +10,22 @@ namespace myBlazorApp.Components
public partial class MyInventory
{
[Parameter]
public List<Item> ItemsInventory { get; set; } = new List<Item>();
public List<InventoryListItem> ItemsInventory { get; set; }
[Parameter]
public int index { get; set; } = 0;
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()
{
Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged;
}
[Inject]
@ -37,9 +40,14 @@ namespace myBlazorApp.Components
[Inject]
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);
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="inventory">
<MyInventory ItemsInventory="@itemsInv" />
<MyInventory ItemsInventory="@itemsInv"/>
</div>
<div id="ItemList">

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

@ -58,9 +58,15 @@ namespace myBlazorApp.Services
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);
}
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<int> Count();
Task<List<Item>> List(int currentPage, int pageSize);
Task<List<Item>> FullList(int pageSize);
Task<Item> GetById(int id);
Task Update(int id, ItemModel model);
Task Delete(int id);
Task<List<CraftingRecipe>> GetRecipes();
Task<List<InventoryListItem>> GetInventoryItems();
Task UpdateInventory(string[] infos);
}
}

Loading…
Cancel
Save