예제 #1
0
        public override void Save(IEnumerable <Card> cards, BuyAgenda agenda)
        {
            string id = cards.ToId();
            int    i  = cards.OrderBy(p => p).Select(p => (int)p.Type).First();

            if (Load(cards) == null)
            {
                lock (locks[i])
                    File.AppendAllText($"{directoryPath}{prefix}{i}.txt", agenda.ToString(id) + Environment.NewLine);
            }
            else
            {
                var dict = new Dictionary <string, string>();
                lock (locks[i])
                {
                    files[i] = null;
                    foreach (var line in File.ReadAllLines($"{directoryPath}{prefix}{i}.txt").Select(l => l.Split(':')))
                    {
                        dict[line[0]] = $"{line[0]}:{line[1]}";
                    }
                    dict[id] = agenda.ToString(id);
                    using (var writer = new StreamWriter($"{directoryPath}{prefix}{i}.txt"))
                        foreach (var a in dict.OrderBy(d => d.Key))
                        {
                            writer.WriteLine(a.Value);
                        }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// This method does not saves other kingdoms.
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        public override BuyAgenda Load(IEnumerable <Card> cards)
        {
            var id = cards.ToId();
            int i  = (int)cards.OrderBy(c => c.Type).First().Type;

            lock (_lock)
            {
                try
                {
                    using (var reader = new StreamReader($"{directoryPath}{prefix}{i}.txt"))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();
                            if (!string.IsNullOrEmpty(line))
                            {
                                if (line.Split(':')[0] == id)
                                {
                                    return(BuyAgenda.FromString(line));
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            return(null);
        }
예제 #3
0
        public override IEnumerator <BuyAgenda> GetEnumerator()
        {
            for (int i = 8; i < 32; i++)
            {
                lock (locks[i])
                {
                    try
                    {
                        if (files[i] == null)
                        {
                            LoadAllAgendas(i);
                        }
                    }
                    catch
                    {
                        continue;
                    }

                    foreach (var item in files[i])
                    {
                        yield return(BuyAgenda.FromString(item.Value));
                    }
                }
            }

            // free memory
            files = new Dictionary <string, string> [33];
        }
예제 #4
0
 public override IEnumerator <BuyAgenda> GetEnumerator()
 {
     for (int i = 8; i < 32; i++)
     {
         lock (_lock)
             if (File.Exists($"{directoryPath}{prefix}{i}.txt"))
             {
                 using (var reader = new StreamReader($"{directoryPath}{prefix}{i}.txt"))
                     while (!reader.EndOfStream)
                     {
                         yield return(BuyAgenda.FromString(reader.ReadLine()));
                     }
             }
     }
 }
예제 #5
0
        /// <summary>
        /// This method does saves other kingdoms.
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        public override BuyAgenda Load(IEnumerable <Card> cards)
        {
            var id = cards.ToId();
            int i  = cards.OrderBy(p => p.Type).Select(p => (int)p.Type).First();

            try
            {
                if (files[i] == null)
                {
                    LoadAllAgendas(i);
                }

                lock (locks[i])
                    return(BuyAgenda.FromString(files[i][id]));
            }
            catch (Exception)
            {
                return(null);
            }
        }