Пример #1
0
        public static int?GetIntegerFieldValue <T>(T o, string fieldName)
        {
            int?      finalValue = null;
            FieldInfo fieldInfo  = ReflectionUtil.FindField(o, fieldName);

            if (fieldInfo != null)
            {
                if (fieldInfo.FieldType == typeof(UInt32))
                {
                    finalValue = (int)ReflectionUtil.GetFieldValue <UInt32>(fieldInfo, o);
                }
                else if (fieldInfo.FieldType == typeof(Int32))
                {
                    finalValue = (int)ReflectionUtil.GetFieldValue <Int32>(fieldInfo, o);
                }
                else if (fieldInfo.FieldType == typeof(UInt16))
                {
                    finalValue = (int)ReflectionUtil.GetFieldValue <UInt16>(fieldInfo, o);
                }
                else if (fieldInfo.FieldType == typeof(Byte))
                {
                    finalValue = (int)ReflectionUtil.GetFieldValue <Byte>(fieldInfo, o);
                }
                else
                {
                    CityInfoRequestHandler.LogMessages("Not supported value type", fieldInfo.FieldType.ToString(), "for", fieldName);
                }
            }
            else
            {
                CityInfoRequestHandler.LogMessages(string.Format("Field {0} not found in object type {1}", fieldName, o.GetType().ToString()));
            }
            return(finalValue);
        }
        public VehiclesInfo(int?districId = null)
        {
            try
            {
                VehicleManager  vm = Singleton <VehicleManager> .instance;
                BuildingManager bm = Singleton <BuildingManager> .instance;

                var Cars = GetValidVehicles(VehicleInfo.VehicleType.Car, districId);
                if (Cars == null)
                {
                    CityInfoRequestHandler.LogMessages("Car List Empty!");
                    return;
                }
                CityInfoRequestHandler.LogMessages(string.Format("Car List with {0} items", Cars.Count()));
                foreach (var myv in Cars)
                {
                    UpdateGeneral(myv);

                    Building b = bm.m_buildings.m_buffer[myv.m_sourceBuilding];
                    bool     buildingisvalid = checkbuilding(b, myv.m_sourceBuilding);
                    uint     transfersize    = myv.m_transferSize;
                    var      reason          = (TransferManager.TransferReason)myv.m_transferType;

                    if (!UpdateCityMaintenance(myv, buildingisvalid, transfersize))
                    {
                        bool importing = (myv.m_flags & Vehicle.Flags.Importing) == Vehicle.Flags.Importing;
                        bool exporting = (myv.m_flags & Vehicle.Flags.Exporting) == Vehicle.Flags.Exporting;
                        switch (myv.Info.m_class.m_service)
                        {
                            #region public transports
                        case ItemClass.Service.PublicTransport:
                            if (myv.Info.m_class.m_subService == ItemClass.SubService.PublicTransportBus)
                            {
                                CityServicesVehicles["PublicTransport"]["bus"]++;
                            }
                            break;

                            #endregion
                        case ItemClass.Service.Commercial:
                            UpdateService(myv, "Commercial", transfersize);
                            break;

                        case ItemClass.Service.Office:
                            UpdateService(myv, "Office", transfersize);
                            break;

                        case ItemClass.Service.Industrial:
                            UpdateService(myv, "Industrial", transfersize);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CityInfoRequestHandler.LogMessages("Error while building vehicles info", ex.Message, ex.StackTrace);
            }
        }
Пример #3
0
        private static DistrictInfo.DoubleServiceData DistrictToDoubleServiceData <TServiceObj>(this District district, string serviceTypeName, string serviceObjectFieldName, string serviceDataCurrent, string serviceDataTotal)
        {
            //CityInfoRequestHandler.LogMessages(serviceTypeName, serviceObjectFieldName, serviceDataCurrent, serviceDataTotal != null ? serviceDataTotal : "null", typeof(TServiceObj).ToString());
            var serviceDataObject = new DistrictInfo.DoubleServiceData();

            serviceDataObject.Name = serviceTypeName;

            try
            {
                FieldInfo serviceDataFieldInfo = ReflectionUtil.FindField(district, serviceObjectFieldName);
                if (serviceDataFieldInfo == null)
                {
                    CityInfoRequestHandler.LogMessages("Primary field not found, looking for", serviceObjectFieldName);
                    return(serviceDataObject);
                }

                if (serviceDataFieldInfo.FieldType != typeof(TServiceObj))
                {
                    CityInfoRequestHandler.LogMessages("UnexpectedObjectType", serviceDataFieldInfo.FieldType.ToString(), "expecting", typeof(TServiceObj).ToString());
                    return(serviceDataObject);
                }
                var districtServiceData = ReflectionUtil.GetFieldValue <TServiceObj>(serviceDataFieldInfo, district);

                var currentValue = ReflectionUtil.GetIntegerFieldValue(districtServiceData, serviceDataCurrent);
                if (!currentValue.HasValue)
                {
                    CityInfoRequestHandler.LogMessages("Second field not found, looking for", serviceDataCurrent, "in", serviceObjectFieldName);
                    return(serviceDataObject);
                }
                else
                {
                    serviceDataObject.Current = currentValue.Value;
                }

                if (serviceDataTotal != null)
                {
                    var totalValue = ReflectionUtil.GetIntegerFieldValue(districtServiceData, serviceDataTotal);
                    if (!totalValue.HasValue)
                    {
                        CityInfoRequestHandler.LogMessages("Second field not found, looking for", serviceDataTotal, "in", serviceObjectFieldName);
                        return(serviceDataObject);
                    }
                    else
                    {
                        serviceDataObject.Available = totalValue.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                CityInfoRequestHandler.LogMessages(ex.Message);
            }
            return(serviceDataObject);
        }
 public CityInfoRequestHandler(IWebServer server)
     : base(server, new Guid("2ABCA7D3-E71C-40A5-BD32-50C08D5D4855"), "Slurp City Info", "MrSlurp", 100, "/SlurpUI/CityInfo")
 {
     _instance = this;
 }
        public static DistrictInfo GetDistrictInfo(int districtID)
        {
            var districtManager = Singleton <DistrictManager> .instance;
            var district        = GetDistrict(districtID);

            if (!district.IsValid())
            {
                return(null);
            }

            String districtName = String.Empty;

            if (districtID == 0)
            {
                // The district with ID 0 is always the global district.
                // It receives an auto-generated name by default, but the game always displays the city name instead.
                districtName = "City";
            }
            else
            {
                districtName = districtManager.GetDistrictName(districtID);
            }

            var pollution = Math.Round((district.m_groundData.m_finalPollution / (Double)byte.MaxValue), 2);

            #region data model familly to game service/zone type list & dictionary
            //
            // warning these strings match object field names in game models and should not be changed
            // see specified class to understand
            // in DistrictPrivateData
            List <string> ServiceZoneTypes = new List <string> {
                "Residential", "Commercial", "Industrial", "Office", "Player"
            };
            List <string> NoPlayerZoneTypes = new List <string> {
                "Residential", "Commercial", "Industrial", "Office"
            };
            List <string> JobServiceZoneTypes = new List <string> {
                "Commercial", "Industrial", "Office", "Player"
            };
            List <string> ImportExportTypes = new List <string> {
                "Agricultural", "Forestry", "Goods", "Oil", "Ore"
            };

            // in DistrictProductionData
            Dictionary <string, string> ProductionsTypes = new Dictionary <string, string> {
                { "Electricity", "ElectricityCapacity" },
                { "Water", "WaterCapacity" },
                { "Sewage", "SewageCapacity" },
                { "GarbageA", "GarbageCapacity" },
                { "GarbageC", "GarbageAmount" },
                { "Incineration", "IncinerationCapacity" },
                { "Cremate", "CremateCapacity" },
                { "DeadA", "DeadAmount" },
                { "DeadC", "DeadCapacity" },
                { "Heal", "HealCapacity" },
                { "LowEducation", "Education1Capacity" },
                { "MediumEducation", "Education2Capacity" },
                { "HighEducation", "Education3Capacity" },
            };
            // in DistrictConsumptionData
            // NOTE : consumptions are stored in each kind of service, cool we can create consumption distributions pie
            Dictionary <string, string> ConsumptionsTypes = new Dictionary <string, string> {
                { "Dead", "DeadCount" },
                { "Sick", "SickCount" },
                { "Electricity", "ElectricityConsumption" },
                { "Water", "WaterConsumption" },
                { "Sewage", "SewageAccumulation" },
                { "Garbage", "GarbageAccumulation" },
                { "Income", "IncomeAccumulation" },
                { "WaterPollution", "WaterPollution" },
                { "Building", "BuildingCount" },
                { "GarbagePiles", "GarbagePiles" },
                { "ImportAmount", "ImportAmount" },
            };

            Dictionary <string, string> PrivateDataTypes = new Dictionary <string, string> {
                { "Abandoned", "AbandonedCount" },
                { "BuildingArea", "BuildingArea" },
                //{"BuildingCount","BuildingCount"}, same data as in DistrictConsumptionData
                { "Burned", "BurnedCount" },
                { "EmptyCount", "EmptyCount" },
                //{"Happiness","Happiness"}, do in diffferent way to be easyly able to display hapiness by type
                //{"CrimeRate","CrimeRate"},
                //{"Health","Health"},

                /* to re add if a day I understand usage
                 * {"Level","Level"},
                 * {"Level1","Level1"},
                 * {"Level2","Level2"},
                 * {"Level3","Level3"},
                 * {"Level4","Level4"},
                 * {"Level5","Level5"},*/
            };

            // in DistrictEducationData
            Dictionary <string, string> EducatedLevels = new Dictionary <string, string> {
                { "No", "educated0" },
                { "Low", "educated1" },
                { "Medium", "educated2" },
                { "High", "educated3" }
            };
            Dictionary <string, string> EducatedDataTypes = new Dictionary <string, string> {
                { "Total", "Count" },
                { "EligibleWorkers", "EligibleWorkers" },
                { "Homeless", "Homeless" },
                { "Unemployed", "Unemployed" },
            };

            // in DistrictAgeData
            Dictionary <string, string> GraduationTypes = new Dictionary <string, string>
            {
                { "LowEducation", "education1" },
                { "MediumEducation", "education2" },
                { "HighEducation", "education3" },
            };
            Dictionary <string, string> StudentTypes = new Dictionary <string, string>
            {
                { "LowStudent", "student1" },
                { "MediumStudent", "student2" },
                { "HighStudent", "student3" },
            };
            Dictionary <string, string> BirthDeathTypes = new Dictionary <string, string>
            {
                { "Births", "birth" },
                { "Deaths", "death" }
            };

            Dictionary <string, string> PopulationType = new Dictionary <string, string> {
                { "Childs", "child" },
                { "Teens", "teen" },
                { "Youngs", "young" },
                { "Adults", "adult" },
                { "Seniors", "senior" },
            };
            #endregion
            var model = new DistrictInfo
            {
                DistrictID   = districtID,
                DistrictName = districtName,
                #region service data generation
                Population = new DistrictServiceData {
                    Categories = new List <ServiceData>(PopulationType.Keys.Select(x => district.GetAgeServiceData(PopulationType[x], x))),
                },
                Happiness = new DistrictServiceData {
                    Categories = new List <ServiceData>(NoPlayerZoneTypes.Select(y => district.GetPrivateServiceData(y, "Happiness")))
                },
                Crime = new DistrictServiceData {
                    Categories = new List <ServiceData>(NoPlayerZoneTypes.Select(y => district.GetPrivateServiceData(y, "CrimeRate")))
                },
                Health = new DistrictServiceData {
                    Categories = new List <ServiceData>(NoPlayerZoneTypes.Select(y => district.GetPrivateServiceData(y, "Health")))
                },
                // warning double lambda, but pretty magical effect
                Privates = new Dictionary <string, DistrictServiceData>(
                    PrivateDataTypes.Keys.ToDictionary(x => x,
                                                       x => new DistrictServiceData {
                    Categories = new List <ServiceData>(ServiceZoneTypes.Select(y => district.GetPrivateServiceData(y, PrivateDataTypes[x])))
                }
                                                       )
                    ),
                Households = new DistrictDoubleServiceData {
                    Categories = new List <DoubleServiceData> {
                        district.GetCountAndAliveServiceData("residential"),
                    },
                },
                Jobs = new DistrictDoubleServiceData {
                    Categories = new List <DoubleServiceData>(JobServiceZoneTypes.Select(x => district.GetCountAndAliveServiceData(x)))
                },
                ImportExport = new Dictionary <string, DistrictServiceData> {
                    { "Import", new DistrictServiceData {
                          Categories = new List <ServiceData>(ImportExportTypes.Select(x => district.GetImportExportServiceData("import", x)))
                      } },
                    { "Export", new DistrictServiceData {
                          Categories = new List <ServiceData>(ImportExportTypes.Select(x => district.GetImportExportServiceData("export", x)))
                      } },
                },
                Productions = new Dictionary <string, DistrictServiceData>(
                    ProductionsTypes.Keys.ToDictionary(x => x,
                                                       x => new DistrictServiceData {
                    Categories = new List <ServiceData> {
                        district.GetProductionServiceData(x, ProductionsTypes[x])
                    }
                }
                                                       )
                    ),
                // warning double lambda, but pretty magical effect
                Consumptions = new Dictionary <string, DistrictServiceData>(
                    ConsumptionsTypes.Keys.ToDictionary(x => x,
                                                        x => new DistrictServiceData {
                    Categories = new List <ServiceData>(ServiceZoneTypes.Select(y => district.GetConsumptionServiceData(y, ConsumptionsTypes[x])))
                }
                                                        )
                    ),
                // warning double lambda, but pretty magical effect
                Educated = new Dictionary <string, DistrictServiceData>(
                    EducatedDataTypes.Keys.ToDictionary(x => x,
                                                        x => new DistrictServiceData {
                    Categories = new List <ServiceData>(EducatedLevels.Keys.Select(y => district.GetEducatedServiceData(EducatedLevels[y], EducatedDataTypes[x], y)))
                }
                                                        )
                    ),
                BirthDeath = new DistrictServiceData {
                    Categories = new List <ServiceData>(BirthDeathTypes.Keys.Select(x => district.GetAgeServiceData(BirthDeathTypes[x], x))),
                },
                Graduations = new Dictionary <string, DistrictServiceData>(
                    GraduationTypes.Keys.ToDictionary(x => x,
                                                      x => new DistrictServiceData {
                    Categories = new List <ServiceData> {
                        district.GetAgeServiceData(GraduationTypes[x])
                    }
                }
                                                      )
                    ),
                Students = new Dictionary <string, DistrictServiceData>(
                    StudentTypes.Keys.ToDictionary(x => x,
                                                   x => new DistrictServiceData {
                    Categories = new List <ServiceData> {
                        district.GetAgeServiceData(StudentTypes[x])
                    }
                }
                                                   )
                    ),

                #endregion
                AverageLandValue    = district.GetLandValue(),
                Pollution           = pollution,
                WeeklyTouristVisits = (int)district.m_tourist1Data.m_averageCount + (int)district.m_tourist2Data.m_averageCount + (int)district.m_tourist3Data.m_averageCount,
                Policies            = GetPolicies().ToArray(),
            };
            if (districtID != 0)
            {
                CityInfoRequestHandler.LogMessages("Building vehicles for", districtID.ToString());
                model.Vehicles = new VehiclesInfo(districtID);
            }
            else
            {
                CityInfoRequestHandler.LogMessages("Building vehicles for city");
                model.Vehicles = new VehiclesInfo();
            }
            return(model);
        }