Exemple #1
0
        public Dictionary <int, string> OrgColorLookup; // ugly way to transfer data to delegate

        public string UPUptakeLookupReplacer(Match match)
        {
            string resultColor = "#000000";
            string id          = match.Groups["id"].Value;

            if (!id.StartsWith("text"))
            {
                Geography    geo = Geography.FromOfficialDesignation(1, GeographyLevel.Municipality, id);
                Organization org = Organizations.GetMostLocalOrganization(geo.Identity, Organization.UPSEid);

                resultColor = OrgColorLookup[org.Identity];
            }

            return(match.Groups["start"].Value + resultColor + match.Groups["middle"].Value + id +
                   match.Groups["end"].Value);
        }
Exemple #2
0
        public string SwedishCityMapOrgStrengthLookupReplacer(Match match)
        {
            // Five groups: "color" and "id" are the keys, "start", "middle" and "end" are what to paste in between.

            Organization org = Organization.FromIdentity(1); // Replace later for more generic

            string resultColorString = "#000000";
            string id = match.Groups["id"].Value;

            // If not constructed for this instance, construct the volunteer cache

            if (this.volunteerCache == null)
            {
                this.volunteerCache = new Dictionary <int, bool>();

                Volunteers volunteers = Volunteers.GetOpen();

                foreach (Volunteer volunteer in volunteers)
                {
                    volunteerCache[volunteer.Geography.Identity] = true;
                }
            }


            try
            {
                Geography geo = Geography.FromOfficialDesignation(1, GeographyLevel.Municipality, id);

                int circuitLeadCount   = 0;
                int cityLeadCount      = 0;
                int cityVolunteerCount = 0;

                RoleLookup lookup = RoleLookup.FromGeographyAndOrganization(geo.Identity, 1);
                if (lookup[RoleType.LocalLead].Count > 0)
                {
                    cityLeadCount = 1;

                    if (lookup[RoleType.LocalDeputy].Count > 0)
                    {
                        cityLeadCount = 2;
                    }
                }

                if (volunteerCache.ContainsKey(geo.Identity))
                {
                    cityVolunteerCount = 1;
                }

                // Move up to circuit level

                while (!(geo.AtLevel(GeographyLevel.ElectoralCircuit)))
                {
                    geo = geo.Parent;
                }

                lookup = RoleLookup.FromGeographyAndOrganization(geo.Identity, 1);
                if (lookup[RoleType.LocalLead].Count > 0)
                {
                    circuitLeadCount = 1;

                    if (lookup[RoleType.LocalDeputy].Count > 0)
                    {
                        circuitLeadCount = 2;
                    }
                }

                float saturation = 1.0f;
                float brightness = 0.15f + 0.2f * circuitLeadCount;
                float hue        = cityLeadCount * 60; // red, yellow, green hues at 0°, 60°, 120°

                if (cityLeadCount < 2 && cityVolunteerCount > 0)
                {
                    hue += 30; // There are volunteers? Place at orange (for none) or yellow-green (for one).
                }

                Color color = ColorFromAhsb(255, hue, saturation, brightness);

                resultColorString = String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
            }
            catch (Exception)
            {
            }

            /*
             * try
             * {
             *  if (lookup.ContainsKey(id))
             *  {
             *      geoId = lookup[id];
             *  }
             *  else
             *  {
             *      geoId = Int32.Parse(id);
             *  }
             *
             *  Geography geo = Geography.FromIdentity(geoId);
             *
             *  RoleLookup officers = RoleLookup.FromGeographyAndOrganization(geo.Identity, org.Identity);
             *
             *  bool hasLead = officers[RoleType.LocalLead].Count > 0;
             *  bool hasSecond = officers[RoleType.LocalDeputy].Count > 0;
             *
             *  Geographies geoTree = geo.GetTree();
             *
             *  int cities = 0;
             *  int citiesWithLead = 0;
             *
             *  foreach (Geography localGeo in geoTree)
             *  {
             *      if (localGeo.Identity == geo.Identity)
             *      {
             *          continue;
             *      }
             *
             *      if (localGeo.Name.EndsWith("kommun"))
             *      {
             *          cities++;
             *
             *          officers = RoleLookup.FromGeographyAndOrganization(localGeo.Identity, org.Identity);
             *
             *          if (officers[RoleType.LocalLead].Count > 0)
             *          {
             *              citiesWithLead++;
             *          }
             *      }
             *  }
             *
             *  int cityLeadPercent = 100;
             *
             *  if (cities > 0)
             *  {
             *      cityLeadPercent = citiesWithLead * 100 / cities;
             *  }
             *
             *  // Determine color
             *
             *  Color color = Color.Red;
             *
             *  if (!hasLead)
             *  {
             *      color = Color.Red;
             *  }
             *  else if (cityLeadPercent > 80 && hasSecond)
             *  {
             *      color = Color.Green;
             *  }
             *  else
             *  {
             *      // Find a hue between Orange and Light Green. Say, between 30 and 120.
             *
             *      cityLeadPercent += 30;
             *      if (cityLeadPercent > 120)
             *      {
             *          cityLeadPercent = 120;
             *      }
             *
             *      color = ColorFromAhsb(100, cityLeadPercent, 1.0f, 0.5f);
             *  }
             *
             *  resultColorString = String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
             * }
             * catch (Exception)
             * {
             *  // Ignore - color will be black
             * }
             *
             */


            return(match.Groups["start"].Value + resultColorString + match.Groups["middle"].Value + id +
                   match.Groups["end"].Value);
        }