public void ParseUserHandHistoryFile(FileInfo file)
        {
            this.CurrentHand = null;

            Logger.Info("Importing Hand History File {0} in directory {1}", file.Name, file.DirectoryName);

            Data.HandHistoryFile currentFile = this.Store.GetHandHistoryFile(file.FullName);
            if (currentFile == null)
            {
                string name = System.IO.Path.GetFileNameWithoutExtension(file.Name);
                string path = file.FullName;

                currentFile = new Data.HandHistoryFile();
                currentFile.Path = path;
                currentFile.Name = name;
                currentFile.SiteId = this._siteId;
                currentFile.DateAdded = UnixTimestamp.UtcNow;
                currentFile.MachineName = Environment.MachineName;
                currentFile.User = Environment.UserName;
                currentFile.DateCreated = new UnixTimestamp(file.CreationTimeUtc);

                using (this.Store.Lock())
                {
                    currentFile.Id = this.Store.InsertHandHistoryFile(_siteId,
                        name, path, 0, currentFile.MachineName,
                        currentFile.User, currentFile.DateCreated.UnixTime);
                }
            }

            using (StreamReader sr = new StreamReader(File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                string s = sr.ReadToEnd();
                string[] lines = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                if (lines == null || lines.Length == 0)
                {
                    Logger.Warn("No lines found in file {0}", file);
                    return;
                }

                // last is the last line number parsed;
                // if the file hasn't been parsed yet, last will be zero
                long last = currentFile.LineNumber;

                for (long i = last; i < lines.Length; i++)
                {
                    string line = lines[i];
                    ParseUserHandHistoryLine(file, line, i);
                }

                this.Store.UpdateLastLine(currentFile.Id, lines.Length);
            }

            using (this.Store.Lock())
            {
                using (System.Data.SQLite.SQLiteConnection connection = this.Store.CreateConnection())
                {
                    connection.Open();

                    using (System.Data.SQLite.SQLiteTransaction transaction = connection.BeginTransaction())
                    {
                        foreach (Data.Hand hand in this._hands)
                        {
                            bool saved = hand.Save(this.Store, connection);
                        }

                        transaction.Commit();
                    }

                    this._stats.TotalPlayers = this.Store.GetPlayerCount();
                    this._stats.TotalHands = this.Store.GetHandCount();
                }
            }

            this.CurrentHand = null;
            _hands.Clear();

            Logger.Info("Importing Hand History File {0} complete", file.Name);
        }
Ejemplo n.º 2
0
        public void ParseUserHandHistoryFile(FileInfo file)
        {
            this.CurrentHand = null;

            Logger.Info("Importing Hand History File {0} in directory {1}", file.Name, file.DirectoryName);

            Data.HandHistoryFile currentFile = this.Store.GetHandHistoryFile(file.FullName);
            if (currentFile == null)
            {
                string name = System.IO.Path.GetFileNameWithoutExtension(file.Name);
                string path = file.FullName;

                currentFile             = new Data.HandHistoryFile();
                currentFile.Path        = path;
                currentFile.Name        = name;
                currentFile.SiteId      = this._siteId;
                currentFile.DateAdded   = UnixTimestamp.UtcNow;
                currentFile.MachineName = Environment.MachineName;
                currentFile.User        = Environment.UserName;
                currentFile.DateCreated = new UnixTimestamp(file.CreationTimeUtc);

                using (this.Store.Lock())
                {
                    currentFile.Id = this.Store.InsertHandHistoryFile(_siteId,
                                                                      name, path, 0, currentFile.MachineName,
                                                                      currentFile.User, currentFile.DateCreated.UnixTime);
                }
            }

            using (StreamReader sr = new StreamReader(File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
            {
                string   s     = sr.ReadToEnd();
                string[] lines = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                if (lines == null || lines.Length == 0)
                {
                    Logger.Warn("No lines found in file {0}", file);
                    return;
                }

                // last is the last line number parsed;
                // if the file hasn't been parsed yet, last will be zero
                long last = currentFile.LineNumber;

                for (long i = last; i < lines.Length; i++)
                {
                    string line = lines[i];
                    ParseUserHandHistoryLine(file, line, i);
                }

                this.Store.UpdateLastLine(currentFile.Id, lines.Length);
            }

            using (this.Store.Lock())
            {
                using (System.Data.SQLite.SQLiteConnection connection = this.Store.CreateConnection())
                {
                    connection.Open();

                    using (System.Data.SQLite.SQLiteTransaction transaction = connection.BeginTransaction())
                    {
                        foreach (Data.Hand hand in this._hands)
                        {
                            bool saved = hand.Save(this.Store, connection);
                        }

                        transaction.Commit();
                    }

                    this._stats.TotalPlayers = this.Store.GetPlayerCount();
                    this._stats.TotalHands   = this.Store.GetHandCount();
                }
            }

            this.CurrentHand = null;
            _hands.Clear();

            Logger.Info("Importing Hand History File {0} complete", file.Name);
        }