parent
6d0a62b0da
commit
bffb1baee2
@ -0,0 +1,51 @@
|
||||
<CascadingValue Value="@this">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-6">
|
||||
|
||||
<div>Available items:</div>
|
||||
<div>
|
||||
<div class="css-grid">
|
||||
|
||||
@foreach (var item in Items)
|
||||
{
|
||||
<CraftingItem Item="item" NoDrop="true"/>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-6">
|
||||
<div>Recipe</div>
|
||||
|
||||
<div>
|
||||
|
||||
<div class="css-recipe">
|
||||
<CraftingItem Index="0"/>
|
||||
<CraftingItem Index="1"/>
|
||||
<CraftingItem Index="2"/>
|
||||
<CraftingItem Index="3"/>
|
||||
<CraftingItem Index="4"/>
|
||||
<CraftingItem Index="5"/>
|
||||
<CraftingItem Index="6"/>
|
||||
<CraftingItem Index="7"/>
|
||||
<CraftingItem Index="8"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>Result</div>
|
||||
<div>
|
||||
<CraftingItem Item="RecipeResult"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12">
|
||||
<div>Actions</div>
|
||||
<div class="actions" id="actions">
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CascadingValue>
|
@ -0,0 +1,77 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace TP_Blazor.Components;
|
||||
|
||||
public partial class Crafting
|
||||
{
|
||||
private Item _recipeResult;
|
||||
|
||||
public Crafting()
|
||||
{
|
||||
Actions = new ObservableCollection<CraftingAction>();
|
||||
Actions.CollectionChanged += OnActionsCollectionChanged;
|
||||
this.RecipeItems = new List<Item> { null, null, null, null, null, null, null, null, null };
|
||||
}
|
||||
|
||||
public ObservableCollection<CraftingAction> Actions { get; set; }
|
||||
public Item CurrentDragItem { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public List<Item> Items { get; set; }
|
||||
|
||||
public List<Item> RecipeItems { get; set; }
|
||||
|
||||
public Item RecipeResult
|
||||
{
|
||||
get => this._recipeResult;
|
||||
set
|
||||
{
|
||||
if (this._recipeResult == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this._recipeResult = value;
|
||||
this.StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
[Parameter]
|
||||
public List<CraftingRecipe> Recipes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the java script runtime.
|
||||
/// </summary>
|
||||
[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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace TP_Blazor.Components;
|
||||
|
||||
public class CraftingAction
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public int Index { get; set; }
|
||||
public Item Item { get; set; }
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<div
|
||||
class="item"
|
||||
ondragover="event.preventDefault();"
|
||||
draggable="true"
|
||||
@ondragstart="@OnDragStart"
|
||||
@ondrop="@OnDrop"
|
||||
@ondragenter="@OnDragEnter"
|
||||
@ondragleave="@OnDragLeave">
|
||||
|
||||
@if (Item != null)
|
||||
{
|
||||
@Item.DisplayName
|
||||
}
|
||||
</div>
|
@ -0,0 +1,59 @@
|
||||
namespace TP_Blazor.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 });
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
.item {
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
border: 1px solid;
|
||||
overflow: hidden;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace TP_Blazor.Components;
|
||||
|
||||
public class CraftingRecipe
|
||||
{
|
||||
public Item Give { get; set; }
|
||||
public List<List<string>> Have { get; set; }
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
@using TP_Blazor.Models
|
||||
@typeparam TItem
|
||||
|
||||
<div>
|
||||
@if ((Items?.Count ?? 0) != 0)
|
||||
{
|
||||
@foreach (var item in Items)
|
||||
{
|
||||
@ShowTemplate(item)
|
||||
}
|
||||
}
|
||||
</div>
|
@ -0,0 +1,9 @@
|
||||
namespace TP_Blazor.Components;
|
||||
|
||||
public partial class ShowItems<TItem>
|
||||
{
|
||||
[Parameter]
|
||||
public List<TItem> Items { get; set; }
|
||||
[Parameter]
|
||||
public RenderFragment<TItem> ShowTemplate { get; set; }
|
||||
}
|
@ -1,13 +1,84 @@
|
||||
using TP_Blazor.Components;
|
||||
using TP_Blazor.Models;
|
||||
|
||||
namespace TP_Blazor.Pages;
|
||||
|
||||
public partial class Index
|
||||
{
|
||||
private Cake CakeItem = new Cake
|
||||
/* private Cake CakeItem = new Cake
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Black Forest",
|
||||
Cost = 50
|
||||
};
|
||||
|
||||
public List<Cake> Cakes { get; set; }
|
||||
|
||||
protected override Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
LoadCakes();
|
||||
StateHasChanged();
|
||||
return base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
|
||||
public void LoadCakes()
|
||||
{
|
||||
Cakes = new List<Cake>
|
||||
{
|
||||
new Cake
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Red Velvet",
|
||||
Cost = 60
|
||||
},
|
||||
new Cake
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Chocolate",
|
||||
Cost = 70
|
||||
},
|
||||
new Cake
|
||||
{
|
||||
Id = 3,
|
||||
Name = "Vanilla",
|
||||
Cost = 80
|
||||
},
|
||||
new Cake
|
||||
{
|
||||
Id = 4,
|
||||
Name = "Strawberry",
|
||||
Cost = 90
|
||||
},
|
||||
new Cake
|
||||
{
|
||||
Id = 5,
|
||||
Name = "Blueberry",
|
||||
Cost = 100
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
[Inject]
|
||||
public IDataService DataService { get; set; }
|
||||
|
||||
public List<Item> Items { get; set; } = new List<Item>();
|
||||
|
||||
private List<CraftingRecipe> Recipes { get; set; } = new List<CraftingRecipe>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
@page "/pets1"
|
||||
|
||||
<h1>Pets</h1>
|
||||
|
||||
<TableTemplate Items="pets" Context="pet">
|
||||
<TableHeader>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
</TableHeader>
|
||||
<RowTemplate>
|
||||
<td>@pet.PetId</td>
|
||||
<td>@pet.Name</td>
|
||||
</RowTemplate>
|
||||
</TableTemplate>
|
@ -0,0 +1,17 @@
|
||||
namespace TP_Blazor.Pages;
|
||||
|
||||
public partial class Pets1
|
||||
{
|
||||
private List<Pet> pets = new()
|
||||
{
|
||||
new Pet { PetId = 2, Name = "Mr. Bigglesworth" },
|
||||
new Pet { PetId = 4, Name = "Salem Saberhagen" },
|
||||
new Pet { PetId = 7, Name = "K-9" }
|
||||
};
|
||||
|
||||
private class Pet
|
||||
{
|
||||
public int PetId { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<root>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>1.3</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="btnTitle" xml:space="preserve">
|
||||
<value>Valider</value>
|
||||
</data>
|
||||
<data name="Title" xml:space="preserve">
|
||||
<value>Titre</value>
|
||||
</data>
|
||||
</root>
|
@ -0,0 +1,57 @@
|
||||
using TP_Blazor.Components;
|
||||
using TP_Blazor.Factories;
|
||||
|
||||
namespace TP_Blazor.Services;
|
||||
|
||||
public class DataApiService : IDataService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public DataApiService(
|
||||
HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task Add(ItemModel model)
|
||||
{
|
||||
// Get the item
|
||||
var item = ItemFactory.Create(model);
|
||||
|
||||
// Save the data
|
||||
await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item);
|
||||
}
|
||||
|
||||
public async Task<int> Count()
|
||||
{
|
||||
return await _http.GetFromJsonAsync<int>("https://localhost:7234/api/Crafting/count");
|
||||
}
|
||||
|
||||
public async Task<List<Item>> List(int currentPage, int pageSize)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
|
||||
}
|
||||
|
||||
public async Task<Item> GetById(int id)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<Item>($"https://localhost:7234/api/Crafting/{id}");
|
||||
}
|
||||
|
||||
public async Task Update(int id, ItemModel model)
|
||||
{
|
||||
// Get the item
|
||||
var item = ItemFactory.Create(model);
|
||||
|
||||
await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item);
|
||||
}
|
||||
|
||||
public async Task Delete(int id)
|
||||
{
|
||||
await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}");
|
||||
}
|
||||
|
||||
public async Task<List<CraftingRecipe>> GetRecipes()
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
@typeparam TItem
|
||||
@using System.Diagnostics.CodeAnalysis
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>@TableHeader</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Items)
|
||||
{
|
||||
if (RowTemplate is not null)
|
||||
{
|
||||
<tr>@RowTemplate(item)</tr>
|
||||
}
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
@ -0,0 +1,16 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
|
||||
namespace TP_Blazor.Shared;
|
||||
|
||||
public partial class TableTemplate<TItem>
|
||||
{
|
||||
[Parameter]
|
||||
public RenderFragment? TableHeader { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public RenderFragment<TItem>? RowTemplate { get; set; }
|
||||
|
||||
[Parameter, AllowNull]
|
||||
public IReadOnlyList<TItem> Items { get; set; }
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
global using TP_Blazor.Models;
|
||||
global using TP_Blazor.Services;
|
||||
global using TP_Blazor.Data;
|
||||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Linq;
|
||||
global using System.Threading.Tasks;
|
||||
global using Microsoft.AspNetCore.Components;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
Loading…
Reference in new issue