Пример #1
0
            public bool Equals(PlayersKey obj)
            {
                if (obj == null)
                {
                    return(false);
                }

                return(PlayerName == obj.PlayerName && PokersiteId == obj.PokersiteId);
            }
Пример #2
0
        static void MigratePlayerStatistic()
        {
            var players = ReadPlayers();

            var playersDataFolder = StringFormatter.GetPlayersDataFolderPath();

            if (!Directory.Exists(playersDataFolder))
            {
                Console.WriteLine($"Directory \"{playersDataFolder}\" not found. No data to be migrated.");
                return;
            }

            var statFiles = Directory.GetFiles(playersDataFolder, "*.stat", SearchOption.AllDirectories);

            Console.WriteLine($"Total files found: {statFiles.Length}");
            Console.WriteLine("Reading data...");

            var playerStatistic = new List <Playerstatistic>();

            var  maxBuffer     = 25000;
            var  totalMigrated = 0;
            long totalTime     = 0;

            var sw = new Stopwatch();

            sw.Start();

            foreach (var file in statFiles)
            {
                try
                {
                    using (var sr = new StreamReader(file, new UTF8Encoding(false, true)))
                    {
                        while (sr.Peek() >= 0)
                        {
                            if (playerStatistic.Count >= maxBuffer)
                            {
                                sw.Stop();
                                Console.WriteLine($"Read {playerStatistic.Count} rows [{sw.ElapsedMilliseconds}]");
                                InsertPlayerStatistic(playerStatistic, ref totalMigrated, ref totalTime);
                                sw.Restart();
                            }

                            try
                            {
                                var line = sr.ReadLine().Replace("\0", string.Empty);

                                if (string.IsNullOrWhiteSpace(line))
                                {
                                    continue;
                                }

                                var byteAfter64 = Convert.FromBase64String(line.Replace('-', '+').Replace('_', '/'));

                                using (MemoryStream afterStream = new MemoryStream(byteAfter64))
                                {
                                    var stat = Serializer.Deserialize <Playerstatistic>(afterStream);

                                    var playerKey = new PlayersKey {
                                        PlayerName = stat.PlayerName, PokersiteId = stat.PokersiteId
                                    };

                                    if (!players.ContainsKey(playerKey))
                                    {
                                        Console.WriteLine($"Player \"{stat.PlayerName}\" [PlayerName={stat.PlayerName};PokerSiteId={stat.PokersiteId}] not found. Skipped.");
                                        continue;
                                    }

                                    stat.PlayerId = players[playerKey].PlayerId;

                                    playerStatistic.Add(stat);
                                }
                            }
                            catch (Exception e)
                            {
                                WriteException(e);
                            }
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    WriteException(e);
                }
            }

            sw.Stop();
            Console.WriteLine($"Read {playerStatistic.Count} rows [{sw.ElapsedMilliseconds}]");
            InsertPlayerStatistic(playerStatistic, ref totalMigrated, ref totalTime);

            Console.WriteLine($"Migrated rows of PlayerStatistic: {totalMigrated} [{totalTime} ms]");
        }