Exemplo n.º 1
0
        public async Task CreateRelationshipGirlDrop(WhereToGetShipGirl wtg, ShipGirl shipGirl, string note, CargoContext cargoContext)
        {
            ShipGirlWhereToGetShipGirl mtm = new ShipGirlWhereToGetShipGirl
            {
                FK_ShipGirl           = shipGirl,
                FK_WhereToGetShipGirl = wtg,
                Note = note
            };

            if (cargoContext.ShipGirlWhereToGetShipGirl.Count(e => e.FK_ShipGirl.ShipID == mtm.FK_ShipGirl.ShipID &&
                                                              e.FK_WhereToGetShipGirl.Name == mtm.FK_WhereToGetShipGirl.Name) == 0)
            {
                cargoContext.ShipGirlWhereToGetShipGirl.Add(mtm);
                await cargoContext.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Download ShipGirl's drop locations and update them or save if they doesn't exist.
        /// </summary>
        /// <param name="id">ShipGirl's id</param>
        public override async Task Download(string id)
        {
            Status = Statuses.InProgress;
            List <WTGShipGirlJsonWrapper> wrappedDrops;

            try
            {
                string responseJson = await GetData("shipDrops", DropFields, "shipDrops.ID=\'" + id + "\'");

                wrappedDrops = JsonConvert.DeserializeObject <List <WTGShipGirlJsonWrapper> >(responseJson);
            }
            catch (JsonException)
            {
                Status = Statuses.ErrorInDeserialization;
                Logger.Write($"Failed to desirialize WTG for shipgril. Shipgirl ID: {id}", this.GetType().ToString());
                return;
            }
            catch
            {
                Status = Statuses.DownloadError;
                Logger.Write($"Failed to get WTG data for shipgirl from server. Shipgirl ID: {id}", this.GetType().ToString());
                return;
            }

            WTGShipGirlJsonWrapper wrappedDrop = wrappedDrops.FirstOrDefault();

            if (wrappedDrop == null)
            {
                Status = Statuses.EmptyResponse;
                return;
            }

            // Adding location if didn't exists and creating connection between drop location and ship girl
            using (CargoContext cargoContext = new CargoContext())
            {
                // get dropped ship girl
                ShipGirl shipGirl = await cargoContext.ShipGirls.FindAsync(wrappedDrop.WtgShipGirlJson.ID);

                if (shipGirl != null)
                {
                    // Remove old connections
                    var oldConnections =
                        await cargoContext.ShipGirlWhereToGetShipGirl
                        .Where(e => e.FK_ShipGirl.ShipID == shipGirl.ShipID).ToListAsync();

                    cargoContext.ShipGirlWhereToGetShipGirl.RemoveRange(oldConnections);
                    await cargoContext.SaveChangesAsync();

                    // get all drops locations
                    Dictionary <string, string> dictionary = wrappedDrop.WtgShipGirlJson.GetDrops();

                    if (dictionary.Count > 0)
                    {
                        foreach (string key in dictionary.Keys)
                        {
                            // get location
                            WhereToGetShipGirl whereToGetShipGirl =
                                await cargoContext.WhereToGetShipGirls.FindAsync(key);

                            // check if it exists, if not create
                            if (whereToGetShipGirl == null)
                            {
                                whereToGetShipGirl = cargoContext.WhereToGetShipGirls.Add(new WhereToGetShipGirl {
                                    Name = key
                                });
                                cargoContext.SaveChanges();
                            }

                            //  create connection
                            await CreateRelationshipGirlDrop(whereToGetShipGirl, shipGirl, dictionary[key], cargoContext);
                        }
                    }
                }
            }

            Status = Statuses.DownloadComplete;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Download all cases of getting a ship girl and save them
        /// </summary>
        public override async Task Download()
        {
            Status            = Statuses.InProgress;
            StatusDataMessage = "Downloading data.";
            List <WTGShipGirlJsonWrapper> wrappedDrops;

            try
            {
                string responseJson = await GetData("shipDrops", DropFields, "");

                wrappedDrops = JsonConvert.DeserializeObject <List <WTGShipGirlJsonWrapper> >(responseJson);
            }
            catch (JsonException)
            {
                Status = Statuses.ErrorInDeserialization;
                Logger.Write($"Failed to desirialize WTG.", this.GetType().ToString());
                return;
            }
            catch
            {
                Status = Statuses.DownloadError;
                Logger.Write($"Failed to get data for WTG from server.", this.GetType().ToString());
                return;
            }

            TotalDataCount    = wrappedDrops.Count;
            StatusDataMessage = "Placing ShipGirls on Maps...";
            // Adding location if didn't exists and creating connection between drop location and ship girl
            using (CargoContext cargoContext = new CargoContext())
            {
                foreach (WTGShipGirlJsonWrapper wrappedDrop in wrappedDrops)
                {
                    // get dropped ship girl
                    ShipGirl shipGirl = await cargoContext.ShipGirls.FindAsync(wrappedDrop.WtgShipGirlJson.ID);

                    if (shipGirl != null)
                    {
                        // get all drops locations
                        Dictionary <string, string> dictionary = wrappedDrop.WtgShipGirlJson.GetDrops();

                        if (dictionary.Count > 0)
                        {
                            foreach (string key in dictionary.Keys)
                            {
                                // get location
                                WhereToGetShipGirl whereToGetShipGirl =
                                    await cargoContext.WhereToGetShipGirls.FindAsync(key);

                                // check if it exists, if not create
                                if (whereToGetShipGirl == null)
                                {
                                    whereToGetShipGirl = cargoContext.WhereToGetShipGirls.Add(new WhereToGetShipGirl {
                                        Name = key
                                    });
                                    cargoContext.SaveChanges();
                                }

                                //  create connection
                                await CreateRelationshipGirlDrop(whereToGetShipGirl, shipGirl, dictionary[key], cargoContext);
                            }
                        }
                    }

                    lock (locker)
                    {
                        CurrentDataCount++;
                    }
                }
            }

            StatusDataMessage = "Complete.";
            Status            = Statuses.DownloadComplete;
        }