switched to new way
This commit is contained in:
parent
f22d182c8f
commit
4a65c8e17b
96 changed files with 4991 additions and 10025 deletions
71
src/Poe2Trade.Bot/TradeQueue.cs
Normal file
71
src/Poe2Trade.Bot/TradeQueue.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using Poe2Trade.Core;
|
||||
using Serilog;
|
||||
|
||||
namespace Poe2Trade.Bot;
|
||||
|
||||
public class TradeQueue
|
||||
{
|
||||
private readonly Queue<TradeInfo> _queue = new();
|
||||
private readonly TradeExecutor _executor;
|
||||
private readonly AppConfig _config;
|
||||
private bool _processing;
|
||||
|
||||
public TradeQueue(TradeExecutor executor, AppConfig config)
|
||||
{
|
||||
_executor = executor;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public int Length => _queue.Count;
|
||||
public bool IsProcessing => _processing;
|
||||
|
||||
public event Action? TradeCompleted;
|
||||
public event Action? TradeFailed;
|
||||
|
||||
public void Enqueue(TradeInfo trade)
|
||||
{
|
||||
var existingIds = _queue.SelectMany(t => t.ItemIds).ToHashSet();
|
||||
var newIds = trade.ItemIds.Where(id => !existingIds.Contains(id)).ToList();
|
||||
if (newIds.Count == 0)
|
||||
{
|
||||
Log.Information("Skipping duplicate trade: {ItemIds}", string.Join(",", trade.ItemIds));
|
||||
return;
|
||||
}
|
||||
|
||||
var deduped = trade with { ItemIds = newIds };
|
||||
_queue.Enqueue(deduped);
|
||||
Log.Information("Trade enqueued: {Count} items, queue={QueueLen}", newIds.Count, _queue.Count);
|
||||
_ = ProcessNext();
|
||||
}
|
||||
|
||||
private async Task ProcessNext()
|
||||
{
|
||||
if (_processing || _queue.Count == 0) return;
|
||||
_processing = true;
|
||||
|
||||
var trade = _queue.Dequeue();
|
||||
try
|
||||
{
|
||||
Log.Information("Processing trade: {SearchId} ({Count} items)", trade.SearchId, trade.ItemIds.Count);
|
||||
var success = await _executor.ExecuteTrade(trade);
|
||||
if (success)
|
||||
{
|
||||
Log.Information("Trade completed");
|
||||
TradeCompleted?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Information("Trade failed");
|
||||
TradeFailed?.Invoke();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Trade execution error");
|
||||
}
|
||||
|
||||
_processing = false;
|
||||
await Helpers.RandomDelay(_config.BetweenTradesDelayMs, _config.BetweenTradesDelayMs + 3000);
|
||||
_ = ProcessNext();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue