Example #1
0
        public void SaveIndexFile()
        {
            if (_indexFile.Exists)
            {
                _indexFile.Delete();
            }

            var jsonIndex = new JsonIndex
            {
                AvailableIndex = Index,
                Entries        = _indexEntries.Select(keyVal => new JsonIndex.JsonEntry
                {
                    TargetedIndex   = keyVal.Key,
                    ExpireTimestamp = keyVal.Value
                })
                                 .ToArray()
            };

            using (var writer = File.CreateText(_indexFile.ToString()))
            {
                var str = JsonConvert.SerializeObject(jsonIndex, Formatting.Indented);
                writer.Write(str);
            }

            _log.Debug("Successfully wrote index file.");
        }
Example #2
0
        private void ParseIndexFile()
        {
            if (_indexFile.Exists) // check if file exists
            {
                using (var reader = File.OpenText(_indexFile.ToString()))
                {
                    _parsedIndex = (JsonIndex)Titan.Instance.JsonSerializer.Deserialize(reader, typeof(JsonIndex));
                    // read it and deserialize it into a Index object
                }

                var lowest = _parsedIndex.AvailableIndex; // find the available index and set it to lowest

                if (lowest == -1)
                {
                    lowest++;
                }

                foreach (var expireEntry in _parsedIndex.Entries)
                {
                    // check if a entry that is marked for expiration is already expired and ready to bot
                    if (expireEntry.ExpireTimestamp <= TimeUtil.GetCurrentTicks())
                    {
                        _log.Debug("Index {Index} has expired. It is now available for botting.",
                                   expireEntry.TargetedIndex);

                        if (lowest > expireEntry.TargetedIndex) // if thats the case, check if its lower than the available one
                        {
                            lowest = expireEntry.TargetedIndex;
                        }

                        _parsedIndex.Entries = _parsedIndex.Entries.Where(val => val != expireEntry)
                                               .ToArray(); // and remove it from the expiration list
                    }
                    else
                    {
                        if (!_indexEntries.ContainsKey(expireEntry.TargetedIndex))
                        {
                            _indexEntries.Add(expireEntry.TargetedIndex, expireEntry.ExpireTimestamp);
                        }

                        _log.Debug("Index #{Index} hasn't expired yet. It will expire on {Time}.",
                                   expireEntry.TargetedIndex,
                                   TimeUtil.TicksToDateTime(expireEntry.ExpireTimestamp).ToShortTimeString());

                        if (expireEntry.TargetedIndex == lowest)
                        {
                            lowest++;
                        }
                    }
                }

                Index = lowest;
            }
            else
            {
                Index = 0;
                SaveIndexFile(); // it doesn't exist, we're gonna' create it!
            }

            var valid = false;

            foreach (var keyVal in Accounts)
            {
                if (keyVal.Key == Index && Index != -1)
                {
                    _log.Debug("Using index #{Index} for botting.", Index);
                    valid = true;
                }
            }

            if (!valid)
            {
                _log.Warning("Index #{index} doesn't exist. The Bot will use index " +
                             "#{ForcedIndex}. Please keep in mind that it may already " +
                             "been used.", Index, Index = 0);
            }
        }