Skip to content

Commit

Permalink
Update PerformTrade/HandleAbortedBatchTrade methods in all games.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdawg1989 committed Nov 7, 2024
1 parent 6ff9c9a commit d583aef
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 113 deletions.
75 changes: 43 additions & 32 deletions SysBot.Pokemon/BDSP/BotTrade/PokeTradeBotBS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,6 @@ private void CleanupAllBatchTradesFromQueue(PokeTradeDetail<PB8> detail)
Log($"Cleaned up batch trades for TrainerID: {detail.Trainer.ID}, UniqueTradeID: {detail.UniqueTradeID}");
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PB8> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
if (detail.TotalBatchTrades > 1)
{
// Send notification once before cleanup
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades.");

CleanupAllBatchTradesFromQueue(detail);

// Mark this specific trade as canceled
detail.TradeCanceled(this, result);

await EnsureOutsideOfUnionRoom(token).ConfigureAwait(false);
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task DoTrades(SAV8BS sav, CancellationToken token)
{
var type = Config.CurrentRoutineType;
Expand Down Expand Up @@ -1205,7 +1185,6 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8BS sav, PokeTradeDetai

// Finally do cleanup
Hub.Queues.CompleteTrade(this, poke);
CleanupAllBatchTradesFromQueue(poke);
_batchTracker.ClearReceivedPokemon(poke.Trainer.ID);
break;
}
Expand Down Expand Up @@ -1249,18 +1228,11 @@ private async Task PerformTrade(SAV8BS sav, PokeTradeDetail<PB8> detail, PokeRou
else
result = await PerformLinkCodeTrade(sav, detail, token).ConfigureAwait(false);

if (result == PokeTradeResult.Success)
{
PB8? receivedPokemon = await ReadPokemon(BoxStartOffset, BoxFormatSlotSize, token).ConfigureAwait(false);
return;
}

// Handle failed trades
if (detail.TotalBatchTrades > 1)
if (detail.Type == PokeTradeType.Batch)
{
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
}
else
else if (result != PokeTradeResult.Success)
{
HandleAbortedTrade(detail, type, priority, result);
}
Expand All @@ -1269,14 +1241,53 @@ private async Task PerformTrade(SAV8BS sav, PokeTradeDetail<PB8> detail, PokeRou
{
Log(socket.Message);
result = PokeTradeResult.ExceptionConnection;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
throw;
}
catch (Exception e)
{
Log(e.Message);
result = PokeTradeResult.ExceptionInternal;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PB8> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
detail.IsProcessing = false;

if (detail.TotalBatchTrades > 1)
{
if (result != PokeTradeResult.Success)
{
if (result.ShouldAttemptRetry() && detail.Type != PokeTradeType.Random && !detail.IsRetry)
{
detail.IsRetry = true;
Hub.Queues.Enqueue(type, detail, Math.Min(priority, PokeTradePriorities.Tier2));
detail.SendNotification(this, $"Oops! Something happened during batch trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades}. I'll requeue you for another attempt.");
}
else
{
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades: {result}");
CleanupAllBatchTradesFromQueue(detail);
detail.TradeCanceled(this, result);
await EnsureOutsideOfUnionRoom(token).ConfigureAwait(false);
}
}
else
{
CleanupAllBatchTradesFromQueue(detail);
}
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

Expand Down
75 changes: 43 additions & 32 deletions SysBot.Pokemon/LA/BotTrade/PokeTradeBotLA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,6 @@ private void CleanupAllBatchTradesFromQueue(PokeTradeDetail<PA8> detail)
Log($"Cleaned up batch trades for TrainerID: {detail.Trainer.ID}, UniqueTradeID: {detail.UniqueTradeID}");
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PA8> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
if (detail.TotalBatchTrades > 1)
{
// Send notification once before cleanup
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades.");

CleanupAllBatchTradesFromQueue(detail);

// Mark this specific trade as canceled
detail.TradeCanceled(this, result);

await ExitTrade(false, token).ConfigureAwait(false);
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task PerformTrade(SAV8LA sav, PokeTradeDetail<PA8> detail, PokeRoutineType type, uint priority, CancellationToken token)
{
PokeTradeResult result;
Expand All @@ -296,18 +276,11 @@ private async Task PerformTrade(SAV8LA sav, PokeTradeDetail<PA8> detail, PokeRou
else
result = await PerformLinkCodeTrade(sav, detail, token).ConfigureAwait(false);

if (result == PokeTradeResult.Success)
{
PA8? receivedPokemon = await ReadPokemon(BoxStartOffset, BoxFormatSlotSize, token).ConfigureAwait(false);
return;
}

// Handle failed trades
if (detail.TotalBatchTrades > 1)
if (detail.Type == PokeTradeType.Batch)
{
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
}
else
else if (result != PokeTradeResult.Success)
{
HandleAbortedTrade(detail, type, priority, result);
}
Expand All @@ -316,14 +289,53 @@ private async Task PerformTrade(SAV8LA sav, PokeTradeDetail<PA8> detail, PokeRou
{
Log(socket.Message);
result = PokeTradeResult.ExceptionConnection;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
throw;
}
catch (Exception e)
{
Log(e.Message);
result = PokeTradeResult.ExceptionInternal;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PA8> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
detail.IsProcessing = false;

if (detail.TotalBatchTrades > 1)
{
if (result != PokeTradeResult.Success)
{
if (result.ShouldAttemptRetry() && detail.Type != PokeTradeType.Random && !detail.IsRetry)
{
detail.IsRetry = true;
Hub.Queues.Enqueue(type, detail, Math.Min(priority, PokeTradePriorities.Tier2));
detail.SendNotification(this, $"Oops! Something happened during batch trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades}. I'll requeue you for another attempt.");
}
else
{
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades: {result}");
CleanupAllBatchTradesFromQueue(detail);
detail.TradeCanceled(this, result);
await ExitTrade(false, token).ConfigureAwait(false);
}
}
else
{
CleanupAllBatchTradesFromQueue(detail);
}
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

Expand Down Expand Up @@ -560,7 +572,6 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8LA sav, PokeTradeDetai

// cleanup
Hub.Queues.CompleteTrade(this, poke);
CleanupAllBatchTradesFromQueue(poke);
_batchTracker.ClearReceivedPokemon(poke.Trainer.ID);
await ExitTrade(false, token).ConfigureAwait(false);
break;
Expand Down
91 changes: 47 additions & 44 deletions SysBot.Pokemon/SV/BotTrade/PokeTradeBotSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,6 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV9SV sav, PokeTradeDetai

// Finally do cleanup
Hub.Queues.CompleteTrade(this, poke);
CleanupAllBatchTradesFromQueue(poke);
_batchTracker.ClearReceivedPokemon(poke.Trainer.ID);
break;
}
Expand Down Expand Up @@ -1328,56 +1327,21 @@ private void CleanupAllBatchTradesFromQueue(PokeTradeDetail<PK9> detail)
Log($"Cleaned up batch trades for TrainerID: {detail.Trainer.ID}, UniqueTradeID: {detail.UniqueTradeID}");
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PK9> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
if (detail.TotalBatchTrades > 1)
{
// Send notification once before cleanup
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades.");

CleanupAllBatchTradesFromQueue(detail);

// Mark this specific trade as canceled
detail.TradeCanceled(this, result);

// Attempt to recover
if (await RecoverToPortal(token).ConfigureAwait(false))
{
Log("Recovered to portal after batch trade failure.");
}
else
{
Log("Failed to recover to portal after batch trade failure.");
await RecoverToOverworld(token).ConfigureAwait(false);
}
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task PerformTrade(SAV9SV sav, PokeTradeDetail<PK9> detail, PokeRoutineType type, uint priority, CancellationToken token)
{
PokeTradeResult result;
try
{
// Keep track of the first trade in batch for cleanup purposes
bool isFirstInBatch = detail.BatchTradeNumber == 1;
result = await PerformLinkCodeTrade(sav, detail, token).ConfigureAwait(false);

if (result == PokeTradeResult.Success)
{
PK9? receivedPokemon = await ReadPokemon(BoxStartOffset, BoxFormatSlotSize, token).ConfigureAwait(false);
return;
}
if (detail.Type == PokeTradeType.Batch)
result = await PerformBatchTrade(sav, detail, token).ConfigureAwait(false);
else
result = await PerformLinkCodeTrade(sav, detail, token).ConfigureAwait(false);

// Handle failed trades
if (detail.TotalBatchTrades > 1)
if (detail.Type == PokeTradeType.Batch)
{
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
}
else
else if (result != PokeTradeResult.Success)
{
HandleAbortedTrade(detail, type, priority, result);
}
Expand All @@ -1386,14 +1350,53 @@ private async Task PerformTrade(SAV9SV sav, PokeTradeDetail<PK9> detail, PokeRou
{
Log(socket.Message);
result = PokeTradeResult.ExceptionConnection;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
throw;
}
catch (Exception e)
{
Log(e.Message);
result = PokeTradeResult.ExceptionInternal;
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
if (detail.Type == PokeTradeType.Batch)
await HandleAbortedBatchTrade(detail, type, priority, result, token).ConfigureAwait(false);
else
HandleAbortedTrade(detail, type, priority, result);
}
}

private async Task HandleAbortedBatchTrade(PokeTradeDetail<PK9> detail, PokeRoutineType type, uint priority, PokeTradeResult result, CancellationToken token)
{
detail.IsProcessing = false;

if (detail.TotalBatchTrades > 1)
{
if (result != PokeTradeResult.Success)
{
if (result.ShouldAttemptRetry() && detail.Type != PokeTradeType.Random && !detail.IsRetry)
{
detail.IsRetry = true;
Hub.Queues.Enqueue(type, detail, Math.Min(priority, PokeTradePriorities.Tier2));
detail.SendNotification(this, $"Oops! Something happened during batch trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades}. I'll requeue you for another attempt.");
}
else
{
detail.SendNotification(this, $"Trade {detail.BatchTradeNumber}/{detail.TotalBatchTrades} failed. Canceling remaining batch trades: {result}");
CleanupAllBatchTradesFromQueue(detail);
detail.TradeCanceled(this, result);
await ExitTradeToPortal(false, token).ConfigureAwait(false);
}
}
else
{
CleanupAllBatchTradesFromQueue(detail);
}
}
else
{
HandleAbortedTrade(detail, type, priority, result);
}
}

Expand Down
Loading

0 comments on commit d583aef

Please sign in to comment.