feat: add events for drop and chat message

This commit is contained in:
k0t9i 2023-01-31 16:14:13 +04:00
parent 31febdd341
commit d2b20e0666
15 changed files with 101 additions and 69 deletions

View File

@ -13,7 +13,6 @@ using Client.Domain.ValueObjects;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.Reflection; using System.Reflection;
using Client.Application.Views; using Client.Application.Views;
using Client.Domain.ViewModels;
using Client.Application.ViewModels; using Client.Application.ViewModels;
using Client.Domain.Helpers; using Client.Domain.Helpers;
using Client.Infrastructure.Helpers; using Client.Infrastructure.Helpers;
@ -101,7 +100,7 @@ namespace Client
.AddSingleton<PlayerHandler>() .AddSingleton<PlayerHandler>()
.AddSingleton<ChatMessageHandler>() .AddSingleton<ChatMessageHandler>()
.AddSingleton(typeof(MainViewModelInterface), typeof(MainViewModel)); .AddSingleton<MainViewModel>();
} }
} }
} }

View File

@ -3,7 +3,6 @@ using Client.Domain.Common;
using Client.Domain.Entities; using Client.Domain.Entities;
using Client.Domain.Events; using Client.Domain.Events;
using Client.Domain.ValueObjects; using Client.Domain.ValueObjects;
using Client.Domain.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@ -20,31 +19,14 @@ namespace Client.Application.ViewModels
{ {
public class MainViewModel : public class MainViewModel :
NotifyPropertyChanged, NotifyPropertyChanged,
MainViewModelInterface,
EventHandlerInterface<HeroCreatedEvent>, EventHandlerInterface<HeroCreatedEvent>,
EventHandlerInterface<HeroDeletedEvent>, EventHandlerInterface<HeroDeletedEvent>,
EventHandlerInterface<CreatureCreatedEvent>, EventHandlerInterface<CreatureCreatedEvent>,
EventHandlerInterface<CreatureDeletedEvent> EventHandlerInterface<CreatureDeletedEvent>,
EventHandlerInterface<DropCreatedEvent>,
EventHandlerInterface<DropDeletedEvent>,
EventHandlerInterface<ChatMessageCreatedEvent>
{ {
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) public void Handle(HeroCreatedEvent @event)
{ {
Hero = new HeroSummaryInfoViewModel(@event.Hero); Hero = new HeroSummaryInfoViewModel(@event.Hero);
@ -72,6 +54,24 @@ namespace Client.Application.ViewModels
Creatures.RemoveAll(x => x.Id == @event.Id); 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<ChatMessageViewModel> ChatMessages { get; } = new ObservableCollection<ChatMessageViewModel>(); public ObservableCollection<ChatMessageViewModel> ChatMessages { get; } = new ObservableCollection<ChatMessageViewModel>();
public ObservableCollection<CreatureListViewModel> Creatures { get; } = new ObservableCollection<CreatureListViewModel>(); public ObservableCollection<CreatureListViewModel> Creatures { get; } = new ObservableCollection<CreatureListViewModel>();
public ObservableCollection<DropListViewModel> Drops { get; } = new ObservableCollection<DropListViewModel>(); public ObservableCollection<DropListViewModel> Drops { get; } = new ObservableCollection<DropListViewModel>();

View File

@ -1,4 +1,4 @@
using Client.Domain.ViewModels; using Client.Application.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -21,12 +21,10 @@ namespace Client.Application.Views
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private readonly MainViewModelInterface mainViewModel; public MainWindow(MainViewModel mainViewModel)
public MainWindow(MainViewModelInterface mainViewModel)
{ {
InitializeComponent(); InitializeComponent();
this.mainViewModel = mainViewModel;
DataContext = mainViewModel; DataContext = mainViewModel;
} }
} }

View File

@ -5,11 +5,11 @@ using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Client.Application.ViewModels;
using Client.Domain.Events; using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.Parsers; using Client.Domain.Parsers;
using Client.Domain.Transports; using Client.Domain.Transports;
using Client.Domain.ViewModels;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Client namespace Client
@ -64,7 +64,7 @@ namespace Client
private void SubscribeAllHandlers() private void SubscribeAllHandlers()
{ {
var viewModel = serviceProvider.GetRequiredService<MainViewModelInterface>(); var viewModel = serviceProvider.GetRequiredService<MainViewModel>();
eventBus.Subscrbe((EventHandlerInterface<HeroCreatedEvent>)viewModel); eventBus.Subscrbe((EventHandlerInterface<HeroCreatedEvent>)viewModel);
eventBus.Subscrbe((EventHandlerInterface<HeroDeletedEvent>)viewModel); eventBus.Subscrbe((EventHandlerInterface<HeroDeletedEvent>)viewModel);
eventBus.Subscrbe((EventHandlerInterface<CreatureCreatedEvent>)viewModel); eventBus.Subscrbe((EventHandlerInterface<CreatureCreatedEvent>)viewModel);

View File

@ -109,8 +109,8 @@ namespace Client.Domain.Entities
IsHostile = isHostile; IsHostile = isHostile;
NpcId = npcId; NpcId = npcId;
SpoilState = spoilState; SpoilState = spoilState;
FullName = fullName; this.fullName = FullName = fullName;
VitalStats = vitalStats; this.vitalStats = VitalStats = vitalStats;
} }
private void FullName_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) private void FullName_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)

View File

@ -62,8 +62,8 @@ namespace Client.Domain.Entities
{ {
Id = id; Id = id;
Transform = transform; Transform = transform;
FullName = fullName; this.fullName = FullName = fullName;
Phenotype = phenotype; this.phenotype = Phenotype = phenotype;
} }
private void Phenotype_PropertyChanged(object? sender, PropertyChangedEventArgs e) private void Phenotype_PropertyChanged(object? sender, PropertyChangedEventArgs e)

View File

@ -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;
}
}
}

View 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 DropCreatedEvent : EventInterface
{
public readonly Drop Drop;
public DropCreatedEvent(Drop drop)
{
Drop = drop;
}
}
}

View 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 DropDeletedEvent : EventInterface
{
public readonly uint Id;
public DropDeletedEvent(uint id)
{
Id = id;
}
}
}

View File

@ -6,9 +6,9 @@ using System.Threading.Tasks;
using Client.Domain.DTO; using Client.Domain.DTO;
using Client.Domain.Entities; using Client.Domain.Entities;
using Client.Domain.Enums; using Client.Domain.Enums;
using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.ValueObjects; using Client.Domain.ValueObjects;
using Client.Domain.ViewModels;
namespace Client.Domain.Service namespace Client.Domain.Service
{ {
@ -23,19 +23,17 @@ namespace Client.Domain.Service
{ {
throw new ArgumentNullException(nameof(message)); throw new ArgumentNullException(nameof(message));
} }
messages.Add(message); eventBus.Publish(new ChatMessageCreatedEvent(message));
mainViewModel.AddChatMessage(message);
} }
} }
public ChatMessageHandler(EntityFactoryInterface<ChatMessage> factory, MainViewModelInterface mainViewModel) public ChatMessageHandler(EntityFactoryInterface<ChatMessage> factory, EventBusInterface eventBus)
{ {
this.factory = factory; this.factory = factory;
this.mainViewModel = mainViewModel; this.eventBus = eventBus;
} }
private readonly EntityFactoryInterface<ChatMessage> factory; private readonly EntityFactoryInterface<ChatMessage> factory;
private readonly MainViewModelInterface mainViewModel; private readonly EventBusInterface eventBus;
private List<ChatMessage> messages = new List<ChatMessage>();
} }
} }

View File

@ -1,6 +1,6 @@
using Client.Domain.Entities; using Client.Domain.Entities;
using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -13,18 +13,18 @@ namespace Client.Domain.Service
{ {
public override void OnCreate(Drop entity) public override void OnCreate(Drop entity)
{ {
mainViewModel.AddDrop(entity); eventBus.Publish(new DropCreatedEvent(entity));
} }
public override void OnDelete(Drop entity) public override void OnDelete(Drop entity)
{ {
mainViewModel.RemoveDrop(entity); eventBus.Publish(new DropDeletedEvent(entity.Id));
} }
public DropHandler(EntityFactoryInterface<Drop> factory, MainViewModelInterface mainViewModel) : base(factory) public DropHandler(EntityFactoryInterface<Drop> factory, EventBusInterface eventBus) : base(factory)
{ {
this.mainViewModel = mainViewModel; this.eventBus = eventBus;
} }
private readonly MainViewModelInterface mainViewModel; private readonly EventBusInterface eventBus;
} }
} }

View File

@ -2,7 +2,6 @@
using Client.Domain.Events; using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.Helpers; using Client.Domain.Helpers;
using Client.Domain.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -2,7 +2,6 @@
using Client.Domain.Events; using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.Helpers; using Client.Domain.Helpers;
using Client.Domain.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -1,7 +1,6 @@
using Client.Domain.Entities; using Client.Domain.Entities;
using Client.Domain.Events; using Client.Domain.Events;
using Client.Domain.Factories; using Client.Domain.Factories;
using Client.Domain.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -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);
}
}