public async Task <IActionResult> Patch(Guid id, [FromBody] Deck deck) { using (var repository = new Repository(HttpContext.GetUserGuid())) { if (repository.ServiceUser == null) { return(Forbid()); } await repository.Context.Factions.LoadAsync(); await repository.Context.CardTypes.LoadAsync(); var deckModel = await repository.Context.Decks.Include(x => x.DeckCards) .ThenInclude(dc => dc.Card) .Where(x => x.Guid == id).FirstOrDefaultAsync(); if (deckModel == null) { return(NotFound()); } await repository.Context.Entry(deckModel).Reference(x => x.Creator).LoadAsync(); await repository.Context.Entry(deckModel).Reference(x => x.Status).LoadAsync(); await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); if (repository.ServiceUser.Role.Guid != PredefinedGuids.Administrator && deckModel.Creator.Guid != repository.ServiceUser.Guid) { return(Forbid()); } StatusModel statusModel = null; if (deck.Status != null) { statusModel = await repository.Context.Statuses.FindByGuidAsync(deck.Status.Guid); } deckModel.Patch(deck, statusModel, repository.ServiceUser); deck = deckModel.FromDal(true); System.IO.File.WriteAllText(Repository.GetDeckJsonFile(deck.Guid), JsonConvert.SerializeObject(deck)); System.IO.File.WriteAllText(Repository.GetDeckFormatFile(deck.Guid), GetDeckFormat(deckModel)); await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); var generateMissing = repository.ServiceUser.Guid == PredefinedGuids.Administrator || repository.ServiceUser.Guid == PredefinedGuids.ServiceUser; await repository.Context.SaveChangesAsync(); BackgroundJob.Schedule( () => DeckGenerator.GenerateDeck(deck.Guid, deck.GeneratePdf, deck.ExportTiles, generateMissing, true), TimeSpan.Zero); deck.IsEditable = true; return(Ok(deck)); } }
public void Should_Contain_52_Cards() { var deckGenerator = new DeckGenerator(); var deck = deckGenerator.GenerateDeck(); var cardCount = deck.Count(); Assert.Equal(52, cardCount); }
public void Should_Have_13_Of_Each_Suit() { var deckGenerator = new DeckGenerator(); var deck = deckGenerator.GenerateDeck(); var suitCounts = deck .GroupBy( card => card.Suit, (rank, cards) => cards.Count()); Assert.True(suitCounts.All(rankCount => rankCount == 13)); }
public void Should_Have_Four_Of_Each_Rank() { var deckGenerator = new DeckGenerator(); var deck = deckGenerator.GenerateDeck(); var rankCounts = deck .GroupBy( card => card.Rank, (rank, cards) => cards.Count()); Assert.True(rankCounts.All(rankCount => rankCount == 4)); }
public void Should_Have_Distinct_Cards() { var deckGenerator = new DeckGenerator(); var deck = deckGenerator.GenerateDeck(); var cardCount = deck.Count(); var distinctCardCount = deck .Distinct() .Count(); Assert.Equal(cardCount, distinctCardCount); }
public void InitDeck(List <LessonTypes> selectedLessons) { List <BaseCard> cards; if (GameManager.DebugModeEnabled) { var prebuiltCards = GameManager.GetPlayerTestDeck(NetworkId); cards = DeckGenerator.GenerateDeck(prebuiltCards, selectedLessons); } else { cards = DeckGenerator.GenerateDeck(selectedLessons); } Deck.Initialize(cards, DeckGenerator.GetRandomCharacter()); }
public static void ShouldHaveUniqueCards() { var uniqueCards = new List <string>(); var deck = DeckGenerator.GenerateDeck(); foreach (var card in deck) { if (uniqueCards.Any(x => x == card.ToString())) { Assert.False(true, $"Duplicate card found: {card}"); } else { uniqueCards.Add(card.ToString()); } } }
public async Task <HttpResponseMessage> Patch([FromUri] Guid id, [FromBody] Deck deck) { var principal = HttpContext.Current.User as Principal; using (var repository = new Repository(principal.UserId)) { await repository.Context.Factions.LoadAsync(); await repository.Context.CardTypes.LoadAsync(); var deckModel = await repository.Context.Decks.Include(x => x.DeckCards.Select(dc => dc.Card)).Where(x => x.Guid == id).FirstOrDefaultAsync(); if (deckModel == null) { Request.CreateResponse(HttpStatusCode.NotFound); } await repository.Context.Entry(deckModel).Reference(x => x.Creator).LoadAsync(); await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); if (repository.ServiceUser.Role.Guid != PredefinedGuids.Administrator && deckModel.Creator.Guid != repository.ServiceUser.Guid) { return(Request.CreateResponse(HttpStatusCode.Forbidden)); } deckModel.Patch(deck, repository.ServiceUser); deck = deckModel.FromDal(true); File.WriteAllText(Repository.GetDeckJsonFile(deck.Guid), JsonConvert.SerializeObject(deck)); File.WriteAllText(Repository.GetDeckFormatFile(deck.Guid), GetDeckFormat(deckModel)); await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); var generateMissing = repository.ServiceUser.Guid == PredefinedGuids.Administrator || repository.ServiceUser.Guid == PredefinedGuids.ServiceUser; DeckGenerator.GenerateDeck(deck.Guid, generateMissing); await repository.Context.SaveChangesAsync(); //BackgroundJob.Schedule(()=>DeckGenerator.GenerateDeck(deck.Guid)) return(Request.CreateResponse(deck)); } }
public void InitDeck(List <LessonTypes> selectedLessons) { prebuiltCards = GameManager.GetPlayerTestDeck(NetworkId); Debug.Log(prebuiltCards.ToString()); List <BaseCard> cards; BaseCard startingCharacter; if (GameManager.DebugModeEnabled) { DrawInitialHand(); //cards = DeckGenerator.GenerateDeck(selectedLessons); //cards = DeckGenerator.GenerateDeck(prebuiltCards, selectedLessons); //startingCharacter = GameManager.GetPlayerTestCharacter(NetworkId); } else { cards = DeckGenerator.GenerateDeck(selectedLessons); startingCharacter = DeckGenerator.GetRandomCharacter(); } //Deck.Initialize( cards, startingCharacter); }
public void Should_Have_Sorted_Cards() { var deckGenerator = new DeckGenerator(); var deck = deckGenerator.GenerateDeck(); // We need to scope the deck enumerator separately from where we iterate it, // so a foreach won't work. We'll just manually advance the enumerator. var deckEnumerator = deck.GetEnumerator(); // This check depends on the specific way suits and ranks are combined to create cards. // That makes it a bad unit test, but I wanted to ensure the cards are sorted for // purposes of the demo. foreach (var suit in Suit.All) { foreach (var rank in Rank.All) { deckEnumerator.MoveNext(); Assert.Equal(suit, deckEnumerator.Current.Suit); Assert.Equal(rank, deckEnumerator.Current.Rank); } } }
static void Main(string[] args) { //GameDriver controller = new GameDriver(); //controller.InitializeGame(); ////foreach(Card card in controller.Deck.CardsInCollection) ////{ //// Console.WriteLine(card.GetDisplayName()); ////} ////controller.Deck.Shuffle(); ////Console.WriteLine(); ////foreach (Card card in controller.Deck.CardsInCollection) ////{ //// Console.WriteLine(card.GetDisplayName()); ////} ////Console.ReadKey(); //Console.WriteLine("Player's hand: "); //foreach(Card card in controller.GameState.HumanPlayer.CardsInHand.CardsInCollection) //{ // Console.WriteLine(card.GetDisplayName()); //} //Console.WriteLine(); //Console.WriteLine("Opponent's hand: "); //foreach (Card card in controller.GameState.AIPlayer.CardsInHand.CardsInCollection) //{ // Console.WriteLine(card.GetDisplayName()); //} //Console.ReadLine(); CardCollection deck = DeckGenerator.GenerateDeck(); CardCollection testHand = new CardCollection(); testHand.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Pnakotic_Manuscripts)); testHand.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Cthulhu)); testHand.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Deep_Ones)); testHand.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Rlyeh)); testHand.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Elder_Things)); Card testCard = deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Miskatonic_University); CardCollection testHand2 = new CardCollection(); testHand2.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Miskatonic_University)); testHand2.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Nyarlathotep)); testHand2.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Shub_Niggurath)); testHand2.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Mountains_Of_Madness)); testHand2.AddCardToCollection(deck.CardsInCollection.First(c => c.CardNameEnum == CardNames.Hastur)); Player testPlayer = new Player("me"); testPlayer.CardsInPlay = testHand; Player testPlayer2 = new Player("you"); testPlayer2.CardsInPlay = testHand2; int myScore = ScoreCalculator.CalculateScore(testPlayer, testPlayer2); int yourScore = ScoreCalculator.CalculateScore(testPlayer2, testPlayer); Console.WriteLine(myScore); Console.WriteLine(yourScore); Console.ReadKey(); }
public static void ShouldGenerate52Cards() { var deck = DeckGenerator.GenerateDeck(); deck.Count.ShouldBe(52); }