public static Phrase AddNewPhrase()
        {
            try
            {
                lock (lockdb)
                {
                    using (var db = new DataBase())
                    {

                        Phrase w = db.Phrases.Where(x => x.Level == 0
                            && x.Lesson.IsBought)
                            .OrderBy(x => x.LessonId)
                            .FirstOrDefault();

                            //Phrase  w = db.Phrases.Where(x => x.Level == 0
                            //&& x.Lesson.IsBought)

                            //.OrderBy(x => x.LessonId)
                            //.ThenBy(x=>x.EPhrase)
                            //.FirstOrDefault();

                        if(w !=null)
                        {
                            w.Level = 1;
                            db.SubmitChanges();
                        }
                        return w;
                      }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static Word AddNewWord()
        {
            try
            {
                lock (lockdb)
                {
                    using (var db = new DataBase())
                    {
                        Word w = db.Words.Where(x => x.Level == 0
                            && x.Lesson.IsBought)
                            .OrderBy(x => x.LessonId)
                            .FirstOrDefault();

                        if (w != null)
                        {
                            w.Level = 1;
                            db.SubmitChanges();
                        }
                        return w;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public void GetLevelWithElements3Test()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;
                foreach (var t in temp)
                {
                    i = 5;
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                b.SubmitChanges();
                Assert.AreEqual(5, WordEngine.GetLevelWithElements());

            }
        }
        public void DettachPhraseTest()
        {
            DataBase.Create();
            using (var db = new DataBase())
            {
                db.Lessons.InsertOnSubmit(new Lesson() { LessonId = 1, Name = "lesson" });
                db.SubmitChanges();
                var w = new Phrase() { PhraseId = 1 };
                var l = db.Lessons.First();
                l.AttacPhrase(w);
                db.Phrases.InsertOnSubmit(w);
                db.SubmitChanges();
                Assert.AreNotEqual(null, w.Lesson);
                l.DetachPhrase(w);
                Assert.AreEqual(null, w.Lesson);

            }
        }
        public void AttachWordTest()
        {
            DataBase.Create();
            using (var db = new DataBase())
            {
                db.Lessons.InsertOnSubmit(new Lesson() { LessonId=1, Name = "lesson" });
                db.SubmitChanges();
                var w = new Word() { WordId=1, EWord = "go" };
                var l = db.Lessons.First();
                l.AttachWord(w);
                db.Words.InsertOnSubmit(w);
                db.SubmitChanges();
                w = db.Words.First();
                Assert.AreEqual(l.LessonId, w.LessonId);
                Assert.AreNotEqual(null, w.Lesson);
                Assert.AreEqual(l.LessonId, w.Lesson.LessonId);

            }
        }
        public void AddNewWordTest()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;
                foreach (var t in temp)
                {
                    i = 0;
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 0};
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 0};
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                b.SubmitChanges();
                var l = b.Lessons.First(x => x.LessonId == 1);
                l.IsBought = false;
               // b.Lessons.InsertOnSubmit(l);
                b.SubmitChanges();
                Assert.IsFalse(b.Words.Any(x => x.Level == 1));
                l = b.Lessons.First(x => x.LessonId == 3);
                l.IsBought = true;
              //  b.Lessons.InsertOnSubmit(l);
                b.SubmitChanges();
                WordRepository.AddNewWord();
                Assert.AreEqual(1, b.Words.Count(x=>x.Level==1 && x.LessonId==3));

            }
        }
        public static void Buy(string Key)
        {
            // s.Buy(Key);
            using (var db = new DataBase())
            {

                var les = db.Lessons.FirstOrDefault(x => x.Key == Key);
                if(les != null)
                {
                    les.IsBought = !les.IsBought;
                    db.SubmitChanges();
                    Init();
                }

            }
        }
 public void InitializeTest()
 {
     DataBase.Create();
     using(var db = new DataBase())
     {
         var z =db.Lessons.Where(x => x.Key == "Series1").First();
         Assert.AreEqual(true, z.IsBought);
         z.IsBought = false;
         db.SubmitChanges();
         Assert.AreEqual(false, db.Lessons.Where(x => x.Key == "Series1").First().IsBought);
     }
     LessonsManager.Initilize();
     using (var db = new DataBase())
     {
         var z = db.Lessons.Where(x => x.Key == "Series1").First();
         Assert.AreEqual(true, z.IsBought);
     }
 }
        public void IdLevelWitheMaxCountElementsTest()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                b.DeleteDatabase();
                b.CreateDatabase();
                b.Lessons.InsertOnSubmit(new Lesson());
                b.Lessons.InsertOnSubmit(new Lesson());
                b.Lessons.InsertOnSubmit(new Lesson());
                b.Lessons.InsertOnSubmit(new Lesson());
                b.Lessons.InsertOnSubmit(new Lesson());
                b.SubmitChanges();
                var temp = b.Lessons;
                Word w;
                int i;

                foreach (var t in temp)
                {

                    i = 2;
                    w = new Word() { Level = i++};
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                b.SubmitChanges();
                Assert.AreEqual(4, WordRepository.LevelWitheMaxCountElements());

            }
        }
        public void GetElementTest()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;

                foreach (var t in temp)
                {
                    i = 0;
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                b.SubmitChanges();
                Word wor = WordRepository.GetElement(1, 8);
                Assert.AreEqual(1, wor.Level);
                Assert.AreEqual(9, wor.LessonId);

            }
        }
        public void CountElementsInLevelsTest()
        {
            DataBase.Create();
            using(var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;
                foreach(var t in temp)
                {
                    i = 0;
                    w = new Word() { Level = i++};
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = i++ };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                b.SubmitChanges();
                Assert.AreEqual(18,WordRepository.CountElementsInLevels(1, 2));

            }
        }
        public void LevelTest()
        {
            Word w;
            using (var b = new DataBase())
            {
                b.CreateDatabase();
                b.Lessons.InsertOnSubmit(new Lesson());
                b.SubmitChanges();
                Lesson t = b.Lessons.First();
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                for (int i = 0; i < 1; i++)
                {
                    w = new Word() { Level = 1 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 2 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 3 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 4 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 5 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                b.SubmitChanges();
            }
            //Assert.AreEqual(5, WordEngine.level(2));
            //Assert.AreEqual(5, WordEngine.level(1));
            //Assert.AreEqual(5, WordEngine.level(0.893));
            //Assert.AreEqual(5, WordEngine.level(0.892));
            //Assert.AreEqual(4, WordEngine.level(0.891));
            //Assert.AreEqual(4, WordEngine.level(0.88));
            //Assert.AreEqual(3, WordEngine.level(0.86));
            //Assert.AreEqual(3, WordEngine.level(0.85));
            //Assert.AreEqual(2, WordEngine.level(0.84));
            //Assert.AreEqual(2, WordEngine.level(0.75));
            //Assert.AreEqual(1, WordEngine.level(0.74));
            //Assert.AreEqual(1, WordEngine.level(0));
            Assert.AreEqual(5, WordEngine.level(2));
            Assert.AreEqual(5, WordEngine.level(1));
            Assert.AreEqual(1, WordEngine.level(0.99));
            Assert.AreEqual(1, WordEngine.level(0.5));
            Assert.AreEqual(1, WordEngine.level(0));
        }
        internal static void Save(Word word)
        {
            try
            {
                using (var db = new DataBase())
                {
                    Word w = db.Words.First(x => x.WordId == word.WordId);
                    w.Right = word.Right;
                    w.Level = word.Level;
                    w.Count = word.Count;
                    db.SubmitChanges();
                }

            }
            catch (Exception ex)
            {

            }
        }
        public void UpdateLeveltest2()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;
                foreach (var t in temp)
                {
                    i = 0;
                    w = new Word() { Level = 1 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 2 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                b.SubmitChanges();
                int c = b.Words.Count(x => x.Level == 1);
                WordEngine.UpdateLevel();
                Assert.AreEqual(c , b.Words.Count(x => x.Level == 1));

            }
        }
        public void perLevelTest()
        {
            Word w;
            using (var b = new DataBase())
            {
                b.CreateDatabase();
                b.Lessons.InsertOnSubmit(new Lesson());
                b.SubmitChanges();
                Lesson t = b.Lessons.First();
                for (int i = 0; i < 0; i++)
                {
                    w = new Word() { Level = 0 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                for (int i = 0; i < 5; i++)
                {
                    w = new Word() { Level = 1 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);

                }
                for (int i = 0; i < 5; i++)
                {
                    w = new Word() { Level = 2 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 5; i++)
                {
                    w = new Word() { Level = 3 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 5; i++)
                {
                    w = new Word() { Level = 4 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                for (int i = 0; i < 5; i++)
                {
                    w = new Word() { Level = 5 };
                    t.AttachWord(w);
                    b.Words.InsertOnSubmit(w);
                }
                b.SubmitChanges();
            }
            double[] per = new double[5] { 0.970373176, 0.004, 0.007872, 0.008572608, 0.009182216};
            TimeSpan sp = new TimeSpan();
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            double[] per2 = WordEngine.perLevel();
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            Assert.AreEqual(per.Count(), per2.Count());

            for (int i = 0; i < per.Count();i++ )
                Assert.AreEqual(per[i], per2[i]);
        }
        public static void Save()
        {
            try{
              using (var db = new DataBase())
                {
                    db.SubmitChanges();
                }

            }
            catch (Exception ex)
            {

            }
        }
        public static void SetLevel(int Lesson, int maxLearningLevel)
        {
            try
            {
                using (var db = new DataBase())
                {
                    var phrases = db.Phrases.Where(x => x.Lesson.LessonId == Lesson);
                    foreach (var phrase in phrases)
                        phrase.Level = maxLearningLevel;
                    db.SubmitChanges();
                }

            }
            catch (Exception ex)
            {

            }
        }
Example #18
0
        static void createfordebug()
        {
            using (var db = new DataBase())
            {
                var les = new List<Lesson>();
                int i = 1;            
                les.Add(new Lesson() { LessonId = i++, Description = "The One With the East German Laundry Detergent", IsBought = true, Key = "Series1", Name = "1 сезон 1 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series2", Name = "1 сезон 2 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series3", Name = "1 сезон 3 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series4", Name = "1 сезон 4 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series5", Name = "1 сезон 5 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series6", Name = "1 сезон 6 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series7", Name = "1 сезон 7 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series8", Name = "1 сезон 8 серия" });
                les.Add(new Lesson() { LessonId = i++, Description = "One time You have then bee", IsBought = false, Key = "Series9", Name = "1 сезон 9 серия" });
                db.Lessons.InsertAllOnSubmit(les);
                db.SubmitChanges();
                var temp = db.Lessons;
                Word w =new Word();
                
                foreach (var t in temp)
                {
                    for(int j=0; j<2;j++)
                    {
                         w = new Word() { Level = 0, EWord=GetRandomString(4), RWord=GetRandomString(5), Count=0, EPhrase=GetRandomString(10), RPhrase=GetRandomString(10), Transcription=GetRandomString(10), Right=0 };
                         t.AttachWord(w);
                         db.Words.InsertOnSubmit(w);
                    }
                   
                }
                Phrase ph;
                foreach (var t in temp)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        ph = new Phrase() { Level = 0,  Count = 0, EPhrase = GetRandomString(40), RPhrase = GetRandomString(40),  Right = 0 };
                        t.AttacPhrase(ph);
                        db.Phrases.InsertOnSubmit(ph);
                    }

                }
                db.SubmitChanges();
            }
        }
        internal static void Save(Phrase phrase)
        {
            try
            {
                using (var db = new DataBase())
                {
                    Phrase w = db.Phrases.First(x => x.PhraseId == phrase.PhraseId);
                    w.Right = phrase.Right;
                    w.Level = phrase.Level;
                    w.Count = phrase.Count;
                    db.SubmitChanges();
                }

            }
            catch (Exception ex)
            {

            }
        }
        public void KnowTest()
        {
            DataBase.Create();
            using (var b = new DataBase())
            {
                var temp = b.Lessons;
                Word w;
                int i;
                var t = temp.First();
                w = new Word() { Level = 1, Right=0};
                t.AttachWord(w);
                b.Words.InsertOnSubmit(w);
                b.SubmitChanges();
                WordEngine.Know(w);
                w = b.Words.First(x=>x.WordId==w.WordId);
                Assert.AreEqual(1, w.Right);
                Assert.AreEqual(1, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(0, w.Right);
                Assert.AreEqual(2, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(1, w.Right);
                Assert.AreEqual(2, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(2, w.Right);
                Assert.AreEqual(2, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(0, w.Right);
                Assert.AreEqual(3, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(1, w.Right);
                Assert.AreEqual(3, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(2, w.Right);
                Assert.AreEqual(3, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(3, w.Right);
                Assert.AreEqual(3, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(0, w.Right);
                Assert.AreEqual(4, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(1, w.Right);
                Assert.AreEqual(4, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(2, w.Right);
                Assert.AreEqual(4, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(3, w.Right);
                Assert.AreEqual(4, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(4, w.Right);
                Assert.AreEqual(4, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(0, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(1, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(2, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(3, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(4, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(5, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.Know(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(6, w.Right);
                Assert.AreEqual(5, w.Level);

                WordEngine.DontKnow(w);
                w = b.Words.First(x => x.WordId == w.WordId);
                Assert.AreEqual(0, w.Right);
                Assert.AreEqual(1, w.Level);

            }
        }