internal double GetModifiedAmount(ResourceAmount startingAmount, ResourceCheckpoint lastCheckpoint) { Resource resource = startingAmount.Resource; double modifiedAmount = startingAmount.Count; foreach (ModifierDuration modifier in GetModifierDurations(resource.id)) { double percentageModifiedTime = 1d; if (modifier.ExpirationTimeUTC is DateTime expirationTime) { if (expirationTime < DateTime.UtcNow) { TimeSpan modifiedTime = expirationTime - lastCheckpoint.CheckpointTimeUTC; TimeSpan totalTime = DateTime.UtcNow - lastCheckpoint.CheckpointTimeUTC; percentageModifiedTime = modifiedTime.TotalSeconds / totalTime.TotalSeconds; } } double amountToBeModified = startingAmount.Count * percentageModifiedTime; modifiedAmount += amountToBeModified * modifier.Modifier.percentageIncrease; } return(modifiedAmount); }
public StoreSaveState Load(ResourceRegistry resourceRegistry, FactoryRegistry factoryRegistry, ModifierRegistry modiferRegistry) { string path = string.Format(@"{0}{1}", directoryPath, FILE_NAME); StoreSaveState storage = null; try { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { using (StreamReader reader = new StreamReader(fileStream)) { storage = JsonConvert.DeserializeObject <StoreSaveState>(reader.ReadToEnd()); } } } catch (FileNotFoundException) { throw new FileNotFoundException(string.Format("An existing save file could not be found to load at: {0}", path)); } ResourceCheckpoint checkpoint = new ResourceCheckpoint(storage.ResourceCheckpoint.CheckpointTimeUTC, storage.ResourceCheckpoint.ResourceAmounts, resourceRegistry); FactoryManager factoryManager = new FactoryManager(storage.FactoryManager.FactoryAmounts, factoryRegistry); ModifierManager modifierManager = new ModifierManager(storage.ModiferManager.ModifierDurations, modiferRegistry); return(new StoreSaveState() { ResourceCheckpoint = checkpoint, FactoryManager = factoryManager, ModiferManager = modifierManager }); }
public Dictionary <Guid, double> Produce(ResourceCheckpoint lastCheckpoint, Dictionary <Guid, double> resourceTotals, ModifierManager modifierManager) { for (int i = 0; i < Count; i++) { resourceTotals = Factory.Produce(lastCheckpoint, resourceTotals, modifierManager); } return(resourceTotals); }
public Dictionary <Guid, double> Produce(ResourceCheckpoint lastCheckpoint, Dictionary <Guid, double> resourceTotals, ModifierManager modifierManager) { foreach (FactoryAmount factoryAmount in FactoryAmounts) { resourceTotals = factoryAmount.Produce(lastCheckpoint, resourceTotals, modifierManager); } return(resourceTotals); }
public Modifier AddModifier(Modifier modifier) { ResourceCheckpoint checkpoint = new ResourceCheckpoint(GetResourceAmounts()); Modifier addedModifier = ModifierManager.AddModifier(modifier); UpdateCheckpoint(checkpoint); return(addedModifier); }
internal Dictionary <Guid, double> Produce(ResourceCheckpoint lastCheckpoint, Dictionary <Guid, double> resourceTotals, ModifierManager modifierManager) { DateTime checkpointTimeUTC = lastCheckpoint.CheckpointTimeUTC; double operationalSeconds = double.MaxValue; foreach (Consumer consumer in consumers) { Resource resource = consumer.resource; resourceTotals.TryGetValue(resource.id, out double resourceCount); double potentialConsumption = consumer.GetConsumption(checkpointTimeUTC); double consumption = Math.Min(potentialConsumption, resourceCount); double consumerOperationalSeconds = consumption / consumer.ConsumptionPerSecond; operationalSeconds = Math.Min(operationalSeconds, consumerOperationalSeconds); } //Shortcircuit if not operational for any amount of time. if (operationalSeconds == 0) { return(resourceTotals); } foreach (Consumer consumer in consumers) { double consumption = operationalSeconds * consumer.ConsumptionPerSecond; Resource resource = consumer.resource; resourceTotals.TryGetValue(resource.id, out double resourceCount); if (resourceTotals.ContainsKey(resource.id)) { resourceTotals[resource.id] = resourceCount - consumption; } } foreach (Provider provider in providers) { double provision; if (consumers.Count == 0) { provision = provider.GetProvision(checkpointTimeUTC); } else { provision = operationalSeconds * provider.ProvisionPerSecond; } double modifedProvision = modifierManager.GetModifiedAmount(new ResourceAmount(provider.resource, provision), lastCheckpoint); Resource resource = provider.resource; resourceTotals.TryGetValue(resource.id, out double resourceCount); if (resourceTotals.ContainsKey(resource.id)) { resourceTotals[resource.id] = resourceCount + modifedProvision; } else { resourceTotals.Add(resource.id, resourceCount + modifedProvision); } } return(resourceTotals); }
public void InitializeCheckpoint(List <ResourceAmount> startingResources) { ResourceCheckpoint = new ResourceCheckpoint(startingResources); }
private void UpdateCheckpoint(ResourceCheckpoint checkpoint) { ResourceCheckpoint = checkpoint; ModifierManager.RemoveExpiredModifiers(); Save(); }