ADD: functionnal drag and drop for the inventory part and memorization of the inventory

master
Lucie Bedouret 3 years ago
parent 0ce7e05aba
commit ff94267e86

@ -116,7 +116,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <returns>The async task.</returns> /// <returns>The async task.</returns>
[HttpPut] [HttpPut]
[Route("")] [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); 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."); throw new Exception("Unable to get the inventory.");
} }
var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.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]);
if (inventoryItem == null) data.ToArray()[Int32.Parse(infos[1])-1].Number = Int32.Parse(infos[2]);
{
throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}");
}
inventoryItem.ItemName = item.ItemName;
inventoryItem.Position = item.Position;
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions)); 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": "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
}
]

@ -20,5 +20,7 @@ namespace Minecraft.Crafting.Api.Models
/// Gets or sets the position. /// Gets or sets the position.
/// </summary> /// </summary>
public int Position { get; set; } public int Position { get; set; }
public int Number { get; set; }
} }
} }

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

@ -1,5 +1,7 @@
using System; using System;
namespace myBlazorApp.Components; namespace myBlazorApp.Components;
using System.Reflection.Metadata;
using Blazorise; using Blazorise;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using myBlazorApp.Models; using myBlazorApp.Models;
@ -7,13 +9,14 @@ using myBlazorApp.Models;
public partial class InventoryItem public partial class InventoryItem
{ {
[Parameter] [Parameter]
public Item? Item { get; set; } public string Item { get; set; }
public string state { get; set; } = null;
[Parameter] [Parameter]
public int Index { get; set; } public int Index { get; set; }
[Parameter]
public int Number { get; set; }
[Parameter] [Parameter]
public bool NoDrop { get; set; } public bool NoDrop { get; set; }
@ -26,22 +29,7 @@ public partial class InventoryItem
{ {
return; 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 });
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() internal void OnDragLeave()
@ -50,8 +38,7 @@ public partial class InventoryItem
{ {
return; 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 });
state = "left";
} }
internal void OnDrop() internal void OnDrop()
@ -60,20 +47,60 @@ public partial class InventoryItem
{ {
return; 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.Item = Parent.CurrentDragItem;
this.Number = Parent.CurrentDragNumber;
Parent.CurrentInventoryItem.Item = tmpItem;
Parent.CurrentInventoryItem.Number = tmpNumber;
} }
Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index });
state = null; }
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() private void OnDragStart()
{ {
Parent.CurrentInventoryItem = this;
Parent.CurrentDragIndex = this.Index;
Parent.CurrentDragItem = this.Item; Parent.CurrentDragItem = this.Item;
this.Item = null; Parent.CurrentDragNumber = this.Number;
Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); this.Item = "null";
state = "start"; 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,15 +1,11 @@
<CascadingValue Value="@this"> @using myBlazorApp.Models
<CascadingValue Value="@this">
<h3>My Inventory</h3> <h3>My Inventory</h3>
<div class="inventory-items"> <div class="inventory-items">
@foreach (var item in ItemsInventory) @foreach(InventoryListItem i in ItemsInventory)
{ {
<InventoryItem Index="@index" Item="item" NoDrop="true" /> <InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" />
index++;
}
@for (int i = 0; i < 18; i++)
{
<InventoryItem Index="i" Item="null" />
} }
</div> </div>

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

@ -5,7 +5,7 @@
<div class="body"> <div class="body">
<div class="inventory"> <div class="inventory">
<MyInventory ItemsInventory="@itemsInv" /> <MyInventory ItemsInventory="@itemsInv"/>
</div> </div>
<div id="ItemList"> <div id="ItemList">

@ -4,6 +4,7 @@ using Blazored.Modal.Services;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using myBlazorApp.Components;
using myBlazorApp.Factories; using myBlazorApp.Factories;
using myBlazorApp.Modals; using myBlazorApp.Modals;
using myBlazorApp.Models; using myBlazorApp.Models;
@ -13,7 +14,7 @@ namespace myBlazorApp.Pages
{ {
public partial class Inventory 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>(); private List<Item> items = new List<Item>();
@ -51,8 +52,8 @@ namespace myBlazorApp.Pages
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
items = await DataService.List(e.Page, e.PageSize); items = await DataService.List(e.Page, e.PageSize);
itemsInv = await DataService.List(e.Page, e.PageSize);
totalItem = await DataService.Count(); totalItem = await DataService.Count();
itemsInv = await DataService.GetInventoryItems();
} }
} }

@ -57,6 +57,17 @@ namespace myBlazorApp.Services
{ {
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:8635/api/Crafting/recipe"); return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:8635/api/Crafting/recipe");
} }
/* TASKS FOR INVENTORY */
public async Task<List<InventoryListItem>> GetInventoryItems()
{
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);
}
} }
} }

@ -182,5 +182,15 @@ namespace myBlazorApp.Services
return Task.FromResult(items); return Task.FromResult(items);
} }
public async Task<List<InventoryListItem>> GetInventoryItems()
{
return new List<InventoryListItem>();
}
public async Task UpdateInventory(string[] infos)
{
}
} }
} }

@ -13,6 +13,8 @@ namespace myBlazorApp.Services
Task Update(int id, ItemModel model); Task Update(int id, ItemModel model);
Task Delete(int id); Task Delete(int id);
Task<List<CraftingRecipe>> GetRecipes(); Task<List<CraftingRecipe>> GetRecipes();
Task<List<InventoryListItem>> GetInventoryItems();
Task UpdateInventory(string[] infos);
} }
} }

Loading…
Cancel
Save