public void MakeGuess_WhenLastMoveAndNoWinner_ShouldReturnStateGameOver() { User user=new User() { SessionKey="10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id=1 }; Game updatedGame = new Game(); List<Guess> guesses = new List<Guess>(); guesses.Add(new Guess() { Position = 1, User=user }); mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { MovesLeft=1, Guesses = guesses, GameStatus = "InProgress", UserInTurn = 1, RedUserId = 1 }); mock.Setup(g => g.Users.GetAll()).Returns(new User[]{ user }.AsQueryable()); mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game); GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 2); guessService.MakeGuess(guessModel, out gameState); Assert.IsTrue(gameState.gameOver); Assert.IsNull(gameState.Winner); Assert.AreEqual(updatedGame.GameStatus, "Finished"); }
public ActionResult Index(User player, string key, Dictionary<string, string> move) { ViewBag.Message = ""; var existing = player; var board = DB.Games(key) ?? new Game { Key = key, Next = "X" }; int x = -1, y = -1; if (move != null) { foreach (var item in move) { var keys = item.Key.Split('-'); if (keys.Length != 2) continue; x = int.Parse(keys[0]); y = int.Parse(keys[1]); } } if (board.isValid(x, y)) { board.assignPlayer(player); if (!board.isCurrentPlay(player)) { ViewBag.Message = "Not your Play!"; } else { if (!board.isMarked(x, y)) { board.mark(x, y, player); } else { ViewBag.Message = "That Cell is already marked"; } } } var won = board.isWin(); if (won != string.Empty) { if (won == player.Marker) ViewBag.Message = "You Won!"; else ViewBag.Message = "You Lost!"; } //DB.User(existing.SessionID, existing); DB.Games(key, board); ViewBag.player = existing; return View(board); }
public static void ValidateUserInGame(Game game,User user) { if(game==null||user==null) { throw new ArgumentNullException("Game or user is null"); } if (game.RedUserId != user.Id && game.BlueUserId != user.Id) { throw new ServerErrorException("User not in game", "INV_GAME_USR"); } }
public void AddUser_WhenFieldsIsValid_ShouldBeInDb() { var username = "******"; User user = new User() { Username = "******", Nickname = "kaluhckua", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5", SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", ConnectionId = "5262b767-b2cc-46a9-9142-10fef01ded1e", }; uowData.Users.Add(user); uowData.SaveChanges(); var actual = dbContext.Set<User>().Find(user.Id); Assert.IsNotNull(actual); Assert.AreEqual(actual.Username, username); }
public void CreateUser_WhenUsernameAlreadyExists_ShouldThrowException() { User[] users = new User[] { new User { Username = "******", Nickname = "nickaname" }, new User { Username = "******", Nickname = "otherNickaname" }, }; UserRegisterModel userModel = new UserRegisterModel() { Username = "******", Nickname = "nickname2", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5", ConnectionId = "75ccd4c3-fd0f-4a1d-80bb-885fb1bb5296", }; mock.Setup(m => m.Users.GetAll()).Returns(users.AsQueryable()); UserService userService = new UserService(mock.Object); userService.CreateUser(userModel); }
public bool isCurrentPlay(User player) { return player.Marker == Next; }
public void assignPlayer(User player) { if (First == null) { First = player; player.Marker = "X"; } else if (Second == null && First.SessionID != player.SessionID) { Second = player; player.Marker = "O"; } else if (First.SessionID == player.SessionID) { player.Marker = "X"; } else if (Second.SessionID == player.SessionID) { player.Marker = "O"; } }
public void GetGameState_WhenWinerIsRedUser_ShouldReturnGameState() { User redUser = new User() { SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 1, Nickname = "RedUser", }; User blueUser = new User() { SessionKey = "20043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 2, Nickname = "BlueUser", }; mock.Setup(g => g.Users.GetAll()).Returns(new User[] { redUser }.AsQueryable()); mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { GameStatus = "InProgress", RedUserId = 1, BlueUserId = 2, RedUser = redUser, BlueUser = blueUser, Title = "Title", Winner = redUser, }); GameService gameService = new GameService(mock.Object); GameState gameState = gameService.GetGameState(1, "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef"); Assert.AreEqual("RedUser",gameState.Winner); }
public void GetGameState_WhenUserInGameIsRedUserAndNoWinner_ShouldReturnGameState() { User redUser=new User() { SessionKey="10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id=1, Nickname="RedUser", }; User blueUser = new User() { SessionKey = "20043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 2, Nickname="BlueUser", }; mock.Setup(g => g.Users.GetAll()).Returns(new User[]{redUser}.AsQueryable()); List<Guess> guesses = new List<Guess>(); guesses.Add(new Guess() { Id=1, Position=1, User=redUser }); guesses.Add(new Guess() { Id = 2, Position = 2, User = redUser }); guesses.Add(new Guess() { Id = 3, Position = 3, User = blueUser }); mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { GameStatus="InProgress", RedUserId=1, BlueUserId=2, RedUser=redUser, BlueUser=blueUser, Guesses=guesses, Title="Title", Winner=null, }); GameService gameService = new GameService(mock.Object); GameState gameState = gameService.GetGameState(1, "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef"); Assert.AreEqual("O",gameState.Symbol); Assert.AreEqual("BlueUser", gameState.Opponent); Assert.AreEqual("InProgress", gameState.State); Assert.AreEqual("Title", gameState.Title); Assert.IsNull(gameState.Winner); Assert.AreEqual(2, gameState.O.Count()); Assert.AreEqual(1, gameState.X.Count()); }
public void LoginUser_WhenUserLoginModelIsValid_ShouldReturnSessionKeyAndNickname() { User updatedUser = new User(); UserLoginModel loginModel = new UserLoginModel() { AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5", Username = "******", ConnectionId = "newConnectionId" }; UserService userService = new UserService(mock.Object); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { new User { Username = "******", Nickname = "nickname", ConnectionId = "75ccd4c3-fd0f-4a1d-80bb-885fb1bb5296", SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5" }, }.AsQueryable()); mock.Setup(u => u.Users.Update(It.IsAny<User>())).Callback((User user) => updatedUser = user); string nickname; string sessionKey = userService.LoginUser(loginModel, out nickname); Assert.AreEqual("nickname",nickname); Assert.IsNotNull(sessionKey); Assert.AreEqual(loginModel.ConnectionId, updatedUser.ConnectionId); }
public void MakeGuess_WhenPlacedMaerkerAndGameIsNotOver_ShouldUpdateGame() { User redUser = new User() { SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 1 }; User blueUser = new User() { SessionKey = "20043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 2 }; Game updatedGame = new Game(); List<Guess> guesses = new List<Guess>(); guesses.Add(new Guess() { Position = 1, User = blueUser }); mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { MovesLeft = 8, Guesses = guesses, GameStatus = "InProgress", UserInTurn = 1, RedUser=redUser, RedUserId = 1, BlueUser=blueUser, BlueUserId=2 }); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { redUser }.AsQueryable()); mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game); GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 2); guessService.MakeGuess(guessModel, out gameState); Assert.AreEqual(2, updatedGame.UserInTurn); Assert.IsFalse(gameState.gameOver); Assert.IsNull(gameState.Winner); Assert.AreEqual(updatedGame.GameStatus, "InProgress"); }
/// <summary> /// /// </summary> /// <param name="game"></param> /// <param name="position">The position where to place the marker, should be between 0 and 9</param> /// <returns>True if the marker position was not already taken</returns> private void PlaceMarker(Game game, User user, int position) { int count = game.Guesses.Where(g => g.Position == position).Count(); if (count != 0) { throw new ServerErrorException("The position is occupied", "INV_POS"); } Guess newGuess = new Guess() { Position = position, User = user, Game = game, }; game.Guesses.Add(newGuess); game.MovesLeft--; }
public void SaveConnectionId_WhenDataIsValid_ShouldUpdataConnectionId() { Mock<IUowData> mock = new Mock<IUowData>(); User updatedUser = new User(); UserService userService = new UserService(mock.Object); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { new User { SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL" }, }.AsQueryable()); mock.Setup(u => u.Users.Update(It.IsAny<User>())).Callback((User user) => updatedUser = user); userService.SaveConnectionId("100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", "1234"); Assert.AreEqual("1234",updatedUser.ConnectionId ); }
public void LogoutUser_WhenCompletedLogin_ShouldSetSessinKeyToNull() { User updatedUser = new User(); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { new User { Id=1, Username = "******", Nickname = "nickname", ConnectionId = "75ccd4c3-fd0f-4a1d-80bb-885fb1bb5296", SessionKey = "100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL", AuthCode = "80a63648010756ed51eecccf94c01bc0015048c5" }, }.AsQueryable()); mock.Setup(g => g.Users.Update(It.IsAny<User>())).Callback((User user) => updatedUser = user); UserService userService = new UserService(mock.Object); userService.LogoutUser("100431CZhiZTwwJAh8VTo559HfIyYf8lXyq74Bi2UkBP64ZoLL"); Assert.IsNull(updatedUser.SessionKey); }
public void mark(int x, int y, User player) { Data[x][y] = player.Marker; Next = Next == "X" ? "O" : "X"; }
public static User User(string key, User user) { System.Web.HttpContext.Current.Cache["user" + key] = user; return user; }
public void CreateUser(UserRegisterModel registerUser) { if (registerUser == null) { throw new ArgumentNullException("UserRegisteModel is null"); } ValidateUsername(registerUser.Username); ValidateNickname(registerUser.Nickname); ValidateAuthCode(registerUser.AuthCode); var usernameToLower = registerUser.Username.ToLower(); var nickmameToLower = registerUser.Nickname.ToLower(); var user = this.Data.Users.GetAll().FirstOrDefault(u => u.Username.ToLower() == usernameToLower || u.Nickname.ToLower() == nickmameToLower); if (user != null) { if (user.Username.ToLower() == usernameToLower) { throw new ServerErrorException("Username already exists", "ERR_DUP_USR"); } if (user.Nickname.ToLower() == nickmameToLower) { throw new ServerErrorException("Nickname already exists", "ERR_DUP_NICK"); } } User newUser = new User() { Username = registerUser.Username, Nickname = registerUser.Nickname, AuthCode = registerUser.AuthCode, ConnectionId = registerUser.ConnectionId, }; this.Data.Users.Add(newUser); this.Data.SaveChanges(); }
public void MakeGuess_WhenPlaceThirdRow_ShouldWinning() { User redUser = new User() { SessionKey = "10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", Id = 1 }; Game updatedGame = new Game(); List<Guess> guesses = new List<Guess>(); guesses.Add(new Guess() { Position = 7, User = redUser }); guesses.Add(new Guess() { Position = 8, User = redUser }); mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { MovesLeft = 9, Guesses = guesses, GameStatus = "InProgress", UserInTurn = 1, RedUserId = 1 }); mock.Setup(g => g.Users.GetAll()).Returns(new User[] { redUser }.AsQueryable()); mock.Setup(u => u.Games.Update(It.IsAny<Game>())).Callback((Game game) => updatedGame = game); GuessModel guessModel = new GuessModel("10043IOvy7N9Bn9BDAk2mtT7ZcYKtZbBpdp00ZoIpJikyIJtef", 1, 9); guessService.MakeGuess(guessModel, out gameState); Assert.AreEqual(1, updatedGame.Winner.Id); Assert.IsTrue(gameState.gameOver); }
public void GetCreator_WhenGameIsValid_ShouldReturnCreatorUser() { User redUser = new User() { Nickname = "RedUser", }; mock.Setup(g => g.Games.GetById(It.IsAny<int>())).Returns( new Game() { RedUser = redUser, }); GameService gameService = new GameService(mock.Object); User creator = gameService.GetCreator(1); Assert.AreEqual("RedUser", creator.Nickname); }
public void ValidateUserInGame_WhenRedUserIsUser_ShouldThrowException() { User redUser = new User() { Id = 1, }; Game game = new Game(); game.RedUserId = 1; BaseService.ValidateUserInGame(game, redUser); }
public void UpdateUserInTurn(Game game, User user, User opponent) { if (game.UserInTurn == user.Id) { Clients.Client(user.ConnectionId).waitingForMarkerPlacement(game.Id); Clients.Client(opponent.ConnectionId).waitingForOpponent(game.Id, "play"); } else { Clients.Client(opponent.ConnectionId).waitingForMarkerPlacement(game.Id); Clients.Client(user.ConnectionId).waitingForOpponent(game.Id, "play"); } }