예제 #1
0
        public ActionResult Start(int id = 0)
        {
            using (var galleristContext = new GalleristComponentsDbContext())
            {
                using (var identityContext = new ApplicationDbContext())
                {
                    //todo: set start time of game to datetime.now
                    var gameResponse = GameManager.GetGame(id, galleristContext);
                    var game         = gameResponse.Game;
                    game.StartTime = DateTime.Now;

                    if (game.IsStarted)
                    {
                        return(Redirect("~/Game/Play/" + gameResponse.Game.Id));
                    }

                    if (gameResponse.Success)
                    {
                        game.CreateRandomSetup();
                        game.StartGame();
                        galleristContext.SaveChanges();

                        //todo make a helper in email manager for this
                        //todo also use the user and url params for signalr ajax update of game list
                        var gameUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Game/Play/" + game.Id;
                        foreach (var player in game.Players)
                        {
                            var user = identityContext.Users.Single(m => m.Id == player.UserId);
                            if (!user.AllowsEmails || string.IsNullOrWhiteSpace(user.Email))
                            {
                                continue;
                            }

                            var emailTitle = user.UserName + ", your game has started!"; //todo: get full name of player. We don't have names in the system yet
                            var emailBody  = "A game that you are a member of has started. You can play it by visiting The Gallerist Online" +
                                             " and viewing your active games or by clicking the following link: " + gameUrl;

                            EmailManager.SendEmail(emailTitle, emailBody, new List <string> {
                                user.Email
                            });
                        }
                        //todo expand module to use signalr for all game list actions
                        PushHelper singleton = PushHelper.GetPushEngine();
                        singleton.UpdateMyGamesList(game.Players.Where(p => p.UserName != User.Identity.Name).Select(p => p.UserName).ToList(), gameUrl, game.Id);
                        return(Redirect("~/Game/Play/" + gameResponse.Game.Id));
                    }
                    else
                    {
                        ViewBag.Message = gameResponse.Message;
                        ViewBag.Title   = gameResponse.Title;
                        return(View("GameError"));
                    }
                }
            }
        }