Ver código fonte

Add MapSeed function

This function takes the ID of a seed and builds a complete map for that
seed.
ApisNecros 1 ano atrás
pai
commit
eb5928e7bf
1 arquivos alterados com 56 adições e 1 exclusões
  1. 56 1
      5/5_1.ts

+ 56 - 1
5/5_1.ts

@@ -136,10 +136,46 @@ function ParseSourceDestinationRange(lineNumber: number, almanac: string[]): Map
  * @param haystack The list to find the ID in
  * @returns {IdentifiableObject|undefined} The object by that ID if found, or undefined
  */
-function FindObjectByID(needle: number, haystack: IdentifiableObject[]): IdentifiableObject|undefined {
+function FindObjectByID(needle: number, haystack: IdentifiableObject[]|MappableObject[]): IdentifiableObject|MappableObject|undefined {
     return haystack.find((obj) => obj.ID == needle);
 }
 
+/**
+ * Map a Seed ID to all of its other attributes
+ *
+ * Given a seed ID, finds its soil type, fertilizer type, water type,
+ * light type, temperature type, humidity type, and location ID.
+ *
+ * @param {number} seedID 
+ * @param {RangeMaps} rangeMaps A parsed map of ranges
+ * @returns 
+ */
+function MapSeed(seedID: number, rangeMaps: RangeMaps): SeedMap|undefined {
+    // Make sure the seed with that ID exists before continueing
+    if(!FindObjectByID(seedID, rangeMaps.Seeds)) { return undefined; }
+    
+    // Initialize our seed map object
+    const seed: SeedMap = {
+        ID: seedID,
+        SoilID: 0,
+        FertilizerID: 0,
+        WaterID: 0,
+        LightID: 0,
+        TemperatureID: 0,
+        HumidityID: 0,
+        LocationID: 0,
+    };
+
+    seed.SoilID =        (FindObjectByID(seedID, rangeMaps.SeedToSoil) as MappableObject)?.PointsTo || seedID;
+    seed.FertilizerID =  (FindObjectByID(seed.SoilID, rangeMaps.SoilToFertilizer) as MappableObject)?.PointsTo || seed.SoilID;
+    seed.WaterID =       (FindObjectByID(seed.FertilizerID, rangeMaps.FertilizerToWater) as MappableObject)?.PointsTo || seed.FertilizerID;
+    seed.LightID =       (FindObjectByID(seed.WaterID, rangeMaps.WaterToLight) as MappableObject)?.PointsTo || seed.WaterID;
+    seed.TemperatureID = (FindObjectByID(seed.LightID, rangeMaps.LightToTemperature) as MappableObject)?.PointsTo|| seed.LightID;
+    seed.HumidityID =    (FindObjectByID(seed.TemperatureID, rangeMaps.TemperatureToHumidity) as MappableObject)?.PointsTo || seed.TemperatureID;
+    seed.LocationID =    (FindObjectByID(seed.HumidityID, rangeMaps.HumidityToLocation) as MappableObject)?.PointsTo || seed.HumidityID;
+
+    return seed;
+}
 
 interface IdentifiableObject {
     ID: number,
@@ -160,4 +196,23 @@ type RangeMaps = {
     LightToTemperature: MappableObject[],
     TemperatureToHumidity: MappableObject[],
     HumidityToLocation: MappableObject[],
+}
+
+type SeedMap = {
+    /** The ID of the seed */
+    ID: number,
+    /** The ID of the soil type the seed needs planted in */
+    SoilID: number,
+    /** The ID of the fertilizer type the soil needs */
+    FertilizerID: number,
+    /** The ID of the water type the fertilizer needs */
+    WaterID: number,
+    /** The ID of the light type the water needs */
+    LightID: number,
+    /** The ID of the temperature type the light needs */
+    TemperatureID: number,
+    /** The ID of the humidity type the temperature needs */
+    HumidityID: number,
+    /** The ID of the location the humidity needs */
+    LocationID: number,
 }