feat: add event bus
This commit is contained in:
15
Client/Domain/Events/EventBusInterface.cs
Normal file
15
Client/Domain/Events/EventBusInterface.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public interface EventBusInterface
|
||||
{
|
||||
void Subscrbe<T>(EventHandlerInterface<T> handler) where T : EventInterface;
|
||||
void Unsubscrbe<T>(EventHandlerInterface<T> handler) where T : EventInterface;
|
||||
void Publish<T>(T @event) where T : EventInterface;
|
||||
}
|
||||
}
|
17
Client/Domain/Events/EventHandlerInterface.cs
Normal file
17
Client/Domain/Events/EventHandlerInterface.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public interface EventHandlerInterface
|
||||
{
|
||||
}
|
||||
|
||||
public interface EventHandlerInterface<T> : EventHandlerInterface where T : EventInterface
|
||||
{
|
||||
void Handle(T @event);
|
||||
}
|
||||
}
|
12
Client/Domain/Events/EventInterface.cs
Normal file
12
Client/Domain/Events/EventInterface.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public interface EventInterface
|
||||
{
|
||||
}
|
||||
}
|
19
Client/Domain/Events/HeroCreatedEvent.cs
Normal file
19
Client/Domain/Events/HeroCreatedEvent.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Client.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public class HeroCreatedEvent : EventInterface
|
||||
{
|
||||
public readonly Hero Hero;
|
||||
|
||||
public HeroCreatedEvent(Hero hero)
|
||||
{
|
||||
Hero = hero;
|
||||
}
|
||||
}
|
||||
}
|
13
Client/Domain/Events/HeroDeletedEvent.cs
Normal file
13
Client/Domain/Events/HeroDeletedEvent.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Client.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public class HeroDeletedEvent : EventInterface
|
||||
{
|
||||
}
|
||||
}
|
19
Client/Domain/Events/NpcCreatedEvent.cs
Normal file
19
Client/Domain/Events/NpcCreatedEvent.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Client.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public class NpcCreatedEvent : EventInterface
|
||||
{
|
||||
public readonly NPC NPC;
|
||||
|
||||
public NpcCreatedEvent(NPC npc)
|
||||
{
|
||||
NPC = npc;
|
||||
}
|
||||
}
|
||||
}
|
19
Client/Domain/Events/NpcDeletedEvent.cs
Normal file
19
Client/Domain/Events/NpcDeletedEvent.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Client.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Client.Domain.Events
|
||||
{
|
||||
public class NpcDeletedEvent : EventInterface
|
||||
{
|
||||
public readonly uint Id;
|
||||
|
||||
public NpcDeletedEvent(uint id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using Client.Domain.Entities;
|
||||
using Client.Domain.Events;
|
||||
using Client.Domain.Factories;
|
||||
using Client.Domain.Helpers;
|
||||
using Client.Domain.ViewModels;
|
||||
@@ -14,9 +15,9 @@ namespace Client.Domain.Service
|
||||
{
|
||||
public override void OnCreate(Hero entity)
|
||||
{
|
||||
mainViewModel.CreateHero(entity);
|
||||
entity.ExperienceInfo.ExpToLevel = experienceHelper.GetExperienceToLevel(entity.ExperienceInfo.Level + 1);
|
||||
entity.ExperienceInfo.ExpToPrevLevel = experienceHelper.GetExperienceToLevel(entity.ExperienceInfo.Level);
|
||||
eventBus.Publish(new HeroCreatedEvent(entity));
|
||||
}
|
||||
|
||||
public override void OnUpdate(Hero entity)
|
||||
@@ -30,16 +31,16 @@ namespace Client.Domain.Service
|
||||
|
||||
public override void OnDelete(Hero entity)
|
||||
{
|
||||
mainViewModel.DeleteHero();
|
||||
eventBus.Publish(new HeroDeletedEvent());
|
||||
}
|
||||
|
||||
public HeroHandler(EntityFactoryInterface<Hero> factory, MainViewModelInterface mainViewModel, ExperienceHelperInterface experienceHelper) : base(factory)
|
||||
public HeroHandler(EntityFactoryInterface<Hero> factory, EventBusInterface eventBus, ExperienceHelperInterface experienceHelper) : base(factory)
|
||||
{
|
||||
this.mainViewModel = mainViewModel;
|
||||
this.eventBus = eventBus;
|
||||
this.experienceHelper = experienceHelper;
|
||||
}
|
||||
|
||||
private readonly MainViewModelInterface mainViewModel;
|
||||
private readonly EventBusInterface eventBus;
|
||||
private readonly ExperienceHelperInterface experienceHelper;
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Client.Domain.Entities;
|
||||
using Client.Domain.Events;
|
||||
using Client.Domain.Factories;
|
||||
using Client.Domain.Helpers;
|
||||
using Client.Domain.ViewModels;
|
||||
@@ -14,22 +15,22 @@ namespace Client.Domain.Service
|
||||
{
|
||||
public override void OnCreate(NPC entity)
|
||||
{
|
||||
mainViewModel.AddNpc(entity);
|
||||
entity.Level = npcInfoHelper.GetLevel(entity.NpcId);
|
||||
entity.AggroRadius = npcInfoHelper.GetAggroRadius(entity.NpcId);
|
||||
eventBus.Publish(new NpcCreatedEvent(entity));
|
||||
}
|
||||
public override void OnDelete(NPC entity)
|
||||
{
|
||||
mainViewModel.RemoveNpc(entity);
|
||||
eventBus.Publish(new NpcDeletedEvent(entity.Id));
|
||||
}
|
||||
|
||||
public NpcHandler(EntityFactoryInterface<NPC> factory, MainViewModelInterface mainViewModel, NpcInfoHelperInterface npcInfoHelper) : base(factory)
|
||||
public NpcHandler(EntityFactoryInterface<NPC> factory, EventBusInterface eventBus, NpcInfoHelperInterface npcInfoHelper) : base(factory)
|
||||
{
|
||||
this.mainViewModel = mainViewModel;
|
||||
this.eventBus = eventBus;
|
||||
this.npcInfoHelper = npcInfoHelper;
|
||||
}
|
||||
|
||||
private readonly MainViewModelInterface mainViewModel;
|
||||
private readonly EventBusInterface eventBus;
|
||||
private readonly NpcInfoHelperInterface npcInfoHelper;
|
||||
}
|
||||
}
|
||||
|
@@ -12,10 +12,6 @@ namespace Client.Domain.ViewModels
|
||||
public interface MainViewModelInterface
|
||||
{
|
||||
void AddChatMessage(ChatMessage chatMessage);
|
||||
void CreateHero(Hero hero);
|
||||
void DeleteHero();
|
||||
void AddNpc(NPC npc);
|
||||
void RemoveNpc(NPC npc);
|
||||
void AddPlayer(Player player);
|
||||
void RemovePlayer(Player player);
|
||||
void AddDrop(Drop drop);
|
||||
|
Reference in New Issue
Block a user