Exemplo n.º 1
0
        private static void AugmentShipInfo(Ship ship, List <Ship> storedShips)
        {
            ShipsConfiguration     shipsConfiguration = ShipsConfiguration.FromFile();
            Dictionary <int, Ship> lookup             = shipsConfiguration.Ships.ToDictionary(o => o.LocalId);

            Ship shipConfig;

            // Start with our current ship
            if (lookup.TryGetValue(ship.LocalId, out shipConfig))
            {
                // Already exists; grab the relevant information and supplement it
                // Ship config name might be just whitespace, in which case we unset it
                if (shipConfig.Name != null && shipConfig.Name.Trim().Length > 0)
                {
                    ship.Name = shipConfig.Name.Trim();
                }
                if (shipConfig.PhoneticName != null && shipConfig.PhoneticName.Trim().Length > 0)
                {
                    ship.PhoneticName = shipConfig.PhoneticName.Trim();
                }
                ship.CallSign = shipConfig.CallSign;
                ship.Role     = shipConfig.Role;
            }
            else
            {
                // Doesn't already exist; add a callsign and default role
                ship.CallSign = Ship.generateCallsign();
                ship.Role     = ShipRole.Multipurpose;
            }

            // Work through our shipyard
            foreach (Ship storedShip in storedShips)
            {
                if (lookup.TryGetValue(storedShip.LocalId, out shipConfig))
                {
                    // Already exists; grab the relevant information and supplement it
                    storedShip.Name     = shipConfig.Name;
                    storedShip.CallSign = shipConfig.CallSign;
                    storedShip.Role     = shipConfig.Role;
                }
                else
                {
                    // Doesn't already exist; add a callsign and default role
                    storedShip.CallSign = Ship.generateCallsign();
                    storedShip.Role     = ShipRole.Multipurpose;
                }
            }

            // Update our configuration with the new data (this also removes any old redundant ships)
            shipsConfiguration.Ships = new List <Ship>();
            shipsConfiguration.Ships.Add(ship);
            shipsConfiguration.Ships.AddRange(storedShips);
            shipsConfiguration.ToFile();
        }