| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Diagnostics.Contracts; |
| | 5 | |
|
| | 6 | | // The round-robin assignment class assigns interceptors to the targets in a |
| | 7 | | // round-robin order using the new paradigm. |
| | 8 | | public class RoundRobinAssignment : IAssignment { |
| | 9 | | // Previous target index that was assigned. |
| 0 | 10 | | private int prevTargetIndex = -1; |
| | 11 | |
|
| | 12 | | // Assign a target to each interceptor that has not been assigned a target yet. |
| | 13 | | [Pure] |
| | 14 | | public IEnumerable<IAssignment.AssignmentItem> Assign(in IReadOnlyList<Interceptor> interceptors, |
| 0 | 15 | | in IReadOnlyList<ThreatData> targets) { |
| 0 | 16 | | List<IAssignment.AssignmentItem> assignments = new List<IAssignment.AssignmentItem>(); |
| | 17 | |
|
| | 18 | | // Get the list of interceptors that are available for assignment. |
| 0 | 19 | | List<Interceptor> assignableInterceptors = IAssignment.GetAssignableInterceptors(interceptors); |
| 0 | 20 | | if (assignableInterceptors.Count == 0) { |
| 0 | 21 | | return assignments; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | // Get the list of active threats that need to be addressed. |
| 0 | 25 | | List<ThreatData> activeThreats = IAssignment.GetActiveThreats(targets); |
| 0 | 26 | | if (activeThreats.Count == 0) { |
| 0 | 27 | | return assignments; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | // Perform round-robin assignment. |
| 0 | 31 | | foreach (Interceptor interceptor in assignableInterceptors) { |
| | 32 | | // Determine the next target index in a round-robin fashion. |
| 0 | 33 | | int nextTargetIndex = (prevTargetIndex + 1) % activeThreats.Count; |
| 0 | 34 | | ThreatData selectedThreat = activeThreats[nextTargetIndex]; |
| | 35 | |
|
| | 36 | | // Assign the interceptor to the selected threat. |
| 0 | 37 | | assignments.Add(new IAssignment.AssignmentItem(interceptor, selectedThreat.Threat)); |
| | 38 | |
|
| | 39 | | // Update the previous target index. |
| 0 | 40 | | prevTargetIndex = nextTargetIndex; |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return assignments; |
| 0 | 44 | | } |
| | 45 | | } |