Ejemplo n.º 1
0
        public static IQueryable<CityInfo> GetCities(string username)
        {
            IQueryable<CityInfo> cities;
            using (var db = new DatabaseContext())
            {
                IQueryable<CityInfo> query = from c in db.CityInfoes
                        where c.User.Equals(username)
                        select c;

                cities = query.ToList().AsQueryable();
            }

            return cities;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static IQueryable<RuleSet> GetRuleSets(string username)
        {
            IQueryable<RuleSet> ruleSets;

            using (var db = new DatabaseContext())
            {
                IQueryable<RuleSet> query =
                    from c in db.RuleSets
                    where c.User.Equals(username)
                    select c;
                ruleSets = query.ToList().AsQueryable();
            }

            return ruleSets;
        }
Ejemplo n.º 3
0
        public static double GetValue(string scoringIdName, CityInfo city, DatabaseContext db)
        {
            ScoringIdentifier scoringId = db.ScoringIdentifiers.First(s => s.Name.Equals(scoringIdName) || s.ShortName.Equals(scoringIdName));

            object objVal = city.GetType().GetProperty(scoringId.PropertyName).GetValue(city);
            double value;

            if (objVal.GetType() == typeof(int))
            {
                value = (double)(int)objVal;
            }
            else
            {
                // Identifier validity should have already been checked, so this should be a double.
                value = (double)objVal;
            }

            return value;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteRuleSet(int id)
        {
            using (var db = new DatabaseContext())
            {
                RuleSet ruleSet = db.RuleSets.First(i => i.RuleSetId == id);
                db.RuleSets.Remove(ruleSet);

                // Remove references to this rule set (formula) from any rankings in which it appears.
                var rankings = from r in db.Rankings
                               where r.RuleSetId == ruleSet.RuleSetId
                               select r;
                foreach (Ranking ranking in rankings)
                {
                    ranking.RuleSetId = -1;
                }

                db.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ruleset"></param>
        public static void CreateRuleSet(string name, string formula, string username)
        {
            DateTime now = DateTime.Now;
            bool isValidFormula = ValidateFormula(formula);

            var ruleSet = new RuleSet
            {
                RuleSetName = name,
                Formula = formula,
                User = username,
                Created = now,
                Valid = isValidFormula
            };

            using (var db = new DatabaseContext())
            {
                db.RuleSets.Add(ruleSet);
                db.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        public static void DeleteCity(int cityId)
        {
            using (var db = new DatabaseContext())
            {
                CityInfo cityInfo = db.CityInfoes.First(i => i.CityInfoId == cityId);
                db.CityInfoes.Remove(cityInfo);

                // Remove the city from any rankings it was present in.
                var rankedRefs = from r in db.RankingMembers
                                 where r.CityInfoId == cityInfo.CityInfoId
                                 select r;
                foreach (RankingMember rankedReference in rankedRefs)
                {
                    db.RankingMembers.Remove(rankedReference);
                }

                db.SaveChanges();
            }

            // TODO: Also delete city files... or mark them as "orphan" somehow.
            //       Maybe the city ref should just be marked as orphan in the db... that way we can save the ranked instances.
        }
Ejemplo n.º 7
0
 public static bool IsValueIdentifier(string canditateString, DatabaseContext db)
 {
     return db.ScoringIdentifiers.Any(s => s.Name == canditateString || s.ShortName == canditateString);
 }
Ejemplo n.º 8
0
        public static IQueryable<ScoringIdentifier> GetScoringIdentifiers()
        {
            IQueryable<ScoringIdentifier> identifiers;
            var db = new DatabaseContext();

            identifiers = from s in db.ScoringIdentifiers
                          orderby s.DisplayOrder
                          select s;

            return identifiers;
        }
Ejemplo n.º 9
0
        private static string getBadFormulaIds(string formula, List<string> cityIds)
        {
            // Make sure all value identifiers are valid.

            string badIds = "";
            using (var db = new DatabaseContext())
            {
                foreach (string id in cityIds)
                {
                    if (!GetCityValue.IsValueIdentifier(id, db))
                    {
                        badIds = badIds + id + " ";
                    }
                }
            }
            return badIds;
        }
Ejemplo n.º 10
0
        public static void UpdateScoringIdentifiers(string serverHome)
        {
            using (DatabaseContext db = new DatabaseContext())
            {
                // Clear all scoring ids.
                // ... inefficiently, but this only runs at application start so it will do.
                foreach (var scoringId in db.ScoringIdentifiers)
                {
                    db.ScoringIdentifiers.Remove(scoringId);
                }

                // Add all scoring ids.
                foreach (var scoringId in getScoringIdentifiers(serverHome))
                {
                    db.ScoringIdentifiers.Add(scoringId);
                }

                db.SaveChanges();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool IsDuplicateName(string name, string username)
        {
            bool result;
            using (var db = new DatabaseContext())
            {
                result = db.RuleSets.Any(r => r.User.Equals(username) && r.RuleSetName.Equals(name));
            }

            return result;
        }
Ejemplo n.º 12
0
        private static void storeCity(City parserCity, string username, string filepath, Stream cityFileStream)
        {
            // Fetch relevant data from parserCity.
            var city = new CityInfo(parserCity, username, filepath, DateTime.Now);

            // Save .sc2 file on the server.
            cityFileStream.Position = 0;
            using (Stream outputStream = File.OpenWrite(filepath))
            {
                cityFileStream.CopyTo(outputStream);
            }

            // Save parsed city data to database.
            using (var db = new DatabaseContext())
            {
                db.CityInfoes.Add(city);
                db.SaveChanges();
            }
        }