diff --git a/src/app/Edelstein.Application.Server/ProgramHost.cs b/src/app/Edelstein.Application.Server/ProgramHost.cs index 114f04573..704234c91 100644 --- a/src/app/Edelstein.Application.Server/ProgramHost.cs +++ b/src/app/Edelstein.Application.Server/ProgramHost.cs @@ -8,6 +8,7 @@ using Edelstein.Common.Gameplay.Game.Combat; using Edelstein.Common.Gameplay.Game.Continents; using Edelstein.Common.Gameplay.Game.Conversations; +using Edelstein.Common.Gameplay.Game.Objects.Mob.Rewards; using Edelstein.Common.Gameplay.Game.Objects.NPC; using Edelstein.Common.Gameplay.Game.Quests; using Edelstein.Common.Gameplay.Handling; @@ -29,6 +30,7 @@ using Edelstein.Protocol.Gameplay.Game.Contexts; using Edelstein.Protocol.Gameplay.Game.Continents; using Edelstein.Protocol.Gameplay.Game.Conversations; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; using Edelstein.Protocol.Gameplay.Game.Objects.NPC; using Edelstein.Protocol.Gameplay.Game.Quests; using Edelstein.Protocol.Gameplay.Login; @@ -178,6 +180,7 @@ public async Task StartAsync(CancellationToken cancellationToken) b.RegisterType().As().SingleInstance(); b.RegisterType().As().SingleInstance(); b.RegisterType().As().SingleInstance(); + b.RegisterType().As().SingleInstance(); b.RegisterType().As().SingleInstance(); b.RegisterType().As().SingleInstance(); b.RegisterType().As().SingleInstance(); diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobReward.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobReward.cs new file mode 100644 index 000000000..35fffb848 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobReward.cs @@ -0,0 +1,24 @@ +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Rewards; + +public record MobReward : IMobReward +{ + public int? ItemID { get; init; } + + public int? Money { get; init; } + + public int? NumberMin { get; init; } + public int? NumberMax { get; init; } + + public int? ReqQuest { get; init; } + + public int? ReqLevelMin { get; init; } + public int? ReqLevelMax { get; init; } + + public int? ReqMobLevelMin { get; init; } + public int? ReqMobLevelMax { get; init; } + + public DateTime? DateStart { get; init; } + public DateTime? DateEnd { get; init; } +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManager.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManager.cs new file mode 100644 index 000000000..80372b720 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManager.cs @@ -0,0 +1,46 @@ +using System.Collections.Immutable; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; +using Edelstein.Protocol.Gameplay.Game.Objects.User; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Rewards; + +public class MobRewardManager : IMobRewardManager +{ + private readonly IDictionary _rewards; + private readonly ICollection _rewardsAll; + + public MobRewardManager() + { + _rewards = new Dictionary(); + _rewardsAll = new List(); + } + + public Task Insert(int mobID, IMobReward reward) + { + if (!_rewards.TryGetValue(mobID, out var rewards)) + { + rewards = new MobRewards(); + _rewards[mobID] = rewards; + } + + rewards.Items.Add(reward); + return Task.CompletedTask; + } + + public Task InsertAll(IMobReward reward) + { + _rewardsAll.Add(reward); + return Task.CompletedTask; + } + + public Task> RetrieveAll(int mobID) + => Task.FromResult>(_rewardsAll + .Concat(_rewards.TryGetValue(mobID, out var rewards) ? rewards.Items : ImmutableArray.Empty) + .ToImmutableArray()); + + public async Task> RetrieveAvailable(IFieldUser user, IFieldMob mob) + => (await RetrieveAll(mob.Template.ID)) + .Where(r => true) // TODO + .ToImmutableArray(); +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManagerInit.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManagerInit.cs new file mode 100644 index 000000000..12f367715 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewardManagerInit.cs @@ -0,0 +1,26 @@ +using Edelstein.Common.Gameplay.Game.Objects.Mob.Templates; +using Edelstein.Protocol.Gameplay.Contracts; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; +using Edelstein.Protocol.Utilities.Pipelines; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Rewards; + +public class MobRewardManagerInit : IPipelinePlug +{ + private readonly ITemplateManager _templates; + private readonly IMobRewardManager _manager; + + public MobRewardManagerInit(ITemplateManager templates, IMobRewardManager manager) + { + _templates = templates; + _manager = manager; + } + + public async Task Handle(IPipelineContext ctx, StageStart message) + { + foreach (var rewards in await _templates.RetrieveAll()) + foreach (var item in rewards.Items) + await _manager.Insert(rewards.ID, item); + } +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewards.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewards.cs new file mode 100644 index 000000000..b50656f68 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Rewards/MobRewards.cs @@ -0,0 +1,11 @@ +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Rewards; + +public class MobRewards +{ + public ICollection Items { get; } + + public MobRewards() + => Items = new List(); +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplate.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplate.cs new file mode 100644 index 000000000..c775e9da0 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplate.cs @@ -0,0 +1,21 @@ +using System.Collections.Immutable; +using Duey.Abstractions; +using Edelstein.Common.Gameplay.Game.Objects.NPC.Templates; +using Edelstein.Protocol.Gameplay.Game.Objects.NPC; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Templates; + +public class MobRewardsTemplate : ITemplate +{ + public int ID { get; } + public ICollection Items { get; } + + public MobRewardsTemplate(int id, IDataNode property) + { + ID = id; + Items = property.Children + .Select(p => new MobRewardsTemplateItem(Convert.ToInt32(p.Name), p.Cache())) + .ToImmutableArray(); + } +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateItem.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateItem.cs new file mode 100644 index 000000000..e09353415 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateItem.cs @@ -0,0 +1,40 @@ +using Duey.Abstractions; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Templates; + +public class MobRewardsTemplateItem : ITemplate, IMobReward +{ + public int ID { get; } + + public int? ItemID { get; } + + public int? Money { get; } + + public int? NumberMin { get; } + public int? NumberMax { get; } + + public int? ReqQuest { get; } + + public int? ReqLevelMin { get; } + public int? ReqLevelMax { get; } + + public int? ReqMobLevelMin { get; } + public int? ReqMobLevelMax { get; } + + public DateTime? DateStart { get; } + public DateTime? DateEnd { get; } + + public MobRewardsTemplateItem(int id, IDataNode property) + { + ID = id; + + ItemID = property.ResolveInt("item"); + + Money = property.ResolveInt("money"); + + NumberMin = property.ResolveInt("min"); + NumberMax = property.ResolveInt("max"); + } +} diff --git a/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateLoader.cs b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateLoader.cs new file mode 100644 index 000000000..2d2ef0272 --- /dev/null +++ b/src/common/Edelstein.Common.Gameplay.Game/Objects/Mob/Templates/MobRewardsTemplateLoader.cs @@ -0,0 +1,33 @@ +using Duey.Abstractions; +using Edelstein.Common.Gameplay.Game.Objects.NPC.Templates; +using Edelstein.Common.Utilities.Templates; +using Edelstein.Protocol.Utilities.Templates; + +namespace Edelstein.Common.Gameplay.Game.Objects.Mob.Templates; + +public class MobRewardsTemplateLoader : ITemplateLoader +{ + private readonly IDataNamespace _data; + private readonly ITemplateManager _manager; + + public MobRewardsTemplateLoader(IDataNamespace data, ITemplateManager manager) + { + _data = data; + _manager = manager; + } + + public async Task Load() + { + await Task.WhenAll(_data.ResolvePath("Server/Reward.img")?.Children + .Select(async n => + { + var id = Convert.ToInt32(n.Name); + await _manager.Insert(new TemplateProviderEager( + id, + new MobRewardsTemplate(id, n.Cache()) + )); + }) ?? Array.Empty()); + + return _manager.Count; + } +} diff --git a/src/protocol/Edelstein.Protocol.Gameplay.Game/Contexts/GameContextManagers.cs b/src/protocol/Edelstein.Protocol.Gameplay.Game/Contexts/GameContextManagers.cs index 67e520757..5cd1a31cf 100644 --- a/src/protocol/Edelstein.Protocol.Gameplay.Game/Contexts/GameContextManagers.cs +++ b/src/protocol/Edelstein.Protocol.Gameplay.Game/Contexts/GameContextManagers.cs @@ -2,6 +2,7 @@ using Edelstein.Protocol.Gameplay.Game.Combat; using Edelstein.Protocol.Gameplay.Game.Continents; using Edelstein.Protocol.Gameplay.Game.Conversations; +using Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; using Edelstein.Protocol.Gameplay.Game.Objects.NPC; using Edelstein.Protocol.Gameplay.Game.Quests; using Edelstein.Protocol.Gameplay.Models.Inventories; @@ -18,6 +19,7 @@ public record GameContextManagers( IContiMoveManager ContiMove, INamedConversationManager Conversation, INPCShopManager NPCShop, + IMobRewardManager MobReward, ISkillManager Skill, IQuestManager Quest, IModifiedQuestTimeManager QuestTime diff --git a/src/protocol/Edelstein.Protocol.Gameplay.Game/Objects/Mob/Rewards/IMobRewardManager.cs b/src/protocol/Edelstein.Protocol.Gameplay.Game/Objects/Mob/Rewards/IMobRewardManager.cs index 5d3f571ae..f7e1ae104 100644 --- a/src/protocol/Edelstein.Protocol.Gameplay.Game/Objects/Mob/Rewards/IMobRewardManager.cs +++ b/src/protocol/Edelstein.Protocol.Gameplay.Game/Objects/Mob/Rewards/IMobRewardManager.cs @@ -1,5 +1,4 @@ using Edelstein.Protocol.Gameplay.Game.Objects.User; -using Edelstein.Protocol.Utilities.Repositories.Methods; namespace Edelstein.Protocol.Gameplay.Game.Objects.Mob.Rewards; @@ -8,5 +7,5 @@ public interface IMobRewardManager Task Insert(int mobID, IMobReward reward); Task InsertAll(IMobReward reward); Task> RetrieveAll(int mobID); - Task> RetrieveCalculated(IFieldUser user, IFieldMob mob); + Task> RetrieveAvailable(IFieldUser user, IFieldMob mob); }