From c35f4e317ac09cd2f63ae85bc9867d00d6ec2281 Mon Sep 17 00:00:00 2001 From: k0t9i Date: Thu, 2 Feb 2023 16:54:38 +0400 Subject: [PATCH] feat: add items #wip --- Client/App.xaml.cs | 2 + .../Converters/CenterOfParentConverter.cs | 22 -------- .../Converters/PercentWidthConverter.cs | 26 --------- .../Application/ViewModels/MainViewModel.cs | 18 +++++- Client/Application/Views/MainWindow.xaml | 55 +++++++++++++++++++ Client/Bot.cs | 2 + Client/Domain/Entities/BaseItem.cs | 35 ++++++++++++ Client/Domain/Entities/EtcItem.cs | 26 +++++++++ Client/Domain/Enums/ItemTypeEnum.cs | 17 ++++++ Client/Domain/Events/ItemCreatedEvent.cs | 19 +++++++ Client/Domain/Events/ItemDeletedEvent.cs | 19 +++++++ Client/Domain/Service/ItemHandler.cs | 30 ++++++++++ .../Factories/EntityHandlerFactory.cs | 3 + .../Infrastructure/Factories/ItemFactory.cs | 48 ++++++++++++++++ 14 files changed, 273 insertions(+), 49 deletions(-) delete mode 100644 Client/Application/Converters/CenterOfParentConverter.cs delete mode 100644 Client/Application/Converters/PercentWidthConverter.cs create mode 100644 Client/Domain/Entities/BaseItem.cs create mode 100644 Client/Domain/Entities/EtcItem.cs create mode 100644 Client/Domain/Enums/ItemTypeEnum.cs create mode 100644 Client/Domain/Events/ItemCreatedEvent.cs create mode 100644 Client/Domain/Events/ItemDeletedEvent.cs create mode 100644 Client/Domain/Service/ItemHandler.cs create mode 100644 Client/Infrastructure/Factories/ItemFactory.cs diff --git a/Client/App.xaml.cs b/Client/App.xaml.cs index 2d73d01..77d75ba 100644 --- a/Client/App.xaml.cs +++ b/Client/App.xaml.cs @@ -94,6 +94,7 @@ namespace Client .AddTransient(typeof(EntityFactoryInterface), typeof(EntityFactory)) .AddTransient(typeof(EntityFactoryInterface), typeof(EntityFactory)) .AddTransient(typeof(EntityFactoryInterface), typeof(EntityFactory)) + .AddTransient(typeof(EntityFactoryInterface), typeof(ItemFactory)) .AddSingleton() .AddSingleton() @@ -101,6 +102,7 @@ namespace Client .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton(); } diff --git a/Client/Application/Converters/CenterOfParentConverter.cs b/Client/Application/Converters/CenterOfParentConverter.cs deleted file mode 100644 index 8da9372..0000000 --- a/Client/Application/Converters/CenterOfParentConverter.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Data; - -namespace Client.Application.Converters -{ - public class CenterOfParentConverter : IMultiValueConverter - { - public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) - { - return (double)values[0] + (double)values[2] / 2 - (double)values[1] / 2; - } - - public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) - { - throw new NotImplementedException(); - } - } -} diff --git a/Client/Application/Converters/PercentWidthConverter.cs b/Client/Application/Converters/PercentWidthConverter.cs deleted file mode 100644 index a291255..0000000 --- a/Client/Application/Converters/PercentWidthConverter.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Data; - -namespace Client.Application.Converters -{ - public class PercentWidthConverter : IMultiValueConverter - { - public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) - { - if (!(values[0] is double)) - { - return 0.0; - } - return ((double)values[0] / 100 * (Double)values[1]); - } - - public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) - { - throw new NotImplementedException(); - } - } -} diff --git a/Client/Application/ViewModels/MainViewModel.cs b/Client/Application/ViewModels/MainViewModel.cs index 96243b3..9027dbf 100644 --- a/Client/Application/ViewModels/MainViewModel.cs +++ b/Client/Application/ViewModels/MainViewModel.cs @@ -26,7 +26,9 @@ namespace Client.Application.ViewModels EventHandlerInterface, EventHandlerInterface, EventHandlerInterface, - EventHandlerInterface + EventHandlerInterface, + EventHandlerInterface, + EventHandlerInterface { public void Handle(HeroCreatedEvent @event) { @@ -94,11 +96,25 @@ namespace Client.Application.ViewModels PassiveSkills.RemoveAll(x => x.Id == @event.Id); } + public void Handle(ItemCreatedEvent @event) + { + if (hero != null) + { + Items.Add(@event.Item); + } + } + + public void Handle(ItemDeletedEvent @event) + { + Items.RemoveAll(x => x.Id == @event.Id); + } + public ObservableCollection ChatMessages { get; } = new ObservableCollection(); public ObservableCollection Creatures { get; } = new ObservableCollection(); public ObservableCollection Drops { get; } = new ObservableCollection(); public ObservableCollection ActiveSkills { get; } = new ObservableCollection(); public ObservableCollection PassiveSkills { get; } = new ObservableCollection(); + public ObservableCollection Items { get; } = new ObservableCollection(); public HeroSummaryInfoViewModel? Hero { get; private set; } public Hero? hero; } diff --git a/Client/Application/Views/MainWindow.xaml b/Client/Application/Views/MainWindow.xaml index 67e07cd..95fa44a 100644 --- a/Client/Application/Views/MainWindow.xaml +++ b/Client/Application/Views/MainWindow.xaml @@ -116,6 +116,61 @@ + + Inventory + + + + Items + + + + + + + + + + + + + + + + + Quest Items + + + + + + diff --git a/Client/Bot.cs b/Client/Bot.cs index 054a56d..e478a54 100644 --- a/Client/Bot.cs +++ b/Client/Bot.cs @@ -75,6 +75,8 @@ namespace Client eventBus.Subscrbe((EventHandlerInterface)viewModel); eventBus.Subscrbe((EventHandlerInterface)viewModel); eventBus.Subscrbe((EventHandlerInterface)viewModel); + eventBus.Subscrbe((EventHandlerInterface)viewModel); + eventBus.Subscrbe((EventHandlerInterface)viewModel); eventBus.Subscrbe((EventHandlerInterface)serviceProvider.GetRequiredService()); eventBus.Subscrbe((EventHandlerInterface)serviceProvider.GetRequiredService()); diff --git a/Client/Domain/Entities/BaseItem.cs b/Client/Domain/Entities/BaseItem.cs new file mode 100644 index 0000000..ae8bb29 --- /dev/null +++ b/Client/Domain/Entities/BaseItem.cs @@ -0,0 +1,35 @@ +using Client.Domain.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Entities +{ + public abstract class BaseItem : EntityInterface + { + public uint Id { get; set; } + public uint ItemId { get; set; } + public ItemTypeEnum Type { get; set; } + public string Name { get; set; } + public string IconName { get; set; } + public string Description { get; set; } + public int Mana { get { return mana; } set { mana = value; }} + public uint Weight { get; set; } + + public BaseItem(uint id, uint itemId, ItemTypeEnum type, string name, string iconName, string description, int mana, uint weight) + { + Id = id; + ItemId = itemId; + Type = type; + Name = name; + IconName = iconName; + Description = description; + this.mana = mana; + Weight = weight; + } + + private int mana; + } +} diff --git a/Client/Domain/Entities/EtcItem.cs b/Client/Domain/Entities/EtcItem.cs new file mode 100644 index 0000000..7273ccb --- /dev/null +++ b/Client/Domain/Entities/EtcItem.cs @@ -0,0 +1,26 @@ +using Client.Domain.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Entities +{ + public class EtcItem : BaseItem + { + public uint Amount { get => amount; set => amount = value; } + public bool IsQuest { get; set; } + public bool IsAutoused { get => isAutoused; set => isAutoused = value; } + public EtcItem(uint id, uint itemId, ItemTypeEnum type, string name, string iconName, string description, int mana, uint weight, uint amount, bool isQuest, bool isAutoused) : + base(id, itemId, type, name, iconName, description, mana, weight) + { + this.amount = amount; + IsQuest = isQuest; + this.isAutoused = isAutoused; + } + + private uint amount; + private bool isAutoused; + } +} diff --git a/Client/Domain/Enums/ItemTypeEnum.cs b/Client/Domain/Enums/ItemTypeEnum.cs new file mode 100644 index 0000000..dcb8492 --- /dev/null +++ b/Client/Domain/Enums/ItemTypeEnum.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Enums +{ + public enum ItemTypeEnum + { + None = -1, + Etc, + Armor, + Weapon, + Shield + } +} diff --git a/Client/Domain/Events/ItemCreatedEvent.cs b/Client/Domain/Events/ItemCreatedEvent.cs new file mode 100644 index 0000000..adda961 --- /dev/null +++ b/Client/Domain/Events/ItemCreatedEvent.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 ItemCreatedEvent : EventInterface + { + public readonly BaseItem Item; + + public ItemCreatedEvent(BaseItem item) + { + Item = item; + } + } +} diff --git a/Client/Domain/Events/ItemDeletedEvent.cs b/Client/Domain/Events/ItemDeletedEvent.cs new file mode 100644 index 0000000..4fa6e70 --- /dev/null +++ b/Client/Domain/Events/ItemDeletedEvent.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 ItemDeletedEvent : EventInterface + { + public readonly uint Id; + + public ItemDeletedEvent(uint id) + { + Id = id; + } + } +} diff --git a/Client/Domain/Service/ItemHandler.cs b/Client/Domain/Service/ItemHandler.cs new file mode 100644 index 0000000..85524c8 --- /dev/null +++ b/Client/Domain/Service/ItemHandler.cs @@ -0,0 +1,30 @@ +using Client.Domain.Entities; +using Client.Domain.Events; +using Client.Domain.Factories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Domain.Service +{ + public class ItemHander : EntityHandler + { + public override void OnCreate(BaseItem entity) + { + eventBus.Publish(new ItemCreatedEvent(entity)); + } + public override void OnDelete(BaseItem entity) + { + eventBus.Publish(new ItemDeletedEvent(entity.Id)); + } + + public ItemHander(EntityFactoryInterface factory, EventBusInterface eventBus) : base(factory) + { + this.eventBus = eventBus; + } + + private readonly EventBusInterface eventBus; + } +} diff --git a/Client/Infrastructure/Factories/EntityHandlerFactory.cs b/Client/Infrastructure/Factories/EntityHandlerFactory.cs index 3958939..497ba48 100644 --- a/Client/Infrastructure/Factories/EntityHandlerFactory.cs +++ b/Client/Infrastructure/Factories/EntityHandlerFactory.cs @@ -39,6 +39,9 @@ namespace Client.Infrastructure.Factories case MessageTypeEnum.Skill: result = serviceProvider.GetService(); break; + case MessageTypeEnum.Item: + result = serviceProvider.GetService(); + break; } if (result == null) diff --git a/Client/Infrastructure/Factories/ItemFactory.cs b/Client/Infrastructure/Factories/ItemFactory.cs new file mode 100644 index 0000000..ae7ad31 --- /dev/null +++ b/Client/Infrastructure/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using Client.Domain.Entities; +using Client.Domain.Enums; +using Client.Domain.Factories; +using Client.Infrastructure.Parsers.Converters; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Client.Infrastructure.Factories +{ + public class ItemFactory : EntityFactoryInterface + { + public BaseItem? Create(string data) + { + var type = JsonConvert.DeserializeObject(data, settings); + + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + switch (type.Type) + { + case ItemTypeEnum.Etc: + return JsonConvert.DeserializeObject(data, settings); + default: + return JsonConvert.DeserializeObject(data, settings); //fixme temporary + } + + throw new ArgumentException("Invalid item type " + type.Type.ToString()); + } + + public void Update(BaseItem entity, string data) + { + JsonConvert.PopulateObject(data, entity, settings); + } + + private JsonSerializerSettings settings = new JsonSerializerSettings { Converters = { new BooleanConverter() } }; + + private class ItemType + { + public ItemTypeEnum Type { get; set; } + } + } +}