Skip to content

Commit

Permalink
Add new modify methods to take in context argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Sep 25, 2023
1 parent ca6966b commit 068848c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,23 @@ public async Task Modify(Action<IFieldUserModify> action)

public Task ModifyStats(Action<IModifyStatContext>? action = null, bool exclRequest = false)
=> Modify(m => m.Stats(action, exclRequest));
public Task ModifyStats(IModifyStatContext context, bool exclRequest = false)
=> Modify(m => m.Stats(context, exclRequest));

public Task ModifyInventory(Action<IModifyInventoryGroupContext>? action = null, bool exclRequest = false)
=> Modify(m => m.Inventory(action, exclRequest));

public Task ModifyInventory(IModifyInventoryGroupContext context, bool exclRequest = false)
=> Modify(m => m.Inventory(context, exclRequest));

public Task ModifySkills(Action<IModifySkillContext>? action = null, bool exclRequest = false)
=> Modify(m => m.Skills(action, exclRequest));

public Task ModifySkills(IModifySkillContext context, bool exclRequest = false)
=> Modify(m => m.Skills(context, exclRequest));

public Task ModifyTemporaryStats(Action<IModifyTemporaryStatContext>? action = null, bool exclRequest = false)
=> Modify(m => m.TemporaryStats(action, exclRequest));
public Task ModifyTemporaryStats(IModifyTemporaryStatContext context, bool exclRequest = false)
=> Modify(m => m.TemporaryStats(context, exclRequest));

protected override IPacket GetMovePacket(IFieldUserMovePath ctx)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ public class FieldUserModify : IFieldUserModify
public bool IsRequireUpdate { get; private set; } = false;
public bool IsRequireUpdateAvatar { get; private set; } = false;

public async Task Stats(Action<IModifyStatContext>? action = null, bool exclRequest = false)
public Task Stats(Action<IModifyStatContext>? action = null, bool exclRequest = false)
{
var context = new ModifyStatContext(_user.Character);

action?.Invoke(context);

return Stats(context, exclRequest);
}

public async Task Stats(IModifyStatContext context, bool exclRequest = false)
{
if (context.Flag > 0)
IsRequireUpdate = true;

Expand Down Expand Up @@ -55,13 +58,17 @@ public async Task Stats(Action<IModifyStatContext>? action = null, bool exclRequ
_user.Character.Job));
}

public async Task Inventory(Action<IModifyInventoryGroupContext>? action = null, bool exclRequest = false)
public Task Inventory(Action<IModifyInventoryGroupContext>? action = null, bool exclRequest = false)
{
var context = new ModifyInventoryGroupContext(_user.Character.Inventories, _user.StageUser.Context.Templates.Item);
using var packet = new PacketWriter(PacketSendOperations.InventoryOperation);

action?.Invoke(context);

return Inventory(context, exclRequest);
}

public async Task Inventory(IModifyInventoryGroupContext context, bool exclRequest = false)
{
using var packet = new PacketWriter(PacketSendOperations.InventoryOperation);

packet.WriteBool(exclRequest);
packet.Write(context);
packet.WriteBool(false);
Expand All @@ -74,12 +81,16 @@ public async Task Inventory(Action<IModifyInventoryGroupContext>? action = null,

await _user.Dispatch(packet.Build());
}
public async Task Skills(Action<IModifySkillContext>? action = null, bool exclRequest = false)

public Task Skills(Action<IModifySkillContext>? action = null, bool exclRequest = false)
{
var context = new ModifySkillContext(_user.Character);

action?.Invoke(context);
return Skills(context, exclRequest);
}

public async Task Skills(IModifySkillContext context, bool exclRequest = false)
{
IsRequireUpdate = true;

var packet = new PacketWriter(PacketSendOperations.ChangeSkillRecordResult);
Expand All @@ -90,13 +101,16 @@ public async Task Skills(Action<IModifySkillContext>? action = null, bool exclRe

await _user.Dispatch(packet.Build());
}
public async Task TemporaryStats(Action<IModifyTemporaryStatContext>? action = null, bool exclRequest = false)

public Task TemporaryStats(Action<IModifyTemporaryStatContext>? action = null, bool exclRequest = false)
{
var context = new ModifyTemporaryStatContext(_user.Character.TemporaryStats);

action?.Invoke(context);

return TemporaryStats(context, exclRequest);
}

public async Task TemporaryStats(IModifyTemporaryStatContext context, bool exclRequest = false)
{
var isUpdateReset = context.HistoryReset.Records.Any() ||
context.HistoryReset.HasTwoStateStats();
var isUpdateSet = context.HistorySet.Records.Any() ||
Expand Down
19 changes: 14 additions & 5 deletions src/common/Edelstein.Common.Gameplay.Game/Quests/QuestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using Edelstein.Common.Gameplay.Constants;
using Edelstein.Common.Gameplay.Game.Objects.User.Effects;
using Edelstein.Common.Gameplay.Game.Objects.User.Messages;
using Edelstein.Common.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Common.Gameplay.Models.Inventories.Items;
using Edelstein.Common.Gameplay.Models.Inventories.Modify;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Game.Quests;
using Edelstein.Protocol.Gameplay.Game.Quests.Templates;
Expand Down Expand Up @@ -148,25 +150,29 @@ public async Task<QuestResultType> Act(QuestAction action, IQuestTemplate templa

if (!user.StageUser.Context.Managers.Inventory.HasSlotFor(user.Character.Inventories, rewardsCheck))
return QuestResultType.FailedInventory;

var stats = new ModifyStatContext(user.Character);

if (actTemplate.IncEXP > 0)
{
await user.ModifyStats(s => s.EXP += actTemplate.IncEXP.Value);
stats.EXP += actTemplate.IncEXP.Value;
await user.Message(new IncEXPMessage(actTemplate.IncEXP.Value, true));
}

if (actTemplate.IncMoney > 0)
{
await user.ModifyStats(s => s.Money += actTemplate.IncMoney.Value);
stats.Money += actTemplate.IncMoney.Value;
await user.Message(new IncMoneyMessage(actTemplate.IncMoney.Value));
}

if (actTemplate.IncPOP > 0)
{
await user.ModifyStats(s => s.POP += (short)actTemplate.IncPOP.Value);
stats.POP += (short)actTemplate.IncPOP.Value;
await user.Message(new IncPOPMessage(actTemplate.IncPOP.Value));
}

await user.ModifyStats(stats);

var rewards = new List<IQuestTemplateActItem>();

if (rewardsBase != null)
Expand All @@ -192,6 +198,8 @@ public async Task<QuestResultType> Act(QuestAction action, IQuestTemplate templa
if (rewardSelect != null)
rewards.Add(rewardSelect);

var inventory = new ModifyInventoryGroupContext(user.Character.Inventories, _itemTemplates);

if (rewards.Count > 0)
{
var now = DateTime.UtcNow;
Expand All @@ -213,11 +221,12 @@ public async Task<QuestResultType> Act(QuestAction action, IQuestTemplate templa
bundle.Number = (short)reward.Count;

if (slot != null)
await user.ModifyInventory(i => i.Add(slot));
inventory.Add(slot);
} else
await user.ModifyInventory(i => i.Remove(reward.ItemID, Math.Abs((short)reward.Count)));
inventory.Remove(reward.ItemID, Math.Abs((short)reward.Count));
}

await user.ModifyInventory(inventory);
await user.Effect(new QuestEffect(rewards
.Select(r => Tuple.Create(r.ItemID, r.Count))
.ToImmutableList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ Task Converse(

Task Modify(Action<IFieldUserModify> action);
Task ModifyStats(Action<IModifyStatContext>? action = null, bool exclRequest = false);
Task ModifyStats(IModifyStatContext context, bool exclRequest = false);

Task ModifyInventory(Action<IModifyInventoryGroupContext>? action = null, bool exclRequest = false);
Task ModifyInventory(IModifyInventoryGroupContext context, bool exclRequest = false);

Task ModifySkills(Action<IModifySkillContext>? action = null, bool exclRequest = false);
Task ModifySkills(IModifySkillContext context, bool exclRequest = false);

Task ModifyTemporaryStats(Action<IModifyTemporaryStatContext>? action = null, bool exclRequest = false);
Task ModifyTemporaryStats(IModifyTemporaryStatContext context, bool exclRequest = false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ public interface IFieldUserModify
bool IsRequireUpdateAvatar { get; }

Task Stats(Action<IModifyStatContext>? action = null, bool exclRequest = false);
Task Stats(IModifyStatContext context, bool exclRequest = false);

Task Inventory(Action<IModifyInventoryGroupContext>? action = null, bool exclRequest = false);
Task Inventory(IModifyInventoryGroupContext context, bool exclRequest = false);

Task Skills(Action<IModifySkillContext>? action = null, bool exclRequest = false);
Task Skills(IModifySkillContext context, bool exclRequest = false);

Task TemporaryStats(Action<IModifyTemporaryStatContext>? action = null, bool exclRequest = false);
Task TemporaryStats(IModifyTemporaryStatContext context, bool exclRequest = false);
}

0 comments on commit 068848c

Please sign in to comment.