Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsol6 committed Jan 8, 2025
1 parent 8b10e39 commit ba8128a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 67 deletions.
45 changes: 21 additions & 24 deletions Server/Components/Pages/GamePage.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@using Fracture.Server.Modules.MapGenerator.Models
@using Fracture.Server.Modules.MapGenerator.Services
@using Fracture.Server.Modules.MapGenerator.UI.Models
@using Fracture.Server.Modules.NoiseGenerator.Models
@using Fracture.Server.Modules.Pathfinding.Models
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@inject ProtectedSessionStorage ProtectedSessionStore
Expand All @@ -15,23 +14,19 @@
@using Fracture.Server.Components.UI
@using Fracture.Server.Modules.Items.Models
@using System.Collections.ObjectModel
@using System.Reflection.Metadata
@using Fracture.Server.Modules.Database
@using Fracture.Server.Modules.Users
@using Fracture.Server.Components.Pages
@using Fracture.Server.Modules.Shared
@using Fracture.Server.Modules.MapGenerator.UI
@using Modules.Shared.ImageChanger
@using Microsoft.AspNetCore.Session
@using Microsoft.OpenApi.Extensions
@using SQLitePCL
@using Fracture.Server.Modules.Shared.ImageChangers

@layout GameLayout
@inject IUsersRepository UsersRepository
@inject NavigationManager NavigationManager
@inject IItemsRepository ItemsRepository
@inject VersionInfoProvider VersionInfoProvider
@inject IMapGeneratorService MapGeneratorService
@inject BacgroundImageChanger BIC
@inject BackgroundImageChanger BackgroundImageChanger
<link rel="icon" type="image/png" href="assets/icons/favicon.png"/>
@inject MapParametersService MapParametersService

Expand Down Expand Up @@ -69,7 +64,7 @@
</div>
<div class="statManager ">
</div>
<div class="dialogueWindowBg" style="background: url(@bacgroundImage.bgImg);">
<div class="dialogueWindowBg" style="background: url(@BackgroundImage.bgImg);">
<div class="dialogueWindow" @onkeydown="GetBacgroundAsync">
Fracture @VersionInfoProvider.InformationalVersion<br />
tutaj log mojej gry<br />
Expand Down Expand Up @@ -116,7 +111,7 @@
<div @onclick="() => _popup.ShowComponent<MapPopup>(_mapPopupParameters)"><img src="assets/icons/infobutton.svg" /></div>
</div>
</div>
<MapView Map="_map" MapDisplayData="_mapDisplayData" Path="_path"></MapView>
<MapView Map="Map" MapDisplayData="_mapDisplayData" Path="Path"></MapView>
</div>
<div class="settings">
<div class="settingsBtn">
Expand Down Expand Up @@ -146,23 +141,23 @@

private Dictionary<string, object> _equipmentPopupParameters = null!;
private Dictionary<string, object> _mapPopupParameters = null!;
public static MapData? _map { get; set; }
public static MapData? Map { get; set; }


private PopupContainer _popup = null!;
private string _loginInfo = string.Empty;



public static BacgroundImage bacgroundImage { get; set; } = new BacgroundImage(String.Empty);
public static BackgroundImage BackgroundImage { get; set; } = new BackgroundImage(String.Empty);

private MapDisplayData _mapDisplayData = new();

private List<IPathfindingNode>? _path { get; set; }
private NoiseParameters _noiseParameters = new();
private List<IPathfindingNode>? Path { get; set; }




private bool hasChanged => BIC._bacgroundImage.bgImg is not null;


protected override async Task OnInitializedAsync()
{
Expand Down Expand Up @@ -208,21 +203,21 @@
{ "Equipment", _equipment },
{ "Inventory", _inventory }
};
MapParameters mapParameters = MapParametersService.ReadMapParametersFromJson(
MapParameters? mapParameters = MapParametersService.ReadMapParametersFromJson(
"Config/MapParameters/Normal.json");
_map = await MapGeneratorService.GetMap(mapParameters);
Map = await MapGeneratorService.GetMap(mapParameters);
_mapDisplayData.ShowColorMap = true;
_mapPopupParameters = new()
{
{ "Map", _map },
{ "Map", Map },
{ "MapDisplayData", _mapDisplayData }
};

BIC.Map = _map;
BIC._bacgroundImage = bacgroundImage;
BackgroundImageChanger.Map = Map;
BackgroundImageChanger.BackgroundImage = BackgroundImage;
await GetBacgroundAsync();

this.BIC.BgImgChanged += this.OnBgChanged!;
this.BackgroundImageChanger.BgImgChanged += this.OnBgChanged!;

await base.OnInitializedAsync();

Expand All @@ -234,13 +229,15 @@
ProtectedSessionStore.DeleteAsync("username");
}

private async Task GetBacgroundAsync() => await BIC.changeBgImgAsync();

//this piece of codes refreshes GamePage after changing background image. It is necessary to show new background.
private async Task GetBacgroundAsync() => await BackgroundImageChanger.ChangeBgImgAsync();

private void OnBgChanged(object sender, EventArgs e) => this.InvokeAsync(this.StateHasChanged);

public void Dispose()
{
this.BIC.BgImgChanged -= this.OnBgChanged!;
this.BackgroundImageChanger.BgImgChanged -= this.OnBgChanged!;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Fracture.Server.Modules.MapGenerator.Services;

public interface IMapGeneratorService
{
Task<MapData> GetMap(MapParameters mapParameters);
Task<MapData> GetMap(MapParameters? mapParameters);
MapData MapData { get; }
}
4 changes: 2 additions & 2 deletions Server/Modules/MapGenerator/Services/MapGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public MapGeneratorService(ILogger<MapGeneratorService> logger)
this.logger = logger;
}

public async Task<MapData> GetMap(MapParameters mapParameters)
public async Task<MapData> GetMap(MapParameters? mapParameters)
{
MapData = GenerateMap(mapParameters);
return await Task.FromResult(MapData);
}

private MapData GenerateMap(MapParameters mapParameters)
private MapData GenerateMap(MapParameters? mapParameters)
{
var noiseParameters = mapParameters.NoiseParameters;
noiseParameters.Seed = noiseParameters.UseRandomSeed
Expand Down
10 changes: 5 additions & 5 deletions Server/Modules/MapGenerator/UI/MapView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
@using Fracture.Server.Modules.MapGenerator.Models
@using Fracture.Server.Modules.MapGenerator.UI.Models
@using Fracture.Server.Modules.Pathfinding.Models
@using Fracture.Server.Modules.Shared.ImageChanger
@using Fracture.Server.Modules.Shared.ImageChangers
@using Microsoft.AspNetCore.Components.Web

@inject BacgroundImageChanger Bic



<div id="map" tabindex="0" @onkeydown="HandleKeyDown">
Expand Down Expand Up @@ -131,9 +131,9 @@

private async void HandleKeyDown(KeyboardEventArgs e)
{
BacgroundImageChanger _bacgroundImage = new()
BackgroundImageChanger backgroundImage = new()
{
_bacgroundImage = GamePage.bacgroundImage,
BackgroundImage = GamePage.BackgroundImage,
Map = Map
};
if (IsMiniMap==false)
Expand Down Expand Up @@ -189,7 +189,7 @@

private bool CanMoveToTile(int x, int y)
{
if (Map.Grid[x,y].Walkable==true)
if (Map != null && Map.Grid[x,y].Walkable==true)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace Fracture.Server.Modules.Shared.ImageChanger;
namespace Fracture.Server.Modules.Shared.ImageChangers;

public class BacgroundImage
public class BackgroundImage
{
[Required]
public string bgImg { get; set; }

public BacgroundImage(string bgimg)
public BackgroundImage(string bgimg)
{
bgImg = bgimg;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,76 @@
using Fracture.Server.Components.Pages;
using Fracture.Server.Modules.MapGenerator.Models;
using Fracture.Server.Modules.MapGenerator.Models;
using Fracture.Server.Modules.MapGenerator.UI;
using Microsoft.AspNetCore.Components;

namespace Fracture.Server.Modules.Shared.ImageChanger;
namespace Fracture.Server.Modules.Shared.ImageChangers;

public class BacgroundImageChanger
public class BackgroundImageChanger
{
[Parameter]
public required BacgroundImage? _bacgroundImage { get; set; }
public required BackgroundImage? BackgroundImage { get; set; }

[Parameter]
public required MapData Map { get; set; }

public Task changeBgImgAsync()
private ILogger<BackgroundImageChanger> logger;
public Task ChangeBgImgAsync()
{
Console.WriteLine(MapView.CharacterXX);
var biome = Map.Grid[MapView.CharacterXX, MapView.CharacterYY].TerrainType;
Console.WriteLine(biome);


if (BackgroundImage is null)
{
logger.LogError("BackgroundImage is missing");
return Task.CompletedTask;
}

switch (biome)
{
case TerrainType.Forest:
{
_bacgroundImage.bgImg = "../assets/background/river.jpg";
Console.WriteLine(_bacgroundImage.bgImg);
BackgroundImage.bgImg = "../assets/background/river.jpg";
break;
}
case TerrainType.Grassland:
{
_bacgroundImage.bgImg = "../assets/background/river.jpg";
BackgroundImage.bgImg = "../assets/background/river.jpg";
break;
}
case TerrainType.Mountains:
{
_bacgroundImage.bgImg = "../assets/background/mountains.jpg";
BackgroundImage.bgImg = "../assets/background/mountains.jpg";
break;
}
case TerrainType.Beach:
{
_bacgroundImage.bgImg = "../assets/background/lava.jpg";
BackgroundImage.bgImg = "../assets/background/lava.jpg";
break;
}
case TerrainType.HighMountains:
{
_bacgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
BackgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
break;
}
case TerrainType.DeepOcean:
{
_bacgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
BackgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
break;
}
case TerrainType.ShallowWater:
{
_bacgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
BackgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
break;
}
default:
_bacgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
BackgroundImage.bgImg = "../assets/background/mountainsRiver.jpg";
break;
}

return Task.CompletedTask;
}

public event EventHandler<EventArgs> BgImgChanged;
public event EventHandler<EventArgs>? BgImgChanged;

public void NotifyListChanged(object sender, EventArgs e) => BgImgChanged?.Invoke(sender, e);
public void NotifyListChanged(object? sender, EventArgs e) => BgImgChanged?.Invoke(sender, e);
}
12 changes: 0 additions & 12 deletions Server/Modules/Shared/ImageChangers/IBacgroundImageChanger.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Server/Modules/Shared/ImageChangers/IBackgroundImageChanger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Components;

namespace Fracture.Server.Modules.Shared.ImageChangers;

public interface IBackgroundImageChanger
{
[Parameter]
BackgroundImage BackgroundImage { get; set; }

void change();
}
4 changes: 2 additions & 2 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Fracture.Server.Modules.MapGenerator.Services;
using Fracture.Server.Modules.Shared;
using Fracture.Server.Modules.Shared.Configuration;
using Fracture.Server.Modules.Shared.ImageChanger;
using Fracture.Server.Modules.Shared.ImageChangers;
using Fracture.Server.Modules.Shared.NameGenerators;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand All @@ -28,7 +28,7 @@
builder.Services.AddSingleton<IMapGeneratorService, MapGeneratorService>();
builder.Services.AddSingleton<MapParametersService>();

builder.Services.AddScoped<BacgroundImageChanger>();
builder.Services.AddScoped<BackgroundImageChanger>();
builder.Services.AddScoped<IUsersRepository, UsersRepository>();
builder.Services.AddScoped<IItemsRepository, ItemsRepository>();

Expand Down

0 comments on commit ba8128a

Please sign in to comment.