Add: Search bar fini + Gestion de la taille des objets + Gestion de la taille de l'inventaire dynamiquement

master
Thomas Chazot 2 years ago
parent 8fe330b567
commit 401caed8fe

@ -12,6 +12,7 @@
<div class="css-grid"> <div class="css-grid">
<!--
<InventoryItem Index="0" /> <InventoryItem Index="0" />
<InventoryItem Index="1" /> <InventoryItem Index="1" />
<InventoryItem Index="2" /> <InventoryItem Index="2" />
@ -30,6 +31,11 @@
<InventoryItem Index="15" /> <InventoryItem Index="15" />
<InventoryItem Index="16" /> <InventoryItem Index="16" />
<InventoryItem Index="17" /> <InventoryItem Index="17" />
-->
@for(int i=0; i<InventorySize; i++)
{
<InventoryItem Index="@i"/>
}
</div> </div>
</div> </div>
@ -43,7 +49,7 @@
<div class="css-dataGrid"> <div class="css-dataGrid">
<div class="bar"> <div class="bar">
<input type="text" /> <input type="text" oninput="@OnInput"/>
<Button type="submit" @onclick="() => SortByName()">SORT</Button> <Button type="submit" @onclick="() => SortByName()">SORT</Button>
</div> </div>
<DataGrid TItem="Item" <DataGrid TItem="Item"

@ -6,16 +6,22 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
using BlazorApp.Services; using BlazorApp.Services;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components.Web;
namespace BlazorApp.Components namespace BlazorApp.Components
{ {
public partial class InventoryComp public partial class InventoryComp
{ {
[Parameter]
public int InventorySize { get; set; }
public Item CurrentDragItem { get; set; } public Item CurrentDragItem { get; set; }
public int CurrentDragIndex { get; set; } public int CurrentDragIndex { get; set; }
public int CurrentDragNbItem { get; set; }
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
@ -37,6 +43,8 @@ namespace BlazorApp.Components
private int CurrentPage { get; set; } private int CurrentPage { get; set; }
private string Recherche { get; set; }
public InventoryComp() public InventoryComp()
{ {
@ -44,9 +52,17 @@ namespace BlazorApp.Components
Actions.CollectionChanged += OnActionsCollectionChanged; Actions.CollectionChanged += OnActionsCollectionChanged;
this.InventoryItems = new List<Item>(new Item[18]);
} }
protected override void OnInitialized()
{
this.InventoryItems = new List<Item>(new Item[InventorySize]);
}
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems);
@ -58,17 +74,23 @@ namespace BlazorApp.Components
{ {
return; return;
} }
if (!e.CancellationToken.IsCancellationRequested && !IsSorted)
CurrentPage = e.Page;
PageSize = e.PageSize;
if (!e.CancellationToken.IsCancellationRequested && !IsSorted && string.IsNullOrWhiteSpace(Recherche))
{ {
Items = await DataService.List(e.Page, e.PageSize); Items = await DataService.List(e.Page, e.PageSize);
totalItem = await DataService.Count(); totalItem = await DataService.Count();
CurrentPage = e.Page;
PageSize = e.PageSize; }
else if (!string.IsNullOrWhiteSpace(Recherche))
{
(Items, totalItem) = await DataService.SearchList(CurrentPage, PageSize, Recherche);
} }
else else
{ {
CurrentPage = e.Page;
PageSize = e.PageSize;
Items = await DataService.SortedList(CurrentPage, PageSize); Items = await DataService.SortedList(CurrentPage, PageSize);
} }
} }
@ -82,8 +104,26 @@ namespace BlazorApp.Components
} }
} }
void refresh() private async void OnInput(Microsoft.AspNetCore.Components.ChangeEventArgs args)
{
Recherche = (string)args.Value;
if (string.IsNullOrWhiteSpace(Recherche))
{
if (!IsSorted)
{
Items = await DataService.List(CurrentPage, PageSize);
}
else
{ {
Items = await DataService.SortedList(CurrentPage, PageSize);
}
totalItem = await DataService.Count();
}
else
{
(Items, totalItem) = await DataService.SearchList(CurrentPage, PageSize, Recherche);
}
StateHasChanged(); StateHasChanged();
} }

@ -22,7 +22,8 @@
} }
else else
{ {
@Item.DisplayName @Item.DisplayName;
@NbItem
} }
} }

@ -13,9 +13,12 @@ namespace BlazorApp.Components
[Parameter] [Parameter]
public Item Item { get; set; } public Item Item { get; set; }
public int NbItem { get; set; }
[Parameter] [Parameter]
public bool NoDrop { get; set; } public bool NoDrop { get; set; }
[CascadingParameter] [CascadingParameter]
public InventoryComp Parent { get; set; } public InventoryComp Parent { get; set; }
@ -46,6 +49,26 @@ namespace BlazorApp.Components
if (Parent.CurrentDragItem != null) if (Parent.CurrentDragItem != null)
{ {
if (Parent.CurrentDragItem.Equals(Item))
{
if (NbItem + Parent.CurrentDragNbItem > Item.StackSize)
{
int nbTrop = NbItem + Parent.CurrentDragNbItem - Item.StackSize;
NbItem = Item.StackSize;
Parent.CurrentDragNbItem = nbTrop;
}
else
{
NbItem = NbItem + Parent.CurrentDragNbItem;
Parent.CurrentDragNbItem = -1;
}
}
else
{
int changement = NbItem;
NbItem = Parent.CurrentDragNbItem;
Parent.CurrentDragNbItem = changement;
}
Item tmp = this.Item; Item tmp = this.Item;
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
Parent.InventoryItems[this.Index] = this.Item; Parent.InventoryItems[this.Index] = this.Item;
@ -59,18 +82,21 @@ namespace BlazorApp.Components
private void OnDragStart() private void OnDragStart()
{ {
Parent.CurrentDragItem = this.Item;
if (this.Item != null) if (this.Item != null)
{ {
Parent.CurrentDragItem = this.Item;
if (!NoDrop) if (!NoDrop)
{ {
Parent.CurrentDragIndex = this.Index; Parent.CurrentDragIndex = this.Index;
Parent.CurrentDragNbItem = NbItem;
this.Item = null; this.Item = null;
NbItem = -1;
Parent.InventoryItems[Index] = null; Parent.InventoryItems[Index] = null;
} }
else else
{ {
Parent.CurrentDragIndex = -1; Parent.CurrentDragIndex = -1;
Parent.CurrentDragNbItem = 1;
} }
} }
@ -84,10 +110,11 @@ namespace BlazorApp.Components
{ {
return; return;
} }
if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index) if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index && Parent.CurrentDragNbItem!=-1)
{ {
Parent.InventoryItems[this.Index] = this.Item; Parent.InventoryItems[this.Index] = this.Item;
this.Item = Parent.CurrentDragItem; this.Item = Parent.CurrentDragItem;
NbItem = Parent.CurrentDragNbItem;
} }
Parent.Actions.Add(new CraftingAction { Action = "Drag End", Item = this.Item, Index = this.Index }); Parent.Actions.Add(new CraftingAction { Action = "Drag End", Item = this.Item, Index = this.Index });

@ -5,6 +5,6 @@
<div> <div>
<InventoryComp Items="Items" /> <InventoryComp Items="Items" InventorySize="24"/>
</div> </div>

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic;
using BlazorApp.Components; using BlazorApp.Components;
using BlazorApp.Factories; using BlazorApp.Factories;
using BlazorApp.Models; using BlazorApp.Models;
using static System.Formats.Asn1.AsnWriter;
namespace BlazorApp.Services namespace BlazorApp.Services
{ {
@ -61,12 +63,45 @@ namespace BlazorApp.Services
{ {
List<Item> it = await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all/"); List<Item> it = await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all/");
it = it.OrderBy(i => i.DisplayName).ToList(); it = it.OrderBy(i => i.DisplayName).ToList();
if ((currentPage - 1) * 10+ pageSize > it.Count)
int indexDeb = (currentPage - 1) * pageSize;
if (indexDeb + pageSize > it.Count)
{
int tmp = indexDeb + pageSize - it.Count;
return it.GetRange(indexDeb, pageSize - tmp);
}
return it.GetRange(indexDeb, pageSize);
}
public async Task<(List<Item>, int)> SearchList(int currentPage, int pageSize, string recherche)
{
IEnumerable<Item> it = await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all/");
it = it.OrderBy(i => i.DisplayName).ToList();
it = from item in it
where item.DisplayName.StartsWith(recherche)
select item;
if (currentPage*10 > it.Count())
{
currentPage = it.Count() / pageSize + 1;
}
int indexDeb = (currentPage - 1) * pageSize;
if (it.Count() == 0)
{
return (it.ToList(), it.Count());
}
if (indexDeb + pageSize > it.Count())
{ {
int tmp = ((currentPage - 1) * 10 + pageSize) - it.Count; int tmp = (indexDeb + pageSize) - it.Count();
return it.GetRange((currentPage - 1) * 10, pageSize - tmp); return( it.ToList().GetRange(indexDeb, pageSize - tmp), it.Count());
} }
return it.GetRange((currentPage - 1) * 10, pageSize); return (it.ToList().GetRange(indexDeb, pageSize), it.Count());
} }

@ -189,6 +189,12 @@ namespace BlazorApp.Services
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<(List<Item>, int)> SearchList(int currentPage, int pageSize, string recherche)
{
throw new NotImplementedException();
}
} }
} }

@ -15,6 +15,7 @@ namespace BlazorApp.Services
Task Delete(int id); Task Delete(int id);
Task<List<CraftingRecipe>> GetRecipes(); Task<List<CraftingRecipe>> GetRecipes();
Task<List<Item>> SortedList(int currentPage, int pageSize); Task<List<Item>> SortedList(int currentPage, int pageSize);
Task<(List<Item>, int)> SearchList(int currentPage, int pageSize, string recherche);
} }
} }

Loading…
Cancel
Save