-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement leader notifications for contract and tech completion
- Loading branch information
Showing
6 changed files
with
144 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using ContractConfigurator; | ||
using Contracts; | ||
using KSP.Localization; | ||
using KSP.UI.Screens; | ||
using ROUtils; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine; | ||
using static KSP.UI.Screens.MessageSystem; | ||
|
||
namespace RP0.Singletons | ||
{ | ||
public class LeaderNotifications : HostedSingleton | ||
{ | ||
public LeaderNotifications(SingletonHost host) : base(host) { } | ||
|
||
public override void Awake() | ||
{ | ||
SubscribeToEvents(); | ||
} | ||
|
||
public void SubscribeToEvents() | ||
{ | ||
GameEvents.OnTechnologyResearched.Add(OnTechnologyResearched); | ||
GameEvents.Contract.onCompleted.Add(OnContractComplete); | ||
} | ||
|
||
public static void ShowNotificationForNewLeaders(IEnumerable<StrategyConfigRP0> newLeaders) | ||
{ | ||
string leaderString = string.Join("\n", newLeaders.Select(s => s.Title)); | ||
if (!string.IsNullOrEmpty(leaderString)) | ||
{ | ||
PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), | ||
new Vector2(0.5f, 0.5f), | ||
"LeaderUnlocked", | ||
Localizer.Format("#rp0_Leaders_LeadersUnlockedTitle"), | ||
Localizer.Format("#rp0_Leaders_LeadersUnlocked") + leaderString, | ||
Localizer.GetStringByTag("#autoLOC_190905"), | ||
true, | ||
HighLogic.UISkin).HideGUIsWhilePopup(); | ||
} | ||
} | ||
|
||
public static void AddNewLeadersUnlockedMessage(IEnumerable<StrategyConfigRP0> newLeaders) | ||
{ | ||
var leaderLines = newLeaders.Select(s => s.title) | ||
.Distinct() | ||
.Select(s => $"• {s}"); | ||
string leaderString = string.Join("\n", leaderLines); | ||
if (!string.IsNullOrEmpty(leaderString)) | ||
{ | ||
MessageSystem.Instance?.AddMessage( | ||
new Message(Localizer.Format("#rp0_Leaders_LeadersUnlockedTitle"), | ||
Localizer.Format("#rp0_Leaders_LeadersUnlocked") + leaderString, | ||
MessageSystemButton.MessageButtonColor.BLUE, | ||
MessageSystemButton.ButtonIcons.MESSAGE)); | ||
} | ||
} | ||
|
||
private void OnContractComplete(Contract data) | ||
{ | ||
if (data is ConfiguredContract cc) | ||
{ | ||
var leadersUnlockedByThis = LeaderUtils.GetLeadersUnlockedByContract(cc); | ||
AddNewLeadersUnlockedMessage(leadersUnlockedByThis); | ||
} | ||
} | ||
|
||
private void OnTechnologyResearched(GameEvents.HostTargetAction<RDTech, RDTech.OperationResult> data) | ||
{ | ||
var leadersUnlockedByThis = LeaderUtils.GetLeadersUnlockedByTech(data.host.techID); | ||
AddNewLeadersUnlockedMessage(leadersUnlockedByThis); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using ContractConfigurator; | ||
using RP0.Requirements; | ||
using Strategies; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace RP0 | ||
{ | ||
public static class LeaderUtils | ||
{ | ||
public static IEnumerable<StrategyConfigRP0> GetLeadersUnlockedByTech(string techID) | ||
{ | ||
return GetAllLeaderStrategies() | ||
.Where(s => s.RequirementsBlock != null && | ||
(s.RequirementsBlock.Op is Any || | ||
s.RequirementsBlock.Op is All && s.RequirementsBlock.Reqs.Count == 1) && | ||
s.RequirementsBlock.ChildBlocks.Count == 0 && | ||
s.RequirementsBlock.Reqs.Any(r => !r.IsInverted && | ||
r is TechRequirement cr && | ||
cr.TechName == techID)); | ||
} | ||
|
||
public static IEnumerable<StrategyConfigRP0> GetLeadersUnlockedByContract(ConfiguredContract cc) | ||
{ | ||
return GetAllLeaderStrategies() | ||
.Where(s => s.RequirementsBlock != null && | ||
(s.RequirementsBlock.Op is Any || | ||
s.RequirementsBlock.Op is All && s.RequirementsBlock.Reqs.Count == 1) && | ||
s.RequirementsBlock.ChildBlocks.Count == 0 && | ||
s.RequirementsBlock.Reqs.Any(r => !r.IsInverted && | ||
r is Requirements.ContractRequirement cr && | ||
cr.ContractName == cc.contractType.name)); | ||
} | ||
|
||
public static IEnumerable<StrategyConfigRP0> GetLeadersUnlockedByProgram(string programName) | ||
{ | ||
return GetAllLeaderStrategies() | ||
.Where(s => s.RequirementsBlock != null && | ||
(s.RequirementsBlock.Op is Any || | ||
s.RequirementsBlock.Op is All && s.RequirementsBlock.Reqs.Count == 1) && | ||
s.RequirementsBlock.ChildBlocks.Count == 0 && | ||
s.RequirementsBlock.Reqs.Any(r => !r.IsInverted && | ||
r is ProgramRequirement pr && | ||
pr.ProgramName == programName)); | ||
} | ||
|
||
public static IEnumerable<StrategyConfigRP0> GetAllUnlockedLeaders() | ||
{ | ||
return GetAllLeaderStrategies().Where(s => s.IsUnlocked()); | ||
} | ||
|
||
private static IEnumerable<StrategyConfigRP0> GetAllLeaderStrategies() | ||
{ | ||
return StrategySystem.Instance.SystemConfig.Strategies | ||
.OfType<StrategyConfigRP0>() | ||
.Where(s => s.DepartmentName != "Programs"); | ||
} | ||
} | ||
} |