diff --git a/BlazorApp1/Components/Crafting.razor b/BlazorApp1/Components/Crafting.razor new file mode 100644 index 0000000..c8f143d --- /dev/null +++ b/BlazorApp1/Components/Crafting.razor @@ -0,0 +1,51 @@ + + + + + + Available items: + + + + @foreach (var item in Items) + { + + } + + + + + + + Recipe + + + + + + + + + + + + + + + + + Result + + + + + + + Actions + + + + + + + \ No newline at end of file diff --git a/BlazorApp1/Components/Crafting.razor.cs b/BlazorApp1/Components/Crafting.razor.cs new file mode 100644 index 0000000..90514d6 --- /dev/null +++ b/BlazorApp1/Components/Crafting.razor.cs @@ -0,0 +1,80 @@ +using BlazorApp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace BlazorApp1.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} diff --git a/BlazorApp1/Components/Crafting.razor.css b/BlazorApp1/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/BlazorApp1/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} diff --git a/BlazorApp1/Components/Crafting.razor.js b/BlazorApp1/Components/Crafting.razor.js new file mode 100644 index 0000000..8fdb58e --- /dev/null +++ b/BlazorApp1/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/BlazorApp1/Components/CraftingAction.cs b/BlazorApp1/Components/CraftingAction.cs new file mode 100644 index 0000000..3a77345 --- /dev/null +++ b/BlazorApp1/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using BlazorApp1.Models; + +namespace BlazorApp1.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/BlazorApp1/Components/CraftingItem.razor b/BlazorApp1/Components/CraftingItem.razor new file mode 100644 index 0000000..c1043b8 --- /dev/null +++ b/BlazorApp1/Components/CraftingItem.razor @@ -0,0 +1,14 @@ + + + @if (Item != null) + { + @Item.DisplayName + } + \ No newline at end of file diff --git a/BlazorApp1/Components/CraftingItem.razor.cs b/BlazorApp1/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..695e694 --- /dev/null +++ b/BlazorApp1/Components/CraftingItem.razor.cs @@ -0,0 +1,63 @@ +using BlazorApp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorApp1.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + } +} diff --git a/BlazorApp1/Components/CraftingItem.razor.css b/BlazorApp1/Components/CraftingItem.razor.css new file mode 100644 index 0000000..b2d4521 --- /dev/null +++ b/BlazorApp1/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/BlazorApp1/Components/CraftingRecipe.cs b/BlazorApp1/Components/CraftingRecipe.cs new file mode 100644 index 0000000..533c726 --- /dev/null +++ b/BlazorApp1/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using BlazorApp1.Models; + +namespace BlazorApp1.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/BlazorApp1/Pages/Index.razor b/BlazorApp1/Pages/Index.razor index 381542a..46c3e22 100644 --- a/BlazorApp1/Pages/Index.razor +++ b/BlazorApp1/Pages/Index.razor @@ -1,5 +1,6 @@ @page "/" @using System.Globalization +@using BlazorApp1.Components Index @@ -8,4 +9,7 @@ Welcome to your new app. CurrentCulture: @CultureInfo.CurrentCulture + + + diff --git a/BlazorApp1/Pages/Index.razor.cs b/BlazorApp1/Pages/Index.razor.cs new file mode 100644 index 0000000..2bd06dd --- /dev/null +++ b/BlazorApp1/Pages/Index.razor.cs @@ -0,0 +1,31 @@ +using BlazorApp1.Components; +using BlazorApp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorApp1.Pages +{ + public partial class Index + { + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + + StateHasChanged(); + } + } +} diff --git a/BlazorApp1/Pages/_Layout.cshtml b/BlazorApp1/Pages/_Layout.cshtml index fa86d76..d8b9b1c 100644 --- a/BlazorApp1/Pages/_Layout.cshtml +++ b/BlazorApp1/Pages/_Layout.cshtml @@ -30,6 +30,7 @@ + diff --git a/BlazorApp1/Services/DataLocalService.cs b/BlazorApp1/Services/DataLocalService.cs index 01833b0..65d85d4 100644 --- a/BlazorApp1/Services/DataLocalService.cs +++ b/BlazorApp1/Services/DataLocalService.cs @@ -1,4 +1,5 @@ -using BlazorApp1.Factories; +using BlazorApp1.Components; +using BlazorApp1.Factories; using BlazorApp1.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; @@ -54,7 +55,11 @@ public class DataLocalService : IDataService public async Task Count() { - return (await _localStorage.GetItemAsync("data")).Length; + var list = await _localStorage.GetItemAsync("data"); + if (list == null) { return 0; } + else { + return (await _localStorage.GetItemAsync("data")).Length; + } } public async Task> List(int currentPage, int pageSize) @@ -161,4 +166,22 @@ public async Task GetById(int id) // Save the data await _localStorage.SetItemAsync("data", currentData); } + public Task> GetRecipes() + { + var items = new List + { + new CraftingRecipe + { + Give = new Item { DisplayName = "Diamond", Name = "diamond" }, + Have = new List> + { + new List { "dirt", "dirt", "dirt" }, + new List { "dirt", null, "dirt" }, + new List { "dirt", "dirt", "dirt" } + } + } + }; + + return Task.FromResult(items); + } } \ No newline at end of file diff --git a/BlazorApp1/Services/IDataServicee.cs b/BlazorApp1/Services/IDataServicee.cs index 4b94277..bf55d0a 100644 --- a/BlazorApp1/Services/IDataServicee.cs +++ b/BlazorApp1/Services/IDataServicee.cs @@ -1,4 +1,5 @@ -using BlazorApp1.Models; +using BlazorApp1.Components; +using BlazorApp1.Models; public interface IDataService { @@ -11,4 +12,7 @@ public interface IDataService Task Update(int id, ItemModel model); Task Delete(int id); + + Task> GetRecipes(); + } \ No newline at end of file
CurrentCulture: @CultureInfo.CurrentCulture