Skip to content
Merged

Cleanup #2057

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 158 additions & 2 deletions NosCore.sln

Large diffs are not rendered by default.

551 changes: 9 additions & 542 deletions src/NosCore.GameObject/ComponentEntities/Entities/Character.cs

Large diffs are not rendered by default.

62 changes: 8 additions & 54 deletions src/NosCore.GameObject/ComponentEntities/Entities/MapMonster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,31 @@
using NodaTime;
using NosCore.Data.Dto;
using NosCore.Data.StaticEntities;
using NosCore.GameObject.ComponentEntities.Extensions;
using NosCore.GameObject.ComponentEntities.Interfaces;
using NosCore.GameObject.Services.MapInstanceGenerationService;
using NosCore.GameObject.Services.ShopService;
using NosCore.GameObject.Services.SpeedCalculationService;
using NosCore.PathFinder.Interfaces;
using NosCore.Shared.Enumerations;
using Serilog;
using System;
using System.Collections.Concurrent;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NosCore.GameObject.ComponentEntities.Entities
{
public class MapMonster(ILogger logger, IHeuristic distanceCalculator, IClock clock,
ISpeedCalculationService speedCalculationService)
public class MapMonster(ISpeedCalculationService speedCalculationService)
: MapMonsterDto, INonPlayableEntity
{
public NpcMonsterDto NpcMonster { get; private set; } = null!;
public NpcMonsterDto NpcMonster { get; set; } = null!;

public IDisposable? Life { get; private set; }
public IDisposable? Life { get; set; }
public ConcurrentDictionary<IAliveEntity, int> HitList => new();

public void Initialize(NpcMonsterDto npcMonster)
public bool IsSitting { get; set; }
public byte Speed
{
NpcMonster = npcMonster;
Mp = NpcMonster?.MaxMp ?? 0;
Hp = NpcMonster?.MaxHp ?? 0;
PositionX = MapX;
PositionY = MapY;
IsAlive = true;
Level = NpcMonster?.Level ?? 0;
get => speedCalculationService.CalculateSpeed(this);
set { }
}

public bool IsSitting { get; set; }
public byte Speed => speedCalculationService.CalculateSpeed(this);
public byte Size { get; set; } = 10;
public int Mp { get; set; }
public int Hp { get; set; }
Expand Down Expand Up @@ -73,43 +60,10 @@ public void Initialize(NpcMonsterDto npcMonster)
public int MaxMp => NpcMonster.MaxMp;

public short Race => NpcMonster.Race;
public Shop? Shop => null;
public Shop? Shop { get; set; }

public byte Level { get; set; }

public byte HeroLevel { get; set; }

internal void StopLife()
{
Life?.Dispose();
Life = null;
}

public Task StartLifeAsync()
{
Life?.Dispose();

async Task LifeAsync()
{
try
{
if (!MapInstance.IsSleeping)
{
await MonsterLifeAsync();
}
}
catch (Exception e)
{
logger.Error(e.Message, e);
}
}
Life = Observable.Interval(TimeSpan.FromMilliseconds(400)).Select(_ => LifeAsync()).Subscribe();
return Task.CompletedTask;
}

private Task MonsterLifeAsync()
{
return this.MoveAsync(distanceCalculator, clock);
}
}
}
88 changes: 4 additions & 84 deletions src/NosCore.GameObject/ComponentEntities/Entities/MapNpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,30 @@
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
//

using Mapster;
using NodaTime;
using NosCore.Data.Dto;
using NosCore.Data.StaticEntities;
using NosCore.GameObject.ComponentEntities.Extensions;
using NosCore.GameObject.ComponentEntities.Interfaces;
using NosCore.GameObject.Networking.ClientSession;
using NosCore.GameObject.Services.ItemGenerationService;
using NosCore.GameObject.Services.MapInstanceGenerationService;
using NosCore.GameObject.Services.NRunService;
using NosCore.GameObject.Services.ShopService;
using NosCore.PathFinder.Interfaces;
using NosCore.Shared.Enumerations;
using Serilog;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;

namespace NosCore.GameObject.ComponentEntities.Entities
{
public class MapNpc(IItemGenerationService? itemProvider, ILogger logger, IHeuristic distanceCalculator,
IClock clock)
: MapNpcDto, INonPlayableEntity, IRequestableEntity
public class MapNpc : MapNpcDto, INonPlayableEntity, IRequestableEntity
{
public NpcMonsterDto NpcMonster { get; private set; } = null!;
public NpcMonsterDto NpcMonster { get; set; } = null!;

public IDisposable? Life { get; private set; }
public IDisposable? Life { get; set; }
public ConcurrentDictionary<IAliveEntity, int> HitList => new();

public void Initialize(NpcMonsterDto npcMonster, ShopDto? shopDto, NpcTalkDto? npcTalkDto, List<ShopItemDto> shopItemsDto)
{
NpcMonster = npcMonster;
Mp = NpcMonster?.MaxMp ?? 0;
Hp = NpcMonster?.MaxHp ?? 0;
Speed = NpcMonster?.Speed ?? 0;
PositionX = MapX;
PositionY = MapY;
IsAlive = true;

Task RequestExecAsync(RequestData request)
{
return ShowDialogAsync(request);
}
Requests[typeof(INrunEventHandler)]?.Select(RequestExecAsync).Subscribe();
var shopObj = shopDto;
if (shopObj == null)
{
return;
}

var shopItemsList = new ConcurrentDictionary<int, ShopItem>();
Parallel.ForEach(shopItemsDto, shopItemGrouping =>
{
var shopItem = shopItemGrouping.Adapt<ShopItem>();
shopItem.ItemInstance = itemProvider!.Create(shopItemGrouping.ItemVNum, -1);
shopItemsList[shopItemGrouping.ShopItemId] = shopItem;
});
Shop = shopObj.Adapt<Shop>();
Shop.Name = npcTalkDto?.Name ?? new I18NString();
Shop.OwnerCharacter = null;
Shop.ShopItems = shopItemsList;
}
public SemaphoreSlim HitSemaphore { get; } = new SemaphoreSlim(1, 1);

public byte Speed { get; set; }
Expand Down Expand Up @@ -101,49 +59,11 @@ Task RequestExecAsync(RequestData request)
public byte Level { get; set; }

public byte HeroLevel { get; set; }
public Shop? Shop { get; private set; }
public Shop? Shop { get; set; }

public Dictionary<Type, Subject<RequestData>> Requests { get; set; } = new()
{
[typeof(INrunEventHandler)] = new()
};

private Task ShowDialogAsync(RequestData requestData)
{
return requestData.ClientSession.SendPacketAsync(this.GenerateNpcReq(Dialog ?? 0));
}

internal void StopLife()
{
Life?.Dispose();
Life = null;
}

public Task StartLifeAsync()
{
Life?.Dispose();

async Task LifeAsync()
{
try
{
if (!MapInstance.IsSleeping)
{
await MonsterLifeAsync();
}
}
catch (Exception e)
{
logger.Error(e.Message, e);
}
}
Life = Observable.Interval(TimeSpan.FromMilliseconds(400)).Select(_ => LifeAsync()).Subscribe();
return Task.CompletedTask;
}

private Task MonsterLifeAsync()
{
return this.MoveAsync(distanceCalculator, clock);
}
}
}
16 changes: 3 additions & 13 deletions src/NosCore.GameObject/ComponentEntities/Entities/Pet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace NosCore.GameObject.ComponentEntities.Entities
{
public class Pet : MapMonsterDto, INamedEntity //TODO replace MapMonsterDTO by the correct PetDTO
{
public IDisposable? Life { get; private set; }
public NpcMonsterDto NpcMonster { get; private set; } = null!;
public IDisposable? Life { get; set; }
public NpcMonsterDto NpcMonster { get; set; } = null!;
public short Effect { get; set; }
public short EffectDelay { get; set; }
public Instant LastMove { get; set; }
Expand Down Expand Up @@ -62,16 +62,6 @@ public class Pet : MapMonsterDto, INamedEntity //TODO replace MapMonsterDTO by t

public short Race => NpcMonster.Race;

public Shop? Shop => null;


internal void Initialize(NpcMonsterDto npcMonster)
{
NpcMonster = npcMonster;
Mp = NpcMonster.MaxMp;
Hp = NpcMonster.MaxHp;
Speed = NpcMonster.Speed;
IsAlive = true;
}
public Shop? Shop { get; set; }
}
}
Loading
Loading