static GamesService() { activeSessions = new ConcurrentDictionary <Guid, SessionWithTurns>(); activeSessionsByUser = new ConcurrentDictionary <string, Lazy <ConcurrentDictionary <Guid, SessionWithTurns> > >(); using (var db = new ApplicationDbContext()) { foreach (var session in db.Sessions.Where(s => s.Hand1.Cards.Any() && s.Hand2.Cards.Any() && s.Hand3.Cards.Any() && s.Hand4.Cards.Any())) { var sessionWithTurns = new SessionWithTurns(session.Direction, ToModel(session.Hand1), ToModel(session.Hand2), ToModel(session.Hand3), ToModel(session.Hand4)) { LastPlayed = session.LastPlayed, Deck = new Stack <Card>(session.Deck.Where(c => !c.IsDiscarded).OrderByDescending(c => c.Order).Select(c => c.Card).ToList()), DiscardPile = new Stack <Card>(session.Deck.Where(c => c.IsDiscarded).OrderBy(c => c.Order).Select(c => c.Card).ToList().Concat(new[] { session.DiscardPileTop })), }; activeSessions.TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(session.Hand1.UserId, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(session.Hand2.UserId, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(session.Hand3.UserId, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(session.Hand4.UserId, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); } } }
public Guid Create(string userId1, string userId2, string userId3, string userId4) { var cardsList = db.Cards.ToList(); cardsList.Shuffle(); var cards = new Stack <Card>(cardsList); var hand1 = new Hand { IsTheirTurn = true, UserId = userId1, Cards = Enumerable.Range(0, 7).Select(_ => cards.Pop()).ToList(), }; var hand2 = new Hand { IsTheirTurn = false, UserId = userId2, Cards = Enumerable.Range(0, 7).Select(_ => cards.Pop()).ToList(), }; var hand3 = new Hand { IsTheirTurn = false, UserId = userId3, Cards = Enumerable.Range(0, 7).Select(_ => cards.Pop()).ToList(), }; var hand4 = new Hand { IsTheirTurn = false, UserId = userId4, Cards = Enumerable.Range(0, 7).Select(_ => cards.Pop()).ToList(), }; var firstCard = cards.Pop(); var deck = cards.Select((c, i) => new CardInPile { Card = c, IsDiscarded = false, Order = i, }).ToList(); var session = new Session { Direction = Direction.Counterclockwise, DiscardPileTop = firstCard, Deck = deck, LastPlayed = DateTime.UtcNow, }; db.Sessions.Add(session); db.SaveChanges(); hand1.Session = session; session.Hand1 = hand1; hand2.Session = session; session.Hand2 = hand2; hand3.Session = session; session.Hand3 = hand3; hand4.Session = session; session.Hand4 = hand4; db.SaveChanges(); db.Entry(hand1).Reference(a => a.User).Load(); db.Entry(hand2).Reference(a => a.User).Load(); db.Entry(hand3).Reference(a => a.User).Load(); db.Entry(hand4).Reference(a => a.User).Load(); var sessionWithTurns = new SessionWithTurns(session.Direction, ToModel(hand1), ToModel(hand2), ToModel(hand3), ToModel(hand4)) { LastPlayed = session.LastPlayed, Deck = cards, DiscardPile = new Stack <Card>(new[] { firstCard }), }; activeSessions.TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(userId1, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(userId2, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(userId3, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); activeSessionsByUser.GetOrAdd(userId4, _ => new ConcurrentDictionary <Guid, SessionWithTurns>()) .TryAdd(session.Id, sessionWithTurns); return(session.Id); }