Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Fix current stage resource endpoints, add resourceNameList
Browse files Browse the repository at this point in the history
Fixed the current resource endpoints to work with 1.2+
Added an API endpoint to return the list of resources you can query
Make resource lookup case-insensitive
  • Loading branch information
Thomas Cannon committed Dec 5, 2016
1 parent 3b92fb7 commit fdf3e47
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Telemachus/src/DataLinkFormatters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class ActiveResourceListJSONFormatter : ResourceListJSONFormatter
{
public override object prepareForSerialization(object input)
{
return SumResources((List<Vessel.ActiveResource>)input,
return SumResources((List<SimplifiedResource>)input,
x => x.amount);
}
}
Expand All @@ -172,7 +172,7 @@ public class ActiveResourceTotalListJSONFormatter : ResourceListJSONFormatter
{
public override object prepareForSerialization(object input)
{
return SumResources((List<Vessel.ActiveResource>)input,
return SumResources((List<SimplifiedResource>)input,
x => x.maxAmount);
}
}
Expand Down
73 changes: 55 additions & 18 deletions Telemachus/src/DataLinkHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,19 @@ public ResourceDataLinkHandler(VesselChangeDetector vesselChangeDetector, Format
dataSources => { return getsResourceValues(dataSources); },
"r.resourceMax", "Max Resource Information [string resource type]",
formatters.MaxResourceList, APIEntry.UnitType.UNITLESS));
registerAPI(new APIEntry(
dataSources => {
List<String> names = new List<String>();
PartResourceDefinitionList resourceDefinitionList = PartResourceLibrary.Instance.resourceDefinitions;
foreach (PartResourceDefinition resourceDefinition in resourceDefinitionList)
{
names.Add(resourceDefinition.name);
}

return names;
},
"r.resourceNameList", "List of resource names",
formatters.StringArray, APIEntry.UnitType.UNITLESS));
}

#endregion
Expand All @@ -1980,7 +1993,7 @@ protected List<PartResource> getsResourceValues(DataSources datasources)
return resourceCache.get(datasources);
}

protected List<Vessel.ActiveResource> getsActiveResourceValues(DataSources datasources)
protected List<SimplifiedResource> getsActiveResourceValues(DataSources datasources)
{
activeResourceCache.vessel = datasources.vessel;
return activeResourceCache.get(datasources);
Expand Down Expand Up @@ -2026,7 +2039,7 @@ public Vessel vessel

public List<T> get(DataSources dataSources)
{
string ID = dataSources.args[0];
string ID = dataSources.args[0].ToLowerInvariant();
List<T> avail = null, ret = null;

lock (cacheLock)
Expand All @@ -2051,7 +2064,19 @@ public List<T> get(DataSources dataSources)
#endregion
}

public class ActiveResourceCache : ModuleCache<Vessel.ActiveResource>
public class SimplifiedResource
{
public double amount { get; set; }
public double maxAmount { get; set; }

public SimplifiedResource(double amount, double maxAmount)
{
this.amount = amount;
this.maxAmount = maxAmount;
}
}

public class ActiveResourceCache : ModuleCache<SimplifiedResource>
{
#region ModuleCache

Expand All @@ -2076,23 +2101,34 @@ protected override void refresh(Vessel vessel)
try
{
partModules.Clear();

foreach (Part part in vessel.parts)
HashSet<Part> activeParts = new HashSet<Part>();
foreach(Part part in vessel.GetActiveParts())
{
if (part.Resources.Count > 0)
if (part.inverseStage == vessel.currentStage)
{
foreach (Vessel.ActiveResource resource in vessel.GetActiveResources())
{
List<Vessel.ActiveResource> list = null;
if (list == null)
{
list = new List<Vessel.ActiveResource>();
partModules[resource.info.name] = list;
}
activeParts.Add(part);
activeParts.UnionWith(part.crossfeedPartSet.GetParts());
}
}

list.Add(resource);
}
PartSet activePartSet = new PartSet(activeParts);
PartResourceDefinitionList resourceDefinitionList = PartResourceLibrary.Instance.resourceDefinitions;

foreach(PartResourceDefinition resourceDefinition in resourceDefinitionList)
{
String key = resourceDefinition.name.ToString().ToLowerInvariant();
double amount = 0;
double maxAmount = 0;
bool pulling = true;

activePartSet.GetConnectedResourceTotals(resourceDefinition.id, out amount, out maxAmount, pulling);

if(!partModules.ContainsKey(key)){
partModules[key] = new List<SimplifiedResource>();
}

partModules[key].Add(new SimplifiedResource(amount, maxAmount));
PluginLogger.debug("SIZE OF " + key + " " + partModules[key].Count + " " + amount );
}
}
catch (Exception e)
Expand Down Expand Up @@ -2137,12 +2173,13 @@ protected override void refresh(Vessel vessel)

foreach (PartResource partResource in part.Resources)
{
String key = partResource.resourceName.ToLowerInvariant();
List<PartResource> list = null;
partModules.TryGetValue(partResource.resourceName, out list);
partModules.TryGetValue(key, out list);
if (list == null)
{
list = new List<PartResource>();
partModules[partResource.resourceName] = list;
partModules[key] = list;

}

Expand Down

0 comments on commit fdf3e47

Please sign in to comment.