< Summary

Class:CommsManager
Assembly:bamlab.micromissiles
File(s):/github/workspace/Assets/Scripts/Managers/CommsManager.cs
Covered lines:11
Uncovered lines:26
Coverable lines:37
Total lines:60
Line coverage:29.7% (11 of 37)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:11
Method coverage:63.6% (7 of 11)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CommsManager()0%110100%
AddNode(...)0%110100%
ContainsNode(...)0%110100%
Awake()0%12300%
Start()0%2100%
FixedUpdate()0%110100%
SendMessage(...)0%110100%
RegisterSimulationEnded()0%2100%
RegisterNewAgent(...)0%6200%

File(s)

/github/workspace/Assets/Scripts/Managers/CommsManager.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3
 4// The comunication manager manages the communication nodes and handles communication between
 5// agents.
 6public class CommsManager : MonoBehaviour {
 57  public static CommsManager Instance { get; private set; }
 8
 9  // Mailbox for queued messages.
 210  private readonly Mailbox _mailbox = new Mailbox();
 11
 12  // Map from agent to the communication node.
 213  private readonly HashSet<CommsNode> _nodes = new HashSet<CommsNode>();
 14
 15  // Add a communication node. This function should only be used by the IADS and in tests.
 316  public void AddNode(CommsNode node) => _nodes.Add(node);
 17
 318  public bool ContainsNode(CommsNode node) => _nodes.Contains(node);
 19
 020  private void Awake() {
 021    if (Instance != null && Instance != this) {
 022      Destroy(gameObject);
 023      return;
 24    }
 025    Instance = this;
 026  }
 27
 028  private void Start() {
 029    SimManager.Instance.OnSimulationStarted += _mailbox.Clear;
 030    SimManager.Instance.OnSimulationEnded += RegisterSimulationEnded;
 031    SimManager.Instance.OnNewInterceptor += RegisterNewAgent;
 032    SimManager.Instance.OnNewLauncher += RegisterNewAgent;
 033  }
 34
 335  private void FixedUpdate() {
 336    _mailbox.Deliver();
 337  }
 38
 239  public void SendMessage(Message message) {
 240    _mailbox.Send(message);
 241  }
 42
 043  private void RegisterSimulationEnded() {
 044    _nodes.Clear();
 045    _mailbox.Clear();
 046  }
 47
 048  private void RegisterNewAgent(IAgent agent) {
 049    if (agent.CommsNode != null) {
 050      _nodes.Add(agent.CommsNode);
 051      return;
 52    }
 53
 054    var commsNode = new CommsNode(agent.StaticConfig.AgentType);
 055    agent.CommsNode = commsNode;
 056    agent.OnTerminated +=
 057        _ => _nodes.Remove(commsNode);
 058    _nodes.Add(commsNode);
 059  }
 60}