public PropertyActionModel SetPropertyState(Property property, BoardUser bu) { PropertyActionModel response = new PropertyActionModel(); response.PropertyCost = property.Price; response.PropertyName = property.Name; if (property.Group == "No-Group") { response.isChanceorCommunity = true; } else { response.isChanceorCommunity = false; response.isPropertyPurchasable = property.User == null && bu.Money >= property.Price; } if (property.Group != "Utilities" && property.Group != "Railroad" && property.Group != "No-Group") { response.CanBuildHouse = bu.CanBuildHouse(property); response.CanBuildHotel = bu.CanBuildHotel(property); response.HouseCost = property.GetHouseCost(); response.HotelCost = property.GetHotelCost(); } else { response.CanBuildHouse = false; response.CanBuildHotel = false; } return(response); }
public async Task <IActionResult> CreateBoard(BoardCreateModel boardModel) { var board = _mapper.Map <Board>(boardModel); if (_context.Boards.Any(b => b.UrlName.Equals(board.UrlName))) { return(BadRequest("Friendly name is already taken")); } var regUser = await _context.RegisteredUsers.FindAsync(User.GetUserId()); board.OwnerId = regUser.Id; var boardUser = new BoardUser { RegisteredUserId = board.OwnerId, Name = boardModel.UserDisplayName }; board.Users = new List <BoardUser> { boardUser }; await _context.Boards.AddAsync(board); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(Board), new { id = board.Id }, _mapper.Map <BoardModel>(board))); }
public BoardUser AssignBoardUser(BoardUser boardUser) { try { { var newBoardUserEntityEntry = _context.BoardUsers.Add(boardUser); if (newBoardUserEntityEntry != null && newBoardUserEntityEntry.State == EntityState.Added) { var affectedRows = _context.SaveChanges(); if (affectedRows > 0) { _logger.LogInformation($"The {boardUser.ApplicationUser} was added to {boardUser.Board.Name}."); return(newBoardUserEntityEntry.Entity); } } } _logger.LogWarning($"The board or user was null."); return(null); } catch (Exception ex) { _logger.LogError(ex, $"When adding user to board."); throw; } }
public IHttpActionResult PutBoardUser(int id, BoardUser boardUser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != boardUser.BoardId) { return(BadRequest()); } db.Entry(boardUser).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!BoardUserExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public BoardUser RemoveBoardUser(BoardUser boardUser) { try { { var removeBoardUserEntityEntry = _context.BoardUsers.Update(boardUser); if (removeBoardUserEntityEntry != null && removeBoardUserEntityEntry.State == EntityState.Modified) { var affectedRows = _context.SaveChanges(); if (affectedRows > 0) { _logger.LogInformation($"The {boardUser.ApplicationUser} was set to false from {boardUser.Board.Name}."); return(removeBoardUserEntityEntry.Entity); } } } _logger.LogWarning($"The board or user was null."); return(null); } catch (Exception ex) { _logger.LogError(ex, $"When setting user to false from {boardUser.Board.Name}."); throw; } }
public IHttpActionResult PostBoardUser(BoardUser boardUser) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.BoardUsers.Add(boardUser); try { db.SaveChanges(); } catch (DbUpdateException) { if (BoardUserExists(boardUser.BoardId)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = boardUser.BoardId }, boardUser)); }
public Board InviteUsersToBoard(int id, string userName, User user) { // set role of the board user as "PendingInvitee" as below Board board = null; var retrievedUserId = _kanbanContext.Users.Where(u => u.Username == userName).First().UserId; var owner = _kanbanContext.BoardUsers.Where(b => b.BoardId == id && b.UserId == retrievedUserId && b.Role == BoardUserRole.Owner.ToString()).First(); if (owner != null) { var inUser = _kanbanContext.Users.Where(u => u.Username == user.Username).FirstOrDefault(); if ((inUser != null)) { var boardUser = new BoardUser() { BoardId = id, UserId = inUser.UserId, Role = BoardUserRole.PendingInvitee.ToString() }; _kanbanContext.BoardUsers.Add(boardUser); _kanbanContext.SaveChanges(); board = _kanbanContext.Boards.Include(u => u.BoardUsers).Where(u => u.BoardId == id).First(); } } return(board); }
public IHttpActionResult BuildHotel(EndMoveModel m) { Board board = db.Boards.Find(m.BoardId); BoardUser bu = board.GetBoardUser(board.ActiveBoardPlayer.UserName); Property p = board.GetPropertyWithPos(bu.Position); bu.BuildHotel(p); Move newMove = new Move(); newMove.Roll = 0; newMove.Board = board; newMove.Description = board.ActiveBoardPlayer.UserName + " built a hotel."; newMove.UserName = board.ActiveBoardPlayer.UserName; newMove.User = board.ActiveBoardPlayer; board.Moves.Add(newMove); MoveResponseModel response = new MoveResponseModel(); response.move = newMove; response.ActivePlayer = board.ActiveBoardPlayer; try { db.SaveChanges(); } catch (DbUpdateException) { throw; } return(CreatedAtRoute("DefaultApi", null, response)); }
public BoardUser CreateBoardUser(BoardUser boardUser) { _unitOfWork.Repository <BoardUser>().Update(boardUser); _unitOfWork.Complete(); return(boardUser); }
// GET: api/Game/{id}/GetActivePlayer public ActivePlayerModel GetActivePlayer(int id) { Board board = db.Boards.Find(id); ActivePlayerModel response = new ActivePlayerModel(board); response.user = board.ActiveBoardPlayer; BoardUser bu = board.GetBoardUser(board.ActiveBoardPlayer.UserName); response.HasRolled = bu.HasRolled; Property property = (from p in board.Properties where p.Position == bu.Position select p).FirstOrDefault(); response.propertyState = SetPropertyState(property, bu); if (response.isGameOver) { try { db.SaveChanges(); } catch (DbUpdateException) { throw; } } return(response); }
public void HasPermission_AllCardAndActivityPermissions_IsCollaborator_ReturnsTrue(BoardUserPermission permission) { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.Collaborator); var result = service.HasPermission(boardUser, permission); Assert.True(result); }
public void HasPermission_CanViewBoard_IsCollaborator_ReturnsTrue() { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.Collaborator); var result = service.HasPermission(boardUser, BoardUserPermission.CAN_VIEW_BOARD); Assert.True(result); }
public void HasPermission_IsOwner_ReturnsTrue(BoardUserPermission permission) { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.Owner); var result = service.HasPermission(boardUser, permission); Assert.True(result); }
public void HasPermission_PermissionNotValid_ThrowsInvalidOperationException() { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.Collaborator); void action() => service.HasPermission(boardUser, (BoardUserPermission)100000000); Assert.Throws <InvalidOperationException>(action); }
public void HasPermission_IsNone_ReturnsFalse(BoardUserPermission permission) { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.None); var result = service.HasPermission(boardUser, permission); Assert.False(result); }
public void HasPermission_CanDeleteBoard_IsCollaborator_ReturnsFalse() { var service = new BoardUserPermissionService(); var boardUser = new BoardUser(1, "user-id", BoardUserType.Collaborator); var result = service.HasPermission(boardUser, BoardUserPermission.CAN_DELETE_BOARD); Assert.False(result); }
public SuccessResponse <BoardUserDto> CreateBoardUser([FromBody] CreateBoardUserDto boardUserDto, Guid id) { var model = new BoardUser { BoardId = id, UserId = boardUserDto.UserId, MemberType = BoardMemberType.Member }; var res = _boardService.CreateBoardUser(model); return(new SuccessResponse <BoardUserDto>(_mapper.Map <BoardUserDto>(res))); }
public static void Print(this BoardUser bu) { Console.WriteLine("Username: {0}", bu.UserName); Console.WriteLine("${0}", bu.Money); Console.WriteLine("Position: {0}", bu.Position); Console.WriteLine("Turn: {0}", bu.Turn); Console.WriteLine("InJail: {0}", bu.InJail); Console.WriteLine("HasGetOutOfJail: {0}", bu.HasGetOutOfJail); Console.WriteLine("GameOver: {0}", bu.GameOver); }
public static void SellHotel(BoardUser bu, Property p) { if (p.NumHotels > 0) { int hotelPrice = (int)(p.Price * Property.HotelCostPercentage); bu.Money += hotelPrice; p.NumHotels = 0; } }
public static bool CanBuildHotel(this BoardUser bu, Property p) { int hotelPrice = (int)(p.Price * Property.HotelCostPercentage); if (OwnsGroup(bu.User, bu.Board, p.Group) && p.NumHouses >= 4 && bu.Money >= hotelPrice) { return(true); } return(false); }
public void ChangeType_InvalidBoardUserType_ThrowsInvalidOperationException() { var boardId = 1; var userId = "user-id"; var type = BoardUserType.Owner; var boardUser = new BoardUser(boardId, userId, type); void action() => boardUser.ChangeType((BoardUserType)1000); Assert.Throws <InvalidOperationException>(action); }
public static void SellHouse(this User user, Board b, Property p, int n = 1) { BoardUser bu = b.GetBoardUser(user.UserName); if (p.NumHouses > 0) { int housePrice = (int)(p.Price * Property.HouseCostPercentage); bu.Money += n * housePrice; p.NumHouses -= n; } }
public void ChangeType_UpdatesTypeValue() { var boardId = 1; var userId = "user-id"; var type = BoardUserType.Owner; var newType = BoardUserType.Collaborator; var boardUser = new BoardUser(boardId, userId, type); boardUser.ChangeType(newType); Assert.Equal(newType, boardUser.Type); }
public void Ctor_CreateBoardUserWithExpectedPropertyValues() { var boardId = 1; var userId = "user-id"; var type = BoardUserType.Owner; var boardUser = new BoardUser(boardId, userId, type); Assert.Equal(boardId, boardUser.BoardId); Assert.Equal(userId, boardUser.UserId); Assert.Equal(type, boardUser.Type); }
public IHttpActionResult BuyProperty(BuyPropertyModel m) { Board board = db.Boards.Find(m.BoardId); if (board == null) { return(null); } Move newMove = new Move(); newMove.User = board.ActiveBoardPlayer; BoardUser boardUser = board.GetBoardUser(board.ActiveBoardPlayer.UserName); int position = boardUser.Position; Property property = (from p in board.Properties where p.Position == position select p).FirstOrDefault(); if (property.User == null) { // give player option to buy if they have enough money if (boardUser.Money >= property.Price) { boardUser.Money -= property.Price; property.User = board.ActiveBoardPlayer; newMove.Description = board.ActiveBoardPlayer.UserName + " purchased " + property.Name; } else { newMove.Description = board.ActiveBoardPlayer.UserName + " does not have enough money to buy " + property.Name; } board.Moves.Add(newMove); try { db.SaveChanges(); } catch (DbUpdateException) { throw; } return(CreatedAtRoute("DefaultApi", null, newMove)); } else { return(null); } }
public IHttpActionResult DeleteBoardUser(int id) { BoardUser boardUser = db.BoardUsers.Find(id); if (boardUser == null) { return(NotFound()); } db.BoardUsers.Remove(boardUser); db.SaveChanges(); return(Ok(boardUser)); }
public static Move EndCurrentPlayerTurn(this Board b) { User player = b.GetPlayerWithCurrentTurn(); Move newMove = new Move(); newMove.Roll = 0; newMove.Board = b; newMove.Description = player.UserName + " ended their Turn."; newMove.UserName = player.UserName; newMove.User = player; if (b.Moves == null) { b.Moves = new Collection <Move>(); } var nextPlayer = b.GetUserWithNextTurn(); if (nextPlayer != null) { b.ActiveBoardPlayer = nextPlayer; } var otherPlayer = b.GetUserWithNextFirstTurn(player); if (otherPlayer != null) { b.ActiveBoardPlayer = otherPlayer; } else { // All 1st turns completed get user with highest Roll Value if (b.Moves.Last().IsFirstMove == true) { b.ActiveBoardPlayer = (from m in b.Moves where m.IsFirstMove == true orderby m.Roll descending select m.User).Distinct().FirstOrDefault(); } } BoardUser bu = b.GetBoardUser(player.UserName); bu.HasRolled = false; b.Moves.Add(newMove); return(newMove); }
// GET: api/Game/{id}/GetPropInfo/{propId} public PropertyActionModel GetPropInfo(int id, int position) { Board board = db.Boards.Find(id); BoardUser bu = board.GetBoardUser(board.ActiveBoardPlayer.UserName); Property property = (from p in board.Properties where p.Position == position select p).FirstOrDefault(); return(SetPropertyState(property, bu)); }
/// <summary> /// Return the number of players still in the game. /// </summary> /// <param name="b"></param> /// <returns></returns> public static int GetNumberofActivePlayers(this Board b) { int count = 0; foreach (User user in b.Users) { BoardUser bu = b.GetBoardUser(user.UserName); if (!bu.GameOver) { ++count; } } return(count); }
public bool OnPost() { string action = Request.Form["action"]; string username = Request.Form["username"]; string password = Request.Form["password"]; if (username.Length > 50) { username = username.Substring(0, 50); } if (password.Length > 50) { password = password.Substring(0, 50); } switch (action) { case "login": try { var user = db.BoardUsers.Where( u => u.username == username && u.password == password ).Single(); loginSuccessful(user); return(true); } catch { return(false); } case "register": try { var newUser = new BoardUser { username = username, password = password, }; db.BoardUsers.Add(newUser); db.SaveChanges(); loginSuccessful(newUser); return(true); } catch { return(false); } default: return(false); } }