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 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 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; }
partial void DeleteGameAction(GameAction instance);
public bool JoinGame(int gameID, string userLogin) { User user = ctx.User.Single(u => u.Login == userLogin); bool hasFreeSeat = false; if (ctx.UserSeat.Where(us => us.GameID == gameID).Count() < seatCount) hasFreeSeat = true; if (hasFreeSeat) { // 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 true; } else return false; }
partial void InsertGameAction(GameAction instance);
partial void UpdateGameAction(GameAction instance);
private void detach_GameAction(GameAction entity) { this.SendPropertyChanging(); entity.User = null; }
private void attach_GameAction(GameAction entity) { this.SendPropertyChanging(); entity.User = this; }
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) }
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 } }
private static void UserLeft(Game game, GameAction action) { }