protected void Produce(ICargoContainer cargoContainer) { foreach (var produce in Produces) { Produce(produce, cargoContainer); } }
/// <summary> /// Binds the tree nodes for a planet's cargo or a ship's cargo. /// </summary> /// <param name="cc"></param> /// <param name="ccnode"></param> private void BindUnitsIn(ICargoContainer cc, TreeNode ccnode) { foreach (var ur in cc.Cargo.Units.GroupBy(u => u.Design.Role)) { // unit roles var urnode = ccnode.AddItemWithImage(ur.Count() + "x " + ur.Key, ur.Key, ur.Majority(u => u.Icon)); foreach (var ud in ur.GroupBy(u => u.Design)) { // unit designs var udnode = urnode.AddItemWithImage(ud.Count() + "x " + ud.Key.Name, ud.Key, ud.Key.Icon); foreach (var u in ud) { // units var unode = udnode.AddItemWithImage(u.Name, u, u.Icon); var orders = Empire.Current.Commands.OfType <AddOrderCommand>() .Where(x => x.Order is RecycleFacilityOrCargoOrder o && x.Executor == cc && o.Target == u) .Select(x => ((RecycleFacilityOrCargoOrder)x.Order).Behavior.Verb); if (orders.Any()) { unode.Text += $" ({string.Join(", ", orders)})"; var n = unode.Parent; while (n != null) { if (!n.Text.EndsWith("*")) { n.Text += " *"; } n = n.Parent; } } } } } }
protected virtual void ProcessCargos(ICargoContainer entity) { foreach (var cargo in entity.Cargos.Collection) { ProcessorsProvider.Process(cargo); } }
private void Produce(IProduce produce, ICargoContainer container) { if (produce == null) { throw new ArgumentNullException(nameof(produce)); } if (!produce.From.Any() && !produce.To.Any()) { return; } //проверяем, что ингридиентов достаточно, чтобы приготовить foreach (var cargoFrom in produce.From) { var fromCargo = container.Cargos.Collection.FirstOrDefault(c => c.Type == cargoFrom.Type); if (fromCargo == null) { return; } if (fromCargo.Count < cargoFrom.Count * produce.Speed) { return; } } //проверяем, что место достаточно, чтобы хранить ингридиенты foreach (var cargoTo in produce.To) { var count = cargoTo.Count * produce.Speed; if (count > CanAddCargoMore(cargoTo.Type, container)) { return; } } //тратим ингридиенты foreach (var cargoFrom in produce.From) { var fromCargo = container.Cargos.Collection.First(c => c.Type == cargoFrom.Type); fromCargo.Count -= cargoFrom.Count * produce.Speed; } //создаем ингридиенты foreach (var cargoTo in produce.To) { var cargoToAdd = NewInstanceFactory.GetNewInstance <ICargo>(cargoTo.Type); cargoToAdd.Count = cargoTo.Count * produce.Speed; AddCargo(cargoToAdd, container); } }
public virtual decimal CanAddCargoMore(string type, ICargoContainer container) { if (!CanStoreCargos.TryGetValue(type, out var canStoreAtAll)) { return(0); } var count = GetFullCargoCount(type, container); var result = canStoreAtAll - count; return(result > 0 ? result : 0); }
public bool AddCargo(ICargo cargo, ICargoContainer container) { if (CanAddCargoMore(cargo.Type, container) < cargo.Count) { return(false); } var existedCargo = container.Cargos.Collection.FirstOrDefault(c => c.Type == cargo.Type); if (existedCargo != null) { existedCargo.Count += cargo.Count; } else { container.Cargos.Add(cargo); } return(true); }
public decimal GetFullCargoCount(string type, ICargoContainer container) { return(container.Cargos.Collection.Where(c => c.Type == type).Sum(c => c.Count)); }