コード例 #1
0
        public ActionResult TakeGameAction(int id, GameActionState gameAction, string actionLocation = "")
        {
            using (var galleristContext = new GalleristComponentsDbContext())
            {
                using (var identityContext = new ApplicationDbContext()) //may not need this anymore after adding the authorize helper
                {
                    var gameResponse = GameManager.GetGame(id, galleristContext);
                    var game         = gameResponse.Game;

                    //todo todo todo
                    //make sure the player taking an action is the current player
                    //compare current action to game state to make sure a valid action was taken (e.g. player can't move to board spot A from board spot A) [states..]
                    //todo make getting user by username a method (is it one already?)
                    var currentUser = identityContext.Users.FirstOrDefault(m => m.UserName == User.Identity.Name);

                    //todo refactor some of this into a game logic module
                    var player = game.Players.FirstOrDefault(p => p.UserId == currentUser.Id);
                    if (player == null)
                    {
                        return(View("GameError"));
                    }
                    if (player.Id != game.CurrentPlayerId)
                    {
                        return(View("GameError"));
                    }

                    var actionInvoker = new ActionContextInvoker(game);

                    if (!actionInvoker.DoAction(gameAction, actionLocation))
                    {
                        return(Redirect("~/Game/Play/" + id));
                    }

                    //check if it is one of the special cases where the action must be confirmed before allowing the next step to proceed (e.g. player must draw cards)
                    //if yes take an intermediate step, still remains current player's turn
                    //if no, continue doing logic things  //determine order of bumped player's actions, can these happen at the end of current player's turn?
                    //check player locations for action taken by current player to see if any players need to be "bumped" to another spot
                    //somewhere in here we need to inject bumped player into turn order, we also need a way to specify that the current "turn" for a bumped player is not a full turn [bumped turn flag or something]
                    //let said player take bumped turn if necessary
                    //again do the updatey
                    //need some signalr stuff so we can show the action to everyone when it is done (intermediate step or not) as well as update money, influence, board, etc.
                    //update money, influence, board, etc.
                    galleristContext.SaveChanges();

                    var nextPlayer = identityContext.Users.First(m => m.Id == game.CurrentPlayer.UserId);
                    if (nextPlayer.AllowsEmails)
                    {
                        var gameUrl    = Request.Url.GetLeftPart(UriPartial.Authority) + "/Game/Play/" + game.Id;
                        var emailTitle = nextPlayer.UserName + ", it is your turn!";
                        var emailBody  = "It is your turn in a game you are playing. You can take your turn by visiting The Gallerist Online" +
                                         " and viewing your active games or by clicking the following link: " + gameUrl;

                        EmailManager.SendEmail(emailTitle, emailBody, new List <string> {
                            nextPlayer.Email
                        });
                    }
                    PushHelper singleton = PushHelper.GetPushEngine();
                    singleton.RefreshGame(game.Players.Where(p => p.UserName != User.Identity.Name).Select(p => p.UserName).ToList());
                    return(Redirect("~/Game/Play/" + id));
                }
            }
        }