public void AddBanter(string message, int gameId, int userTeamId, string who) { string name = Context.User.Identity.Name; foreach (var connectionId in _connections.GetConnections(who)) { Clients.Client(connectionId).testAdd(name + ": " + message, gameId); } try { _service.AddBanter(gameId, userTeamId, message); var _updatedBanter = _service.GetGameBanter(1); BantersDto _bantersDto = new BantersDto() {Banter = _updatedBanter, CurrentGameId = gameId, CurrentUserTeamId = userTeamId}; // this method i create a dummy controller that can return a partialview to the page - thus i can actually get ALL the banter and refresh the entire view instead of appending bits... //http://stackoverflow.com/questions/22098233/partialview-to-html-string var bogusController = ViewRenderer.CreateController<HomeController>(); // var partialViewGuts = ViewRenderer.RenderPartialViewToString(bogusController.ControllerContext, "view", model); var partialViewGuts = ViewRenderer.RenderPartialView("~/Views/ViewPlayers/banterlist.cshtml", _bantersDto, bogusController.ControllerContext); Clients.All.addNewBanterToPage(partialViewGuts); } catch (Exception ex) { Clients.All.addNewBanterToPage("There has been a problem getting the banter"); } }
private TeamOverViewViewModel GetTeamOverViewVm(int userTeamId, int? gameId, string footballClub, string searchString, bool getNextTeam) { BantersDto _bantersDto = new BantersDto() {Banter = new List<Banter>(), CurrentGameId = (gameId==null) ? 0 : (int)gameId, CurrentUserTeamId = userTeamId}; IEnumerable<SelectListItem> _clubs = _players.GetList().OrderBy(p => p.Club).Select(p => new SelectListItem(){Value=p.Club, Text =p.Club }).Distinct(); // if userTeamId==0, just return banter & clubs. if (userTeamId == 0) { return new TeamOverViewViewModel() { PlayersSearchedFor = null, PlayersChosen = null, OtherTeams = null, GameId = 0, GameName = null, GameCurrentOrderPosition = 0, UserTeamId = 0, UserName = null, UserId = 0, UserTeamOrderPosition = 0, AddedPlayerMessage = null, HasEnded = false, Banters = _bantersDto, //_banterForThisGame, Clubs = _clubs }; } // else get the userteam & other data for full TeamOverView else { try { // setup message to be displayed to User once they have added their chosen player string _addedPlayerMessage = "Current player is {0}. There are {1} turns left before your go."; int _tmpTurnsLeft = 0; UserTeam _userTeam = null; UserProfile _user = null; IEnumerable<UserTeam_Player> _playersPickedForThisTeam = null; IEnumerable<PlayerDto> _playersNotPickedForAnyTeam = new List<PlayerDto>(); Game _game = null; // get this UserTeam, and from it, the User and current Game (the gameid param seems redundant) if (!_userTeams.TryGet(out _userTeam, x => x.Id == userTeamId)) throw new ApplicationException(); if (!_users.TryGet(out _user, x => x.UserId == _userTeam.UserId)) throw new ApplicationException(); if (!_games.TryGet(out _game, x => x.Id == _userTeam.GameId)) throw new ApplicationException(); IEnumerable<UserTeam> _otherUserTeams = _userTeams.GetList(u => u.GameId == _userTeam.GameId); List<UserTeam> _test = _otherUserTeams.ToList(); if (_game.HasEnded) { //if we want the NEXT userTeam (rather than current), we need to find out where we are in the order of play. if (getNextTeam) { int _utCount = _game.UserTeams.Count; int _orderToGet = (_userTeam.OrderPosition + 1 == _utCount) ? 0 : _userTeam.OrderPosition + 1; UserTeam _nextUserTeam = _userTeams.Get(u => u.OrderPosition == _orderToGet && u.GameId == _game.Id); _userTeam = _nextUserTeam; } _addedPlayerMessage = "The game has ended."; //_banterForThisGame = _banters.GetList(b => b.GameId == _userTeam.GameId); //_banterForThisGame = GetGameBanter(_userTeam.GameId); _bantersDto = new BantersDto() { Banter = GetGameBanter(_userTeam.GameId), CurrentGameId = _userTeam.GameId, CurrentUserTeamId = userTeamId }; _playersPickedForThisTeam = GetAllChosenUserTeamPlayersForTeam(_userTeam.Id); _playersNotPickedForAnyTeam = GetPlayersNotPickedForAnyTeam(_game.Id, footballClub, searchString); } else { //if we want the NEXT userTeam (rather than current), we need to find out where we are in the order of play. if(getNextTeam) { int _utCount = _game.UserTeams.Count; int _orderToGet = (_userTeam.OrderPosition + 1 == _utCount) ? 0 : _userTeam.OrderPosition + 1; UserTeam _nextUserTeam = _userTeams.Get(u => u.OrderPosition == _orderToGet && u.GameId == _game.Id); _userTeam = _nextUserTeam; } // setup the TeamOverViewViewModel properties _bantersDto = new BantersDto() { Banter = GetGameBanter(_userTeam.GameId), CurrentGameId = _userTeam.GameId, CurrentUserTeamId = userTeamId }; _playersPickedForThisTeam = GetAllChosenUserTeamPlayersForTeam(_userTeam.Id); _playersNotPickedForAnyTeam = GetPlayersNotPickedForAnyTeam(_game.Id, footballClub, searchString); List<UserTeam> _userTeamsForGame = _userTeams.GetList(x => x.GameId == _game.Id).ToList(); // this next bit is to set the 'turns left' part of the addedplayer message // i don't really need it.... // if it's NOT the user's turn if (_userTeam.OrderPosition != _game.CurrentOrderPosition) { // get the user who's turn it actually is UserTeam _activeUt = _userTeams.Get( s => s.OrderPosition == _game.CurrentOrderPosition && s.GameId == _game.Id, u => u.User); // then get their username string _tmpActiveUserName = _users.Get(x => x.UserId == _activeUt.UserId).UserName; // set a message to display the number of turns left until the user's turn switch (_activeUt.OrderPosition > _userTeam.OrderPosition) { case true: _tmpTurnsLeft = (_userTeam.OrderPosition == _userTeamsForGame.Count) ? 1 : _activeUt.OrderPosition - _userTeam.OrderPosition; break; case false: _tmpTurnsLeft = (_userTeam.OrderPosition == _userTeamsForGame.Count) ? 1 : _userTeam.OrderPosition - _activeUt.OrderPosition; break; } _addedPlayerMessage = String.Format(_addedPlayerMessage, _tmpActiveUserName, _tmpTurnsLeft); } } return new TeamOverViewViewModel() { PlayersSearchedFor = _playersNotPickedForAnyTeam, PlayersChosen = _playersPickedForThisTeam, OtherTeams = _otherUserTeams, GameId = _userTeam.GameId, GameName = _game.Name, GameCurrentOrderPosition = _game.CurrentOrderPosition, UserTeamId = _userTeam.Id, UserId = _userTeam.UserId, //UserName = _user.UserName, UserName = _userTeam.User.UserName, UserTeamOrderPosition = _userTeam.OrderPosition, AddedPlayerMessage = _addedPlayerMessage, HasEnded = _game.HasEnded, Banters = _bantersDto, //_banterForThisGame, Clubs = _clubs }; } catch (Exception _exp) { OperationStatus.CreateFromException( String.Format("GetTeamOverViewViewModel failed. userteam:{0}, footballclub:{1}, searchstring:{2}", userTeamId, footballClub, searchString), _exp, true); throw; } } }