diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor new file mode 100644 index 0000000..774d351 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/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/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.cs new file mode 100644 index 0000000..d09e19b --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.cs @@ -0,0 +1,80 @@ +using CetteAppliVaMarcher.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace CetteAppliVaMarcher.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/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.css b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.css new file mode 100644 index 0000000..1c0cb8b --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/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/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.js b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/Crafting.razor.js new file mode 100644 index 0000000..3fcb76d --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/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/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingAction.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingAction.cs new file mode 100644 index 0000000..1c1eda4 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using CetteAppliVaMarcher.Models; + +namespace CetteAppliVaMarcher.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor new file mode 100644 index 0000000..db4ca36 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor @@ -0,0 +1,14 @@ +
+ + @if (Item != null) + { + @Item.DisplayName + } +
\ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..b0cf521 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.cs @@ -0,0 +1,63 @@ +using CetteAppliVaMarcher.Models; +using Microsoft.AspNetCore.Components; + +namespace CetteAppliVaMarcher.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/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.css b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.css new file mode 100644 index 0000000..d6f5ec3 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingRecipe.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingRecipe.cs new file mode 100644 index 0000000..5633ee7 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using CetteAppliVaMarcher.Models; + +namespace CetteAppliVaMarcher.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor index 7818c65..0c75033 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor @@ -9,6 +9,10 @@ Welcome to your new app. +
+ +
+

CurrentCulture: @CultureInfo.CurrentCulture

\ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor.cs new file mode 100644 index 0000000..57e814a --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Index.razor.cs @@ -0,0 +1,22 @@ +using CetteAppliVaMarcher.Components; +using CetteAppliVaMarcher.Models; +using CetteAppliVaMarcher.Services; +using Microsoft.AspNetCore.Components; + +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 OnInitializedAsync() + { + await base.OnInitializedAsync(); + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + } +} \ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/_Layout.cshtml b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/_Layout.cshtml index 6397313..ffae3cb 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/_Layout.cshtml +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/_Layout.cshtml @@ -35,6 +35,7 @@ + diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs index d2256cb..b306af8 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using CetteAppliVaMarcher.Components; using CetteAppliVaMarcher.Factories; using CetteAppliVaMarcher.Models; using Microsoft.AspNetCore.Components; @@ -159,5 +160,23 @@ namespace CetteAppliVaMarcher.Services // 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); + } } } diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs index e9f221a..94aae1f 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs @@ -1,4 +1,5 @@ -using CetteAppliVaMarcher.Models; +using CetteAppliVaMarcher.Components; +using CetteAppliVaMarcher.Models; namespace CetteAppliVaMarcher.Services { @@ -10,5 +11,6 @@ namespace CetteAppliVaMarcher.Services Task GetById(int id); Task Update(int id, ItemModel model); Task Delete(int id); + Task>GetRecipes(); } } \ No newline at end of file