From 0ce7e05aba64c55774805be593281329493a09e3 Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Wed, 7 Dec 2022 12:00:42 +0100 Subject: [PATCH 1/2] =?UTF-8?q?ADD:=20Drag=20and=20drop=20fonctionnels,=20?= =?UTF-8?q?mais=20besoin=20de=20corriger=20les=20bugs=20d=C3=BBs=20aux=20a?= =?UTF-8?q?nciens=20=C3=A9l=C3=A9ments=20de=20la=20liste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/InventoryItem.razor | 2 +- .../Components/InventoryItem.razor.cs | 29 +++++++++++++++++-- .../myBlazorApp/Components/MyInventory.razor | 6 ++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor index 3755db3..809e8ba 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor @@ -3,7 +3,6 @@ class="inventory-item" ondragover="event.preventDefault();" draggable="true" - @ondragstart="@OnDragStart" @ondrop="@OnDrop" @ondragenter="@OnDragEnter" @@ -12,6 +11,7 @@ @if (Item != null) { @Item.DisplayName + @Item.StackSize } else { diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs index 3efe7a0..913b773 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs @@ -7,7 +7,9 @@ using myBlazorApp.Models; public partial class InventoryItem { [Parameter] - public Item Item { get; set; } + public Item? Item { get; set; } + + public string state { get; set; } = null; [Parameter] public int Index { get; set; } @@ -25,6 +27,21 @@ public partial class InventoryItem return; } Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + state = "enter"; + if (this.Item != null) + { + Thread.Sleep(2000); + if (state == "enter") + { + Item item = Parent.CurrentDragItem; + Parent.CurrentDragItem = this.Item; + this.Item = item; + Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = Parent.CurrentDragItem, Index = this.Index }); + + } + } + } internal void OnDragLeave() @@ -34,6 +51,7 @@ public partial class InventoryItem return; } Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + state = "left"; } internal void OnDrop() @@ -42,15 +60,20 @@ public partial class InventoryItem { return; } - this.Item = Parent.CurrentDragItem; + if (this.Item == null) + { + this.Item = Parent.CurrentDragItem; + } Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); - + state = null; } private void OnDragStart() { Parent.CurrentDragItem = this.Item; + this.Item = null; Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + state = "start"; } } diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor b/myBlazorApp/myBlazorApp/Components/MyInventory.razor index f4718ae..c32ed17 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor @@ -2,16 +2,16 @@

My Inventory

- @foreach (var item in ItemsInventory) { index++; } - @for (int i = @index; i < 18; i++) + @for (int i = 0; i < 18; i++) { - + } +
From ff94267e86400910d51628c87e02b15ad46b423c Mon Sep 17 00:00:00 2001 From: Lucie Bedouret Date: Sat, 17 Dec 2022 22:34:24 +0100 Subject: [PATCH 2/2] ADD: functionnal drag and drop for the inventory part and memorization of the inventory --- .../Controllers/InventoryController.cs | 14 +--- .../Data/inventory.json | 76 +++++++++++++++++ .../Models/InventoryModel.cs | 2 + .../myBlazorApp/Components/InventoryAction.cs | 3 +- .../Components/InventoryItem.razor | 23 +++--- .../Components/InventoryItem.razor.cs | 81 ++++++++++++------- .../Components/InventoryListItem.cs | 22 +++++ .../myBlazorApp/Components/MyInventory.razor | 12 +-- .../Components/MyInventory.razor.cs | 16 +++- myBlazorApp/myBlazorApp/Pages/Inventory.razor | 2 +- .../myBlazorApp/Pages/Inventory.razor.cs | 7 +- .../myBlazorApp/Services/DataApiService.cs | 11 +++ .../myBlazorApp/Services/DataLocalService.cs | 10 +++ .../myBlazorApp/Services/IDataService.cs | 2 + 14 files changed, 215 insertions(+), 66 deletions(-) create mode 100644 myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json create mode 100644 myBlazorApp/myBlazorApp/Components/InventoryListItem.cs diff --git a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs index 40c97bb..c0443a5 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs @@ -116,7 +116,7 @@ namespace Minecraft.Crafting.Api.Controllers /// The async task. [HttpPut] [Route("")] - public Task UpdateInventory(InventoryModel item) + public Task UpdateInventory(string[] infos) { var data = JsonSerializer.Deserialize>(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)); diff --git a/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json new file mode 100644 index 0000000..806f26e --- /dev/null +++ b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json @@ -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": "lulu", + "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 + } +] \ No newline at end of file diff --git a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs index 0fa1936..22b1b81 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs @@ -20,5 +20,7 @@ namespace Minecraft.Crafting.Api.Models /// Gets or sets the position. /// public int Position { get; set; } + + public int Number { get; set; } } } \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs index 6fb225c..bdb47b6 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs @@ -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; } } } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor index 809e8ba..7502998 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor @@ -1,17 +1,16 @@  -
- @if (Item != null) +
+ @if (Item != "null") { - @Item.DisplayName - @Item.StackSize +

@Item

+

@Number

} else { diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs index 913b773..85cc073 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs @@ -1,5 +1,7 @@ using System; namespace myBlazorApp.Components; + +using System.Reflection.Metadata; using Blazorise; using Microsoft.AspNetCore.Components; using myBlazorApp.Models; @@ -7,13 +9,14 @@ using myBlazorApp.Models; public partial class InventoryItem { [Parameter] - public Item? Item { get; set; } - - public string state { get; set; } = null; + public string Item { get; set; } [Parameter] public int Index { get; set; } + [Parameter] + public int Number { get; set; } + [Parameter] public bool NoDrop { get; set; } @@ -26,22 +29,7 @@ public partial class InventoryItem { return; } - Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); - state = "enter"; - if (this.Item != null) - { - Thread.Sleep(2000); - if (state == "enter") - { - Item item = Parent.CurrentDragItem; - Parent.CurrentDragItem = this.Item; - this.Item = item; - Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = item, Index = this.Index }); - Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = Parent.CurrentDragItem, Index = this.Index }); - - } - } - + Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number }); } internal void OnDragLeave() @@ -50,8 +38,7 @@ public partial class InventoryItem { return; } - Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); - state = "left"; + Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number }); } internal void OnDrop() @@ -60,20 +47,60 @@ public partial class InventoryItem { return; } - if (this.Item == null) + 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.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); - state = null; + 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; - this.Item = null; - Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); - state = "start"; + 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}); + } } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs new file mode 100644 index 0000000..f7a3ef1 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs @@ -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; } + } + +} + diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor b/myBlazorApp/myBlazorApp/Components/MyInventory.razor index c32ed17..cef2ec2 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor @@ -1,15 +1,11 @@ - +@using myBlazorApp.Models +

My Inventory

- @foreach (var item in ItemsInventory) + @foreach(InventoryListItem i in ItemsInventory) { - - index++; - } - @for (int i = 0; i < 18; i++) - { - + }
diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs index ae5b809..0359582 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs @@ -10,19 +10,22 @@ namespace myBlazorApp.Components public partial class MyInventory { [Parameter] - public List ItemsInventory { get; set; } = new List(); + public List ItemsInventory { get; set; } - [Parameter] public int index { get; set; } = 0; public ObservableCollection 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(); 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); + } } } } diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor b/myBlazorApp/myBlazorApp/Pages/Inventory.razor index a77d941..76f911c 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor @@ -5,7 +5,7 @@
- +
diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs index 9fcb34a..c6c5ee5 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs @@ -4,6 +4,7 @@ using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; +using myBlazorApp.Components; using myBlazorApp.Factories; using myBlazorApp.Modals; using myBlazorApp.Models; @@ -11,9 +12,9 @@ using myBlazorApp.Services; namespace myBlazorApp.Pages { - public partial class Inventory + public partial class Inventory { - public List itemsInv = new List(); + public List itemsInv = new List(); private List items = new List(); @@ -51,8 +52,8 @@ namespace myBlazorApp.Pages if (!e.CancellationToken.IsCancellationRequested) { items = await DataService.List(e.Page, e.PageSize); - itemsInv = await DataService.List(e.Page, e.PageSize); totalItem = await DataService.Count(); + itemsInv = await DataService.GetInventoryItems(); } } diff --git a/myBlazorApp/myBlazorApp/Services/DataApiService.cs b/myBlazorApp/myBlazorApp/Services/DataApiService.cs index ca17b30..06c87eb 100644 --- a/myBlazorApp/myBlazorApp/Services/DataApiService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataApiService.cs @@ -57,6 +57,17 @@ namespace myBlazorApp.Services { return await _http.GetFromJsonAsync>("https://localhost:8635/api/Crafting/recipe"); } + + /* TASKS FOR INVENTORY */ + public async Task> GetInventoryItems() + { + return await _http.GetFromJsonAsync>("https://localhost:8635/api/Inventory"); + } + + public async Task UpdateInventory(string[] infos) + { + await _http.PutAsJsonAsync("https://localhost:8635/api/Inventory", infos); + } } } diff --git a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs index 2bbe9d9..82e2857 100644 --- a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs @@ -182,5 +182,15 @@ namespace myBlazorApp.Services return Task.FromResult(items); } + + public async Task> GetInventoryItems() + { + return new List(); + } + + public async Task UpdateInventory(string[] infos) + { + + } } } diff --git a/myBlazorApp/myBlazorApp/Services/IDataService.cs b/myBlazorApp/myBlazorApp/Services/IDataService.cs index 845bcee..a455022 100644 --- a/myBlazorApp/myBlazorApp/Services/IDataService.cs +++ b/myBlazorApp/myBlazorApp/Services/IDataService.cs @@ -13,6 +13,8 @@ namespace myBlazorApp.Services Task Update(int id, ItemModel model); Task Delete(int id); Task> GetRecipes(); + Task> GetInventoryItems(); + Task UpdateInventory(string[] infos); } }