Ejemplo n.º 1
0
        public static void RecordConsumer()
        {
            // Record consumer writes records to database
            command             = conn.CreateCommand();
            command.Transaction = conn.BeginTransaction();
            // read recordsTotal once a the start
            int _recordsTotal = 0;

            lock (recordsTotalLock)
            {
                _recordsTotal = recordsTotal;
            }
            do
            {
                // Execute Insert command on database, one row per player
                GameRecord record;
                while (true)
                {
                    if (collection.TryTake(out record, 3))
                    {
                        break;
                    }
                }
                SqliteMethods.InsertResultItem(record, command);
                recordsWritten++;
                if (recordsWritten % gamesPerTransaction == 0)
                {
                    command.Transaction.Commit();
                    command             = conn.CreateCommand();
                    command.Transaction = conn.BeginTransaction();
                    lock (recordsAddedLock)
                    {
                        Console.WriteLine($"Records Added: {String.Format("{0:n0}", recordsAdded)}  " +
                                          $"Records Written: {String.Format("{0:n0}", recordsWritten)} Difference: {String.Format("{0:n0}", recordsAdded - recordsWritten)}");
                        lock (differenceLock)
                        {
                            difference = recordsAdded - recordsWritten;
                        }
                    }
                }
            } while (recordsWritten < _recordsTotal);

            // Inevitably we broke out of loop with a partial transaction. Flush it to disk.
            if (command.Transaction != null)
            {
                command.Transaction.Commit();
            }
            // Clean up
            if (command != null)
            {
                command.Dispose();
            }
            Console.WriteLine("Record writing thread ended.");
        }
Ejemplo n.º 2
0
        public static void DisplayMenu()
        {
            bool _exitFlag = false;

            do
            {
                Console.Clear();
                PrintMenuText();
                string sInput = Console.ReadLine();

                if (Int32.TryParse(sInput, out int userChoice))
                {
                    switch (userChoice)
                    {
                    case 1:
                        int _numGames = UtilityMethods.GetIntegerFromUser(3000, 2000000000);
                        var watch     = new System.Diagnostics.Stopwatch();
                        watch.Start();
                        Simulation.SimulateGames(PlayerCount, _numGames);
                        watch.Stop();
                        Console.WriteLine($"Total Execution Time: {(watch.ElapsedMilliseconds / 60000.0).ToString("0.##")} minutes");
                        UtilityMethods.GetKeyPress();
                        break;

                    case 2:
                        Game.PlayGame();
                        UtilityMethods.GetKeyPress();
                        break;

                    case 3:
                        Console.WriteLine("Enter number of players (2 to 8):");
                        // update value of Program.PlayerCount for use by other methods
                        PlayerCount = UtilityMethods.GetIntegerFromUser(2, 8);
                        UtilityMethods.GetKeyPress();
                        break;

                    case 4:
                        SqliteMethods.ShowDatabaseStatistics();
                        UtilityMethods.GetKeyPress();
                        break;

                    case 5:
                        _exitFlag = true;
                        break;

                    default:
                        break;
                    }
                }
            } while (_exitFlag == false);
        }
Ejemplo n.º 3
0
        public static async void SimulateGames(int playerCount, int targetGameCount)
        {
            //Check if we need to build sqlite tables for the first time.
            SqliteMethods.InitDatabaseIfNeeded(playerCount);
            Console.WriteLine("Press any key to continue!");
            Console.ReadKey();
            // total games to be simulated (from user input)
            recordsTotal = targetGameCount;

            // open connection to the database
            conn = SqliteMethods.CreateConnection(playerCount);

            Timing timer = new Timing();

            timer.StartTime();

            // play with thread priorities to get optimal balance of adding and writing GameRecords
            // on your system
            Console.WriteLine("Starting producer and consumer threads...");

            // set thread priority
            producerThread.Priority = ThreadPriority.Lowest;
            consumerThread.Priority = ThreadPriority.Highest;
            producerThread.Name     = "Producer";
            consumerThread.Name     = "Consumer";
            producerThread.Start();
            consumerThread.Start();


            while (true)
            {
                // non blocking pause
                await Task.Delay(120_000).ConfigureAwait(true);

                Console.WriteLine($"Main thread {Thread.CurrentThread.Name} returning from pause");
                if (consumerThread.ThreadState == ThreadState.Stopped &
                    producerThread.ThreadState == ThreadState.Stopped)
                {
                    timer.StopTime();
                    break;
                }
            }

            Console.WriteLine($"Runtime duration: {timer.Result().TotalMinutes} minutes");
            conn.Dispose();
        }
Ejemplo n.º 4
0
        internal static void AssignPostFlopPercentages(Board b)
        {
            //    wrap all players in a transaction
            //    postflop calc
            //    commit transaction
            Trace.WriteLine($"{nameof(AssignPostFlopPercentages)} method running...");
            var conn = SqliteMethods.CreateConnection(b.Players.Count);

            using (SQLiteTransaction tran = conn.BeginTransaction())
            {
                foreach (var p in b.Players)
                {
                    long holeId = Card.CardUniquePrimeDict[p.Hole[0]] * Card.CardUniquePrimeDict[p.Hole[1]];
                    long flopId = Card.CardUniquePrimeDict[b.Cards[0]] * Card.CardUniquePrimeDict[b.Cards[1]] * Card.CardUniquePrimeDict[b.Cards[2]];

                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        p.PostFlopOdds = String.Format("{0:0.0}", SqliteMethods.CalculatePostFlopPercentage(holeId, flopId, cmd));
                    }
                }
                tran.Commit();
            }
            conn.Dispose();
        }