Refactor HistoryEventDefExtensionMethods

This commit is contained in:
amevarashi 2022-08-09 21:30:33 +05:00
parent 1323f5b270
commit 2f9bd5bbf9

View file

@ -1,5 +1,7 @@
using RimWorld; using RimWorld;
using Verse; using Verse;
using System.Linq;
using System.Collections.Generic;
namespace RJWSexperience.Ideology.HistoryEvents namespace RJWSexperience.Ideology.HistoryEvents
{ {
@ -7,18 +9,22 @@ namespace RJWSexperience.Ideology.HistoryEvents
{ {
public static void RecordEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner) public static void RecordEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{ {
DefExtension_SecondaryEvents secondaryEvents = def.GetModExtension<DefExtension_SecondaryEvents>(); //Log.Message($"[RSI] Recording event {def.ToStringWithPartner(pawn, partner)}");
List<TwoPawnEventRule> secondaryEventRules = def.GetModExtension<DefExtension_SecondaryEvents>()?.generationRules;
if (secondaryEvents != null) if (!secondaryEventRules.NullOrEmpty())
{ {
foreach (TwoPawnEventRule rule in secondaryEvents.generationRules) //Log.Message($"[RSI] Event has {secondaryEventRules?.Count} secondary events");
foreach (var rule in secondaryEventRules.Where(rule => rule.Applies(pawn, partner)))
{ {
if (rule.Applies(pawn, partner)) //Log.Message($"[RSI] Recording secondary event {def.defName}");
rule.historyEventDef.RecordEventWithPartner(pawn, partner); rule.historyEventDef.RecordEventWithPartner(pawn, partner);
} }
} }
Find.HistoryEventsManager.RecordEvent(def.CreateEventWithPartner(pawn, partner)); HistoryEvent historyEvent = def.CreateEventWithPartner(pawn, partner);
Find.HistoryEventsManager.RecordEvent(historyEvent);
//Log.Message($"[RSI] Recorded event {historyEvent.def.ToStringWithPartner(pawn, partner)}");
} }
public static HistoryEvent CreateEvent(this HistoryEventDef def, Pawn pawn) public static HistoryEvent CreateEvent(this HistoryEventDef def, Pawn pawn)
@ -28,18 +34,21 @@ namespace RJWSexperience.Ideology.HistoryEvents
public static HistoryEvent CreateEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner) public static HistoryEvent CreateEventWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{ {
DefExtension_EventOverrides overrides = def.GetModExtension<DefExtension_EventOverrides>(); //Log.Message($"[RSI] Creating event {def.ToStringWithPartner(pawn, partner)}");
HistoryEventDef overrideEvent = def.GetModExtension<DefExtension_EventOverrides>()?.overrideRules.FirstOrFallback(rule => rule.Applies(pawn, partner))?.historyEventDef;
if (overrides == null) if (overrideEvent != null)
return new HistoryEvent(def, pawn.Named(HistoryEventArgsNames.Doer), partner.Named(ArgsNamesCustom.Partner));
foreach (TwoPawnEventRule rule in overrides.overrideRules)
{ {
if (rule.Applies(pawn, partner)) //Log.Message($"[RSI] Event overridden by {overrideEvent.ToStringWithPartner(pawn, partner)}");
return rule.historyEventDef.CreateEventWithPartner(pawn, partner); return overrideEvent.CreateEventWithPartner(pawn, partner);
} }
return new HistoryEvent(def, pawn.Named(HistoryEventArgsNames.Doer), partner.Named(ArgsNamesCustom.Partner)); return new HistoryEvent(def, pawn.Named(HistoryEventArgsNames.Doer), partner.Named(ArgsNamesCustom.Partner));
} }
private static string ToStringWithPartner(this HistoryEventDef def, Pawn pawn, Pawn partner)
{
return $"{def.defName}, doer {pawn.NameShortColored}, partner {partner.NameShortColored}";
}
} }
} }