public ActionResult Spectate(Int32 id) { String key = String.Format("Game_{0}_Passphrase", id); String passphrase = String.Empty; Int32 currentUserId = Authentication.Security.CurrentUserId; if (Session[key] != null) { passphrase = Encoding.ASCII.GetString(MachineKey.Unprotect((Session[key] as Byte[]), Session.SessionID)); } Entities.User user = _selectUser.Execute(currentUserId); Entities.JoinResponse response = _joinGame.Execute(id, user, passphrase, Entities.Enums.GamePlayerType.Spectator); if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.GameOver)) { return(Redirect("/GameListing")); } if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.BadPassphrase) == false && response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.SpectatorsFull) == false) { Entities.Models.Game.Board.GameBoard model = new Entities.Models.Game.Board.GameBoard(response.Game, currentUserId, Entities.Enums.GamePlayerType.Spectator); return(View("~/Views/Game/Board/Index.cshtml", model)); } else { return(Redirect(Url.Action("Index", "GameListing", new { id = id }))); } }
/// <summary> /// Join a game /// </summary> /// <param name="gameID">The id of the game to join</param> /// <param name="user">The current user</param> /// <param name="passphrase">The passphrase for the game</param> /// <param name="playerType">Type of player joining</param> /// <returns>The response to a join request</returns> public Entities.JoinResponse Execute(Int32 gameID, Entities.User user, String passphrase, Entities.Enums.GamePlayerType playerType) { Entities.Filters.Game.Select filter = new Entities.Filters.Game.Select(); filter.GameID = gameID; filter.DataToSelect |= Entities.Enums.Game.Select.Rounds; filter.DataToSelect |= Entities.Enums.Game.Select.GamePlayerCards; Entities.Game game = _selectGame.Execute(filter); Entities.JoinResponse response = _joinGame.Execute(game, user, passphrase, playerType); if (response.Game != null) { if (response.Game.IsWaiting() && response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.SuccessfulAlreadyPlayer) == false) { _sendMessage.UpdateWaiting(response.Game, true); } else if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.NewRoundStart) == true) { _sendMessage.UpdateGame(response.Game, true); } else { _sendMessage.UpdateLobby(response.Game, true); } } return(response); }
/// <summary> /// Join a game /// </summary> /// <param name="gameID">The game to join</param> /// <param name="user">The current user</param> /// <param name="passphrase">The passphrase for the game</param> /// <param name="playerType">Type of player joining</param> /// <returns>The response to a join request</returns> public Entities.JoinResponse Execute(Entities.Game game, Entities.User user, String passphrase, Entities.Enums.GamePlayerType playerType) { Entities.JoinResponse response = new Entities.JoinResponse(); Boolean wasWaiting = game.IsWaiting(); if (game.GameOver.HasValue) { response.Result = Entities.Enums.Game.JoinResponseCode.GameOver; } else { if (playerType == Entities.Enums.GamePlayerType.Spectator) { AsSpectator(game, user, passphrase, response); } else { AsPlayer(game, user, passphrase, response, wasWaiting); } } return(response); }
/// <summary> /// Join a game /// </summary> /// <param name="gameID">The game to join</param> /// <param name="user">The current user</param> /// <param name="passphrase">The passphrase for the game</param> /// <param name="playerType">Type of player joining</param> /// <returns>The response to a join request</returns> public Entities.JoinResponse Execute(Entities.Game game, Entities.User user, String passphrase, Entities.Enums.GamePlayerType playerType) { Entities.JoinResponse response = new Entities.JoinResponse(); Boolean wasWaiting = game.IsWaiting(); if (game.GameOver.HasValue) { response.Result = Entities.Enums.Game.JoinResponseCode.GameOver; } else { if (playerType == Entities.Enums.GamePlayerType.Spectator) { AsSpectator(game, user, passphrase, response); } else { AsPlayer(game, user, passphrase, response, wasWaiting); } } return response; }
private void AsPlayer(Entities.Game game, Entities.User user, String passphrase, Entities.JoinResponse response, Boolean wasWaiting) { if (game.IsCurrentPlayer(user.UserId) == false) { if (_validatePassphrase.Execute(game, passphrase) == false) { response.Result |= Entities.Enums.Game.JoinResponseCode.BadPassphrase; } else if (game.IsFull()) { response.Result |= Entities.Enums.Game.JoinResponseCode.FullGame; } else { Boolean successful = _joinGame.Execute(game, user, Entities.Enums.GamePlayerType.Player); if (successful == false) { response.Result |= Entities.Enums.Game.JoinResponseCode.FullGame; } else { if (wasWaiting && !game.IsWaiting()) { Entities.User newCommander = game.NextCommander(null); if (newCommander != null) { if (_startRound.Execute(game, game.NextCommander(null)) == true) { response.Result |= Entities.Enums.Game.JoinResponseCode.NewRoundStart; } } else { response.Result |= Entities.Enums.Game.JoinResponseCode.WaitingOnWinnerSelection; } } } } } else { response.Result |= Entities.Enums.Game.JoinResponseCode.SuccessfulAlreadyPlayer; } if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.BadPassphrase) == false && response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.FullGame) == false) { response.Game = game; } }
private void AsSpectator(Entities.Game game, Entities.User user, String passphrase, Entities.JoinResponse response) { if (game.IsCurrentSpectator(user.UserId) == false) { if (_validatePassphrase.Execute(game, passphrase) == false) { response.Result |= Entities.Enums.Game.JoinResponseCode.BadPassphrase; } else if (game.MaxSpectatorsReached()) { response.Result |= Entities.Enums.Game.JoinResponseCode.SpectatorsFull; } else { Boolean successful = _joinGame.Execute(game, user, Entities.Enums.GamePlayerType.Spectator); if (successful == false) { response.Result |= Entities.Enums.Game.JoinResponseCode.SpectatorsFull; } } } else { response.Result |= Entities.Enums.Game.JoinResponseCode.SuccessfulAlreadyPlayer; } if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.BadPassphrase) == false && response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.FullGame) == false) { response.Game = game; } }
public ActionResult Index(Int32 id) { String key = String.Format("Game_{0}_Passphrase", id); String leaveGameJobIdKey = String.Format("LeaveGame_{0}_JobId", id); String passphrase = String.Empty; String jobId = String.Empty; Int32 currentUserId = Authentication.Security.CurrentUserId; if (Session[key] != null) { passphrase = Encoding.ASCII.GetString(MachineKey.Unprotect((Session[key] as Byte[]), Session.SessionID)); } if (Session[leaveGameJobIdKey] != null) { jobId = Encoding.ASCII.GetString(MachineKey.Unprotect((Session[leaveGameJobIdKey] as Byte[]), Session.SessionID)); BackgroundJob.Delete(jobId); Session.Remove(leaveGameJobIdKey); } Entities.User user = _selectUser.Execute(currentUserId); Entities.JoinResponse response = _joinGame.Execute(id, user, passphrase, Entities.Enums.GamePlayerType.Player); if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.GameOver)) { return(Redirect("/GameListing")); } if (response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.BadPassphrase) == false && response.Result.HasFlag(Entities.Enums.Game.JoinResponseCode.FullGame) == false) { Entities.Filters.GamePlayerKickVote.SelectForGame kickVoteFilter = new Entities.Filters.GamePlayerKickVote.SelectForGame(); kickVoteFilter.GameID = id; List <Entities.GamePlayerKickVote> votes = _selectKickVotes.Execute(kickVoteFilter); IEnumerable <IGrouping <Int32, Entities.GamePlayerKickVote> > grouped = votes.GroupBy(x => x.KickUserId); Entities.Models.Game.Board.VoteToKick kickModel = null; List <Entities.Models.Game.Board.VoteToKick> votesToKick = new List <Entities.Models.Game.Board.VoteToKick>(); foreach (IGrouping <Int32, Entities.GamePlayerKickVote> group in grouped) { if (group.FirstOrDefault(x => x.VotedUserId == currentUserId) == null) { kickModel = new Entities.Models.Game.Board.VoteToKick(group.First().KickUser, group.Count(x => x.Vote), group.Count(x => !x.Vote)); votesToKick.Add(kickModel); } } List <Entities.GameRound> completedRounds = _selectGameRounds.Execute(new Entities.Filters.GameRound.SelectCompleted(id)); Entities.Models.Game.Board.GameBoard model = new Entities.Models.Game.Board.GameBoard(response.Game, currentUserId, Entities.Enums.GamePlayerType.Player, votesToKick, completedRounds); return(View("~/Views/Game/Board/Index.cshtml", model)); } else { return(Redirect(Url.Action("Index", "GameListing", new { id = id }))); } }