Exemple #1
0
        public async Task <RewardQueueItem> FindByIndexAsync(int index)
        {
            if (queueFile.Exists)
            {
                try
                {
                    await successSemaphore.WaitAsync();

                    List <RewardQueueItem> items = JsonSerializer.Deserialize <List <RewardQueueItem> >(await File.ReadAllTextAsync(queueFile.FullName), options);

                    if (items.Count > 0)
                    {
                        RewardQueueItem item = items.Find(item => items.IndexOf(item) == (index - 1));

                        if (item != null)
                        {
                            return(item);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e, $"Could not find next queue item by index: {index}");
                }
                finally
                {
                    successSemaphore.Release();
                }
            }

            return(null);
        }
Exemple #2
0
        public async Task <RewardQueueItem> DequeueItemAsync()
        {
            if (queueFile.Exists)
            {
                try
                {
                    await successSemaphore.WaitAsync();

                    List <RewardQueueItem> items = JsonSerializer.Deserialize <List <RewardQueueItem> >(await File.ReadAllTextAsync(queueFile.FullName), options);

                    if (items.Count > 0)
                    {
                        RewardQueueItem item = items[0];

                        if (items.Remove(item))
                        {
                            await File.WriteAllTextAsync(queueFile.FullName, JsonSerializer.Serialize(items, options));

                            logger.LogInformation($"Request: '{item.Request.RewardTitle}' removed from the queue.");
                            return(item);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Could not dequeue item");
                }
                finally
                {
                    successSemaphore.Release();
                }
            }

            return(null);
        }
Exemple #3
0
        private async Task <int> QueueReplayId(RewardQueueItem item)
        {
            if (!queueFile.Exists)
            {
                await File.WriteAllTextAsync(queueFile.FullName, JsonSerializer.Serialize(new List <RewardQueueItem> {
                    item
                }, options));

                return(1);
            }
            else
            {
                List <RewardQueueItem> items = new (JsonSerializer.Deserialize <List <RewardQueueItem> >(await File.ReadAllTextAsync(queueFile.FullName), options)) { item };
                await File.WriteAllTextAsync(queueFile.FullName, JsonSerializer.Serialize(items, options));

                return(items.Count);
            }
        }
Exemple #4
0
        public async Task <(RewardQueueItem Item, int Position)?> RemoveItemAsync(string login)
        {
            if (queueFile.Exists)
            {
                try
                {
                    await successSemaphore.WaitAsync();

                    List <RewardQueueItem> items = JsonSerializer.Deserialize <List <RewardQueueItem> >(await File.ReadAllTextAsync(queueFile.FullName), options);

                    if (items.Count > 0)
                    {
                        RewardQueueItem item = items.Find(item => item.Request.Login.Equals(login, StringComparison.OrdinalIgnoreCase));

                        if (item != null)
                        {
                            int position = items.IndexOf(item) + 1;

                            if (items.Remove(item))
                            {
                                await File.WriteAllTextAsync(queueFile.FullName, JsonSerializer.Serialize(items, options));

                                logger.LogInformation($"Request: '{item.Request.RewardTitle}' removed from the queue.");
                                return(item, position);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Could not remove item from queue");
                }
                finally
                {
                    successSemaphore.Release();
                }
            }

            return(null);
        }
Exemple #5
0
        private async Task AddToFailedRequestsAsync(RewardQueueItem item)
        {
            try
            {
                await failedSemaphore.WaitAsync();

                if (failedFile.Exists)
                {
                    List <RewardQueueItem> items = new(JsonSerializer.Deserialize <List <RewardQueueItem> >(await File.ReadAllTextAsync(failedFile.FullName), options)) { item };
                    await File.WriteAllTextAsync(failedFile.FullName, JsonSerializer.Serialize(items, options));
                }
                else
                {
                    await File.WriteAllTextAsync(failedFile.FullName, JsonSerializer.Serialize(new List <RewardQueueItem> {
                        item
                    }, options));
                }
            }
            finally
            {
                failedSemaphore.Release();
            }
        }