public async Task <ActionResult <Deck> > GetDeck(Guid id) { try { var deck = await deckRepo.GetDecksWithCards(id); var deckDTO = mapper.Map <DeckDTO>(deck); return(Ok(deckDTO)); } catch { return(BadRequest("Something went wrong getting the deck.")); } }
public async Task SetUpNewGame(List <User> users) { try { // Set Czar index to 0 CzarIndex = 0; SelectedCards = new List <CardsAgainstHumanityNoSQL.Models.Card>(); // Save users in this controller lobbyUsers = users; // Create a game document Game game = new Game(); // Give it an id // We generate the id in c# so that we can refer to it during the game currentGameId = ObjectId.GenerateNewId(); game.Id = currentGameId; game.Players = new List <Player>(); // Cycle through the users and add them as players to the game // Set their score to 0 foreach (var user in users) { game.Players.Add(new Player() { Name = user.Name, Score = 0 }); } // Fetch the deck we're going to play with // Using hard coded deck // Makes it possible to upgrade the app with other decks var id = Guid.Parse("fe90fd43-c841-4dcd-a524-78ec08f5a1ad"); Deck deck = await _deckRepo.GetDecksWithCards(id); // Convert ICollection to List List <CardsAgainstHumanity.Models.Card> cards = deck.Cards.ToList(); // Split the deck in a list of white cards and black cards List <CardsAgainstHumanityNoSQL.Models.Card> WhiteCards = new List <CardsAgainstHumanityNoSQL.Models.Card>(); List <CardsAgainstHumanityNoSQL.Models.Card> BlackCards = new List <CardsAgainstHumanityNoSQL.Models.Card>(); foreach (var card in cards) { // Category 1 are black cards if (card.CategoryID == 1) { WhiteCards.Add(new CardsAgainstHumanityNoSQL.Models.Card() { CardID = card.CardID, CardText = card.CardText }); } // Category 2 are black cards if (card.CategoryID == 2) { BlackCards.Add(new CardsAgainstHumanityNoSQL.Models.Card() { CardID = card.CardID, CardText = card.CardText }); } // Category 3 are blank cards and aren't implemented yet // They are not mandatory to play } // Generate a array of integers the length of the amount of cards // This is used to shuffle the cards // Ex: 200 cards will generate a list from 0 to 199. // We then randomly assign these values as the index of the card in the deck int[] valuesWC = Enumerable.Range(0, WhiteCards.Count()).ToArray(); int[] valuesBC = Enumerable.Range(0, BlackCards.Count()).ToArray(); // We keep track of the position index so we can sort the deck // In the NoSQL docs it was said to not rely on the order in the db as a way to sort them game.WhitePositionIndex = WhiteCards.Count(); game.BlackPositionIndex = BlackCards.Count(); foreach (var card in WhiteCards) { // Get random number out of the list of indexes Random rnd = new Random(); int indexOfPosition = rnd.Next(valuesWC.Length); int positionValue = valuesWC[indexOfPosition]; // Assign this to the card card.Position = positionValue; // Delete the index out of the list so it can't be used anymore valuesWC = valuesWC.Where(val => val != positionValue).ToArray(); } foreach (var card in BlackCards) { // Get random number out of the list of indexes Random rnd = new Random(); int indexOfPosition = rnd.Next(valuesBC.Length); int positionValue = valuesBC[indexOfPosition]; // Assign this to the card card.Position = positionValue; // Delete the index out of the list so it can't be used anymore valuesBC = valuesBC.Where(val => val != positionValue).ToArray(); } game.WhiteCards = WhiteCards; game.BlackCards = BlackCards; var obj = await _gameRepo.NewGame(game); } catch (Exception err) { throw (err); } }