Added before and after events for router actions
This commit is contained in:
@@ -8,8 +8,18 @@ using SPTarkov.Server.Core.Utils;
|
||||
|
||||
namespace SPTarkov.Server.Core.DI;
|
||||
|
||||
public interface IOnBeforeEventRequestData;
|
||||
public interface IOnAfterEventRequestData;
|
||||
|
||||
public record StaticDynamicOnBeforeEventRequestData(string Url, IRequestData RequestData, MongoId SessionId, string Output)
|
||||
: IOnBeforeEventRequestData;
|
||||
public record StaticDynamicOnAfterEventRequestData(string Url, IRequestData RequestData, MongoId SessionId, string Output, object Result)
|
||||
: IOnAfterEventRequestData;
|
||||
public abstract class Router
|
||||
{
|
||||
public event EventHandler<IOnBeforeEventRequestData>? OnBeforeAction;
|
||||
public event EventHandler<IOnAfterEventRequestData>? OnAfterAction;
|
||||
|
||||
protected IEnumerable<HandledRoute> handledRoutes = [];
|
||||
|
||||
public virtual string GetTopLevelRoute()
|
||||
@@ -29,6 +39,15 @@ public abstract class Router
|
||||
return handledRoutes;
|
||||
}
|
||||
|
||||
protected void TriggerOnBeforeAction(IOnBeforeEventRequestData requestData)
|
||||
{
|
||||
OnBeforeAction?.Invoke(this, requestData);
|
||||
}
|
||||
protected void TriggerOnAfterAction(IOnAfterEventRequestData requestData)
|
||||
{
|
||||
OnAfterAction?.Invoke(this, requestData);
|
||||
}
|
||||
|
||||
public bool CanHandle(string url, bool partialMatch = false)
|
||||
{
|
||||
if (partialMatch)
|
||||
@@ -52,7 +71,11 @@ public abstract class StaticRouter(JsonUtil jsonUtil, IEnumerable<RouteAction> r
|
||||
info = (IRequestData?)jsonUtil.Deserialize(body, type);
|
||||
}
|
||||
|
||||
return await action.action(url, info ?? new EmptyRequestData(), sessionId, output);
|
||||
info ??= new EmptyRequestData();
|
||||
TriggerOnBeforeAction(new StaticDynamicOnBeforeEventRequestData(url, info, sessionId, output));
|
||||
var result = await action.action(url, info, sessionId, output);
|
||||
TriggerOnAfterAction(new StaticDynamicOnAfterEventRequestData(url, info, sessionId, output, result));
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override IEnumerable<HandledRoute> GetHandledRoutes()
|
||||
@@ -63,7 +86,7 @@ public abstract class StaticRouter(JsonUtil jsonUtil, IEnumerable<RouteAction> r
|
||||
|
||||
public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable<RouteAction> routes) : Router
|
||||
{
|
||||
public async ValueTask<object> HandleDynamic(string url, string? body, MongoId sessionID, string output)
|
||||
public async ValueTask<object> HandleDynamic(string url, string? body, MongoId sessionId, string output)
|
||||
{
|
||||
var action = routes.First(r => url.Contains(r.url));
|
||||
var type = action.bodyType;
|
||||
@@ -73,7 +96,11 @@ public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable<RouteAction>
|
||||
info = (IRequestData?)jsonUtil.Deserialize(body, type);
|
||||
}
|
||||
|
||||
return await action.action(url, info ?? new EmptyRequestData(), sessionID, output);
|
||||
info ??= new EmptyRequestData();
|
||||
TriggerOnBeforeAction(new StaticDynamicOnBeforeEventRequestData(url, info, sessionId, output));
|
||||
var result = await action.action(url, info, sessionId, output);
|
||||
TriggerOnAfterAction(new StaticDynamicOnAfterEventRequestData(url, info, sessionId, output, result));
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override IEnumerable<HandledRoute> GetHandledRoutes()
|
||||
@@ -82,11 +109,40 @@ public abstract class DynamicRouter(JsonUtil jsonUtil, IEnumerable<RouteAction>
|
||||
}
|
||||
}
|
||||
|
||||
public record ItemRouterOnBeforeEventRequestData(
|
||||
string Url,
|
||||
PmcData PmcData,
|
||||
BaseInteractionRequestData Body,
|
||||
MongoId SessionId,
|
||||
ItemEventRouterResponse Output) : IOnBeforeEventRequestData;
|
||||
public record ItemRouterOnAfterEventRequestData(
|
||||
string Url,
|
||||
PmcData PmcData,
|
||||
BaseInteractionRequestData Body,
|
||||
MongoId SessionId,
|
||||
ItemEventRouterResponse Output,
|
||||
ValueTask<ItemEventRouterResponse> Result) : IOnAfterEventRequestData;
|
||||
|
||||
public record OnAfterEventRequestData<T, R>(string Url, T RequestData, MongoId SessionId, R Output, object Result) : IOnAfterEventRequestData;
|
||||
// The name of this class should be ItemEventRouter, but that name is taken,
|
||||
// So instead I added the definition
|
||||
public abstract class ItemEventRouterDefinition : Router
|
||||
{
|
||||
public abstract ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
public ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
MongoId sessionID,
|
||||
ItemEventRouterResponse output
|
||||
)
|
||||
{
|
||||
TriggerOnBeforeAction(new ItemRouterOnBeforeEventRequestData(url, pmcData, body, sessionID, output));
|
||||
var result = HandleItemEventInternal(url, pmcData, body, sessionID, output);
|
||||
TriggerOnAfterAction(new ItemRouterOnAfterEventRequestData(url, pmcData, body, sessionID, output, result));
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
@@ -95,9 +151,20 @@ public abstract class ItemEventRouterDefinition : Router
|
||||
);
|
||||
}
|
||||
|
||||
public record SaveLoadOnBeforeEventRequestData(SptProfile Profile) : IOnBeforeEventRequestData;
|
||||
public record SaveLoadRouterOnAfterEventRequestData(SptProfile Profile) : IOnAfterEventRequestData;
|
||||
|
||||
public abstract class SaveLoadRouter : Router
|
||||
{
|
||||
public abstract SptProfile HandleLoad(SptProfile profile);
|
||||
public SptProfile HandleLoad(SptProfile profile)
|
||||
{
|
||||
TriggerOnBeforeAction(new SaveLoadOnBeforeEventRequestData(profile));
|
||||
var result = HandleLoadInternal(profile);
|
||||
TriggerOnAfterAction(new SaveLoadRouterOnAfterEventRequestData(profile));
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract SptProfile HandleLoadInternal(SptProfile profile);
|
||||
}
|
||||
|
||||
public record HandledRoute(string route, bool dynamic);
|
||||
|
||||
@@ -20,7 +20,7 @@ public class CustomizationItemEventRouter(ISptLogger<CustomizationItemEventRoute
|
||||
return [new(ItemEventActions.CUSTOMIZATION_BUY, false), new(ItemEventActions.CUSTOMIZATION_SET, false)];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -23,7 +23,7 @@ public class HealthItemEventRouter(HealthCallbacks healthCallbacks) : ItemEventR
|
||||
];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -36,7 +36,7 @@ public class HideoutItemEventRouter(HideoutCallbacks hideoutCallbacks) : ItemEve
|
||||
];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -18,7 +18,7 @@ public class InsuranceItemEventRouter(InsuranceCallbacks insuranceCallbacks) : I
|
||||
return [new(ItemEventActions.INSURE, false)];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -46,7 +46,7 @@ public class InventoryItemEventRouter(InventoryCallbacks inventoryCallbacks, Hid
|
||||
};
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -18,7 +18,7 @@ public class NoteItemEventRouter(NoteCallbacks noteCallbacks) : ItemEventRouterD
|
||||
return [new(ItemEventActions.ADD_NOTE, false), new(ItemEventActions.EDIT_NOTE, false), new(ItemEventActions.DELETE_NOTE, false)];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -24,7 +24,7 @@ public class QuestItemEventRouter(QuestCallbacks questCallbacks) : ItemEventRout
|
||||
];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -23,7 +23,7 @@ public class RagfairItemEventRouter(RagfairCallbacks ragfairCallbacks) : ItemEve
|
||||
};
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -18,7 +18,7 @@ public class RepairItemEventRouter(RepairCallbacks repairCallbacks) : ItemEventR
|
||||
return new List<HandledRoute> { new(ItemEventActions.REPAIR, false), new(ItemEventActions.TRADER_REPAIR, false) };
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -23,7 +23,7 @@ public class TradeItemEventRouter(TradeCallbacks tradeCallbacks) : ItemEventRout
|
||||
];
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -23,7 +23,7 @@ public class WishlistItemEventRouter(WishlistCallbacks wishlistCallbacks) : Item
|
||||
};
|
||||
}
|
||||
|
||||
public override ValueTask<ItemEventRouterResponse> HandleItemEvent(
|
||||
protected override ValueTask<ItemEventRouterResponse> HandleItemEventInternal(
|
||||
string url,
|
||||
PmcData pmcData,
|
||||
BaseInteractionRequestData body,
|
||||
|
||||
@@ -12,7 +12,7 @@ public class HealthSaveLoadRouter : SaveLoadRouter
|
||||
return [new HandledRoute("spt-health", false)];
|
||||
}
|
||||
|
||||
public override SptProfile HandleLoad(SptProfile profile)
|
||||
protected override SptProfile HandleLoadInternal(SptProfile profile)
|
||||
{
|
||||
return profile;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class InraidSaveLoadRouter : SaveLoadRouter
|
||||
return [new HandledRoute("spt-inraid", false)];
|
||||
}
|
||||
|
||||
public override SptProfile HandleLoad(SptProfile profile)
|
||||
protected override SptProfile HandleLoadInternal(SptProfile profile)
|
||||
{
|
||||
profile.InraidData ??= new Inraid { Location = "none", Character = "none" };
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ public class InsuranceSaveLoadRouter : SaveLoadRouter
|
||||
return [new HandledRoute("spt-insurance", false)];
|
||||
}
|
||||
|
||||
public override SptProfile HandleLoad(SptProfile profile)
|
||||
protected override SptProfile HandleLoadInternal(SptProfile profile)
|
||||
{
|
||||
profile.InsuranceList ??= [];
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ProfileSaveLoadRouter : SaveLoadRouter
|
||||
return [new HandledRoute("spt-profile", false)];
|
||||
}
|
||||
|
||||
public override SptProfile HandleLoad(SptProfile profile)
|
||||
protected override SptProfile HandleLoadInternal(SptProfile profile)
|
||||
{
|
||||
profile.CharacterData ??= new Characters { PmcData = new PmcData(), ScavData = new PmcData() };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user