Example #1
0
        public string Generate(int maxRows, string staticFileName = "")
        {
            var context = new _8a_oldContext();
            IQueryable <_8anu.Data.Migration.Model.Crag> oldCrags = context.Set <_8anu.Data.Migration.Model.Crag>();

            if (maxRows > 0)
            {
                oldCrags = oldCrags.Take(maxRows);
            }

            var newCrags = new Dictionary <int, Crag>();

            foreach (var old in oldCrags)
            {
                double?lat = null;
                double?lng = null;
                if (!(string.IsNullOrEmpty(old.GoggleMapX.Trim()) && string.IsNullOrEmpty(old.GoggleMapY.Trim())))
                {
                    try
                    {
                        lat = double.Parse(old.GoggleMapX.Trim(), CultureInfo.InvariantCulture.NumberFormat);
                        lng = double.Parse(old.GoggleMapY.Trim(), CultureInfo.InvariantCulture.NumberFormat);
                    }
                    catch {
                        lat = null;
                        lng = null;
                        Console.WriteLine(Environment.NewLine);
                        Console.WriteLine("crag: " + old.Name + " - id: " + old.Id.ToString() + "exception converting lat & longitude");
                        // Console.WriteLine("exception with original lat: " + old.GoggleMapX);
                        // Console.WriteLine("exception with original lng: " + old.GoggleMapY);
                    }
                }

                var category = old.Type == 0 ? SeedStore.CATEGORY_SPORTSCLIMBING : SeedStore.CATEGORY_BOULDERING;

                var item = new Crag
                {
                    Id           = (int)old.Id,
                    Slug         = old.Slug,
                    Category     = category,
                    Name         = old.Name,
                    Town         = old.City,
                    CountryId    = GetCountryId(old.CountryId),
                    Latitude     = lat,
                    Longitude    = lng,
                    DateCreated  = old.EditDate,
                    DateModified = old.EditDate,
                    Published    = old.Active == 1 ? true : false,
                    Access       = old.AccessInfo,
                    LegacyId     = (int)old.Id
                };

                // increase maxID so we can autoincrement new crags later (sector generator)
                if (item.Id.Value > SeedStore.MaxCragId)
                {
                    SeedStore.MaxCragId = item.Id.Value;
                }

                if (old.CragAreaId > 0)
                {
                    item.AreaId = old.CragAreaId;
                }


                //newCrags.Add(item.DatabaseId.Value, item);
                SeedStore.AddCrag(item);
            }

            // SeedStore.AddCragsRange(newCrags);

            //var json = JsonConvert.SerializeObject(newCrags.Values);
            //return json;
            return("");
        }
Example #2
0
        private Api.Common.DataEntities.Ascent HandleScore(Score score)
        {
            currentScoreId = Convert.ToInt32(score.Id);

            ind++;

            if (ind % 10000 == 0)
            {
                Console.Write(". " + (++logTimes * 10000).ToString("#,##0"));
                Console.WriteLine("crags: {0}, sectors: {1}, b: {2}, r: {3}, a: {4}", new object[] {
                    SeedStore.CragCount, SeedStore.SectorCount, SeedStore.BoulderCount, SeedStore.RouteCount,
                    SeedStore.AscentCount
                });
            }
            else if (ind % 10000 == 0)
            {
                Console.Write(".");
            }

            // todo: unicode crag name, look for &234 without ; and remove spaces
            // todo: only do the unicode check if we create a new one
            // todo: find out does old 8a get routes difficulty from the first ascent in score DB?

            Crag      crag    = null;
            Sector    sector  = null;
            Zlaggable thingie = null;

            var scoreHasCountry            = false;
            var scoreHasRouteOrBoulderName = false;
            var scoreHasCragName           = false;
            var scoreHasSectorName         = false;

            var cragName   = getValue(score.Crag, "Unknown Crag", out scoreHasCragName);
            var sectorName = getValue(score.CragSector, "Unknown Sector", out scoreHasSectorName);
            var routeName  = getValue(score.Name, "Unknown ", out scoreHasRouteOrBoulderName);

            if (!scoreHasRouteOrBoulderName)
            {
                var ending = score.What == 0 ? "route" : "boulder";
                routeName += ending;
            }
            var verticalCategory       = score.What == 0 ? SeedStore.CATEGORY_SPORTSCLIMBING : SeedStore.CATEGORY_BOULDERING;
            var verticalAscentCategory = score.What == 0 ? SeedStore.ASCENT_CATEGORY_ROUTE : SeedStore.ASCENT_CATEGORY_BOULDER;
            var scoreHasCrag           = false;
            var scoreHasSector         = false;
            var scoreHasRouteOrBoulder = false;
            var countryId = -1;

            // get user
            var user = SeedStore.GetUser(Convert.ToInt32(score.UserId));

            if (user == null)
            {
                Console.WriteLine("score id: " + score.Id.ToString() + " USER DatabaseId: " + score.UserId.ToString() + " NOT FOUND!");
                return(null);
            }

            //
            // get country - default to users country
            //
            var countryISO3 = score.Country;

            try
            {
                if (!string.IsNullOrEmpty(countryISO3))
                {
                    countryId       = SeedStore.GetCountryId(score.Country);
                    scoreHasCountry = true;
                }
                else
                {
                    // if scores country is null, get users country
                    // this will be used if we create new crag
                    countryId = user.CountryId;
                }
            }
            catch
            {
                Console.WriteLine("trouble getting country for score id: " + score.Id + " - countryISO3: " + countryISO3);
                countryId = user.CountryId;
            }

            if (scoreHasCragName)
            {
                // get crag by name only
                crag = SeedStore.GetCragOnlyByName(score.Crag, verticalCategory);
            }
            else
            {
                crag = SeedStore.GetCrag(cragName, verticalCategory, countryId);
            }

            /*
             * // get existing crag
             * if (scoreHasCountry && scoreHasCragName)
             * {
             *  // look for crag first by country
             *  crag = SeedStore.GetCrag(score.Crag, verticalCategory, countryId);
             *
             *  scoreHasCrag |= crag != null;
             *
             *  // todo:
             *  // optional: get existing crag with different category
             * }
             * else if (scoreHasCragName)
             * {
             *  // score has no country defined .. so we just look for the crag name
             *  crag = SeedStore.GetCragOnlyByName(score.Crag, verticalCategory);
             * }
             */


            // im here with my performance tests:
            // return null;

            // create crag if we still don't have one
            if (crag == null)
            {
                var cragId = ++SeedStore.MaxCragId;
                var now    = DateTime.Now;
                crag = new Crag
                {
                    Id           = cragId,
                    Slug         = cragName.ToSlug(postString: cragId.ToString()),
                    Category     = verticalCategory,
                    Name         = cragName,
                    CountryId    = countryId,
                    DateCreated  = now,
                    DateModified = now,
                    Published    = true
                };
                SeedStore.AddCrag(crag);
            }

            // get existing sector if score happens to have a crag
            //if (scoreHasCrag)
            //{
            sector          = SeedStore.GetSector(crag.Id.Value, sectorName);
            scoreHasSector |= sector != null;
            //}

            // create sector if nothing found
            if (sector == null)
            {
                var now = DateTime.Now;
                // create new sector
                var sectorId = ++SeedStore.MaxSectorId;
                sector = new Sector
                {
                    Id           = sectorId,
                    Slug         = "",
                    CragId       = crag.Id.Value,
                    Name         = sectorName,
                    Category     = verticalCategory,
                    DateCreated  = now,
                    DateModified = now
                };
                SeedStore.AddSector(sector);
            }

            // only try to get existing "climbablethingie" if there is already a sector
            //if (scoreHasSector)
            //{
            thingie = SeedStore.GetThingie(sector.Id.Value, routeName, score.What);
            scoreHasRouteOrBoulder |= thingie != null;
            //}

            // get grading system to be used for using with route/boulder and ascent
            var gradingSystem = getGradingSystem(score.Grade, score.What);

            // no thingie (route or boulder) so we make new one
            if (thingie == null)
            {
                var now = DateTime.Now;
                if (score.What == 0) // could check for category but let's speed things up
                {
                    //if (!scoreHasRouteOrBoulderName)
                    //{
                    //    routeName += "route";
                    //}

                    var routeId = ++SeedStore.MaxRouteId;
                    thingie = new Route
                    {
                        Id       = routeId,
                        Name     = routeName,
                        Slug     = "",
                        SectorId = sector.Id.Value
                    };
                    SeedStore.AddRoute(thingie);
                }
                else
                {
                    //if (!scoreHasRouteOrBoulderName)
                    //{
                    //    routeName += "boulder";
                    //}
                    var boulderId = ++SeedStore.MaxBoulderId;
                    thingie = new Boulder
                    {
                        Id       = boulderId,
                        Name     = routeName,
                        Slug     = "",
                        SectorId = sector.Id.Value
                    };
                    SeedStore.AddBoulder(thingie);
                }
                thingie.DateCreated   = now;
                thingie.DateModified  = now;
                thingie.Difficulty    = gradingSystem.Grade;   // 6a+
                thingie.Grade         = gradingSystem.VLGrade; // vl-1-39
                thingie.GradingSystem = gradingSystem.Type;    // french
            }

            SeedStore.AddGrade(thingie.Id.Value, gradingSystem, score.What == 0 ? ZlaggableCategoryEnum.Sportclimbing : ZlaggableCategoryEnum.Bouldering);


            var objectClassLength = score.ObjectClass.Length;
            // comparing lenghts so it's a bit faster
            // lengths
            // 14 = CLS_UserAscent
            // 22 = CLS_UserAscent_Project
            // 18 = CLS_UserAscent_Try


            var type = "";

            if (objectClassLength != 18)
            {
                type = getVerticalAscentType(score.How);
            }
            else
            {
                type = "go";
            }

            var isProject = objectClassLength == 22;

            // create ascent
            var ascent = new Ascent
            {
                Id                 = ++SeedStore.MaxAscentId,
                UserId             = user.Id.Value,
                Date               = ParseScoreDate(score.Date), // climbed that shit date
                Difficulty         = gradingSystem.Grade,        // eg. 8a+
                ZlaggableId        = thingie.Id,                 // ID of route or boulder
                ZlaggableType      = verticalAscentCategory,     // route / boulder
                Comment            = score.Comment,              // user comment
                Score              = score.TotalScore,
                Type               = type,                       // f, os, tr, rp, go
                Rating             = score.Rate,
                Repeat             = score.Repeat == 1 ? true : false,
                Project            = isProject,
                Chipped            = score.Chipped == 1,
                ExcludeFromRanking = score.ExcludeFromRanking == 1,
                Note               = Convert.ToInt32(score.Fa),
                DateCreated        = score.RecDate,
                DateModified       = score.RecDate,
                Recommended        = score.UserRecommended == 1,
                LegacyId           = (int)score.Id

                                     // todo: userRecommended to Route "likes"
                                     // todo: what to do with projectAscentDate?
                                     // todo: what to do with YellowId

                                     // variations -> not used
                                     // steepness -> not used
                                     // altgrade -> not used
            };

            //SeedStore.Ascents.Add(ascent);
            return(ascent);
        }