Пример #1
0
        internal SimpleDb()
        {
            if (!File.Exists(dbPath))
            {
                DummyUsersCreator();
            }

            var linesEncrypted = File.ReadAllLines(dbPath);

            foreach (var lineEncrypted in linesEncrypted)
            {
                var decryptedLine = SimpleDbHash.Decrypt(lineEncrypted);

                var splittedLine = decryptedLine.Split(',');

                if (_users.Any(x => x.Username.Equals(splittedLine[0])))
                {
                    Console.WriteLine("Account already exists for this usr: "******"Reading user: {splittedLine[0]}");
                    _users.Add(new User
                    {
                        AccountState = accountState,
                        Password     = splittedLine[1],
                        Username     = splittedLine[0]
                    });
                }
            }

            timer = new Timer((x) => Flush(), null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
        }
Пример #2
0
        internal void Flush()
        {
            if (!changed)
            {
                Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Flush has nothing to do");
                return;
            }
            if (Lock)
            {
                return;
            }

            changed = false;
            Lock    = true;
            Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Flush started");
            List <string> db = _users
                               .Select(user =>
            {
                Debug.WriteLine($"Flushing user: {user.Username}");
                return(SimpleDbHash.Encrypt($"{user.Username},{user.Password}," +
                                            $"{user.AccountState.ToString(CultureInfo.InvariantCulture)}"));
            })
                               .ToList();

            File.WriteAllLines(dbPath, db);
            Lock = false;
            Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Flush finished");
        }