using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")] [assembly: AssemblyCompany("endo5501")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("ゲーム非依存の電力集計・整形ロジック(ユニットテスト対象)")] [assembly: AssemblyFileVersion("0.1.0.0")] [assembly: AssemblyInformationalVersion("0.1.0")] [assembly: AssemblyProduct("PowerMeter.Core")] [assembly: AssemblyTitle("PowerMeter.Core")] [assembly: AssemblyVersion("0.1.0.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Embedded] internal sealed class IsReadOnlyAttribute : Attribute { } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace PowerMeter.Core { public readonly struct NetworkSample { public int PlanetId { get; } public int StarId { get; } public long EnergyCapacity { get; } public long EnergyRequired { get; } public long EnergyServed { get; } public long EnergyCharge { get; } public long EnergyDischarge { get; } public long EnergyStored { get; } public NetworkSample(int planetId, int starId, long energyCapacity, long energyRequired, long energyServed, long energyCharge = 0L, long energyDischarge = 0L, long energyStored = 0L) { PlanetId = planetId; StarId = starId; EnergyCapacity = energyCapacity; EnergyRequired = energyRequired; EnergyServed = energyServed; EnergyCharge = energyCharge; EnergyDischarge = energyDischarge; EnergyStored = energyStored; } } public static class PowerAggregator { public static PowerSnapshot Aggregate(IEnumerable samples, PowerScope scope, int planetId, int starId, int tickPerSecond) { if (samples == null) { return PowerSnapshot.Invalid; } int num = 0; long num2 = 0L; long num3 = 0L; long num4 = 0L; long num5 = 0L; long num6 = 0L; long num7 = 0L; foreach (NetworkSample sample in samples) { if (IsInScope(sample, scope, planetId, starId)) { num++; num2 += sample.EnergyCapacity; num3 += sample.EnergyRequired; num4 += sample.EnergyServed; num5 += sample.EnergyCharge; num6 += sample.EnergyDischarge; num7 += sample.EnergyStored; } } if (num == 0) { return PowerSnapshot.Invalid; } double num8 = num4 + num5 - num6; if (num8 < 0.0) { num8 = 0.0; } double satisfactionRatio = ((num3 > 0) ? ((double)num4 / (double)num3) : 1.0); double utilizationRatio = ((num2 > 0) ? (num8 / (double)num2) : 0.0); return new PowerSnapshot(isValid: true, num, (double)num2 * (double)tickPerSecond, num8 * (double)tickPerSecond, (double)num3 * (double)tickPerSecond, (double)num4 * (double)tickPerSecond, (double)num5 * (double)tickPerSecond, (double)num6 * (double)tickPerSecond, satisfactionRatio, utilizationRatio, num7); } private static bool IsInScope(NetworkSample sample, PowerScope scope, int planetId, int starId) { return scope switch { PowerScope.Planet => sample.PlanetId == planetId, PowerScope.Star => sample.StarId == starId, _ => true, }; } } public static class PowerFormatter { private static readonly string[] WattUnits = new string[7] { "W", "kW", "MW", "GW", "TW", "PW", "EW" }; private static readonly string[] JouleUnits = new string[7] { "J", "kJ", "MJ", "GJ", "TJ", "PJ", "EJ" }; public static string FormatWatt(double watt) { return Format(watt, WattUnits, alwaysSigned: false); } public static string FormatSignedWatt(double watt) { return Format(watt, WattUnits, alwaysSigned: true); } public static string FormatJoule(double joule) { return Format(joule, JouleUnits, alwaysSigned: false); } public static string FormatPercent(double ratio) { if (double.IsNaN(ratio) || ratio <= 0.0) { return "0%"; } if (ratio >= 1.0) { return "100%"; } int num = (int)Math.Floor(ratio * 100.0); if (num > 99) { num = 99; } return num.ToString(CultureInfo.InvariantCulture) + "%"; } private static string Format(double value, string[] units, bool alwaysSigned) { if (double.IsNaN(value) || double.IsInfinity(value)) { return "- " + units[0]; } bool flag = value < 0.0; double num = Math.Abs(value); if (num < 1.0) { return "0 " + units[0]; } int num2 = 0; while (num >= 1000.0 && num2 < units.Length - 1) { num /= 1000.0; num2++; } int digits = DecimalsFor(num); double num3 = Math.Round(num, digits, MidpointRounding.AwayFromZero); if (num3 >= 1000.0 && num2 < units.Length - 1) { num2++; num3 /= 1000.0; digits = DecimalsFor(num3); num3 = Math.Round(num3, digits, MidpointRounding.AwayFromZero); } string text = ((!flag) ? (alwaysSigned ? "+" : string.Empty) : "-"); return text + num3.ToString("F" + digits.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture) + " " + units[num2]; } private static int DecimalsFor(double mantissa) { if (mantissa >= 100.0) { return 0; } if (!(mantissa >= 10.0)) { return 2; } return 1; } } public enum PowerScope { Planet, Star, Global } public readonly struct PowerSnapshot { public bool IsValid { get; } public int NetworkCount { get; } public double CapacityWatt { get; } public double GenerationWatt { get; } public double ConsumptionWatt { get; } public double ServedWatt { get; } public double ChargeWatt { get; } public double DischargeWatt { get; } public double NetChargeWatt => ChargeWatt - DischargeWatt; public double SatisfactionRatio { get; } public double UtilizationRatio { get; } public double StoredJoule { get; } public static PowerSnapshot Invalid => default(PowerSnapshot); public PowerSnapshot(bool isValid, int networkCount, double capacityWatt, double generationWatt, double consumptionWatt, double servedWatt, double chargeWatt, double dischargeWatt, double satisfactionRatio, double utilizationRatio, double storedJoule) { IsValid = isValid; NetworkCount = networkCount; CapacityWatt = capacityWatt; GenerationWatt = generationWatt; ConsumptionWatt = consumptionWatt; ServedWatt = servedWatt; ChargeWatt = chargeWatt; DischargeWatt = dischargeWatt; SatisfactionRatio = satisfactionRatio; UtilizationRatio = utilizationRatio; StoredJoule = storedJoule; } } }