private string RetrieveGoogleAPIKey() //================================================================================================================ // Retrieve the google API key from an external source // // Returns // Google API key //================================================================================================================ { string googleAPIKey = ""; SSCasino_DBContext dbCasino = null; try { // Connect to the database dbCasino = new SSCasino_DBContext(); // Retrieve the API key googleAPIKey = dbCasino.CasinoInfo.Where(e => e.CasinoId == 1).Select(e => e.GoogleAPIKey).FirstOrDefault(); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(googleAPIKey); }
private void SaveYouTubeSearchResults(YouTubeSearchRequest searchRequest) //================================================================================================================ // Save the given YouTube search results to the session and database // // Parameters // searchRequest: YouTube search request data populated with results //================================================================================================================ { // Save the results locally in the session Session[searchRequest.SessionKey] = searchRequest.SearchResults; SSCasino_DBContext dbCasino = null; try { // Connect to the database dbCasino = new SSCasino_DBContext(); // If the results are is NOT in the database, save them if (!dbCasino.YouTubeSearchResults.Any(e => e.SessionKey == searchRequest.SessionKey)) { // Save the results to the database dbCasino.YouTubeSearchResults.AddRange(searchRequest.SearchResults); dbCasino.SaveChanges(); } } finally { if (dbCasino != null) { dbCasino.Dispose(); } } }
private void LoadShufflerCards(SSCasino_DBContext dbCasino, ShufflerControlPanel shufflerCP, int cardPackId) //================================================================================================================ // Load the shuffler control panel with card packs // // Parameters // dbCasino: Database connection // shufflerCP: Shuffler control panel // cardPackId: Unique id of the desired card pack or zero // // Returns // None // // Developer Notes // The control panel object passed to this function is modified //================================================================================================================ { // Load all card packs for then selection list shufflerCP.CardPacks = dbCasino.CardPacks.ToList(); // If a card pack exists for the given id, use it // Otherwise the default card pack is used if (dbCasino.CardPacks.Any(e => e.CardPackId == cardPackId)) { shufflerCP.SelectedCardPack = dbCasino.CardPacks.Where(e => e.CardPackId == cardPackId).FirstOrDefault(); } else { shufflerCP.SelectedCardPack = dbCasino.CardPacks.Where(e => e.DefaultPack == 1).FirstOrDefault(); } }
private CardPack CreateCardPackModel(SSCasino_DBContext dbCasino, int cardPackId = 0, SiteHelpers.SampleSizes sampleSize = SiteHelpers.SampleSizes.FullDeck) //================================================================================================================ // Retrieve a pack of cards based on the given pack id // // Parameters // dbCasino: Open Connection to the database // cardPackId: (optional) Unique identifier for a card pack // sampleSize: (optional) Number of cards to load // // Returns // A full or partial pack of cards //================================================================================================================ { // Create the model CardPack model = new CardPack(); switch (sampleSize) { case SiteHelpers.SampleSizes.FourCardSample: string[] fourSampleCards = SiteHelpers.FourCardSampleCards.Split(' '); if (dbCasino.CardPacks.Any(e => e.CardPackId == cardPackId)) { model = dbCasino.CardPacks.Where(e => e.CardPackId == cardPackId).FirstOrDefault(); model.CardDeck = dbCasino.PlayingCardsView.Where(e => ((e.CardPackId == cardPackId) && (fourSampleCards.Contains(e.CardCode)))).OrderBy(e => e.CardSuitOrder).ToList(); } else { model = dbCasino.CardPacks.Where(e => e.DefaultPack == 1).FirstOrDefault(); model.CardDeck = dbCasino.PlayingCardsView.Where(e => ((e.DefaultPack == 1) && (fourSampleCards.Contains(e.CardCode)))).OrderBy(e => e.CardSuitOrder).ToList(); } break; case SiteHelpers.SampleSizes.SixCardSample: string[] sixSampleCards = SiteHelpers.SixCardSampleCards.Split(' '); if (dbCasino.PlayingCardsView.Any(e => e.CardPackId == cardPackId)) { model = dbCasino.CardPacks.Where(e => e.CardPackId == cardPackId).FirstOrDefault(); model.CardDeck = dbCasino.PlayingCardsView.Where(e => ((e.CardPackId == cardPackId) && (sixSampleCards.Contains(e.CardCode)))).OrderBy(e => e.CardSuitOrder).ToList(); } else { model = dbCasino.CardPacks.Where(e => e.DefaultPack == 1).FirstOrDefault(); model.CardDeck = dbCasino.PlayingCardsView.Where(e => ((e.DefaultPack == 1) && (sixSampleCards.Contains(e.CardCode)))).OrderBy(e => e.CardSuitOrder).ToList(); } break; case SiteHelpers.SampleSizes.FullDeck: if (dbCasino.PlayingCardsView.Any(e => e.CardPackId == cardPackId)) { model = dbCasino.CardPacks.Include("CardDeck").Where(e => e.CardPackId == cardPackId).FirstOrDefault(); } else { model = dbCasino.CardPacks.Include("CardDeck").Where(e => e.DefaultPack == 1).FirstOrDefault(); } break; } return(model); }
public ActionResult ShufflerControlPanel_ChangeCardPack() //================================================================================================================ // This action is invoked when a new card pack is selected. // // Event Arguments // arg_CardPackId: Unique id of a card pack // // Returns // The card examples view //================================================================================================================ { // Retrieve the id of the new card pack int cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0; CardPack model = null; SSCasino_DBContext dbCasino = null; try { // Connect the the database // Create a card pack model for the specified card pack dbCasino = new SSCasino_DBContext(); model = CreateCardPackModel(dbCasino, cardPackId: cardPackId); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(PartialView("_Shuffler_CardExamplesCBP", model)); }
//================================================================================================================ //================================================================================================================ #endregion // Card Shuffler #region Randomness //================================================================================================================ //================================================================================================================ public ActionResult Randomness() //================================================================================================================ // This action is invoked when the randomness comparison page is requested. // // Event Arguments // arg_CardPackId: Unique id of the selected card pack // arg_SampleSize: Unique id of the selected sample size // // Returns // The card shuffler page view //================================================================================================================ { // Safety check for the server if ((bool)Session[SiteHelpers.ServerRunning] == false) { return(View("ServerNotRunning", new PokerPlayground())); } // Retrieve the selected card pack int cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0; int sampleSize = !string.IsNullOrEmpty(Request.Params[ARG_SAMPLE_SIZE]) ? int.Parse(Request.Params[ARG_SAMPLE_SIZE]) : (int)SiteHelpers.SampleSizes.FourCardSample; // Create the model Randomness model = new Randomness(); // Populate the model SSCasino_DBContext dbCasino = null; try { // Connect the the database dbCasino = new SSCasino_DBContext(); // Get card packs for the card shuffler model.ShuffledPackageFisher.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); model.ShuffledPackageNaive.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); // Load the shuffler control panel with card packs LoadShufflerCards(dbCasino, model.ControlPanel, cardPackId); // Get the total number of shuffles and aggregated results GetShuffleResults(model.ShuffleResultsData); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(View("Randomness", model)); }
private void ResetRandomnessPage(SSCasino_DBContext dbCasino, Randomness model) //================================================================================================================ // Reset the data for the randomness page // // Parameters // dbCasino: Open connection to the database // model: Reference to a randomness model //================================================================================================================ { // Reset the shuffing results // Get default card packs SiteHelpers.ClearCombinedShuffleResults(); model.ShuffledPackageFisher.CardPack = CreateCardPackModel(dbCasino, sampleSize: SiteHelpers.SampleSizes.FourCardSample); model.ShuffledPackageNaive.CardPack = CreateCardPackModel(dbCasino, sampleSize: SiteHelpers.SampleSizes.FourCardSample); }
//================================================================================================================ //================================================================================================================ #endregion // Home #region Card Shuffler //================================================================================================================ //================================================================================================================ public ActionResult CardShuffler() //================================================================================================================ // This action is invoked when the card shuffler page is requested. // // Event Arguments // arg_CardPackId: Unique id of the selected card pack // // Returns // The card shuffler page view //================================================================================================================ { // Safety check for the server if ((bool)Session[SiteHelpers.ServerRunning] == false) { return(View("ServerNotRunning", new PokerPlayground())); } // Retrieve the selected card pack int cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0; // Create the model CardShuffler model = new CardShuffler(); // Populate the model SSCasino_DBContext dbCasino = null; try { // Connect the the database dbCasino = new SSCasino_DBContext(); // Get a card pack for the card shuffler // Load the shuffler control panel with card packs model.CardPack = CreateCardPackModel(dbCasino, cardPackId, SiteHelpers.SampleSizes.FullDeck); LoadShufflerCards(dbCasino, model.ControlPanel, cardPackId); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(View("CardShuffler", model)); }
private void RecordUnhandledException(Exception exception) //================================================================================================================ // This routine will write exception information to the database. // // Parameters // exception: Run time exception //================================================================================================================ { SSCasino_DBContext dbCasino = null; try { // Connect to the database dbCasino = new SSCasino_DBContext(); // Write the exception data SqlParameter paramSource = new SqlParameter("@Source", exception.Source); SqlParameter paramMessage = new SqlParameter("@Message", exception.Message); SqlParameter paramTraget = new SqlParameter("@TargetSite", exception.TargetSite); SqlParameter paramTrace = new SqlParameter("@StackTrace", exception.StackTrace); dbCasino.Database.ExecuteSqlCommand("site_RecordException @Source, @Message, @TargetSite, @StackTrace", paramSource, paramMessage, paramTraget, paramTrace); } catch { // Develoer Notes // To avoid an endless loop if an error occurrs while recording error information, a session flag is set // if this happens. This session flag is checked before attempting to recording error information. // // Oops, that's a critical error! Session[SiteHelpers.CriticalError] = true; } finally { if (dbCasino != null) { dbCasino.Dispose(); } } }
//================================================================================================================ //================================================================================================================ #endregion // Constants #region General //================================================================================================================ //================================================================================================================ public static bool CheckServerConnection() //================================================================================================================ // Check to see if the server is running // // Returns // True/False //================================================================================================================ { bool serverIsRunning = true; // Attempt to connect to the server SSCasino_DBContext dbCasino = null; try { // Attempt to open a database connection dbCasino = new SSCasino_DBContext(); dbCasino.Database.CommandTimeout = 2; dbCasino.Database.Connection.Open(); if ((dbCasino.Database.Connection.State == System.Data.ConnectionState.Closed) || (dbCasino.Database.Connection.State == System.Data.ConnectionState.Broken)) { serverIsRunning = false; } } catch { serverIsRunning = false; } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(serverIsRunning); }
private bool GetSerachResultsFromDatabase(YouTubeSearchRequest searchRequest) //================================================================================================================ // Retrieve search results from the database based on the given search request // // Parameters // searchRequest: YouTube search request data // // Outputs // The SearchResults list in the searchRequest object is populated // // Retruns // True/False, has the YouTube data limit been exceeded //================================================================================================================ { bool dataAvailable = false; // Populate the model SSCasino_DBContext dbCasino = null; try { // Connect the the database dbCasino = new SSCasino_DBContext(); // Retrieve results frokm the database searchRequest.SearchResults = dbCasino.YouTubeSearchResults.Where(e => e.SessionKey == searchRequest.SessionKey).ToList(); dataAvailable = (searchRequest.SearchResults.Count != 0); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(dataAvailable); }
//================================================================================================================ //================================================================================================================ #endregion // Randomness #region Shuffler Control Panel //================================================================================================================ //================================================================================================================ public ActionResult ShufflerControlPanel_Reset() //================================================================================================================ // This action is invoked when the shuffler control panel is being reset. // // Event Arguments // arg_ShufflerMode: Operation mode for the shuffler // // Returns // The shuffler control panel view //================================================================================================================ { // Retrieve the operation mode for the shuffler SiteHelpers.ShuffleMode shuffleMode = (SiteHelpers.ShuffleMode)(!string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_MODE]) ? int.Parse(Request.Params[ARG_SHUFFLE_MODE]) : 1); // Create the model ShufflerControlPanel model = new ShufflerControlPanel(shuffleMode); // Load it with card packs SSCasino_DBContext dbCasino = null; try { // Connect the the database // Load it with all the card packs dbCasino = new SSCasino_DBContext(); LoadShufflerCards(dbCasino, model, 0); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(PartialView("_Shuffler_ControlPanelCBP", model)); }
public ActionResult Randomness_RefreshResultsGraph() //================================================================================================================ // This action is invoked when the shuffling results graph needs to be refreshed. // // Event Arguments // arg_Action: Action to be performed // arg_ResizeWidth: New size of the parent container // // Returns // The sguffling results grasph //================================================================================================================ { // Retrieve the action to be performed string action = !string.IsNullOrEmpty(Request.Params[ARG_ACTION]) ? Request.Params[ARG_ACTION] : ""; // Create the model // Get the current size of the results graph ShuffleResultsGraph model = new ShuffleResultsGraph(); if (Session[SiteHelpers.ResultsGraphWidth] != null) { model.ResizeWidth = (int)Session[SiteHelpers.ResultsGraphWidth]; model.ResizeHeight = (int)Session[SiteHelpers.ResultsGraphHeight]; } // Retrieve the shuffling results SSCasino_DBContext dbCasino = null; try { // Perform the action switch (action) { case ACT_RESIZE_CHART: // Get the resize width and height int resultsGridWidth = !string.IsNullOrEmpty(Request.Params[ARG_RESULTS_GRAPH_WIDTH]) ? int.Parse(Request.Params[ARG_RESULTS_GRAPH_WIDTH]) : 0; int resultsGridHeight = !string.IsNullOrEmpty(Request.Params[ARG_RESULTS_GRAPH_HEIGHT]) ? int.Parse(Request.Params[ARG_RESULTS_GRAPH_HEIGHT]) : 0; // Calculate the new width and height of the shuffling results graph // Store these values in the sesssion object for use on refeshes model.ResizeWidth = ((resultsGridWidth == 0) ? model.DeafultWidth : resultsGridWidth); model.ResizeHeight = ((resultsGridHeight == 0) ? model.DefaultHeight : resultsGridHeight); Session[SiteHelpers.ResultsGraphWidth] = model.ResizeWidth; Session[SiteHelpers.ResultsGraphHeight] = model.ResizeHeight; break; case ACT_CHANGE_SAMPLE_SIZE: // Reset the shuffing results SiteHelpers.ClearCombinedShuffleResults(); break; case ACT_RESET: // Reset the shuffing results SiteHelpers.ClearCombinedShuffleResults(); break; } // Always get the results GetShuffleResults(model); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(PartialView("_Randomness_ResultsGraph", model)); }
public ActionResult Randomness_PerformAction() //================================================================================================================ // This action is invoked when the card display area is performing an action on the cards. // // Event Arguments // arg_Action: Action to be performed // arg_CardPackId: Unique id of a card pack // arg_ShuffleCount: Number of time to shuffle // arg_SampleSize: Sample size of cards to shuffle // // Returns // The card display view //================================================================================================================ { // Retrieve the action to be performed and the event arguments string action = !string.IsNullOrEmpty(Request.Params[ARG_ACTION]) ? Request.Params[ARG_ACTION] : ""; int cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0; int shuffleCount = !string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_COUNT]) ? int.Parse(Request.Params[ARG_SHUFFLE_COUNT]) : 0; int sampleSize = !string.IsNullOrEmpty(Request.Params[ARG_SAMPLE_SIZE]) ? int.Parse(Request.Params[ARG_SAMPLE_SIZE]) : 0; // Create the model Randomness model = new Randomness(); // Perform the action SSCasino_DBContext dbCasino = null; try { // Connect the the database dbCasino = new SSCasino_DBContext(); // Perform the action switch (action) { case ACT_SHUFFLE: // Get card packs for the shuffler model.ShuffledPackageFisher.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); model.ShuffledPackageNaive.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); // Shuffle the cards model.ShuffledPackageFisher = SiteHelpers.ShuffleCards(model.ShuffledPackageFisher.CardPack, SiteHelpers.ShuffleTypes.FisherYates, shuffleCount, true); model.ShuffledPackageNaive = SiteHelpers.ShuffleCards(model.ShuffledPackageNaive.CardPack, SiteHelpers.ShuffleTypes.Naive, shuffleCount, true); // Merge the Fisher-Yates and Naive shuffle results SiteHelpers.CombinedShuffleResults = (ICollection <ShuffleResult>)model.ShuffledPackageFisher.ShuffleResults.Concat <ShuffleResult>(model.ShuffledPackageNaive.ShuffleResults).ToList(); break; case ACT_CHANGE_CARD_PACK: // Get card packs for the shuffler model.ShuffledPackageFisher.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); model.ShuffledPackageNaive.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); break; case ACT_CHANGE_SAMPLE_SIZE: // Reset the shuffing results // Get card packs based on sample size SiteHelpers.ClearCombinedShuffleResults(); model.ShuffledPackageFisher.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); model.ShuffledPackageNaive.CardPack = CreateCardPackModel(dbCasino, cardPackId, (SiteHelpers.SampleSizes)sampleSize); break; default: // Get default data for the page ResetRandomnessPage(dbCasino, model); break; } // Always get the number of shuffles and the aggregated shuffle results model.ShuffleResultsData.ResizeWidth = (int)Session[SiteHelpers.ResultsGraphWidth]; model.ShuffleResultsData.ResizeHeight = (int)Session[SiteHelpers.ResultsGraphHeight]; GetShuffleResults(model.ShuffleResultsData); } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(PartialView("_Randomness_DisplayCBP", model)); }
public ActionResult CardShuffler_PerformAction() //================================================================================================================ // This action is invoked when the card display area is performing an action on the cards. // // Event Arguments // arg_Action: Action to be performed // arg_CardPackId: Unique id of a card pack // arg_ShuffleCount: Number of time to shuffle // arg_ShuffleType: Shuffle algorithm // // Returns // The card display view //================================================================================================================ { CardPack cardPack = null; // Retrieve the action to be performed and the assigned card pack string action = !string.IsNullOrEmpty(Request.Params[ARG_ACTION]) ? Request.Params[ARG_ACTION] : ""; int cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0; // Connect to the database and perform the action SSCasino_DBContext dbCasino = null; try { // Connect the the database dbCasino = new SSCasino_DBContext(); // Perform the action switch (action) { case ACT_SHUFFLE: // Retrieve an unshuffled pack of cards cardPack = CreateCardPackModel(dbCasino, cardPackId); // Shuffle the cards int shuffleType = !string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_TYPE]) ? int.Parse(Request.Params[ARG_SHUFFLE_TYPE]) : 1; int shuffleCount = !string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_COUNT]) ? int.Parse(Request.Params[ARG_SHUFFLE_COUNT]) : 1; ShuffledPackage shuffledPackage = SiteHelpers.ShuffleCards(cardPack, (SiteHelpers.ShuffleTypes)shuffleType, shuffleCount, false); cardPack = shuffledPackage.CardPack; break; case ACT_CHANGE_CARD_PACK: // Retrieve an unshuffled pack of cards cardPack = CreateCardPackModel(dbCasino, cardPackId); break; default: // Get the default card pack cardPack = CreateCardPackModel(dbCasino); break; } } finally { if (dbCasino != null) { dbCasino.Dispose(); } } return(PartialView("_CardShuffler_DisplayCBP", cardPack.CardDeck)); }