コード例 #1
0
ファイル: AccountController.cs プロジェクト: weiranye/Messi
 public ActionResult Register(RegisterViewModel model)
 {
     if (ModelState.IsValid)
     {
         using (var messi = new Models.Messi())
         {
             var user = (from u in messi.Users
                         where u.UserName == model.UserName
                         select u).FirstOrDefault();
             if (user == null)
             {
                 user = new Models.User()
                 {
                     CountryName = model.CountryName,
                     UserName    = model.UserName,
                     Password    = model.Password,
                     DisplayName = model.DisplayName
                 };
                 messi.Entry(user).State = EntityState.Added;
                 messi.SaveChanges();
             }
             else
             {
                 ModelState.AddModelError("", "User Name already taken!!!");
                 return(View(model));
             }
         }
     }
     // If we got this far, something failed, redisplay form
     return(View(model));
 }
コード例 #2
0
ファイル: GameLogic.cs プロジェクト: weiranye/Messi
        // return null if no available game; otherwise, return the game and reserve it.
        public GameObject GetAndReserveGame(int userId)
        {
            // avaliable game: 1) status is open; 2) not created/played by the given user; 3) has at least one round
            using (Models.Messi messi = new Models.Messi())
            {
                Game anAvailableGame = messi.Games.FirstOrDefault(g => g.StatusId == 1 &&
                                                                  g.Rounds.Where(r => r.UserId == userId).FirstOrDefault() == null &&
                                                                  g.Rounds.Count > 0);

                if (anAvailableGame != null)
                {
                    GameObject game = new GameObject()
                    {
                        GameId   = anAvailableGame.GameId,
                        RoundNum = anAvailableGame.Rounds.Max(r => r.RoundNum + 1),
                        Text     = anAvailableGame.Word
                    };
                    // reserve it
                    anAvailableGame.StatusId = 2;
                    messi.SaveChanges();
                    return(game);
                }
                else
                {
                    return(null);
                }
            }
        }
コード例 #3
0
ファイル: GameLogic.cs プロジェクト: weiranye/Messi
 // change Game status as well if adding the last round
 public int AddRound(Round round)
 {
     using (Models.Messi messi = new Models.Messi())
     {
         messi.Rounds.Add(round);
         if (round.RoundNum == MAX_ROUNDS)
         {
             round.Game.StatusId = 3;
         }
         messi.SaveChanges();
         return(round.RoundId);
     }
 }
コード例 #4
0
ファイル: GameLogic.cs プロジェクト: weiranye/Messi
 // returns GameId
 public static int AddGame(CreateGameObject obj)
 {
     using (Models.Messi messi = new Models.Messi())
     {
         Game newGame = new Game()
         {
             StatusId         = 1,
             ImageUrl1        = obj.ImageUrl1,
             ImageUrl2        = obj.ImageUrl2,
             ImageUrl3        = obj.ImageUrl3,
             ImageUrl4        = obj.ImageUrl4,
             SelectedImageUrl = obj.SelectedImageUrl,
             Word             = obj.Word
         };
         messi.Games.Add(newGame);
         messi.SaveChanges();
         return(newGame.GameId);
     }
 }
コード例 #5
0
ファイル: AccountController.cs プロジェクト: weiranye/Messi
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         using (var messi = new Models.Messi())
         {
             var user = (from u in messi.Users
                         where u.UserName == model.UserName && u.Password == model.Password
                         select u).FirstOrDefault();
             if (user != null)
             {
                 FormsAuthentication.SetAuthCookie(user.UserName + "|" + user.UserId, false);
                 return(RedirectToLocal(returnUrl));
             }
         }
     }
     // If we got this far, something failed, redisplay form
     ModelState.AddModelError("", "The user name or password provided is incorrect.");
     return(View(model));
 }
コード例 #6
0
ファイル: GameLogic.cs プロジェクト: weiranye/Messi
        // <Word, Definition, ImageUrl>
        public static List <Tuple <string, string, string> > GetWordDefImageList()
        {
            List <Tuple <string, string, string> > result = new List <Tuple <string, string, string> >();

            // get 4 random words
            List <string> fourWords = new List <string>();

            while (fourWords.Count < 4)
            {
                Random rnd        = new Random();
                string randomWord = WordRepo.Words[rnd.Next(0, WordRepo.Words.Count)];
                if (!fourWords.Contains(randomWord))
                {
                    fourWords.Add(randomWord);
                }
            }

            string imageUrl   = "";
            string definition = "";

            for (int i = 0; i < 4; i++)
            {
                // try cache first
                using (Models.Messi messi = new Models.Messi())
                {
                    string   word  = fourWords[i];
                    ApiCache cache = messi.ApiCaches.Where(a => a.Word.Equals(word)).FirstOrDefault();
                    if (cache != null)
                    {
                        imageUrl   = cache.ImageUrl;
                        definition = cache.Definition;
                    }
                    else
                    {
                        // get imageUrl from API.
                        DkApiResult dkResult = Helper.ImageLookUp(word);
                        if (dkResult.total < 1)
                        {
                            throw new Exception("Number of images for this word is less than 1. Word: " + word);
                        }
                        else
                        {
                            imageUrl = dkResult.images[0].url;
                        }

                        // get definition from API
                        JObject lmResult = Helper.DefinitionLookUpObj(word);
                        JToken  lmEntry  = (lmResult["Entries"]["Entry"] is JArray) ? lmResult["Entries"]["Entry"][0] : lmResult["Entries"]["Entry"];
                        JToken  lmSense  = (lmEntry["Sense"] is JArray) ? lmEntry["Sense"][0] : lmEntry["Sense"];
                        JToken  lmDEF    = lmSense["DEF"];
                        if (lmDEF == null)
                        {
                            JToken lmSubsense = (lmSense["Subsense"] is JArray) ? lmSense["Subsense"][0] : lmSense["Subsense"];
                            lmDEF = lmSubsense["DEF"];
                        }
                        definition = lmDEF["#text"].ToString();

                        // cache it
                        messi.ApiCaches.Add(new ApiCache()
                        {
                            Word       = word,
                            ImageUrl   = imageUrl,
                            Definition = definition
                        });
                        messi.SaveChanges();
                    }
                    // done. <Word, Definition, ImageUrl>
                    result.Add(new Tuple <string, string, string>(word, definition, imageUrl));
                }
            }
            return(result);
        }