public void DealPocketCards(Game game)
        {
            Console.WriteLine("Dealing pocket cards.");

            // who get's cards?
            List<int> availableCards = Enumerable.Range(1, 52).ToList();

            foreach(int userID in GetUsersInGame(game))
            {
                int card1 = GetCard(availableCards);
                int card2 = GetCard(availableCards);

                GameAction dealAction = new GameAction()
                {
                    ActionTypeID = (int)ActionTypes.InitialCards,
                    GameID = game.GameID,
                    UserID = userID,
                    Timestamp = DateTime.Now,
                    IsCommitted = true,
                    Data = String.Join("|", new object[] { card1, card2 })
                };

                ctx.GameAction.InsertOnSubmit(dealAction);
            }

            ctx.SubmitChanges();
        }
        public int StartNewGame(string gameName, string userLogin)
        {
            User user = ctx.User.Single(u => u.Login == userLogin);

            Game newGame = new Game()
            {
                Name  = gameName,
                IsActive = true,
                StartTime = DateTime.Now,
                Round = 0,
                CurrentButtonPosition = 0
            };

            ctx.Game.InsertOnSubmit(newGame);

            ctx.SubmitChanges();

            // ID generated by database
            int gameID = newGame.GameID;

            // insert join action
            GameAction joinAction = new GameAction()
            {
                ActionTypeID = (int)ActionTypes.UserJoined,
                GameID = gameID,
                IsCommitted = false,
                UserID = user.UserID,
                Timestamp = DateTime.Now
            };
            ctx.GameAction.InsertOnSubmit(joinAction);
            ctx.SubmitChanges();

            return gameID;
        }
        public void WaitForPlayers(Game game, int userID)
        {
            Console.WriteLine("Wait for players.");
            GameAction waitAction = new GameAction()
            {
                ActionTypeID = (int)ActionTypes.WaitForPlayers,
                GameID = game.GameID,
                UserID = userID,
                Timestamp = DateTime.Now,
                IsCommitted = true
            };

            ctx.GameAction.InsertOnSubmit(waitAction);
            ctx.SubmitChanges();
        }
        public void PayBlinds(Game game)
        {
            // the next round begins!
            game.Round++;
            Console.WriteLine(string.Format("Round {0} begins.", game.Round));

            // advance button position
            int buttonPosition = game.CurrentButtonPosition;
            List<UserSeat> seats = ctx.UserSeat.Where(us => us.GameID == game.GameID)
                                           .OrderBy(us => us.Seat)
                                           .ToList();

            // get next occupied seat
            while (!seats.Any(s => s.Seat == buttonPosition))
            {
                buttonPosition++;

                if (buttonPosition > seatCount)
                    buttonPosition = 1;
            }

            Console.WriteLine("Move dealer button, pay blinds.");
            game.CurrentButtonPosition = buttonPosition;

            int dealer = seats.Single(s => s.Seat == buttonPosition).UserID;
            int smallBlind = -1;
            int bigBlind = -1;

            if (seats.Count == 2)
            {
                // special heads up rule: dealer pays the small blind
                smallBlind = seats.Single(s => s.Seat == buttonPosition).UserID;
                // the other player pays the big blind
                bigBlind = seats.Single(s => s.UserID != smallBlind).UserID;
            }
            else // get players left of dealer seat
            {
                int cursor = buttonPosition;
                while (smallBlind == -1 || bigBlind == -1)
                {
                    cursor++;
                    if (cursor > seatCount)
                        cursor = 1;

                    UserSeat nextOccupiedSeat = seats.SingleOrDefault(s => s.Seat == cursor);
                    if (nextOccupiedSeat != null)
                    {
                        if (smallBlind == -1)
                            smallBlind = nextOccupiedSeat.UserID;
                        else
                            bigBlind = nextOccupiedSeat.UserID;
                    }
                }
            }

            // insert "blinds" action
            GameAction payBlindsAction = new GameAction()
            {
                ActionTypeID = (int)ActionTypes.Blinds,
                GameID = game.GameID,
                Timestamp = DateTime.Now,
                IsCommitted = true,
                Data = String.Join("|", new object[] { dealer, smallBlind, bigBlind })
            };

            ctx.GameAction.InsertOnSubmit(payBlindsAction);
            ctx.SubmitChanges(); // submit all (advance game round, button position and playBlindsAction)
        }
        public void PlaceUserOnFreeSeat(Game game, User user, int gameActionID)
        {
            int freeSeat = GetFreeSeat(game);
            UserSeat seat = new UserSeat()
            {
                GameID = game.GameID,
                Seat = freeSeat,
                UserID = user.UserID
            };
            ctx.UserSeat.InsertOnSubmit(seat);
            ctx.SubmitChanges();

            Console.WriteLine(String.Format("{0} is placed on seat {1}.", user.Name, freeSeat));

            GameAction action = ctx.GameAction.Single(ga => ga.GameActionID == gameActionID);
            action.Data = seat.Seat.ToString();

            action.IsCommitted = true;
            ctx.SubmitChanges();
        }
        public int GetFreeSeat(Game game)
        {
            List<int> reservedSeats = ctx.UserSeat.Where(us => us.GameID == game.GameID).Select(us => us.Seat).ToList();
            int freeSeatCount = seatCount - reservedSeats.Count;
            if(freeSeatCount == 0)
                return 0;

            Random rnd = new Random((int)DateTime.Now.Ticks);
            int freeSeatIndex = rnd.Next(1, freeSeatCount);

            // get the n-th free seat
            int freeSeatNumber = 1;
            for (int i = 1; i <= seatCount; i++)
            {
                if(!reservedSeats.Contains(i))
                    freeSeatIndex--;
                if (freeSeatIndex == 0)
                {
                    freeSeatNumber = i;
                    break;
                }

            }

            return freeSeatNumber;
        }
 public List<int> GetUsersInGame(Game game)
 {
     List<int> joinedUsers = ctx.UserSeat.Where(us => us.GameID == game.GameID).Select(us => us.UserID).ToList();
     return joinedUsers;
 }
 partial void DeleteGame(Game instance);
 partial void UpdateGame(Game instance);
 partial void InsertGame(Game instance);
Exemple #11
0
        private static void UserJoined(Game game, GameAction action)
        {
            User user = ctx.GetUserByID(action.UserID.Value);
            Console.WriteLine(String.Format("{0} joined the game.", user.Name));

            // place user, commit action
            ctx.PlaceUserOnFreeSeat(game, user, action.GameActionID);

            // how many users are in the game?
            List<int> usersInGame = ctx.GetUsersInGame(game);

            if (usersInGame.Count == 1)
            {
                // your are the first user, wait for other players
                ctx.WaitForPlayers(game, action.UserID.Value);
            }
            if (usersInGame.Count == 2)
            {
                // you are the second user, start the game
                ctx.PayBlinds(game);

                // deal first cards
                ctx.DealPocketCards(game);
            }
            else if(usersInGame.Count > 2)
            {
                // wait for round to end
            }
        }
Exemple #12
0
 private static void UserLeft(Game game, GameAction action)
 {
 }