Skip to content

Commit

Permalink
Add mobreward template loading, implement mobrewardmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Oct 26, 2023
1 parent 4ca5d83 commit 429b73b
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/app/Edelstein.Application.Server/ProgramHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -178,6 +180,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
b.RegisterType<ContiMoveManager>().As<IContiMoveManager>().SingleInstance();
b.RegisterType<ScriptedConversationManager>().As<INamedConversationManager>().SingleInstance();
b.RegisterType<NPCShopManager>().As<INPCShopManager>().SingleInstance();
b.RegisterType<MobRewardManager>().As<IMobRewardManager>().SingleInstance();
b.RegisterType<SkillManager>().As<ISkillManager>().SingleInstance();
b.RegisterType<ModifiedQuestTimeManager>().As<IModifiedQuestTimeManager>().SingleInstance();
b.RegisterType<QuestManager>().As<IQuestManager>().SingleInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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<int, MobRewards> _rewards;
private readonly ICollection<IMobReward> _rewardsAll;

public MobRewardManager()
{
_rewards = new Dictionary<int, MobRewards>();
_rewardsAll = new List<IMobReward>();
}

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<ICollection<IMobReward>> RetrieveAll(int mobID)
=> Task.FromResult<ICollection<IMobReward>>(_rewardsAll
.Concat(_rewards.TryGetValue(mobID, out var rewards) ? rewards.Items : ImmutableArray<IMobReward>.Empty)
.ToImmutableArray());

public async Task<ICollection<IMobReward>> RetrieveAvailable(IFieldUser user, IFieldMob mob)
=> (await RetrieveAll(mob.Template.ID))
.Where(r => true) // TODO
.ToImmutableArray();
}
Original file line number Diff line number Diff line change
@@ -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<StageStart>
{
private readonly ITemplateManager<MobRewardsTemplate> _templates;
private readonly IMobRewardManager _manager;

public MobRewardManagerInit(ITemplateManager<MobRewardsTemplate> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<IMobReward> Items { get; }

public MobRewards()
=> Items = new List<IMobReward>();
}
Original file line number Diff line number Diff line change
@@ -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<MobRewardsTemplateItem> Items { get; }

public MobRewardsTemplate(int id, IDataNode property)
{
ID = id;
Items = property.Children
.Select(p => new MobRewardsTemplateItem(Convert.ToInt32(p.Name), p.Cache()))
.ToImmutableArray();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<MobRewardsTemplate> _manager;

public MobRewardsTemplateLoader(IDataNamespace data, ITemplateManager<MobRewardsTemplate> manager)
{
_data = data;
_manager = manager;
}

public async Task<int> Load()
{
await Task.WhenAll(_data.ResolvePath("Server/Reward.img")?.Children
.Select(async n =>
{
var id = Convert.ToInt32(n.Name);
await _manager.Insert(new TemplateProviderEager<MobRewardsTemplate>(
id,
new MobRewardsTemplate(id, n.Cache())
));
}) ?? Array.Empty<Task>());

return _manager.Count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@ public record GameContextManagers(
IContiMoveManager ContiMove,
INamedConversationManager Conversation,
INPCShopManager NPCShop,
IMobRewardManager MobReward,
ISkillManager Skill,
IQuestManager Quest,
IModifiedQuestTimeManager QuestTime
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,5 +7,5 @@ public interface IMobRewardManager
Task Insert(int mobID, IMobReward reward);
Task InsertAll(IMobReward reward);
Task<ICollection<IMobReward>> RetrieveAll(int mobID);
Task<ICollection<IMobReward>> RetrieveCalculated(IFieldUser user, IFieldMob mob);
Task<ICollection<IMobReward>> RetrieveAvailable(IFieldUser user, IFieldMob mob);
}

0 comments on commit 429b73b

Please sign in to comment.