refactor: create only new entity

This commit is contained in:
k0t9i 2023-11-11 14:33:22 +04:00
parent 63833745b6
commit 129381e13c
9 changed files with 36 additions and 11 deletions

View File

@ -92,6 +92,7 @@ namespace Client
.AddSingleton(typeof(NpcInfoHelperInterface), typeof(ConfigurationNpcInfoHelper))
.AddSingleton(typeof(EventBusInterface), typeof(InMemoryEventBus))
.AddTransient(typeof(EntityFactoryInterface<Entity>), typeof(EntityFactory<Entity>))
.AddTransient(typeof(EntityFactoryInterface<Hero>), typeof(EntityFactory<Hero>))
.AddTransient(typeof(EntityFactoryInterface<Drop>), typeof(EntityFactory<Drop>))
.AddTransient(typeof(EntityFactoryInterface<NPC>), typeof(EntityFactory<NPC>))

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Client.Domain.Entities
{
public class Entity : EntityInterface
{
public uint Id { get; set; }
}
}

View File

@ -20,7 +20,7 @@ namespace Client.Domain.Service
eventBus.Publish(new DropDeletedEvent(entity.Id));
}
public DropHandler(EntityFactoryInterface<Drop> factory, EventBusInterface eventBus) : base(factory)
public DropHandler(EntityFactoryInterface<Drop> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus) : base(factory, entityFactory)
{
this.eventBus = eventBus;
}

View File

@ -14,29 +14,38 @@ namespace Client.Domain.Service
{
public void Update(MessageOperationEnum operation, string content)
{
var entity = factory.Create(content);
var baseEntity = entityFactory.Create(content);
if (baseEntity == null)
{
throw new ArgumentNullException(nameof(baseEntity));
}
if (operation == MessageOperationEnum.Create)
{
var entity = factory.Create(content);
if (entity == null)
{
throw new ArgumentNullException(nameof(entity));
}
entities[entity.Id] = entity;
entities[baseEntity.Id] = entity;
OnCreate(entity);
}
else if (operation == MessageOperationEnum.Update)
{
if (entity != null && entities.ContainsKey(entity.Id))
if (entities.ContainsKey(baseEntity.Id))
{
var entity = entities[baseEntity.Id];
factory.Update(entities[entity.Id], content);
OnUpdate(entities[entity.Id]);
}
}
else if (operation == MessageOperationEnum.Delete)
{
if (entity != null)
if (entities.ContainsKey(baseEntity.Id))
{
var entity = entities[baseEntity.Id];
entities.Remove(entity.Id);
OnDelete(entity);
}
@ -66,12 +75,14 @@ namespace Client.Domain.Service
}
public EntityHandler(EntityFactoryInterface<T> factory)
public EntityHandler(EntityFactoryInterface<T> factory, EntityFactoryInterface<Entity> entityFactory)
{
this.factory = factory;
this.entityFactory = entityFactory;
}
private readonly EntityFactoryInterface<T> factory;
private readonly EntityFactoryInterface<Entity> entityFactory;
private Dictionary<uint, T> entities = new Dictionary<uint, T>();
}
}

View File

@ -35,7 +35,7 @@ namespace Client.Domain.Service
eventBus.Publish(new HeroDeletedEvent());
}
public HeroHandler(EntityFactoryInterface<Hero> factory, EventBusInterface eventBus, ExperienceHelperInterface experienceHelper) : base(factory)
public HeroHandler(EntityFactoryInterface<Hero> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus, ExperienceHelperInterface experienceHelper) : base(factory, entityFactory)
{
this.eventBus = eventBus;
this.experienceHelper = experienceHelper;

View File

@ -20,7 +20,7 @@ namespace Client.Domain.Service
eventBus.Publish(new ItemDeletedEvent(entity.Id));
}
public ItemHander(EntityFactoryInterface<ItemInterface> factory, EventBusInterface eventBus) : base(factory)
public ItemHander(EntityFactoryInterface<ItemInterface> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus) : base(factory, entityFactory)
{
this.eventBus = eventBus;
}

View File

@ -33,7 +33,7 @@ namespace Client.Domain.Service
@event.Hero.Target = target;
}
public NpcHandler(EntityFactoryInterface<NPC> factory, EventBusInterface eventBus, NpcInfoHelperInterface npcInfoHelper) : base(factory)
public NpcHandler(EntityFactoryInterface<NPC> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus, NpcInfoHelperInterface npcInfoHelper) : base(factory, entityFactory)
{
this.eventBus = eventBus;
this.npcInfoHelper = npcInfoHelper;

View File

@ -20,7 +20,7 @@ namespace Client.Domain.Service
eventBus.Publish(new CreatureDeletedEvent(entity.Id));
}
public PlayerHandler(EntityFactoryInterface<Player> factory, EventBusInterface eventBus) : base(factory)
public PlayerHandler(EntityFactoryInterface<Player> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus) : base(factory, entityFactory)
{
this.eventBus = eventBus;
}

View File

@ -20,7 +20,7 @@ namespace Client.Domain.Service
eventBus.Publish(new SkillDeletedEvent(entity.Id));
}
public SkillHandler(EntityFactoryInterface<Skill> factory, EventBusInterface eventBus) : base(factory)
public SkillHandler(EntityFactoryInterface<Skill> factory, EntityFactoryInterface<Entity> entityFactory, EventBusInterface eventBus) : base(factory, entityFactory)
{
this.eventBus = eventBus;
}