using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BlackjackClasses { public class User { //private List history = new List(); private Player _house; private Player _user; private Round _currentRound; public Round.GameStatus Status { get { return (_currentRound == null ? Round.GameStatus.UnfinishedGame : _currentRound.status); } } public int ID = -1; public string Email; public string Name; public int HouseScore { get { return _house.Score; } } public int PlayerScore { get { return _user.Score; } } public User(string strname, string strEmail) { Name = strname; Email = strEmail; _house = new Player(); _user = new Player(); } /// /// Checks in the Database to see if the USer exists /// /// /// /// public User(ref bool userExists, string strname, string strEmail) { var db = new DBBlackJackDataContext(); userExists = false; userExists = userExistsInDatabase(db, strname, strEmail); if (!userExists) { Name = strname; Email = strEmail; } _house = new Player(); _user = new Player(); } public void NewGame() { _house = new Player(); _user = new Player(); _currentRound = new Round(_house, _user); _currentRound.firstRound(); } public User(List HouseCards, List Playercards) { _house = new Player(); _user = new Player(); _currentRound = new Round(_house, _user); _currentRound.CheatRound(HouseCards, Playercards); } public void hit() { _currentRound.hit(); } public void stay() { _currentRound.stay(); } public void hit(bool giveToHouse, List cards) { foreach (Card c in cards) { if (giveToHouse) _currentRound.cheatHitDealer(c); else _currentRound.cheatHitPlayer(c); } } public List getCurrentDeck() { return _currentRound.Deck; } public List getHouseHand() { return _house.Hand; } public List getPlayerHand() { return _user.Hand; } public List getRoundHistory() { DBBlackJackDataContext db = new DBBlackJackDataContext(); List rounds = new List(); UserGame userGameRec = new UserGame(); if (userExistsInDatabase(db, Name, Email)) { var query = from x in db.UserGames where x.UserID == ID select x.GameTable; Round r; foreach (GameTable gt in query) { r = new Round(); r.status = (Round.GameStatus) Enum.Parse(typeof(Round.GameStatus), gt.GameStatus, true); r.PlayerDummyScore = gt.PlayerScore; r.HouseDummyScore = gt.DealerScore; rounds.Add(r); } } return rounds; } public void saveRound() { var randy = new Random(); DBBlackJackDataContext db = new DBBlackJackDataContext(); GameTable gameT = new GameTable(); UserGame userGameRec = new UserGame(); if (userExistsInDatabase(db, Name, Email)) { //user = db.UserGames.Single(x => x.UserID == ID); gameT.GameID = (randy.Next() * (int)DateTime.Now.Ticks); gameT.GameStatus = _currentRound.status.ToString(); gameT.DealerScore = _currentRound.HouseScore; gameT.PlayerScore = _currentRound.PlayerScore; userGameRec.UserID = ID; userGameRec.GameTable = gameT; db.GameTables.InsertOnSubmit(gameT); db.UserGames.InsertOnSubmit(userGameRec); db.GameTables.InsertOnSubmit(gameT); db.SubmitChanges(); userGameRec.GameTable = gameT; } else { UserTable user = new UserTable(); user.UserID = (randy.Next() * (int)DateTime.Now.Ticks); user.Name = Name; user.Email = Email; gameT.GameID = (randy.Next() * (int)DateTime.Now.Ticks); gameT.GameStatus = _currentRound.status.ToString(); gameT.DealerScore = _currentRound.HouseScore; gameT.PlayerScore = _currentRound.PlayerScore; userGameRec.GameTable = gameT; user.UserGames.Add(userGameRec); db.UserTables.InsertOnSubmit(user); } db.SubmitChanges(); } public bool userExistsInDatabase(DBBlackJackDataContext db, string userName, string email) { int count = db.UserTables.Count(y => y.Name.ToLower().Equals(userName.ToLower()) && y.Email.ToLower().Equals(email.ToLower())); if (count > 0) { UserTable u = db.UserTables.Single(y => y.Name.ToLower().Equals(userName.ToLower()) && y.Email.ToLower().Equals(email.ToLower())); ID = u.UserID; Name = u.Name; Email = u.Email; return true; } else return false; } } }