From d2b20e0666de99aa9647f5f9cc61902dd10ea024 Mon Sep 17 00:00:00 2001 From: k0t9i Date: Tue, 31 Jan 2023 16:14:13 +0400 Subject: [PATCH] feat: add events for drop and chat message --- Client/App.xaml.cs | 3 +- .../Application/ViewModels/MainViewModel.cs | 44 +++++++++---------- Client/Application/Views/MainWindow.xaml.cs | 8 ++-- Client/Bot.cs | 4 +- Client/Domain/Entities/NPC.cs | 4 +- Client/Domain/Entities/Player.cs | 4 +- .../Domain/Events/ChatMessageCreatedEvent.cs | 20 +++++++++ Client/Domain/Events/DropCreatedEvent.cs | 19 ++++++++ Client/Domain/Events/DropDeletedEvent.cs | 19 ++++++++ Client/Domain/Service/ChatMessageHandler.cs | 12 +++-- Client/Domain/Service/DropHandler.cs | 12 ++--- Client/Domain/Service/HeroHandler.cs | 1 - Client/Domain/Service/NpcHandler.cs | 1 - Client/Domain/Service/PlayerHandler.cs | 1 - .../ViewModels/MainViewModelInterface.cs | 18 -------- 15 files changed, 101 insertions(+), 69 deletions(-) create mode 100644 Client/Domain/Events/ChatMessageCreatedEvent.cs create mode 100644 Client/Domain/Events/DropCreatedEvent.cs create mode 100644 Client/Domain/Events/DropDeletedEvent.cs delete mode 100644 Client/Domain/ViewModels/MainViewModelInterface.cs diff --git a/Client/App.xaml.cs b/Client/App.xaml.cs index 43574f5..34e7a67 100644 --- a/Client/App.xaml.cs +++ b/Client/App.xaml.cs @@ -13,7 +13,6 @@ using Client.Domain.ValueObjects; using Microsoft.Extensions.Configuration; using System.Reflection; using Client.Application.Views; -using Client.Domain.ViewModels; using Client.Application.ViewModels; using Client.Domain.Helpers; using Client.Infrastructure.Helpers; @@ -101,7 +100,7 @@ namespace Client .AddSingleton() .AddSingleton() - .AddSingleton(typeof(MainViewModelInterface), typeof(MainViewModel)); + .AddSingleton(); } } } diff --git a/Client/Application/ViewModels/MainViewModel.cs b/Client/Application/ViewModels/MainViewModel.cs index 557fe54..348b8c4 100644 --- a/Client/Application/ViewModels/MainViewModel.cs +++ b/Client/Application/ViewModels/MainViewModel.cs @@ -3,7 +3,6 @@ using Client.Domain.Common; using Client.Domain.Entities; using Client.Domain.Events; using Client.Domain.ValueObjects; -using Client.Domain.ViewModels; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -20,31 +19,14 @@ namespace Client.Application.ViewModels { public class MainViewModel : NotifyPropertyChanged, - MainViewModelInterface, EventHandlerInterface, EventHandlerInterface, EventHandlerInterface, - EventHandlerInterface + EventHandlerInterface, + EventHandlerInterface, + EventHandlerInterface, + EventHandlerInterface { - - public void AddChatMessage(ChatMessage chatMessage) - { - ChatMessages.Add(new ChatMessageViewModel(chatMessage)); - } - - public void AddDrop(Drop drop) - { - if (hero != null) - { - Drops.Add(new DropListViewModel(drop, hero)); - } - } - - public void RemoveDrop(Drop drop) - { - Drops.RemoveAll(x => x.Id == drop.Id); - } - public void Handle(HeroCreatedEvent @event) { Hero = new HeroSummaryInfoViewModel(@event.Hero); @@ -72,6 +54,24 @@ namespace Client.Application.ViewModels Creatures.RemoveAll(x => x.Id == @event.Id); } + public void Handle(DropCreatedEvent @event) + { + if (hero != null) + { + Drops.Add(new DropListViewModel(@event.Drop, hero)); + } + } + + public void Handle(DropDeletedEvent @event) + { + Drops.RemoveAll(x => x.Id == @event.Id); + } + + public void Handle(ChatMessageCreatedEvent @event) + { + ChatMessages.Add(new ChatMessageViewModel(@event.Message)); + } + public ObservableCollection ChatMessages { get; } = new ObservableCollection(); public ObservableCollection Creatures { get; } = new ObservableCollection(); public ObservableCollection Drops { get; } = new ObservableCollection(); diff --git a/Client/Application/Views/MainWindow.xaml.cs b/Client/Application/Views/MainWindow.xaml.cs index e265050..a04ec16 100644 --- a/Client/Application/Views/MainWindow.xaml.cs +++ b/Client/Application/Views/MainWindow.xaml.cs @@ -1,4 +1,4 @@ -using Client.Domain.ViewModels; +using Client.Application.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -21,12 +21,10 @@ namespace Client.Application.Views /// public partial class MainWindow : Window { - private readonly MainViewModelInterface mainViewModel; - - public MainWindow(MainViewModelInterface mainViewModel) + public MainWindow(MainViewModel mainViewModel) { InitializeComponent(); - this.mainViewModel = mainViewModel; + DataContext = mainViewModel; } } diff --git a/Client/Bot.cs b/Client/Bot.cs index 1fa4717..ee66314 100644 --- a/Client/Bot.cs +++ b/Client/Bot.cs @@ -5,11 +5,11 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using Client.Application.ViewModels; using Client.Domain.Events; using Client.Domain.Factories; using Client.Domain.Parsers; using Client.Domain.Transports; -using Client.Domain.ViewModels; using Microsoft.Extensions.DependencyInjection; namespace Client @@ -64,7 +64,7 @@ namespace Client private void SubscribeAllHandlers() { - var viewModel = serviceProvider.GetRequiredService(); + var viewModel = serviceProvider.GetRequiredService(); eventBus.Subscrbe((EventHandlerInterface)viewModel); eventBus.Subscrbe((EventHandlerInterface)viewModel); eventBus.Subscrbe((EventHandlerInterface)viewModel); diff --git a/Client/Domain/Entities/NPC.cs b/Client/Domain/Entities/NPC.cs index 4cca84a..32fcb69 100644 --- a/Client/Domain/Entities/NPC.cs +++ b/Client/Domain/Entities/NPC.cs @@ -109,8 +109,8 @@ namespace Client.Domain.Entities IsHostile = isHostile; NpcId = npcId; SpoilState = spoilState; - FullName = fullName; - VitalStats = vitalStats; + this.fullName = FullName = fullName; + this.vitalStats = VitalStats = vitalStats; } private void FullName_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) diff --git a/Client/Domain/Entities/Player.cs b/Client/Domain/Entities/Player.cs index 8994d68..52e54df 100644 --- a/Client/Domain/Entities/Player.cs +++ b/Client/Domain/Entities/Player.cs @@ -62,8 +62,8 @@ namespace Client.Domain.Entities { Id = id; Transform = transform; - FullName = fullName; - Phenotype = phenotype; + this.fullName = FullName = fullName; + this.phenotype = Phenotype = phenotype; } private void Phenotype_PropertyChanged(object? sender, PropertyChangedEventArgs e) diff --git a/Client/Domain/Events/ChatMessageCreatedEvent.cs b/Client/Domain/Events/ChatMessageCreatedEvent.cs new file mode 100644 index 0000000..2cbd967 --- /dev/null +++ b/Client/Domain/Events/ChatMessageCreatedEvent.cs @@ -0,0 +1,20 @@ +using Client.Domain.Entities; +using Client.Domain.ValueObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Events +{ + public class ChatMessageCreatedEvent : EventInterface + { + public readonly ChatMessage Message; + + public ChatMessageCreatedEvent(ChatMessage message) + { + Message = message; + } + } +} diff --git a/Client/Domain/Events/DropCreatedEvent.cs b/Client/Domain/Events/DropCreatedEvent.cs new file mode 100644 index 0000000..7bdb76e --- /dev/null +++ b/Client/Domain/Events/DropCreatedEvent.cs @@ -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 DropCreatedEvent : EventInterface + { + public readonly Drop Drop; + + public DropCreatedEvent(Drop drop) + { + Drop = drop; + } + } +} diff --git a/Client/Domain/Events/DropDeletedEvent.cs b/Client/Domain/Events/DropDeletedEvent.cs new file mode 100644 index 0000000..af9edd8 --- /dev/null +++ b/Client/Domain/Events/DropDeletedEvent.cs @@ -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 DropDeletedEvent : EventInterface + { + public readonly uint Id; + + public DropDeletedEvent(uint id) + { + Id = id; + } + } +} diff --git a/Client/Domain/Service/ChatMessageHandler.cs b/Client/Domain/Service/ChatMessageHandler.cs index 97c24dc..36e8b14 100644 --- a/Client/Domain/Service/ChatMessageHandler.cs +++ b/Client/Domain/Service/ChatMessageHandler.cs @@ -6,9 +6,9 @@ using System.Threading.Tasks; using Client.Domain.DTO; using Client.Domain.Entities; using Client.Domain.Enums; +using Client.Domain.Events; using Client.Domain.Factories; using Client.Domain.ValueObjects; -using Client.Domain.ViewModels; namespace Client.Domain.Service { @@ -23,19 +23,17 @@ namespace Client.Domain.Service { throw new ArgumentNullException(nameof(message)); } - messages.Add(message); - mainViewModel.AddChatMessage(message); + eventBus.Publish(new ChatMessageCreatedEvent(message)); } } - public ChatMessageHandler(EntityFactoryInterface factory, MainViewModelInterface mainViewModel) + public ChatMessageHandler(EntityFactoryInterface factory, EventBusInterface eventBus) { this.factory = factory; - this.mainViewModel = mainViewModel; + this.eventBus = eventBus; } private readonly EntityFactoryInterface factory; - private readonly MainViewModelInterface mainViewModel; - private List messages = new List(); + private readonly EventBusInterface eventBus; } } diff --git a/Client/Domain/Service/DropHandler.cs b/Client/Domain/Service/DropHandler.cs index 5bfda56..24b2cf2 100644 --- a/Client/Domain/Service/DropHandler.cs +++ b/Client/Domain/Service/DropHandler.cs @@ -1,6 +1,6 @@ using Client.Domain.Entities; +using Client.Domain.Events; using Client.Domain.Factories; -using Client.Domain.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -13,18 +13,18 @@ namespace Client.Domain.Service { public override void OnCreate(Drop entity) { - mainViewModel.AddDrop(entity); + eventBus.Publish(new DropCreatedEvent(entity)); } public override void OnDelete(Drop entity) { - mainViewModel.RemoveDrop(entity); + eventBus.Publish(new DropDeletedEvent(entity.Id)); } - public DropHandler(EntityFactoryInterface factory, MainViewModelInterface mainViewModel) : base(factory) + public DropHandler(EntityFactoryInterface factory, EventBusInterface eventBus) : base(factory) { - this.mainViewModel = mainViewModel; + this.eventBus = eventBus; } - private readonly MainViewModelInterface mainViewModel; + private readonly EventBusInterface eventBus; } } diff --git a/Client/Domain/Service/HeroHandler.cs b/Client/Domain/Service/HeroHandler.cs index 881df1e..735da11 100644 --- a/Client/Domain/Service/HeroHandler.cs +++ b/Client/Domain/Service/HeroHandler.cs @@ -2,7 +2,6 @@ using Client.Domain.Events; using Client.Domain.Factories; using Client.Domain.Helpers; -using Client.Domain.ViewModels; using System; using System.Collections.Generic; using System.Linq; diff --git a/Client/Domain/Service/NpcHandler.cs b/Client/Domain/Service/NpcHandler.cs index 230b405..5af7ad6 100644 --- a/Client/Domain/Service/NpcHandler.cs +++ b/Client/Domain/Service/NpcHandler.cs @@ -2,7 +2,6 @@ using Client.Domain.Events; using Client.Domain.Factories; using Client.Domain.Helpers; -using Client.Domain.ViewModels; using System; using System.Collections.Generic; using System.Linq; diff --git a/Client/Domain/Service/PlayerHandler.cs b/Client/Domain/Service/PlayerHandler.cs index 3c99866..3be266c 100644 --- a/Client/Domain/Service/PlayerHandler.cs +++ b/Client/Domain/Service/PlayerHandler.cs @@ -1,7 +1,6 @@ using Client.Domain.Entities; using Client.Domain.Events; using Client.Domain.Factories; -using Client.Domain.ViewModels; using System; using System.Collections.Generic; using System.Linq; diff --git a/Client/Domain/ViewModels/MainViewModelInterface.cs b/Client/Domain/ViewModels/MainViewModelInterface.cs deleted file mode 100644 index 73fcf4d..0000000 --- a/Client/Domain/ViewModels/MainViewModelInterface.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Client.Application.ViewModels; -using Client.Domain.Entities; -using Client.Domain.ValueObjects; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Client.Domain.ViewModels -{ - public interface MainViewModelInterface - { - void AddChatMessage(ChatMessage chatMessage); - void AddDrop(Drop drop); - void RemoveDrop(Drop drop); - } -}