Example #1
0
        /// <summary>
        /// updates single horse object
        /// gets data from scrap service
        /// compares with objects in current database
        /// calls merge method if object already in database
        /// </summary>
        /// <param name="jobType"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task UpdateHorsesAsync(string jobType, int id)
        {
            LoadedHorse horse = new LoadedHorse();

            horse = await _scrapDataService.ScrapGenericObject <LoadedHorse>(id, jobType);

            if (horse != null)
            {
                lock (((ICollection)Horses).SyncRoot)
                {
                    //if objects are already in the List
                    if (Horses.Any(h => h.Name.ToLower() == horse.Name.ToLower()))
                    {
                        LoadedHorse doubledHorse = Horses.Where(h => h.Name.ToLower() == horse.Name.ToLower()).Where(h => h.Age == horse.Age).FirstOrDefault();
                        if (doubledHorse != null)
                        {
                            Horses.Remove(doubledHorse);
                            MergeHorsesData(doubledHorse, horse);
                        }
                        else
                        {
                            Horses.Add(horse);
                        }
                    }
                    else
                    {
                        Horses.Add(horse);
                    }
                }
            }
        }
Example #2
0
        public void SortHorses()
        {
            //declares and assigns
            var dt          = new DataTable();
            var iHorseIndex = 0;

            dt.Columns.Add(new DataColumn("Total", System.Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("Horse", System.Type.GetType("System.Object")));

            //process
            //add horses to the datatable
            foreach (var h in Horses)
            {
                dt.Rows.Add(dt.NewRow());
                dt.Rows[iHorseIndex][0]   = h.Total;
                dt.Rows[iHorseIndex++][1] = h;
            }

            //clear all horses
            Horses.Clear();

            //sort the horses in the table
            dt.DefaultView.Sort = "Total desc";
            var dtHorses = dt.DefaultView.ToTable();

            //now readd the sorted horses.
            foreach (DataRow dr in dtHorses.Rows)
            {
                Horses.Add((IHorse)dr[1]);
            }
        }
Example #3
0
        /// <summary>
        /// subscribed to event
        /// loads horses from the file data services
        /// async void eventhandler testing credits: https://stackoverflow.com/a/19415703/11027921
        /// delegates https://docs.microsoft.com/en-us/dotnet/api/system.eventhandler?view=netframework-4.8
        /// </summary>
        public async void OnLoadAllDataAsync()
        {
            string callersName = GetCallerName();

            CommandStartedControlsSetup(callersName);

            if (Horses.Count == 0 && Jockeys.Count == 0 && Races.Count == 0)
            {
                List <LoadedHorse> horses = await _dataServices.GetAllHorsesAsync();

                List <LoadedJockey> jockeys = await _dataServices.GetAllJockeysAsync();

                List <RaceDetails> races = await _dataServices.GetAllRacesAsync();

                foreach (var horse in horses)
                {
                    Horses.Add(new LoadedHorse
                    {
                        Name        = horse.Name,
                        Age         = horse.Age,
                        AllRaces    = horse.AllRaces,
                        AllChildren = horse.AllChildren,
                        Father      = horse.Father,
                        Link        = horse.Link,
                        FatherLink  = horse.FatherLink
                    });
                }

                Horses = Horses.OrderBy(o => o.Age).ToList();

                foreach (var jockey in jockeys)
                {
                    Jockeys.Add(new LoadedJockey
                    {
                        Name     = jockey.Name,
                        AllRaces = jockey.AllRaces,
                        Link     = jockey.Link
                    });
                }

                foreach (var race in races)
                {
                    Races.Add(new RaceDetails
                    {
                        RaceCategory = race.RaceCategory,
                        RaceDate     = race.RaceDate,
                        RaceDistance = race.RaceDistance,
                        RaceLink     = race.RaceLink,
                        HorseList    = race.HorseList
                    });
                }
            }

            PopulateLists();

            CommandCompletedControlsSetup();
            ResetControls();
        }
Example #4
0
        private void GuiUpdate(string msg)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                string name = msg.Split(':')[0];
                int speed   = int.Parse(msg.Split(':')[1]);

                Horses.Add(new HorseVM(name, speed));
                UpdateFastestHorse();
            });
        }
Example #5
0
        /// <summary>
        /// updates list of horses races and children, if found some new
        /// </summary>
        /// <param name="doubledHorse">horse from Horses</param>
        /// <param name="horse">scrapped new horse</param>
        public void MergeHorsesData(LoadedHorse doubledHorse, LoadedHorse horse)
        {
            if (horse.AllRaces != null)
            {
                doubledHorse.AllRaces = doubledHorse.AllRaces.Union(horse.AllRaces).ToList();
            }

            if (horse.AllChildren != null)
            {
                doubledHorse.AllChildren = doubledHorse.AllChildren.Union(horse.AllChildren).ToList();
            }

            Horses.Add(doubledHorse);
        }
Example #6
0
        private Horse makeHorse(int ownerID)
        {
            List <String> maleNames   = ReadFile(@"F:\Code\HorseTrack\HorseTrack\Data\MaleHorseNames.txt");
            List <String> femaleNames = ReadFile(@"F:\Code\HorseTrack\HorseTrack\Data\FemaleHorseNames.txt");
            List <String> breeds      = ReadFile(@"F:\Code\HorseTrack\HorseTrack\Data\Breeds.txt");
            List <String> colors      = ReadFile(@"F:\Code\HorseTrack\HorseTrack\Data\Colors.txt");
            Random        rand        = new Random();

            String name;
            String gender;
            int    _gender = rand.Next(4);

            if (_gender <= 1)
            {
                name = maleNames.ElementAt(rand.Next(maleNames.Count - 1));
                maleNames.Remove(name);
                gender = (_gender == 0) ? "Stallion" : "Gelding";
            }
            else
            {
                name = femaleNames.ElementAt(rand.Next(femaleNames.Count - 1));
                femaleNames.Remove(name);
                gender = "Mare";
            }
            String breed = breeds.ElementAt(rand.Next(breeds.Count - 1));
            String color = colors.ElementAt(rand.Next(colors.Count - 1));
            String age   = rand.Next(20).ToString();

            Horse horse = new Horse
            {
                ID       = Horses.Count,
                OwnderID = ownerID,
                Name     = name,
                Gender   = gender,
                Breed    = breed,
                Color    = color,
                Age      = age
            };

            HorseDict.Add(horse.ID.ToString(), horse.Name);
            Horses.Add(horse);
            return(horse);
        }