public void ProcessPacket(StatisticUpdateDataPacket packet, NebulaConnection conn) { StatisticalSnapShot snapshot; using (StatisticsManager.IsIncomingRequest.On()) { using (BinaryUtils.Reader reader = new BinaryUtils.Reader(packet.StatisticsBinaryData)) { ref FactoryProductionStat[] productionStats = ref GameMain.statistics.production.factoryStatPool; int numOfSnapshots = reader.BinaryReader.ReadInt32(); for (int i = 0; i < numOfSnapshots; i++) { //Clear all current statistical data for (int a = 0; a < GameMain.statistics.production.factoryStatPool.Length; a++) { GameMain.statistics.production.factoryStatPool[a]?.ClearRegisters(); } //Load new snapshot snapshot = new StatisticalSnapShot(reader.BinaryReader); for (int factoryId = 0; factoryId < snapshot.ProductionChangesPerFactory.Length; factoryId++) { if (productionStats[factoryId] == null) { productionStats[factoryId] = new FactoryProductionStat(); productionStats[factoryId].Init(); } for (int changeId = 0; changeId < snapshot.ProductionChangesPerFactory[factoryId].Count; changeId++) { if (snapshot.ProductionChangesPerFactory[factoryId][changeId].IsProduction) { productionStats[factoryId].productRegister[snapshot.ProductionChangesPerFactory[factoryId][changeId].ProductId] += snapshot.ProductionChangesPerFactory[factoryId][changeId].Amount; } else { productionStats[factoryId].consumeRegister[snapshot.ProductionChangesPerFactory[factoryId][changeId].ProductId] += snapshot.ProductionChangesPerFactory[factoryId][changeId].Amount; } } //Import power system statistics productionStats[factoryId].powerGenRegister = snapshot.PowerGenerationRegister[factoryId]; productionStats[factoryId].powerConRegister = snapshot.PowerConsumptionRegister[factoryId]; productionStats[factoryId].powerChaRegister = snapshot.PowerChargingRegister[factoryId]; productionStats[factoryId].powerDisRegister = snapshot.PowerDischargingRegister[factoryId]; //Import fake energy stored values StatisticsManager.PowerEnergyStoredData = snapshot.EnergyStored; //Import Research statistics productionStats[factoryId].hashRegister = snapshot.HashRegister[factoryId]; } GameMain.statistics.production.GameTick(snapshot.CapturedGameTick); } } }
public void CaptureStatisticalSnapshot() { if (!IsStatisticsNeeded || GameMain.statistics?.production == null) { return; } //Calculate number of active factories int factoryNum = 0; for (ushort i = 0; i < GameMain.statistics.production.factoryStatPool.Length; i++) { if (GameMain.statistics.production.factoryStatPool[i] != null) { factoryNum++; } else { break; } } StatisticalSnapShot snapshot = new StatisticalSnapShot(GameMain.gameTick, factoryNum); FactoryProductionStat stat; for (ushort i = 0; i < factoryNum; i++) { if (GameMain.statistics.production.factoryStatPool[i] != null) { stat = GameMain.statistics.production.factoryStatPool[i]; //Collect only those that really changed: for (ushort j = 0; j < stat.productRegister.Length; j++) { //Collect production statistics if (stat.productRegister[j] != 0) { snapshot.ProductionChangesPerFactory[i].Add(new ProductionChangeStruct(true, j, stat.productRegister[j])); } //Collect consumption statistics if (stat.consumeRegister[j] != 0) { snapshot.ProductionChangesPerFactory[i].Add(new ProductionChangeStruct(false, j, stat.consumeRegister[j])); } } //Collect Power statistics snapshot.PowerGenerationRegister[i] = stat.powerGenRegister; snapshot.PowerConsumptionRegister[i] = stat.powerConRegister; snapshot.PowerChargingRegister[i] = stat.powerChaRegister; snapshot.PowerDischargingRegister[i] = stat.powerDisRegister; //Collect Energy Stored Values for (int cursor = 0; cursor < GameMain.data.factories[i].powerSystem.netCursor; cursor++) { snapshot.EnergyStored[i] += GameMain.data.factories[i].powerSystem.netPool[cursor].energyStored; } //Collect Research statistics snapshot.HashRegister[i] = stat.hashRegister; } } StatisticalSnapShots.Add(snapshot); }
public override void ProcessPacket(StatisticUpdateDataPacket packet, NebulaConnection conn) { StatisticalSnapShot snapshot; using (Multiplayer.Session.Statistics.IsIncomingRequest.On()) { using (BinaryUtils.Reader reader = new BinaryUtils.Reader(packet.StatisticsBinaryData)) { bool itemChanged = false; ref FactoryProductionStat[] productionStats = ref GameMain.statistics.production.factoryStatPool; int numOfSnapshots = reader.BinaryReader.ReadInt32(); for (int i = 0; i < numOfSnapshots; i++) { //Load new snapshot snapshot = new StatisticalSnapShot(reader.BinaryReader); for (int factoryId = 0; factoryId < snapshot.ProductionChangesPerFactory.Length; factoryId++) { if (productionStats[factoryId] == null) { productionStats[factoryId] = new FactoryProductionStat(); productionStats[factoryId].Init(); } //Clear current statistical data productionStats[factoryId].PrepareTick(); for (int changeId = 0; changeId < snapshot.ProductionChangesPerFactory[factoryId].Count; changeId++) { StatisticalSnapShot.ProductionChangeStruct productionChange = snapshot.ProductionChangesPerFactory[factoryId][changeId]; if (productionChange.IsProduction) { productionStats[factoryId].productRegister[productionChange.ProductId] += productionChange.Amount; } else { productionStats[factoryId].consumeRegister[productionChange.ProductId] += productionChange.Amount; } } //Import power system statistics productionStats[factoryId].powerGenRegister = snapshot.PowerGenerationRegister[factoryId]; productionStats[factoryId].powerConRegister = snapshot.PowerConsumptionRegister[factoryId]; productionStats[factoryId].powerChaRegister = snapshot.PowerChargingRegister[factoryId]; productionStats[factoryId].powerDisRegister = snapshot.PowerDischargingRegister[factoryId]; //Import fake energy stored values Multiplayer.Session.Statistics.PowerEnergyStoredData = snapshot.EnergyStored; //Import Research statistics productionStats[factoryId].hashRegister = snapshot.HashRegister[factoryId]; //Processs changed registers. FactoryProductionStat.AfterTick() is empty currently so we ignore it. productionStats[factoryId].GameTick(snapshot.CapturedGameTick); itemChanged |= productionStats[factoryId].itemChanged; } } //Trigger GameMain.statistics.production.onItemChange() event when itemChanged is true if (itemChanged) { UIRoot.instance.uiGame.statWindow.OnItemChange(); NebulaModel.Logger.Log.Debug("StatisticsUpdateProcessor: itemChanged"); } } }