ADD : comments for the inventory part

master
Lucie Bedouret 2 years ago
parent 0c9b033c3d
commit 0f1bc79ff3

@ -112,7 +112,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <summary> /// <summary>
/// Updates the inventory. /// Updates the inventory.
/// </summary> /// </summary>
/// <param name="item">The item.</param> /// <param name="infos">The infos about the item (name, position, number, stacksize).</param>
/// <returns>The async task.</returns> /// <returns>The async task.</returns>
[HttpPut] [HttpPut]
[Route("")] [Route("")]

@ -1,33 +1,23 @@
[ [
{ {
"itemName": "Sand", "itemName": "null",
"position": 1, "position": 1
"number": 2,
"stackSize": 64
}, },
{ {
"itemName": "Grass Block", "itemName": "null",
"position": 2, "position": 2
"number": 3,
"stackSize": 64
}, },
{ {
"itemName": "Cobblestone", "itemName": "null",
"position": 3, "position": 3
"number": 3,
"stackSize": 64
}, },
{ {
"itemName": "Sapling", "itemName": "null",
"position": 4, "position": 4
"number": 1,
"stackSize": 64
}, },
{ {
"itemName": "Dirt", "itemName": "null",
"position": 5, "position": 5
"number": 2,
"stackSize": 64
}, },
{ {
"itemName": "null", "itemName": "null",

@ -21,8 +21,14 @@ namespace Minecraft.Crafting.Api.Models
/// </summary> /// </summary>
public int Position { get; set; } public int Position { get; set; }
/// <summary>
/// Gets or sets the number of items in a stack.
/// </summary>
public int Number { get; set; } public int Number { get; set; }
/// <summary>
/// Gets or sets the stack size of an item.
/// </summary>
public int StackSize { get; set; } public int StackSize { get; set; }
} }
} }

@ -3,11 +3,30 @@ using myBlazorApp.Models;
namespace myBlazorApp.Components namespace myBlazorApp.Components
{ {
public class InventoryAction public class InventoryAction
{ {
/// <summary>
/// Gets and sets the action
/// </summary>
public string Action { get; set; } public string Action { get; set; }
/// <summary>
/// Gets and sets the inde where the action affected
/// </summary>
public int Index { get; set; } public int Index { get; set; }
/// <summary>
/// Gets and sets the item name the action affected
/// </summary>
public string ItemName { get; set; } public string ItemName { get; set; }
/// <summary>
/// Gets and sets the number of items the action affected
/// </summary>
public int Number { get; set; } public int Number { get; set; }
/// <summary>
/// Gets and sets the stack size of the item the action affected
/// </summary>
public int StackSize { get; set; } public int StackSize { get; set; }
} }
} }

@ -1,38 +1,77 @@
using System; /// IMPORTS ///
namespace myBlazorApp.Components; using System;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using Blazorise; using Blazorise;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using myBlazorApp.Models; using myBlazorApp.Models;
namespace myBlazorApp.Components;
public partial class InventoryItem public partial class InventoryItem
{ {
/// -------- PARAMETERS -------- ///
/// <summary>
/// gets or sets the item name
/// </summary>
[Parameter] [Parameter]
public string Item { get; set; } public string Item { get; set; }
/// <summary>
/// gets or sets the index
/// </summary>
[Parameter] [Parameter]
public int Index { get; set; } public int Index { get; set; }
/// <summary>
/// gets or sets the number of items
/// </summary>
[Parameter] [Parameter]
public int Number { get; set; } public int Number { get; set; }
/// <summary>
/// gets or sets the stack size
/// </summary>
[Parameter] [Parameter]
public int StackSize { get; set; } public int StackSize { get; set; }
/// <summary>
/// gets or sets the capability to drop the item
/// </summary>
[Parameter] [Parameter]
public bool NoDrop { get; set; } public bool NoDrop { get; set; }
/// <summary>
/// gets or sets the image of the item
/// </summary>
[Parameter] [Parameter]
public string Image { get; set; } public string Image { get; set; }
/// -------- DEPENDENCIES INJECTION -------- ///
/// <summary>
/// Gets or sets the localizer
/// </summary>
[Inject] [Inject]
public IStringLocalizer<InventoryItem> Localizer { get; set; } public IStringLocalizer<InventoryItem> Localizer { get; set; }
/// -------- CASCADING PARAMETER -------- ///
/// <summary>
/// Gets or sets the cascading parameter Parent ( MyInventory ).
/// </summary>
[CascadingParameter] [CascadingParameter]
public MyInventory Parent { get; set; } public MyInventory Parent { get; set; }
/// -------- DRAG AND DROP METHODS -------- ///
/// <summary>
/// Creating a new action when the user enter in an item of the Inventory items
/// </summary>
internal void OnDragEnter() internal void OnDragEnter()
{ {
if (NoDrop) if (NoDrop)
@ -42,6 +81,9 @@ public partial class InventoryItem
Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize }); Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
} }
/// <summary>
/// Creating a new action when the user leaves an item of the Inventory items
/// </summary>
internal void OnDragLeave() internal void OnDragLeave()
{ {
if (NoDrop) if (NoDrop)
@ -51,35 +93,52 @@ public partial class InventoryItem
Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize }); Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
} }
/// <summary>
/// Creating a new action when the user drops an item in an Inventory item in the Invnetory item
/// </summary>
internal void OnDrop() internal void OnDrop()
{ {
if (Parent.CurrentDragItem != "null") if (Parent.CurrentDragItem != "null")
{ {
/// Case when the item comes from the list of items
if(Parent.CurrentDragIndex == 0) if(Parent.CurrentDragIndex == 0)
{ {
this.Number = this.Number + Parent.CurrentDragNumber; if (this.Item == Parent.CurrentDragItem)
{
this.Number = this.Number + Parent.CurrentDragNumber;
}
else
{
this.Number = Parent.CurrentDragNumber;
}
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
this.StackSize = Parent.CurrentDragStackSize; this.StackSize = Parent.CurrentDragStackSize;
Parent.ItemsInventory[this.Index - 1] = new InventoryListItem(this.Item, this.Index, this.Number, this.StackSize); Parent.ItemsInventory[this.Index - 1] = new InventoryListItem(this.Item, this.Index, this.Number, this.StackSize);
} }
/// Case when the item dropped is the same than the item inside the Inventory item
else if (this.Item == Parent.CurrentDragItem) else if (this.Item == Parent.CurrentDragItem)
{ {
this.Number = this.Number + Parent.CurrentDragNumber; this.Number = this.Number + Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Number = 0; Parent.CurrentInventoryItem.Number = 0;
Parent.CurrentInventoryItem.Item = "null"; Parent.CurrentInventoryItem.Item = "null";
Parent.CurrentInventoryItem.StackSize = 0; Parent.CurrentInventoryItem.StackSize = 0;
Parent.ItemsInventory[Parent.CurrentInventoryItem.Index - 1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number, Parent.CurrentInventoryItem.StackSize); Parent.ItemsInventory[Parent.CurrentInventoryItem.Index - 1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number, Parent.CurrentInventoryItem.StackSize);
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = Parent.CurrentInventoryItem.Item, Index = Parent.CurrentInventoryItem.Index, Number = Parent.CurrentInventoryItem.Number, StackSize = Parent.CurrentInventoryItem.StackSize });
} }
/// Case when the item must be exchanged with another or just be inserted.
else else
{ {
string tmpItem = this.Item; string tmpItem = this.Item;
int tmpNumber = this.Number; int tmpNumber = this.Number;
int tmpStackSize = this.StackSize; int tmpStackSize = this.StackSize;
Parent.ItemsInventory[Parent.CurrentDragIndex - 1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize); Parent.ItemsInventory[Parent.CurrentDragIndex - 1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize);
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber; this.Number = Parent.CurrentDragNumber;
this.StackSize = Parent.CurrentDragStackSize; this.StackSize = Parent.CurrentDragStackSize;
Parent.CurrentInventoryItem.Item = tmpItem; Parent.CurrentInventoryItem.Item = tmpItem;
Parent.CurrentInventoryItem.Number = tmpNumber; Parent.CurrentInventoryItem.Number = tmpNumber;
Parent.CurrentInventoryItem.StackSize = tmpStackSize; Parent.CurrentInventoryItem.StackSize = tmpStackSize;
@ -90,27 +149,31 @@ public partial class InventoryItem
} }
/// <summary>
/// Creating a new action when the user end the drag and dropped the item in an Inventory item
/// </summary>
internal void OnDragEnd() internal void OnDragEnd()
{ {
if(NoDrop || Parent.CurrentDragItem == "null") if(NoDrop || Parent.CurrentDragItem == "null")
{ {
return; return;
} }
if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex != this.Index)
if (Parent.CurrentDragIndex != this.Index)
{ {
Parent.ItemsInventory[this.Index-1] = new InventoryListItem(Parent.CurrentDragItem, Parent.CurrentDragIndex, Parent.CurrentDragNumber, Parent.CurrentDragStackSize); Parent.ItemsInventory[this.Index-1] = new InventoryListItem(Parent.CurrentDragItem, Parent.CurrentDragIndex, Parent.CurrentDragNumber, Parent.CurrentDragStackSize);
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber; this.Number = Parent.CurrentDragNumber;
this.StackSize = Parent.CurrentDragStackSize; this.StackSize = Parent.CurrentDragStackSize;
} }
Parent.CurrentDragIndex = -1;
Parent.CurrentDragItem = "null";
Parent.CurrentDragNumber = 0;
Parent.CurrentDragStackSize = 0;
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize });
} }
/// <summary>
/// Creating a new action when the user starts dragging an item
/// </summary>
private void OnDragStart() private void OnDragStart()
{ {
Parent.CurrentInventoryItem = this; Parent.CurrentInventoryItem = this;
@ -118,9 +181,15 @@ public partial class InventoryItem
Parent.CurrentDragItem = this.Item; Parent.CurrentDragItem = this.Item;
Parent.CurrentDragNumber = this.Number; Parent.CurrentDragNumber = this.Number;
Parent.CurrentDragStackSize = this.StackSize; Parent.CurrentDragStackSize = this.StackSize;
this.Item = "null";
this.Number = 0; // remove the element if it's not in the list of items
this.StackSize = 0; if (this.Index != 0)
{
this.Item = "null";
this.Number = 0;
this.StackSize = 0;
}
Parent.Actions.Add(new InventoryAction { Action = "Drag Start", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize}); Parent.Actions.Add(new InventoryAction { Action = "Drag Start", ItemName = this.Item, Index = this.Index, Number = this.Number, StackSize = this.StackSize});
} }

@ -1,11 +1,18 @@
using System; using System;
using System.Xml.Linq;
using myBlazorApp.Models; using myBlazorApp.Models;
namespace myBlazorApp.Components namespace myBlazorApp.Components
{ {
public class InventoryListItem public class InventoryListItem
{ {
//public InventoryListItem() { }
/// <summary>
/// Constructor
/// </summary>
/// <param name="itemName"> name of the item </param>
/// <param name="position"> position of the item in the Inventory </param>
/// <param name="number"> number of items at a certain position </param>
/// <param name="stackSize"> max number of the same item at one position </param>
public InventoryListItem(string itemName, int position, int number, int stackSize) public InventoryListItem(string itemName, int position, int number, int stackSize)
{ {
this.itemName = itemName; this.itemName = itemName;
@ -14,9 +21,24 @@ namespace myBlazorApp.Components
this.stackSize = stackSize; this.stackSize = stackSize;
} }
/// <summary>
/// Gets or sets the name of the item.
/// </summary>
public string itemName { get; set; } public string itemName { get; set; }
public int position { get; set; }
/// <summary>
/// Gets and sets the position of the item in the Inventory.
/// </summary>
public int position { get; set; }
/// <summary>
/// Gets and sets the number of items at a certain position.
/// </summary>
public int number { get; set; } public int number { get; set; }
/// <summary>
/// Gets and sets the max number of the same item at one position.
/// </summary>
public int stackSize { get; set; } public int stackSize { get; set; }
} }

@ -1,4 +1,5 @@
using System; /// IMPORTS ///
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
@ -15,20 +16,46 @@ namespace myBlazorApp.Components
{ {
public partial class MyInventory public partial class MyInventory
{ {
/// -------- INVENTORY PART -------- ///
/// <summary>
/// Get or Set the list of items of the inventory.
/// </summary>
[Parameter] [Parameter]
public List<InventoryListItem> ItemsInventory { get; set; } = new List<InventoryListItem>(); public List<InventoryListItem> ItemsInventory { get; set; } = new List<InventoryListItem>();
public int index { get; set; } = 0; /// <summary>
/// Get or Set the list of actions the user made.
/// </summary>
public ObservableCollection<InventoryAction> Actions { get; set; } public ObservableCollection<InventoryAction> Actions { get; set; }
/// <summary>
/// Get or Set the item's name the user is dragging.
/// </summary>
public string CurrentDragItem { get; set; } public string CurrentDragItem { get; set; }
/// <summary>
/// Get or Set the item's index the user is dragging.
/// </summary>
public int CurrentDragIndex { get; set; } public int CurrentDragIndex { get; set; }
/// <summary>
/// Get or Set the number of items the user is dragging.
/// </summary>
public int CurrentDragNumber { get; set; } public int CurrentDragNumber { get; set; }
/// <summary>
/// Get or Set the item's stack size the user is dragging.
/// </summary>
public int CurrentDragStackSize { get; set; } public int CurrentDragStackSize { get; set; }
public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem();
/// <summary>
/// Get or Set the InvntoryItem the user is dragging.
/// </summary>
public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem();
///-------- LIST PART -------- ///
private List<Item> items = new List<Item>(); private List<Item> items = new List<Item>();
private List<Item> full = new List<Item>(); private List<Item> full = new List<Item>();
@ -47,6 +74,11 @@ namespace myBlazorApp.Components
List<Item> Sorted = new List<Item>(); List<Item> Sorted = new List<Item>();
/// -------- DEPENDENCIES INJECTION -------- ///
/// <summary>
/// gets or sets the data service ( API or Local Stroage )
/// </summary>
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
@ -58,13 +90,6 @@ namespace myBlazorApp.Components
private bool isSorted = false; private bool isSorted = false;
public MyInventory()
{
Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged;
}
[Inject] [Inject]
public IStringLocalizer<MyInventory> Localizer { get; set; } public IStringLocalizer<MyInventory> Localizer { get; set; }
@ -72,18 +97,44 @@ namespace myBlazorApp.Components
[Inject] [Inject]
internal IJSRuntime JavaScriptRuntime { get; set; } internal IJSRuntime JavaScriptRuntime { get; set; }
/// -------- METHODS -------- ///
/// <summary>
/// Constructor of the component.
/// </summary>
public MyInventory()
{
/// Creating new Actions list
Actions = new ObservableCollection<InventoryAction>();
/// subscribing the event "changement in the collection Actions" to the method OnActionsCollectionChanged.
Actions.CollectionChanged += OnActionsCollectionChanged;
}
/// <summary>
/// Method called when the collection "Actions" changes.
/// Invoke the javascript script and call the API to save the inventory's data
/// </summary>
/// <param name="sender"> sender of the event </param>
/// <param name="e"> event arguments </param>
private async void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private async void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
/// Invoke the js script
JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems); JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems);
/// call the dataservice if the action is "end", "drop" or "start"
if (this.Actions.Last().Action == "End" || this.Actions.Last().Action == "Drop" || this.Actions.Last().Action == "Start") 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(), this.Actions.Last().StackSize.ToString() }; string[] infos = new string[] { this.Actions.Last().ItemName, this.Actions.Last().Index.ToString(), this.Actions.Last().Number.ToString(), this.Actions.Last().StackSize.ToString() };
await DataService.UpdateInventory(infos); await DataService.UpdateInventory(infos);
} }
} }
/// <summary>
/// Read data from the data service to display them in the inventory and in the data grid
/// </summary>
/// <param name="e"> Datagrid of items </param>
/// <returns></returns>
private async Task OnReadData(DataGridReadDataEventArgs<Item> e) private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
{ {
@ -94,10 +145,11 @@ namespace myBlazorApp.Components
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
/// Read the items of the inventory
ItemsInventory = await DataService.GetInventoryItems();
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);
ItemsInventory = await DataService.GetInventoryItems();
currentPage = e.Page; currentPage = e.Page;
pageSize = e.PageSize; pageSize = e.PageSize;
if (isSorted == false) if (isSorted == false)

Loading…
Cancel
Save