Esempio n. 1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="proxy">XML proxy.</param>
        /// <exception cref="XNeut.InvalidMessageException">Thrown if an error is encountered.</exception>
        internal SegmentRequirement(XsdNs.SegmentRequirementType proxy)
            : this() // Call the default constructor
        {
            try
            {
                if (proxy.ProcessSegmentID != null)
                {
                    ProcessSegmentIdentifier = new IdentifierType(proxy.ProcessSegmentID);
                }

                m_earliestStartTime = TryGetTime(proxy.EarliestStartTime);
                m_latestEndTime     = TryGetTime(proxy.LatestEndTime);

                // Check if end is before start
                if (m_earliestStartTime.HasValue && m_latestEndTime.HasValue &&
                    m_earliestStartTime.Value > m_latestEndTime.Value)
                {
                    var msg = string.Format("Segment end must not be before start; start at {0} UTC",
                                            m_earliestStartTime.Value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    throw new XNeut.InvalidMessageException(msg);
                }

                if (proxy.EquipmentRequirement != null)
                {
                    foreach (var req in proxy.EquipmentRequirement)
                    {
                        EquipmentRequirements.Add(new EquipmentRequirement(req)); // throws InvalidMessageException
                    }
                }

                if (proxy.MaterialRequirement != null)
                {
                    foreach (var req in proxy.MaterialRequirement)
                    {
                        MaterialRequirements.Add(new MaterialRequirement(req)); // throws InvalidMessageException
                    }
                }

                if (proxy.SegmentRequirement != null)
                {
                    foreach (var req in proxy.SegmentRequirement)
                    {
                        SegmentRequirements.Add(new SegmentRequirement(req)); // throws InvalidMessageException
                    }
                }
            }
            catch (NullReferenceException e)
            {
                throw new XNeut.InvalidMessageException("Failed to read SegmentRequirement - something required is missing", e);
            }
        }
Esempio n. 2
0
        async Task <bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
        {
            if (!HasBoard && eventArgs.Using.TryGetComponent <MachineBoardComponent>(out var machineBoard))
            {
                if (eventArgs.Using.TryRemoveFromContainer())
                {
                    // Valid board!
                    _boardContainer.Insert(eventArgs.Using);

                    // Setup requirements and progress...
                    ResetProgressAndRequirements(machineBoard);

                    if (Owner.TryGetComponent <AppearanceComponent>(out var appearance))
                    {
                        appearance.SetData(MachineFrameVisuals.State, 2);
                    }

                    if (Owner.TryGetComponent(out ConstructionComponent construction))
                    {
                        // So prying the components off works correctly.
                        construction.ResetEdge();
                    }

                    return(true);
                }
            }
            else if (HasBoard)
            {
                if (eventArgs.Using.TryGetComponent <MachinePartComponent>(out var machinePart))
                {
                    if (!Requirements.ContainsKey(machinePart.PartType))
                    {
                        return(false);
                    }

                    if (_progress[machinePart.PartType] != Requirements[machinePart.PartType] &&
                        eventArgs.Using.TryRemoveFromContainer() && _partContainer.Insert(eventArgs.Using))
                    {
                        _progress[machinePart.PartType]++;
                        return(true);
                    }
                }

                if (eventArgs.Using.TryGetComponent <StackComponent>(out var stack))
                {
                    var type = stack.StackTypeId;
                    if (!MaterialRequirements.ContainsKey(type))
                    {
                        return(false);
                    }

                    if (_materialProgress[type] == MaterialRequirements[type])
                    {
                        return(false);
                    }

                    var needed = MaterialRequirements[type] - _materialProgress[type];
                    var count  = stack.Count;

                    if (count < needed && stack.Split(count, Owner.Transform.Coordinates, out var newStack))
                    {
                        _materialProgress[type] += count;
                        return(true);
                    }

                    if (!stack.Split(needed, Owner.Transform.Coordinates, out newStack))
                    {
                        return(false);
                    }

                    if (!_partContainer.Insert(newStack))
                    {
                        return(false);
                    }

                    _materialProgress[type] += needed;
                    return(true);
                }

                foreach (var(compName, info) in ComponentRequirements)
                {
                    if (_componentProgress[compName] >= info.Amount)
                    {
                        continue;
                    }

                    var registration = _componentFactory.GetRegistration(compName);

                    if (!eventArgs.Using.HasComponent(registration.Type))
                    {
                        continue;
                    }

                    if (!eventArgs.Using.TryRemoveFromContainer() || !_partContainer.Insert(eventArgs.Using))
                    {
                        continue;
                    }
                    _componentProgress[compName]++;
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 3
0
        public void RegenerateProgress()
        {
            AppearanceComponent appearance;

            if (!HasBoard)
            {
                if (Owner.TryGetComponent(out appearance))
                {
                    appearance.SetData(MachineFrameVisuals.State, 1);
                }

                Requirements          = null;
                MaterialRequirements  = null;
                ComponentRequirements = null;
                _progress             = null;
                _materialProgress     = null;
                _componentProgress    = null;

                return;
            }

            var board = _boardContainer.ContainedEntities[0];

            if (!board.TryGetComponent <MachineBoardComponent>(out var machineBoard))
            {
                return;
            }

            if (Owner.TryGetComponent(out appearance))
            {
                appearance.SetData(MachineFrameVisuals.State, 2);
            }

            ResetProgressAndRequirements(machineBoard);

            foreach (var part in _partContainer.ContainedEntities)
            {
                if (part.TryGetComponent <MachinePartComponent>(out var machinePart))
                {
                    // Check this is part of the requirements...
                    if (!Requirements.ContainsKey(machinePart.PartType))
                    {
                        continue;
                    }

                    if (!_progress.ContainsKey(machinePart.PartType))
                    {
                        _progress[machinePart.PartType] = 1;
                    }
                    else
                    {
                        _progress[machinePart.PartType]++;
                    }
                }

                if (part.TryGetComponent <StackComponent>(out var stack))
                {
                    var type = stack.StackTypeId;
                    // Check this is part of the requirements...
                    if (!MaterialRequirements.ContainsKey(type))
                    {
                        continue;
                    }

                    if (!_materialProgress.ContainsKey(type))
                    {
                        _materialProgress[type] = 1;
                    }
                    else
                    {
                        _materialProgress[type]++;
                    }
                }

                // I have many regrets.
                foreach (var(compName, amount) in ComponentRequirements)
                {
                    var registration = _componentFactory.GetRegistration(compName);

                    if (!part.HasComponent(registration.Type))
                    {
                        continue;
                    }

                    if (!_componentProgress.ContainsKey(compName))
                    {
                        _componentProgress[compName] = 1;
                    }
                    else
                    {
                        _componentProgress[compName]++;
                    }
                }
            }
        }