From 0f1bc79ff3602c47b5d427d02475bc4d020ffb6a Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Tue, 20 Dec 2022 15:33:15 +0100 Subject: [PATCH] ADD : comments for the inventory part --- .../Controllers/InventoryController.cs | 2 +- .../Data/inventory.json | 30 ++---- .../Models/InventoryModel.cs | 6 ++ .../myBlazorApp/Components/InventoryAction.cs | 21 +++- .../Components/InventoryItem.razor.cs | 97 ++++++++++++++++--- .../Components/InventoryListItem.cs | 28 +++++- .../Components/MyInventory.razor.cs | 86 ++++++++++++---- 7 files changed, 214 insertions(+), 56 deletions(-) diff --git a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs index de68196..5b871c0 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs @@ -112,7 +112,7 @@ namespace Minecraft.Crafting.Api.Controllers /// /// Updates the inventory. /// - /// The item. + /// The infos about the item (name, position, number, stacksize). /// The async task. [HttpPut] [Route("")] diff --git a/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json index 8851dab..559c0cc 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json +++ b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json @@ -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", diff --git a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs index ab7325b..e6c8806 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs @@ -21,8 +21,14 @@ namespace Minecraft.Crafting.Api.Models /// public int Position { get; set; } + /// + /// Gets or sets the number of items in a stack. + /// public int Number { get; set; } + /// + /// Gets or sets the stack size of an item. + /// public int StackSize { get; set; } } } \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs index 2108c84..da9d545 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs @@ -3,11 +3,30 @@ using myBlazorApp.Models; namespace myBlazorApp.Components { public class InventoryAction - { + { + /// + /// Gets and sets the action + /// public string Action { get; set; } + + /// + /// Gets and sets the inde where the action affected + /// public int Index { get; set; } + + /// + /// Gets and sets the item name the action affected + /// public string ItemName { get; set; } + + /// + /// Gets and sets the number of items the action affected + /// public int Number { get; set; } + + /// + /// Gets and sets the stack size of the item the action affected + /// public int StackSize { get; set; } } } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs index 96e8e0b..bbb5d8f 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs @@ -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 -------- /// + + /// + /// gets or sets the item name + /// [Parameter] public string Item { get; set; } + /// + /// gets or sets the index + /// [Parameter] public int Index { get; set; } + /// + /// gets or sets the number of items + /// [Parameter] public int Number { get; set; } + /// + /// gets or sets the stack size + /// [Parameter] public int StackSize { get; set; } + /// + /// gets or sets the capability to drop the item + /// [Parameter] public bool NoDrop { get; set; } + /// + /// gets or sets the image of the item + /// [Parameter] public string Image { get; set; } + + /// -------- DEPENDENCIES INJECTION -------- /// + + /// + /// Gets or sets the localizer + /// [Inject] public IStringLocalizer Localizer { get; set; } + + /// -------- CASCADING PARAMETER -------- /// + + /// + /// Gets or sets the cascading parameter Parent ( MyInventory ). + /// [CascadingParameter] public MyInventory Parent { get; set; } + + /// -------- DRAG AND DROP METHODS -------- /// + + /// + /// Creating a new action when the user enter in an item of the Inventory items + /// 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 }); } + /// + /// Creating a new action when the user leaves an item of the Inventory items + /// 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 }); } + /// + /// Creating a new action when the user drops an item in an Inventory item in the Invnetory item + /// internal void OnDrop() { if (Parent.CurrentDragItem != "null") { + /// Case when the item comes from the list of items 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.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 } + /// + /// Creating a new action when the user end the drag and dropped the item in an Inventory item + /// 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 }); } + + /// + /// Creating a new action when the user starts dragging an item + /// 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; - this.Item = "null"; - this.Number = 0; - this.StackSize = 0; + + // 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}); } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs index 17201db..6009891 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs @@ -1,11 +1,18 @@ using System; +using System.Xml.Linq; using myBlazorApp.Models; namespace myBlazorApp.Components { public class InventoryListItem { - //public InventoryListItem() { } - + + /// + /// Constructor + /// + /// name of the item + /// position of the item in the Inventory + /// number of items at a certain position + /// max number of the same item at one position public InventoryListItem(string itemName, int position, int number, int stackSize) { this.itemName = itemName; @@ -14,9 +21,24 @@ namespace myBlazorApp.Components this.stackSize = stackSize; } + /// + /// Gets or sets the name of the item. + /// public string itemName { get; set; } - public int position { get; set; } + + /// + /// Gets and sets the position of the item in the Inventory. + /// + public int position { get; set; } + + /// + /// Gets and sets the number of items at a certain position. + /// public int number { get; set; } + + /// + /// Gets and sets the max number of the same item at one position. + /// public int stackSize { get; set; } } diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs index 61f5f9b..2d9989e 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs @@ -1,4 +1,5 @@ -using System; +/// IMPORTS /// +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; @@ -15,20 +16,46 @@ namespace myBlazorApp.Components { public partial class MyInventory { + /// -------- INVENTORY PART -------- /// + + /// + /// Get or Set the list of items of the inventory. + /// [Parameter] public List ItemsInventory { get; set; } = new List(); - public int index { get; set; } = 0; - + /// + /// Get or Set the list of actions the user made. + /// public ObservableCollection Actions { get; set; } + /// + /// Get or Set the item's name the user is dragging. + /// public string CurrentDragItem { get; set; } + + /// + /// Get or Set the item's index the user is dragging. + /// public int CurrentDragIndex { get; set; } + + /// + /// Get or Set the number of items the user is dragging. + /// public int CurrentDragNumber { get; set; } + + /// + /// Get or Set the item's stack size the user is dragging. + /// public int CurrentDragStackSize { get; set; } - public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem(); + /// + /// Get or Set the InvntoryItem the user is dragging. + /// + public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem(); + ///-------- LIST PART -------- /// + private List items = new List(); private List full = new List(); @@ -47,6 +74,11 @@ namespace myBlazorApp.Components List Sorted = new List(); + /// -------- DEPENDENCIES INJECTION -------- /// + + /// + /// gets or sets the data service ( API or Local Stroage ) + /// [Inject] public IDataService DataService { get; set; } @@ -58,13 +90,6 @@ namespace myBlazorApp.Components private bool isSorted = false; - public MyInventory() - { - Actions = new ObservableCollection(); - Actions.CollectionChanged += OnActionsCollectionChanged; - - } - [Inject] public IStringLocalizer Localizer { get; set; } @@ -72,18 +97,44 @@ namespace myBlazorApp.Components [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } + + /// -------- METHODS -------- /// + + /// + /// Constructor of the component. + /// + public MyInventory() + { + /// Creating new Actions list + Actions = new ObservableCollection(); + /// subscribing the event "changement in the collection Actions" to the method OnActionsCollectionChanged. + Actions.CollectionChanged += OnActionsCollectionChanged; + } + + /// + /// Method called when the collection "Actions" changes. + /// Invoke the javascript script and call the API to save the inventory's data + /// + /// sender of the event + /// event arguments 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() }; - await DataService.UpdateInventory(infos); - } + 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); + } } - - + /// + /// Read data from the data service to display them in the inventory and in the data grid + /// + /// Datagrid of items + /// private async Task OnReadData(DataGridReadDataEventArgs 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)