Esempio n. 1
0
        public ActionResult Show(Guid gameId)
        {
            var game = _repository.GetGame(gameId);

            var snapshot = _repository.GetGameSnapshot(game);

            var playerRole = game.GetPlayerRole(_player);

            ViewBag.PlayerRole = playerRole;

            if (game.Player2 == null)
            {
                if (playerRole == PlayerRole.Spectator)
                {
                    ViewBag.PlayerRole = playerRole = PlayerRole.Player2;

                    game.Lock(_player); // player1 chosen as first

                    var playerName = string.Format("Player {0}", _player.Id);

                    var message = _messageFactory.CreateJoinedMessage(gameId, playerName, game.CurrentState);
                    _repository.AddMessage(message);

                    snapshot = new GameSnapshot(game, message);
                    _repository.AddGameSnapshot(snapshot);

                    _repository.Save();

                    // allow player2 to receive the message too
                    return(View(new GameSnapshot(game)));
                }

                return(View(snapshot));
            }

            if (_player.Id != game.Player1.Id && _player.Id != game.Player2.Id)
            {
                return(View("AlreadyFull", gameId));
            }

            return(View(snapshot));
        }