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>
/// Updates the inventory.
/// </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>
[HttpPut]
[Route("")]

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

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

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

@ -1,38 +1,77 @@
using System;
namespace myBlazorApp.Components;
/// IMPORTS ///
using System;
using System.Reflection.Metadata;
using Blazorise;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using myBlazorApp.Models;
namespace myBlazorApp.Components;
public partial class InventoryItem
{
/// -------- PARAMETERS -------- ///
/// <summary>
/// gets or sets the item name
/// </summary>
[Parameter]
public string Item { get; set; }
/// <summary>
/// gets or sets the index
/// </summary>
[Parameter]
public int Index { get; set; }
/// <summary>
/// gets or sets the number of items
/// </summary>
[Parameter]
public int Number { get; set; }
/// <summary>
/// gets or sets the stack size
/// </summary>
[Parameter]
public int StackSize { get; set; }
/// <summary>
/// gets or sets the capability to drop the item
/// </summary>
[Parameter]
public bool NoDrop { get; set; }
/// <summary>
/// gets or sets the image of the item
/// </summary>
[Parameter]
public string Image { get; set; }
/// -------- DEPENDENCIES INJECTION -------- ///
/// <summary>
/// Gets or sets the localizer
/// </summary>
[Inject]
public IStringLocalizer<InventoryItem> Localizer { get; set; }
/// -------- CASCADING PARAMETER -------- ///
/// <summary>
/// Gets or sets the cascading parameter Parent ( MyInventory ).
/// </summary>
[CascadingParameter]
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()
{
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 });
}
/// <summary>
/// Creating a new action when the user leaves an item of the Inventory items
/// </summary>
internal void OnDragLeave()
{
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 });
}
/// <summary>
/// Creating a new action when the user drops an item in an Inventory item in the Invnetory item
/// </summary>
internal void OnDrop()
{
if (Parent.CurrentDragItem != "null")
{
/// Case when the item comes from the list of items
if(Parent.CurrentDragIndex == 0)
{
if (this.Item == Parent.CurrentDragItem)
{
this.Number = this.Number + Parent.CurrentDragNumber;
}
else
{
this.Number = Parent.CurrentDragNumber;
}
this.Item = Parent.CurrentDragItem;
this.StackSize = Parent.CurrentDragStackSize;
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)
{
this.Number = this.Number + Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Number = 0;
Parent.CurrentInventoryItem.Item = "null";
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.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
{
string tmpItem = this.Item;
int tmpNumber = this.Number;
int tmpStackSize = this.StackSize;
Parent.ItemsInventory[Parent.CurrentDragIndex - 1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber, tmpStackSize);
this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
this.StackSize = Parent.CurrentDragStackSize;
Parent.CurrentInventoryItem.Item = tmpItem;
Parent.CurrentInventoryItem.Number = tmpNumber;
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()
{
if(NoDrop || Parent.CurrentDragItem == "null")
{
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);
this.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
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()
{
Parent.CurrentInventoryItem = this;
@ -118,9 +181,15 @@ public partial class InventoryItem
Parent.CurrentDragItem = this.Item;
Parent.CurrentDragNumber = this.Number;
Parent.CurrentDragStackSize = this.StackSize;
// remove the element if it's not in the list of items
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});
}

@ -1,11 +1,18 @@
using System;
using System.Xml.Linq;
using myBlazorApp.Models;
namespace myBlazorApp.Components
{
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)
{
this.itemName = itemName;
@ -14,9 +21,24 @@ namespace myBlazorApp.Components
this.stackSize = stackSize;
}
/// <summary>
/// Gets or sets the name of the item.
/// </summary>
public string itemName { 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; }
/// <summary>
/// Gets and sets the max number of the same item at one position.
/// </summary>
public int stackSize { get; set; }
}

@ -1,4 +1,5 @@
using System;
/// IMPORTS ///
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
@ -15,19 +16,45 @@ namespace myBlazorApp.Components
{
public partial class MyInventory
{
/// -------- INVENTORY PART -------- ///
/// <summary>
/// Get or Set the list of items of the inventory.
/// </summary>
[Parameter]
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; }
/// <summary>
/// Get or Set the item's name the user is dragging.
/// </summary>
public string CurrentDragItem { get; set; }
/// <summary>
/// Get or Set the item's index the user is dragging.
/// </summary>
public int CurrentDragIndex { get; set; }
/// <summary>
/// Get or Set the number of items the user is dragging.
/// </summary>
public int CurrentDragNumber { get; set; }
/// <summary>
/// Get or Set the item's stack size the user is dragging.
/// </summary>
public int CurrentDragStackSize { get; set; }
/// <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>();
@ -47,6 +74,11 @@ namespace myBlazorApp.Components
List<Item> Sorted = new List<Item>();
/// -------- DEPENDENCIES INJECTION -------- ///
/// <summary>
/// gets or sets the data service ( API or Local Stroage )
/// </summary>
[Inject]
public IDataService DataService { get; set; }
@ -58,13 +90,6 @@ namespace myBlazorApp.Components
private bool isSorted = false;
public MyInventory()
{
Actions = new ObservableCollection<InventoryAction>();
Actions.CollectionChanged += OnActionsCollectionChanged;
}
[Inject]
public IStringLocalizer<MyInventory> Localizer { get; set; }
@ -72,9 +97,32 @@ namespace myBlazorApp.Components
[Inject]
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)
{
/// Invoke the js script
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")
{
string[] infos = new string[] { this.Actions.Last().ItemName, this.Actions.Last().Index.ToString(), this.Actions.Last().Number.ToString(), this.Actions.Last().StackSize.ToString() };
@ -82,8 +130,11 @@ namespace myBlazorApp.Components
}
}
/// <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)
{
@ -94,10 +145,11 @@ namespace myBlazorApp.Components
if (!e.CancellationToken.IsCancellationRequested)
{
/// Read the items of the inventory
ItemsInventory = await DataService.GetInventoryItems();
totalItem = await DataService.Count();
items = await DataService.List(e.Page, e.PageSize);
full = await DataService.List(e.Page, totalItem);
ItemsInventory = await DataService.GetInventoryItems();
currentPage = e.Page;
pageSize = e.PageSize;
if (isSorted == false)

Loading…
Cancel
Save