Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public RouteRegistry(List <Route> InRoutes = null, TagRegistry InTagRegistry = null)
        {
            TagRegistry             = InTagRegistry;
            TagRegistry.TagDeleted += (Guid Id) =>
            {
                bool TagsRemoved = false;

                for (int i = 0; i < Routes.Count; i++)
                {
                    Route route = Routes[i];
                    if (route.SourceTagId == Id || route.DestinationTagId == Id)
                    {
                        RouteDeleted?.Invoke(route.Id);

                        Routes.RemoveAt(i);
                        i--;

                        TagsRemoved = true;
                    }
                }

                if (TagsRemoved)
                {
                    RoutesUpdated?.Invoke();
                }
            };

            if (InRoutes != null)
            {
                Routes = InRoutes;
            }
        }
Exemplo n.º 2
0
        private async Task LoadRouteData()
        {
            await updateLock.WaitAsync();

            try
            {
                if (_userProvider.UserDetails != null && !_shipProvider.DataRefreshing)
                {
                    Dictionary <int, AutoRoute> newRouteData = null;
                    if (_localStorage.ContainKey("RouteData." + _userProvider.Username))
                    {
                        newRouteData = _localStorage.GetItem <Dictionary <int, AutoRoute> >("RouteData." + _userProvider.Username);
                    }
                    newRouteData ??= new Dictionary <int, AutoRoute>();

                    if (newRouteData != null)
                    {
                        foreach (var route in newRouteData)
                        {
                            foreach (var routeShip in route.Value.Ships)
                            {
                                routeShip.ShipData = _shipProvider.GetShipData(routeShip.ShipId);
                            }
                        }
                        foreach (var route in newRouteData)
                        {
                            route.Value.Ships = route.Value.Ships.Where(t => t.ShipData != null).ToArray();
                        }
                    }

                    _routeData = newRouteData;

                    SaveRouteData();
                }
                else
                {
                    _routeData = null;
                }

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = true
                });
            }
            finally
            {
                updateLock.Release();
            }
        }
Exemplo n.º 3
0
        public void AddRoute(int id, AutoRoute route)
        {
            if (_routeData != null)
            {
                _routeData.Add(id, route);
            }

            SaveRouteData();

            RoutesUpdated?.Invoke(this, new RouteEventArgs
            {
                Routes        = GetRouteData(),
                IsFullRefresh = false
            });
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="TagId"></param>
        public void DeleteRoute(Guid TagId)
        {
            Route Tag = GetRouteById(TagId);

            if (Tag == null)
            {
                return;
            }

            Routes.Remove(Tag);

            RouteDeleted?.Invoke(TagId);

            RoutesUpdated?.Invoke();
        }
Exemplo n.º 5
0
        public void DeleteRoute(AutoRoute route)
        {
            var newRouteData = _routeData.Values.ToList();

            newRouteData.Remove(route);
            newRouteData.Where(r => r.Id > route.Id).ToList().ForEach(r => r.Id--);
            _routeData = newRouteData.ToDictionary(r => r.Id);

            SaveRouteData();

            RoutesUpdated?.Invoke(this, new RouteEventArgs
            {
                Routes        = GetRouteData(),
                IsFullRefresh = false
            });
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Name"></param>
        public Guid CreateRoute(Guid SourceTagId, Guid DestinationTagId, bool Blacklisted, long BandwidthLimit)
        {
            Route Tag = new Route();

            Tag.Id               = Guid.NewGuid();
            Tag.SourceTagId      = SourceTagId;
            Tag.DestinationTagId = DestinationTagId;
            Tag.Blacklisted      = Blacklisted;
            Tag.BandwidthLimit   = BandwidthLimit;

            Routes.Add(Tag);

            RoutesUpdated?.Invoke();

            return(Tag.Id);
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Name"></param>
        public void UpdateRoute(Guid RouteId, Guid SourceTagId, Guid DestinationTagId, bool Blacklisted, long BandwidthLimit)
        {
            Route Tag = GetRouteById(RouteId);

            if (Tag == null)
            {
                return;
            }

            Tag.SourceTagId      = SourceTagId;
            Tag.DestinationTagId = DestinationTagId;
            Tag.Blacklisted      = Blacklisted;
            Tag.BandwidthLimit   = BandwidthLimit;

            RoutesUpdated?.Invoke();
        }
Exemplo n.º 8
0
        public void RemoveShipFromRoute(int routeId, RouteShip ship)
        {
            if (TryGetRoute(routeId, out var route))
            {
                var newShips = route.Ships.ToList();
                newShips.Remove(ship);
                route.Ships = newShips.ToArray();

                SaveRouteData();

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = false
                });
            }
        }
Exemplo n.º 9
0
        public void RemoveCommandFromRoute(int routeId, int index)
        {
            if (TryGetRoute(routeId, out var route))
            {
                index -= 1;
                index  = Math.Max(index, 0);

                var newCommands = route.Commands.ToList();
                newCommands.Remove(newCommands.First(t => t.Index == index));
                newCommands.Where(r => r.Index > index).ToList().ForEach(r => r.Index--);
                route.Commands = newCommands.ToArray();

                SaveRouteData();

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = false
                });
            }
        }
Exemplo n.º 10
0
        public void AddShipToRoute(int routeId, ShipData shipData)
        {
            if (TryGetRoute(routeId, out var route))
            {
                var newShips = route.Ships.ToList();
                newShips.Add(new RouteShip
                {
                    LastCommand = -1,
                    ShipData    = shipData,
                    ShipId      = shipData.ServerId
                });
                route.Ships = newShips.ToArray();

                SaveRouteData();

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = false
                });
            }
        }
Exemplo n.º 11
0
        public void AddCommandToRoute(int routeId, int index, string command)
        {
            if (TryGetRoute(routeId, out var route))
            {
                var list = route.Commands.ToList();
                list.Where(c => c.Index >= index).ToList().ForEach(c => c.Index++);

                list.Insert(index, new RouteCommand
                {
                    Index   = index,
                    Command = command,
                });

                route.Commands = list.ToArray();

                SaveRouteData();

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = false
                });
            }
        }
Exemplo n.º 12
0
        private async void UpdateRoutes(object sender, ElapsedEventArgs args)
        {
            if (await updateLock.WaitAsync(0))
            {
                try
                {
                    if (_routeData != null)
                    {
                        foreach (var route in _routeData.Values)
                        {
                            foreach (var routeShip in route.Ships)
                            {
                                if (routeShip.ShipData.Ship.Location != null)
                                {
                                    RouteCommand command = null;
                                    do
                                    {
                                        if (routeShip.LastCommand > route.Commands.Max(t => t.Index))
                                        {
                                            routeShip.LastCommand = 0;
                                        }
                                        else
                                        {
                                            routeShip.LastCommand++;
                                        }

                                        command = route.Commands.FirstOrDefault(t => t.Index == routeShip.LastCommand);
                                    }while (command == null);

                                    var result = await _commandManager.InvokeCommand(command.Command.Replace("$s", routeShip.ShipData.Id.ToString()), true);

                                    if (result != CommandResult.SUCCESS)
                                    {
                                        var newShips = route.Ships.ToList();
                                        newShips.Remove(routeShip);
                                        route.Ships = newShips.ToArray();

                                        _console.WriteLine("Command failed for " + routeShip.ShipData.DisplayName + " during route. Ship removed from route.");
                                        _console.WriteLine("Command failed: " + command.Command);

                                        Console.Out.WriteLine("Removed ship.");
                                    }
                                }
                            }
                        }

                        SaveRouteData();

                        RoutesUpdated?.Invoke(this, new RouteEventArgs
                        {
                            Routes        = GetRouteData(),
                            IsFullRefresh = false
                        });
                    }
                }
                finally
                {
                    updateLock.Release();
                }
            }
        }