diff --git a/blazor_lab/Components/Crafting.razor b/blazor_lab/Components/Crafting.razor new file mode 100644 index 0000000..65c9b93 --- /dev/null +++ b/blazor_lab/Components/Crafting.razor @@ -0,0 +1,50 @@ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+
+
+
+
+
\ No newline at end of file diff --git a/blazor_lab/Components/Crafting.razor.cs b/blazor_lab/Components/Crafting.razor.cs new file mode 100644 index 0000000..43a5879 --- /dev/null +++ b/blazor_lab/Components/Crafting.razor.cs @@ -0,0 +1,80 @@ +using blazor_lab.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace blazor_lab.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/blazor_lab/Components/Crafting.razor.css b/blazor_lab/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/blazor_lab/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/blazor_lab/Components/Crafting.razor.js b/blazor_lab/Components/Crafting.razor.js new file mode 100644 index 0000000..d55462f --- /dev/null +++ b/blazor_lab/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + const 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/blazor_lab/Components/CraftingAction.cs b/blazor_lab/Components/CraftingAction.cs new file mode 100644 index 0000000..80fc59c --- /dev/null +++ b/blazor_lab/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using blazor_lab.Models; + +namespace blazor_lab.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/blazor_lab/Components/CraftingItem.razor b/blazor_lab/Components/CraftingItem.razor new file mode 100644 index 0000000..ddf25bf --- /dev/null +++ b/blazor_lab/Components/CraftingItem.razor @@ -0,0 +1,13 @@ +
+ + @if (Item != null) + { + @Item.DisplayName + } +
diff --git a/blazor_lab/Components/CraftingItem.razor.cs b/blazor_lab/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..3a255d2 --- /dev/null +++ b/blazor_lab/Components/CraftingItem.razor.cs @@ -0,0 +1,63 @@ +using blazor_lab.Models; +using Microsoft.AspNetCore.Components; + +namespace blazor_lab.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/blazor_lab/Components/CraftingItem.razor.css b/blazor_lab/Components/CraftingItem.razor.css new file mode 100644 index 0000000..b2d4521 --- /dev/null +++ b/blazor_lab/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/blazor_lab/Components/CraftingRecipe.cs b/blazor_lab/Components/CraftingRecipe.cs new file mode 100644 index 0000000..1f32d8b --- /dev/null +++ b/blazor_lab/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using blazor_lab.Models; + +namespace blazor_lab.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/blazor_lab/Data/WeatherForecast.cs b/blazor_lab/Data/WeatherForecast.cs deleted file mode 100644 index e0a2391..0000000 --- a/blazor_lab/Data/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace blazor_lab.Data -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/blazor_lab/Data/WeatherForecastService.cs b/blazor_lab/Data/WeatherForecastService.cs deleted file mode 100644 index 6eab65e..0000000 --- a/blazor_lab/Data/WeatherForecastService.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace blazor_lab.Data -{ - public class WeatherForecastService - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - public Task GetForecastAsync(DateTime startDate) - { - return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }).ToArray()); - } - } -} \ No newline at end of file diff --git a/blazor_lab/Modals/DeleteConfirmation.razor.cs b/blazor_lab/Modals/DeleteConfirmation.razor.cs index 26d6c66..42d57a4 100644 --- a/blazor_lab/Modals/DeleteConfirmation.razor.cs +++ b/blazor_lab/Modals/DeleteConfirmation.razor.cs @@ -1,7 +1,7 @@ using blazor_lab.Models; using blazor_lab.Services; -using Blazored.Modal.Services; using Blazored.Modal; +using Blazored.Modal.Services; using Microsoft.AspNetCore.Components; namespace blazor_lab.Modals diff --git a/blazor_lab/Models/Cake.cs b/blazor_lab/Models/Cake.cs new file mode 100644 index 0000000..b8c9271 --- /dev/null +++ b/blazor_lab/Models/Cake.cs @@ -0,0 +1,9 @@ +namespace blazor_lab.Models +{ + public class Cake + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Cost { get; set; } + } +} diff --git a/blazor_lab/Pages/Add.razor.cs b/blazor_lab/Pages/Add.razor.cs index 6badefb..c1c478f 100644 --- a/blazor_lab/Pages/Add.razor.cs +++ b/blazor_lab/Pages/Add.razor.cs @@ -12,7 +12,7 @@ namespace blazor_lab.Pages public IDataService DataService { get; set; } [Inject] - public NavigationManager NavigationManager { get; set; } + public NavigationManager NavigationManager { get; set; } /// /// The default enchant categories. diff --git a/blazor_lab/Pages/Admin/Index.razor b/blazor_lab/Pages/Admin/Index.razor deleted file mode 100644 index bf98b1c..0000000 --- a/blazor_lab/Pages/Admin/Index.razor +++ /dev/null @@ -1,3 +0,0 @@ -@page "/admin" - -

Index

diff --git a/blazor_lab/Pages/Admin/Users.razor b/blazor_lab/Pages/Admin/Users.razor deleted file mode 100644 index 560b324..0000000 --- a/blazor_lab/Pages/Admin/Users.razor +++ /dev/null @@ -1,2 +0,0 @@ -@page "/admin/users" -

Users

diff --git a/blazor_lab/Pages/Admin/_Imports.razor b/blazor_lab/Pages/Admin/_Imports.razor deleted file mode 100644 index f6c6b4b..0000000 --- a/blazor_lab/Pages/Admin/_Imports.razor +++ /dev/null @@ -1 +0,0 @@ -@layout AdminLayout \ No newline at end of file diff --git a/blazor_lab/Pages/Counter.razor b/blazor_lab/Pages/Counter.razor deleted file mode 100644 index b21f052..0000000 --- a/blazor_lab/Pages/Counter.razor +++ /dev/null @@ -1,18 +0,0 @@ -@page "/counter" - -Counter - -

Counter

- -

Current count: @currentCount

- - - -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/blazor_lab/Pages/FetchData.razor b/blazor_lab/Pages/FetchData.razor deleted file mode 100644 index 6f4b79b..0000000 --- a/blazor_lab/Pages/FetchData.razor +++ /dev/null @@ -1,48 +0,0 @@ -@page "/fetchdata" - -Weather forecast - -@using blazor_lab.Data -@inject WeatherForecastService ForecastService - -

Weather forecast

- -

This component demonstrates fetching data from a service.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await ForecastService.GetForecastAsync(DateTime.Now); - } -} diff --git a/blazor_lab/Pages/Index.razor b/blazor_lab/Pages/Index.razor index 0c5b2dd..9941332 100644 --- a/blazor_lab/Pages/Index.razor +++ b/blazor_lab/Pages/Index.razor @@ -1,13 +1,16 @@ @page "/" @using System.Globalization; +@using Microsoft.AspNetCore.Components; +@using blazor_lab.Models +@using blazor_lab.Components Index -

Hello, world!

+

Crafty McCraftFace

-Welcome to your new app. - - +
+ +

CurrentCulture: @CultureInfo.CurrentCulture diff --git a/blazor_lab/Pages/Index.razor.cs b/blazor_lab/Pages/Index.razor.cs new file mode 100644 index 0000000..e98210a --- /dev/null +++ b/blazor_lab/Pages/Index.razor.cs @@ -0,0 +1,32 @@ +using blazor_lab.Components; +using blazor_lab.Models; +using blazor_lab.Services; +using Microsoft.AspNetCore.Components; + +namespace blazor_lab.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/blazor_lab/Pages/List.razor b/blazor_lab/Pages/List.razor index 90d945b..c714550 100644 --- a/blazor_lab/Pages/List.razor +++ b/blazor_lab/Pages/List.razor @@ -1,7 +1,7 @@ @page "/list" @using Models -

List

+

@Localizer["Title"]

diff --git a/blazor_lab/Pages/List.razor.cs b/blazor_lab/Pages/List.razor.cs index ab6c643..a8e3c87 100644 --- a/blazor_lab/Pages/List.razor.cs +++ b/blazor_lab/Pages/List.razor.cs @@ -5,6 +5,7 @@ using Blazored.Modal; using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; namespace blazor_lab.Pages { @@ -14,6 +15,9 @@ namespace blazor_lab.Pages private int totalItems; + [Inject] + public IStringLocalizer Localizer { get; set; } + [Inject] public IDataService DataService { get; set; } diff --git a/blazor_lab/Pages/_Layout.cshtml b/blazor_lab/Pages/_Layout.cshtml index 44f8e88..26c864a 100644 --- a/blazor_lab/Pages/_Layout.cshtml +++ b/blazor_lab/Pages/_Layout.cshtml @@ -29,8 +29,10 @@ + - + + diff --git a/blazor_lab/Program.cs b/blazor_lab/Program.cs index 77f6d67..259e4b8 100644 --- a/blazor_lab/Program.cs +++ b/blazor_lab/Program.cs @@ -1,20 +1,18 @@ -using blazor_lab.Data; +using blazor_lab.Services; +using Blazored.LocalStorage; +using Blazored.Modal; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; -using Blazored.LocalStorage; -using blazor_lab.Services; -using Blazored.Modal; using Microsoft.AspNetCore.Localization; -using System.Globalization; using Microsoft.Extensions.Options; +using System.Globalization; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); -builder.Services.AddSingleton(); builder.Services.AddHttpClient(); diff --git a/blazor_lab/Resources/Pages.List.fr-FR.resx b/blazor_lab/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..50bb302 --- /dev/null +++ b/blazor_lab/Resources/Pages.List.fr-FR.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Liste des éléments + + \ No newline at end of file diff --git a/blazor_lab/Resources/Pages.List.resx b/blazor_lab/Resources/Pages.List.resx new file mode 100644 index 0000000..a8fea6f --- /dev/null +++ b/blazor_lab/Resources/Pages.List.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Items list + + \ No newline at end of file diff --git a/blazor_lab/Services/DataLocalService.cs b/blazor_lab/Services/DataLocalService.cs index 7c097f7..77556ef 100644 --- a/blazor_lab/Services/DataLocalService.cs +++ b/blazor_lab/Services/DataLocalService.cs @@ -1,8 +1,8 @@ -using blazor_lab.Factories; +using blazor_lab.Components; +using blazor_lab.Factories; using blazor_lab.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Hosting; namespace blazor_lab.Services { @@ -83,7 +83,7 @@ namespace blazor_lab.Services var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); var fileInfo = new FileInfo($"{imagePathInfo}/{item.Name}.png"); - if(fileInfo.Exists) + if (fileInfo.Exists) { File.Delete(fileInfo.FullName); } @@ -103,6 +103,25 @@ namespace blazor_lab.Services return item; } + 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); + } + public async Task> List(int currentPage, int pageSize) { if (await _localStorageService.GetItemAsync("data") == null) @@ -138,7 +157,7 @@ namespace blazor_lab.Services ItemFactory.Update(item, model); - await _localStorageService.SetItemAsync("data", currentData); + await _localStorageService.SetItemAsync("data", currentData); } } } diff --git a/blazor_lab/Services/IDataService.cs b/blazor_lab/Services/IDataService.cs index 9f4572f..4691503 100644 --- a/blazor_lab/Services/IDataService.cs +++ b/blazor_lab/Services/IDataService.cs @@ -1,4 +1,5 @@ -using blazor_lab.Models; +using blazor_lab.Components; +using blazor_lab.Models; namespace blazor_lab.Services { @@ -10,5 +11,6 @@ namespace blazor_lab.Services Task GetById(int id); Task Update(int id, ItemModel model); Task Delete(int id); + Task> GetRecipes(); } } diff --git a/blazor_lab/Shared/CultureSelector.razor b/blazor_lab/Shared/CultureSelector.razor index 6d7f012..0bb1037 100644 --- a/blazor_lab/Shared/CultureSelector.razor +++ b/blazor_lab/Shared/CultureSelector.razor @@ -11,33 +11,4 @@ } -

- -@code -{ - private CultureInfo[] supportedCultures = new[] - { - new CultureInfo("en-US"), - new CultureInfo("fr-FR") - }; - - private CultureInfo Culture - { - get => CultureInfo.CurrentCulture; - set - { - if (CultureInfo.CurrentUICulture == value) - { - return; - } - - var culture = value.Name.ToLower(CultureInfo.InvariantCulture); - - var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); - var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; - - // Redirect the user to the culture controller to set the cookie - this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); - } - } -} \ No newline at end of file +

\ No newline at end of file diff --git a/blazor_lab/Shared/CultureSelector.razor.cs b/blazor_lab/Shared/CultureSelector.razor.cs new file mode 100644 index 0000000..e73bdc1 --- /dev/null +++ b/blazor_lab/Shared/CultureSelector.razor.cs @@ -0,0 +1,33 @@ +using System.Globalization; + +namespace blazor_lab.Shared +{ + public partial class CultureSelector + { + private CultureInfo[] supportedCultures = new[] + { + new CultureInfo("en-US"), + new CultureInfo("fr-FR") + }; + + private CultureInfo Culture + { + get => CultureInfo.CurrentCulture; + set + { + if (CultureInfo.CurrentUICulture == value) + { + return; + } + + var culture = value.Name.ToLower(CultureInfo.InvariantCulture); + + var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); + var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; + + // Redirect the user to the culture controller to set the cookie + this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); + } + } + } +} diff --git a/blazor_lab/Shared/NavMenu.razor b/blazor_lab/Shared/NavMenu.razor index e9baf2f..8c9d622 100644 --- a/blazor_lab/Shared/NavMenu.razor +++ b/blazor_lab/Shared/NavMenu.razor @@ -20,21 +20,6 @@ Home
- - - diff --git a/blazor_lab/Shared/TableTemplate.razor b/blazor_lab/Shared/TableTemplate.razor new file mode 100644 index 0000000..72c3ae5 --- /dev/null +++ b/blazor_lab/Shared/TableTemplate.razor @@ -0,0 +1,28 @@ +@typeparam TItem +@using System.Diagnostics.CodeAnalysis + + + + @TableHeader + + + @foreach (var item in Items) + { + if (RowTemplate is not null) + { + @RowTemplate(item) + } + } + +
+ +@code { + [Parameter] + public RenderFragment? TableHeader { get; set; } + + [Parameter] + public RenderFragment? RowTemplate { get; set; } + + [Parameter, AllowNull] + public IReadOnlyList Items { get; set; } +} \ No newline at end of file diff --git a/blazor_lab/wwwroot/images/with img this time.png b/blazor_lab/wwwroot/images/with img this time.png deleted file mode 100644 index 989ea6a..0000000 Binary files a/blazor_lab/wwwroot/images/with img this time.png and /dev/null differ diff --git a/blazor_lab/wwwroot/images/zdada.png b/blazor_lab/wwwroot/images/zdada.png deleted file mode 100644 index 989ea6a..0000000 Binary files a/blazor_lab/wwwroot/images/zdada.png and /dev/null differ