/
CompleteContractRequirement.cs
98 lines (84 loc) · 3.74 KB
/
CompleteContractRequirement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSP;
using Contracts;
using ContractConfigurator.Util;
using KSP.Localization;
namespace ContractConfigurator
{
/// <summary>
/// ContractRequirement to provide requirement for player having completed other contracts.
/// </summary>
public class CompleteContractRequirement : ContractCheckRequirement
{
protected Duration cooldownDuration;
public override bool LoadFromConfig(ConfigNode configNode)
{
// Load base class
bool valid = base.LoadFromConfig(configNode);
valid &= ConfigNodeUtil.ParseValue<Duration>(configNode, "cooldownDuration", x => cooldownDuration = x, this, new Duration(0.0));
return valid;
}
public override void OnSave(ConfigNode configNode)
{
base.OnSave(configNode);
configNode.AddValue("cooldownDuration", cooldownDuration.Value);
}
public override void OnLoad(ConfigNode configNode)
{
base.OnLoad(configNode);
cooldownDuration = new Duration(ConfigNodeUtil.ParseValue<double>(configNode, "cooldownDuration"));
}
public override bool RequirementMet(ConfiguredContract contract)
{
// Get the count of finished contracts
int finished = 0;
double lastFinished = 0.0;
// Finished contracts - Contract Configurator style
if (ccType != null)
{
IEnumerable<ConfiguredContract> completedContract = ConfiguredContract.CompletedContracts.
Where(c => c.contractType != null && c.contractType.name.Equals(ccType));
finished = completedContract.Count();
if (finished > 0)
{
lastFinished = completedContract.OrderByDescending<ConfiguredContract, double>(c => c.DateFinished).First().DateFinished;
}
}
// Finished contracts - stock style
else if (contractClass != null)
{
// Call the GetCompletedContracts with our type, and get the count
Contract[] completedContract = (Contract[])typeof(ContractSystem).GetMethod("GetCompletedContracts").MakeGenericMethod(contractClass).Invoke(ContractSystem.Instance, new object[] { null });
finished = completedContract.Count();
if (finished > 0)
{
lastFinished = completedContract.OrderByDescending<Contract, double>(c => c.DateFinished).First().DateFinished;
}
}
// Check cooldown
if (cooldownDuration.Value > 0.0 && finished > 0 && lastFinished + cooldownDuration.Value > Planetarium.GetUniversalTime())
{
LoggingUtil.LogDebug(this, "Returning false due to cooldown for {0}", contractType.name);
return false;
}
// Return based on the min/max counts configured
return (finished >= minCount) && (finished <= maxCount);
}
protected override string RequirementText()
{
string title = StringBuilderCache.Format("<color=#{0}>{1}</color>", MissionControlUI.RequirementHighlightColor, ContractTitle());
if (cooldownDuration.Value > 0.0)
{
return Localizer.Format(invertRequirement ? "#cc.req.CompleteContract.cooldown.x" : "#cc.req.CompleteContract.cooldown", title, cooldownDuration.ToString());
}
else
{
return Localizer.Format(invertRequirement ? "#cc.req.CompleteContract.x" : "#cc.req.CompleteContract", title);
}
}
}
}