From 4be42386c97bb26e85ffeda01bbdc1c7baf10da6 Mon Sep 17 00:00:00 2001
From: Thomas TISSIER
Date: Fri, 9 Dec 2022 09:51:00 +0100
Subject: [PATCH] =?UTF-8?q?Tuto=20termin=C3=A9.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
TutoBlazer/Components/Crafting.razor | 52 +++++++++++++
TutoBlazer/Components/Crafting.razor.cs | 80 ++++++++++++++++++++
TutoBlazer/Components/Crafting.razor.css | 19 +++++
TutoBlazer/Components/Crafting.razor.js | 16 ++++
TutoBlazer/Components/CraftingAction.cs | 11 +++
TutoBlazer/Components/CraftingItem.razor | 13 ++++
TutoBlazer/Components/CraftingItem.razor.cs | 64 ++++++++++++++++
TutoBlazer/Components/CraftingItem.razor.css | 6 ++
TutoBlazer/Components/CraftingRecipe.cs | 10 +++
TutoBlazer/Pages/Index.razor | 22 +-----
TutoBlazer/Pages/Index.razor.cs | 29 +++++--
TutoBlazer/Pages/_Layout.cshtml | 1 +
TutoBlazer/Services/DataApiService.cs | 4 +-
TutoBlazer/Services/DataLocalService.cs | 20 +++++
TutoBlazer/Services/IDataService.cs | 5 +-
15 files changed, 324 insertions(+), 28 deletions(-)
create mode 100644 TutoBlazer/Components/Crafting.razor
create mode 100644 TutoBlazer/Components/Crafting.razor.cs
create mode 100644 TutoBlazer/Components/Crafting.razor.css
create mode 100644 TutoBlazer/Components/Crafting.razor.js
create mode 100644 TutoBlazer/Components/CraftingAction.cs
create mode 100644 TutoBlazer/Components/CraftingItem.razor
create mode 100644 TutoBlazer/Components/CraftingItem.razor.cs
create mode 100644 TutoBlazer/Components/CraftingItem.razor.css
create mode 100644 TutoBlazer/Components/CraftingRecipe.cs
diff --git a/TutoBlazer/Components/Crafting.razor b/TutoBlazer/Components/Crafting.razor
new file mode 100644
index 0000000..a09261f
--- /dev/null
+++ b/TutoBlazer/Components/Crafting.razor
@@ -0,0 +1,52 @@
+
+
+
+
+
+
+
+
Available items:
+
+
+
+ @foreach (var item in Items)
+ {
+
+ }
+
+
+
+
+
+
+
Recipe
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Result
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/TutoBlazer/Components/Crafting.razor.cs b/TutoBlazer/Components/Crafting.razor.cs
new file mode 100644
index 0000000..3a033b7
--- /dev/null
+++ b/TutoBlazer/Components/Crafting.razor.cs
@@ -0,0 +1,80 @@
+using Microsoft.AspNetCore.Components;
+using Microsoft.JSInterop;
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
+using TutoBlazer.Models;
+
+namespace TutoBlazer.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/TutoBlazer/Components/Crafting.razor.css b/TutoBlazer/Components/Crafting.razor.css
new file mode 100644
index 0000000..2a388f2
--- /dev/null
+++ b/TutoBlazer/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/TutoBlazer/Components/Crafting.razor.js b/TutoBlazer/Components/Crafting.razor.js
new file mode 100644
index 0000000..8fdb58e
--- /dev/null
+++ b/TutoBlazer/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/TutoBlazer/Components/CraftingAction.cs b/TutoBlazer/Components/CraftingAction.cs
new file mode 100644
index 0000000..f39e620
--- /dev/null
+++ b/TutoBlazer/Components/CraftingAction.cs
@@ -0,0 +1,11 @@
+using TutoBlazer.Models;
+
+namespace TutoBlazer.Components
+{
+ public class CraftingAction
+ {
+ public string Action { get; set; }
+ public int Index { get; set; }
+ public Item Item { get; set; }
+ }
+}
diff --git a/TutoBlazer/Components/CraftingItem.razor b/TutoBlazer/Components/CraftingItem.razor
new file mode 100644
index 0000000..ddf25bf
--- /dev/null
+++ b/TutoBlazer/Components/CraftingItem.razor
@@ -0,0 +1,13 @@
+
+
+ @if (Item != null)
+ {
+ @Item.DisplayName
+ }
+
diff --git a/TutoBlazer/Components/CraftingItem.razor.cs b/TutoBlazer/Components/CraftingItem.razor.cs
new file mode 100644
index 0000000..d70ed07
--- /dev/null
+++ b/TutoBlazer/Components/CraftingItem.razor.cs
@@ -0,0 +1,64 @@
+using Blazorise;
+using Microsoft.AspNetCore.Components;
+using TutoBlazer.Models;
+
+namespace TutoBlazer.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/TutoBlazer/Components/CraftingItem.razor.css b/TutoBlazer/Components/CraftingItem.razor.css
new file mode 100644
index 0000000..b2d4521
--- /dev/null
+++ b/TutoBlazer/Components/CraftingItem.razor.css
@@ -0,0 +1,6 @@
+.item {
+ width: 64px;
+ height: 64px;
+ border: 1px solid;
+ overflow: hidden;
+}
diff --git a/TutoBlazer/Components/CraftingRecipe.cs b/TutoBlazer/Components/CraftingRecipe.cs
new file mode 100644
index 0000000..2fe2da7
--- /dev/null
+++ b/TutoBlazer/Components/CraftingRecipe.cs
@@ -0,0 +1,10 @@
+using TutoBlazer.Models;
+
+namespace TutoBlazer.Components
+{
+ public class CraftingRecipe
+ {
+ public Item Give { get; set; }
+ public List> Have { get; set; }
+ }
+}
diff --git a/TutoBlazer/Pages/Index.razor b/TutoBlazer/Pages/Index.razor
index be00ca7..286963e 100644
--- a/TutoBlazer/Pages/Index.razor
+++ b/TutoBlazer/Pages/Index.razor
@@ -12,23 +12,7 @@ Welcome to your new app.
CurrentCulture: @CultureInfo.CurrentCulture
-
-
-
-
-
-
-
@context.Name
-
$ @context.Cost
-
-
-
-
-
-
-
+
+
+
diff --git a/TutoBlazer/Pages/Index.razor.cs b/TutoBlazer/Pages/Index.razor.cs
index ab999f4..f048fa4 100644
--- a/TutoBlazer/Pages/Index.razor.cs
+++ b/TutoBlazer/Pages/Index.razor.cs
@@ -1,14 +1,31 @@
-using TutoBlazer.Models;
+using Microsoft.AspNetCore.Components;
+using TutoBlazer.Components;
+using TutoBlazer.Models;
namespace TutoBlazer.Pages
{
public partial class Index
{
- private Cake CakeItem = new Cake
+ [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)
{
- Id = 1,
- Name = "Black Forest",
- Cost = 50
- };
+ base.OnAfterRenderAsync(firstRender);
+
+ if (!firstRender)
+ {
+ return;
+ }
+
+ Items = await DataService.List(0, await DataService.Count());
+ Recipes = await DataService.GetRecipes();
+
+ StateHasChanged();
+ }
}
}
diff --git a/TutoBlazer/Pages/_Layout.cshtml b/TutoBlazer/Pages/_Layout.cshtml
index 33c480f..e2659a2 100644
--- a/TutoBlazer/Pages/_Layout.cshtml
+++ b/TutoBlazer/Pages/_Layout.cshtml
@@ -35,5 +35,6 @@
+