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,24 +12,30 @@
<div class="css-grid">
<InventoryItem Index="0" />
<InventoryItem Index="1" />
<InventoryItem Index="2" />
<InventoryItem Index="3" />
<InventoryItem Index="4" />
<InventoryItem Index="5" />
<InventoryItem Index="6" />
<InventoryItem Index="7" />
<InventoryItem Index="8" />
<InventoryItem Index="9" />
<InventoryItem Index="10" />
<InventoryItem Index="11" />
<InventoryItem Index="12" />
<InventoryItem Index="13" />
<InventoryItem Index="14" />
<InventoryItem Index="15" />
<InventoryItem Index="16" />
<InventoryItem Index="17" />
<!--
<InventoryItem Index="0" />
<InventoryItem Index="1" />
<InventoryItem Index="2" />
<InventoryItem Index="3" />
<InventoryItem Index="4" />
<InventoryItem Index="5" />
<InventoryItem Index="6" />
<InventoryItem Index="7" />
<InventoryItem Index="8" />
<InventoryItem Index="9" />
<InventoryItem Index="10" />
<InventoryItem Index="11" />
<InventoryItem Index="12" />
<InventoryItem Index="13" />
<InventoryItem Index="14" />
<InventoryItem Index="15" />
<InventoryItem Index="16" />
<InventoryItem Index="17" />
-->
@for(int i=0; i<InventorySize; i++)
{
<InventoryItem Index="@i"/>
}
</div>
</div>
@ -43,7 +49,7 @@
<div class="css-dataGrid">
<div class="bar">
<input type="text" />
<input type="text" oninput="@OnInput"/>
<Button type="submit" @onclick="() => SortByName()">SORT</Button>
</div>
<DataGrid TItem="Item"

@ -6,16 +6,22 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized;
using BlazorApp.Services;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components.Web;
namespace BlazorApp.Components
{
public partial class InventoryComp
{
[Parameter]
public int InventorySize { get; set; }
public Item CurrentDragItem { get; set; }
public int CurrentDragIndex { get; set; }
public int CurrentDragNbItem { get; set; }
[Inject]
public IDataService DataService { get; set; }
@ -37,6 +43,8 @@ namespace BlazorApp.Components
private int CurrentPage { get; set; }
private string Recherche { get; set; }
public InventoryComp()
{
@ -44,9 +52,17 @@ namespace BlazorApp.Components
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)
{
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems);
@ -58,17 +74,23 @@ namespace BlazorApp.Components
{
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);
totalItem = await DataService.Count();
CurrentPage = e.Page;
PageSize = e.PageSize;
}
else if (!string.IsNullOrWhiteSpace(Recherche))
{
(Items, totalItem) = await DataService.SearchList(CurrentPage, PageSize, Recherche);
}
else
{
CurrentPage = e.Page;
PageSize = e.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();
}

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

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

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

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using BlazorApp.Components;
using BlazorApp.Factories;
using BlazorApp.Models;
using static System.Formats.Asn1.AsnWriter;
namespace BlazorApp.Services
{
@ -61,12 +63,45 @@ namespace BlazorApp.Services
{
List<Item> it = await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all/");
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;
return it.GetRange((currentPage - 1) * 10, pageSize - tmp);
int tmp = (indexDeb + pageSize) - it.Count();
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();
}
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<List<CraftingRecipe>> GetRecipes();
Task<List<Item>> SortedList(int currentPage, int pageSize);
Task<(List<Item>, int)> SearchList(int currentPage, int pageSize, string recherche);
}
}

Loading…
Cancel
Save