Example #1
0
        public void UpdateShipName(string id, string name)
        {
            if (TryGetShipData(id, out var shipData))
            {
                shipData.DisplayName = name;

                SaveShipData();

                ShipsUpdated?.Invoke(this, new ShipEventArgs
                {
                    ShipDetails   = GetShipData(),
                    IsFullRefresh = false
                });
            }
        }
Example #2
0
        public void UpdateShipCargo(string id, Cargo[] cargo)
        {
            if (TryGetShipData(id, out var shipData))
            {
                shipData.Ship.Cargo          = cargo;
                shipData.Ship.SpaceAvailable = shipData.Ship.MaxCargo - cargo.Sum(t => t.Quantity);

                SaveShipData();

                ShipsUpdated?.Invoke(this, new ShipEventArgs
                {
                    ShipDetails   = GetShipData(),
                    IsFullRefresh = false
                });
            }
        }
Example #3
0
        public void AddFlightPlan(string id, FlightPlan flightPlan)
        {
            if (TryGetShipData(id, out var shipData))
            {
                shipData.LastFlightPlan = flightPlan;
                shipData.FlightEnded    = false;
                shipData.TimeElapsed    = 0;
                shipData.Ship.Location  = null;

                var fuel = shipData.Ship.Cargo.FirstOrDefault(t => t.Good == "FUEL");
                if (fuel != null)
                {
                    if (flightPlan.FuelConsumed >= fuel.Quantity)
                    {
                        var cargoList = shipData.Ship.Cargo.ToList();
                        cargoList.Remove(fuel);
                        shipData.Ship.Cargo = cargoList.ToArray();
                    }
                    else
                    {
                        fuel.Quantity    -= flightPlan.FuelConsumed;
                        fuel.TotalVolume -= flightPlan.FuelConsumed;
                    }
                }

                SaveShipData();

                FlightsUpdated?.Invoke(this, new ShipEventArgs
                {
                    ShipDetails   = GetShipData(),
                    IsFullRefresh = false
                });

                ShipsUpdated?.Invoke(this, new ShipEventArgs
                {
                    ShipDetails   = GetShipData(),
                    IsFullRefresh = false
                });
            }
        }
Example #4
0
        public async Task RefreshShipData()
        {
            DataRefreshing = true;

            if (_userProvider.UserDetails != null)
            {
                Dictionary <string, ShipData> newShipData = null;
                var shipResponse = await _http.GetFromJsonAsync <ShipsResponse>("/my/ships", _serializerOptions);

                var ships = shipResponse?.Ships;

                if (_localStorage.ContainKey("ShipData." + _userProvider.Username))
                {
                    newShipData = _localStorage.GetItem <Dictionary <string, ShipData> >("ShipData." + _userProvider.Username);
                }
                newShipData ??= new Dictionary <string, ShipData>();

                if (newShipData.Count > 0)
                {
                    if (ships == null)
                    {
                        newShipData.Clear();
                    }
                    else
                    {
                        var shipsToRemove = newShipData.Keys.Where(t => !ships.Any(s => t == s.Id)).ToArray();
                        foreach (var key in shipsToRemove)
                        {
                            newShipData.Remove(key);
                        }
                    }
                }

                var takenIds = new List <int>();
                foreach (var ship in newShipData)
                {
                    if (takenIds.Contains(ship.Value.Id))
                    {
                        ship.Value.Id = GetNewShipId(newShipData.Values.ToArray());
                    }
                    takenIds.Add(ship.Value.Id);
                }

                if (ships != null)
                {
                    var shipsToAdd = ships.Where(t => !newShipData.ContainsKey(t.Id)).ToArray();
                    var currentIds = newShipData.Select(s => s.Value.Id);
                    for (var x = 0; x < shipsToAdd.Length; x++)
                    {
                        newShipData.Add(shipsToAdd[x].Id, new ShipData
                        {
                            Id          = GetNewShipId(newShipData.Values.ToArray()),
                            DisplayName = shipsToAdd[x].Id,
                            ServerId    = shipsToAdd[x].Id,
                        });
                    }
                }

                var timeNow = DateTimeOffset.UtcNow;
                foreach (var ship in newShipData)
                {
                    ship.Value.Ship = ships.First(t => t.Id == ship.Key);

                    if (ship.Value.Ship.Location == null && ship.Value.LastFlightPlan?.Id != ship.Value.Ship.FlightPlanId)
                    {
                        ship.Value.LastFlightPlan = null;

                        try
                        {
                            var flightPlanResponse = await _http.GetFromJsonAsync <FlightResponse>("/my/flight-plans/" + ship.Value.Ship.FlightPlanId, _serializerOptions);

                            ship.Value.LastFlightPlan = flightPlanResponse.FlightPlan;
                        }
                        catch (Exception) { }
                    }

                    if (ship.Value.LastFlightPlan != null)
                    {
                        ship.Value.LastFlightPlan.TimeRemainingInSeconds = (int)Math.Ceiling(ship.Value.LastFlightPlan.ArrivesAt.Subtract(timeNow).TotalSeconds);
                    }
                    ship.Value.FlightEnded = ship.Value.Ship.Location != null || ship.Value.LastFlightPlan == null || ship.Value.LastFlightPlan?.TimeRemainingInSeconds < 0;
                }

                _shipData = newShipData;

                AssignShipNames();
                SaveShipData();
            }
            else
            {
                _shipData = null;
            }

            DataRefreshing = false;
            LastUpdate     = DateTimeOffset.UtcNow;

            ShipsUpdated?.Invoke(this, new ShipEventArgs
            {
                ShipDetails   = GetShipData(),
                IsFullRefresh = true
            });
        }