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>
[HttpPut]
[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);
@ -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));

@ -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.
/// </summary>
public int Position { get; set; }
public int Number { get; set; }
}
}

@ -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; }
}
}

@ -1,17 +1,16 @@

<div
class="inventory-item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave"
>
@if (Item != null)
<div class="inventory-item"
ondragover="event.preventDefault();"
draggable="true"
@ondragstart="@OnDragStart"
@ondrop="@OnDrop"
@ondragend="@OnDragEnd"
@ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave">
@if (Item != "null")
{
@Item.DisplayName
@Item.StackSize
<p>@Item</p>
<p>@Number</p>
}
else
{

@ -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});
}
}

@ -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>
<div class="inventory-items">
@foreach (var item in ItemsInventory)
@foreach(InventoryListItem i in ItemsInventory)
{
<InventoryItem Index="@index" Item="item" NoDrop="true" />
index++;
}
@for (int i = 0; i < 18; i++)
{
<InventoryItem Index="i" Item="null" />
<InventoryItem Index="@i.position" Item="@i.itemName" Number="@i.number" />
}
</div>

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

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

@ -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<Item> itemsInv = new List<Item>();
public List<InventoryListItem> itemsInv = new List<InventoryListItem>();
private List<Item> items = new List<Item>();
@ -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();
}
}

@ -57,6 +57,17 @@ namespace myBlazorApp.Services
{
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);
}
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 Delete(int id);
Task<List<CraftingRecipe>> GetRecipes();
Task<List<InventoryListItem>> GetInventoryItems();
Task UpdateInventory(string[] infos);
}
}

Loading…
Cancel
Save