private async Task <bool> HostRaidAsync(SAV8SWSH sav, int code, CancellationToken token) { if (Hub.Config.Raid.AutoRoll) { await AutoRollDen(token).ConfigureAwait(false); } // Connect to Y-Comm await EnsureConnectedToYComm(Hub.Config, token).ConfigureAwait(false); // Press A and stall out a bit for the loading await Click(A, 5_000 + Hub.Config.Raid.ExtraTimeLoadRaid, token).ConfigureAwait(false); if (raidBossSpecies == -1) { var data = await Connection.ReadBytesAsync(RaidBossOffset, 2, token).ConfigureAwait(false); raidBossSpecies = BitConverter.ToUInt16(data, 0); } Log($"Initializing raid for {(Species)raidBossSpecies}."); if (code >= 0) { // Set Link code await Click(PLUS, 1_000, token).ConfigureAwait(false); await EnterTradeCode(code, token).ConfigureAwait(false); await Click(PLUS, 2_000, token).ConfigureAwait(false); await Click(A, 1_000, token).ConfigureAwait(false); } if (addFriends && !string.IsNullOrEmpty(Settings.FriendCode)) { EchoUtil.Echo($"```fix\nSend a friend request to Friend Code **{Settings.FriendCode}** to join in! Friends will be added after this raid.```"); } // Invite others, confirm Pokémon and wait await Click(A, 7_000 + Hub.Config.Raid.ExtraTimeOpenRaid, token).ConfigureAwait(false); await Click(DUP, 1_000, token).ConfigureAwait(false); await Click(A, 1_000, token).ConfigureAwait(false); var linkcodemsg = code < 0 ? "no Link Code" : $"code **{code:0000 0000}**"; string raiddescmsg = string.IsNullOrEmpty(Hub.Config.Raid.RaidDescription) ? ((Species)raidBossSpecies).ToString() : Hub.Config.Raid.RaidDescription; RaidLog(sav, linkcodemsg, raiddescmsg); if (Hub.Config.Raid.EchoRaidNotifications) { EchoUtil.Echo($"```fix\nRaid lobby for {raiddescmsg} is open with {linkcodemsg}.```"); } var timetowait = Hub.Config.Raid.MinTimeToWait * 1_000; var timetojoinraid = 175_000 - timetowait; Log("Waiting on raid party..."); // Wait the minimum timer or until raid party fills up. while (timetowait > 0 && !await GetRaidPartyReady(token).ConfigureAwait(false)) { await Task.Delay(1_000, token).ConfigureAwait(false); timetowait -= 1_000; } await Task.Delay(1_000, token).ConfigureAwait(false); if (Hub.Config.Raid.EchoRaidNotifications) { EchoUtil.Echo($"```fix\nRaid is starting now with {linkcodemsg}.```"); } /* Press A and check if we entered a raid. If other users don't lock in, * it will automatically start once the timer runs out. If we don't make it into * a raid by the end, something has gone wrong and we should quit trying. */ while (timetojoinraid > 0 && !await IsInBattle(token).ConfigureAwait(false)) { await Click(A, 0_500, token).ConfigureAwait(false); timetojoinraid -= 0_500; } for (int i = 0; i < 4; i++) { PlayerReady[i] = false; } Log("Finishing raid routine."); await Task.Delay(5_000 + Hub.Config.Raid.ExtraTimeEndRaid, token).ConfigureAwait(false); return(false); }
private async Task <bool> HostRaidAsync(SAV8SWSH sav, int code, CancellationToken token) { // Connect to Y-Comm await EnsureConnectedToYComm(Hub.Config, token).ConfigureAwait(false); // Press A and stall out a bit for the loading await Click(A, 5000, token).ConfigureAwait(false); if (code >= 0) { // Set Link code await Click(PLUS, 1000, token).ConfigureAwait(false); await EnterTradeCode(code, token).ConfigureAwait(false); EchoUtil.Echo($"Raid code is {code}."); // Raid barrier here maybe? await Click(PLUS, 2_000, token).ConfigureAwait(false); await Click(A, 1000, token).ConfigureAwait(false); } // Invite others, confirm Pokémon and wait await Click(A, 7000, token).ConfigureAwait(false); await Click(DUP, 1000, token).ConfigureAwait(false); await Click(A, 1000, token).ConfigureAwait(false); await ClearRaidTrainerName(token).ConfigureAwait(false); // Use Offset to actually calculate this value and press A var timetowait = 3 * 60 * 1000; await Task.Delay(1000, token).ConfigureAwait(false); while (timetowait > 0) { bool result = await GetIsRaidPartyIsFullAsync(token).ConfigureAwait(false); if (result) { break; } await Task.Delay(1000, token).ConfigureAwait(false); timetowait -= 1000; } await Click(A, 500, token).ConfigureAwait(false); if (timetowait > 0) { Log("All participants have joined."); while (!await IsInBattle(token).ConfigureAwait(false)) { await Click(A, 500, token).ConfigureAwait(false); } } else { Log("Not all participants have joined. Continuing anyway!"); while (!await IsInBattle(token).ConfigureAwait(false)) { await Click(A, 500, token).ConfigureAwait(false); } } Log($"Hosting raid as {sav.OT} with code: {code:0000}."); await Task.Delay(5_000, token).ConfigureAwait(false); return(false); }
public BoxLayout8(SAV8SWSH sav, SCBlock block) : base(sav, block.Data) { }
private async Task <PokeTradeResult> PerformLinkCodeTrade(SAV8SWSH sav, PokeTradeDetail <PK8> poke, CancellationToken token) { // Update Barrier Settings UpdateBarrier(poke.IsSynchronized); poke.TradeInitialize(this); Hub.Config.Stream.EndEnterCode(this); if (await CheckIfSoftBanned(token).ConfigureAwait(false)) { await Unban(token).ConfigureAwait(false); } var pkm = poke.TradeData; if (pkm.Species != 0) { await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); } if (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverStart); } while (await CheckIfSearchingForLinkTradePartner(token).ConfigureAwait(false)) { Log("Still searching, reset bot position."); await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); } Log("Opening Y-Comm Menu"); await Click(Y, 2_000, token).ConfigureAwait(false); Log("Selecting Link Trade"); await Click(A, 1_500, token).ConfigureAwait(false); if (poke.Type != PokeTradeType.Random) { Log("Selecting Link Trade Code"); await Click(DDOWN, 500, token).ConfigureAwait(false); for (int i = 0; i < 2; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } // All other languages require an extra A press at this menu. if (GameLang != LanguageID.English && GameLang != LanguageID.Spanish) { await Click(A, 1_500, token).ConfigureAwait(false); } // Loading Screen if (poke.Type != PokeTradeType.Random) { Hub.Config.Stream.StartEnterCode(this); } await Task.Delay(Hub.Config.Timings.ExtraTimeOpenCodeEntry, token).ConfigureAwait(false); var code = poke.Code; Log($"Entering Link Trade Code: {code:0000 0000}..."); poke.SendNotification(this, $"Entering Link Trade Code: {code:0000 0000}..."); await EnterTradeCode(code, Hub.Config, token).ConfigureAwait(false); } // Wait for Barrier to trigger all bots simultaneously. WaitAtBarrierIfApplicable(token); await Click(PLUS, 1_000, token).ConfigureAwait(false); Hub.Config.Stream.EndEnterCode(this); // Confirming and return to overworld. var delay_count = 0; while (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { if (delay_count >= 5) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverPostLinkCode); } for (int i = 0; i < 5; i++) { await Click(A, 0_800, token).ConfigureAwait(false); } delay_count++; } poke.TradeSearching(this); await Task.Delay(0_500, token).ConfigureAwait(false); // Wait for a Trainer... Log("Waiting for trainer..."); bool partnerFound = await WaitForPokemonChanged(LinkTradePartnerPokemonOffset, Hub.Config.Trade.TradeWaitTime * 1_000, 0_200, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (!partnerFound) { await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); return(PokeTradeResult.NoTrainerFound); } // Select Pokemon // pkm already injected to b1s1 await Task.Delay(5_500, token).ConfigureAwait(false); // necessary delay to get to the box properly var TrainerName = await GetTradePartnerName(TradeMethod.LinkTrade, token).ConfigureAwait(false); TrainerName = TrainerName.Replace('&', '+'); Log($"Found Trading Partner: {TrainerName}..."); if (!await IsInBox(token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverOpenBox); } // Confirm Box 1 Slot 1 if (poke.Type == PokeTradeType.Specific) { for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } poke.SendNotification(this, $"Found Trading Partner: {TrainerName}. Waiting for a Pokémon..."); if (poke.Type == PokeTradeType.Dump) { return(await ProcessDumpTradeAsync(poke, token).ConfigureAwait(false)); } // Wait for User Input... var pk = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 25_000, 1_000, token).ConfigureAwait(false); var oldEC = await Connection.ReadBytesAsync(LinkTradePartnerPokemonOffset, 4, token).ConfigureAwait(false); if (pk == null) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } SpecialTradeType itemReq = SpecialTradeType.None; if (poke.Type == PokeTradeType.Seed) { itemReq = CheckItemRequest(ref pk, this, poke, TrainerName, sav); } if (itemReq == SpecialTradeType.FailReturn) { await ExitTrade(Hub.Config, false, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (poke.Type == PokeTradeType.Seed && itemReq == SpecialTradeType.None) { // Immediately exit, we aren't trading anything. return(await EndSeedCheckTradeAsync(poke, pk, token).ConfigureAwait(false)); } if (poke.Type == PokeTradeType.Random) // distribution { // Allow the trade partner to do a Ledy swap. var trade = Hub.Ledy.GetLedyTrade(pk, Hub.Config.Distribution.LedySpecies); if (trade != null) { pkm = trade.Receive; poke.TradeData = pkm; poke.SendNotification(this, "Injecting the requested Pokémon."); await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); await Task.Delay(2_500, token).ConfigureAwait(false); } for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } else if (poke.Type == PokeTradeType.Clone || itemReq != SpecialTradeType.None) { // Inject the shown Pokémon. var clone = (PK8)pk.Clone(); if (itemReq != SpecialTradeType.WonderCard) { if (Hub.Config.Discord.ReturnPK8s) { poke.SendNotification(this, clone, "Here's what you showed me!"); } var la = new LegalityAnalysis(clone); if (!la.Valid) { Log($"Clone request has detected an invalid Pokémon: {(Species)clone.Species}"); if (DumpSetting.Dump) { DumpPokemon(DumpSetting.DumpFolder, "hacked", pk); } var report = la.Report(); Log(report); poke.SendNotification(this, "This Pokémon is not legal per PKHeX's legality checks. I am forbidden from cloning this. Exiting trade."); if (itemReq != SpecialTradeType.None) { poke.SendNotification(this, "SSRYour request isn't legal. Please try a different Pokémon or request."); if (!string.IsNullOrWhiteSpace(Hub.Config.Web.URIEndpoint)) { SpecialRequests.AddToPlayerLimit(TrainerName, -1); } } poke.SendNotification(this, report); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (Hub.Config.Legality.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, $"**Cloned your {(Species)clone.Species}!**\nNow press B to cancel your offer and trade me a Pokémon you don't want."); Log($"Cloned a {(Species)clone.Species}. Waiting for user to change their Pokémon..."); } if (itemReq != SpecialTradeType.WonderCard) { // Separate this out from WaitForPokemonChanged since we compare to old EC from original read. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); if (!partnerFound) { poke.SendNotification(this, "**HEY CHANGE IT NOW OR I AM LEAVING!!!**"); // They get one more chance. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); } var pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 3_000, 1_000, token).ConfigureAwait(false); if (!partnerFound || pk2 == null || SearchUtil.HashByDetails(pk2) == SearchUtil.HashByDetails(pk)) { Log("Trading partner did not change their Pokémon."); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } } await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); pkm = pk; if (itemReq == SpecialTradeType.WonderCard) { poke.SendNotification(this, "SSRDistribution success!"); } else if (itemReq != SpecialTradeType.None && itemReq != SpecialTradeType.Shinify) { poke.SendNotification(this, "SSRSpecial request successful!"); } else if (itemReq == SpecialTradeType.Shinify) { poke.SendNotification(this, "SSRShinify success! Thanks for being part of the community!"); } for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } await Click(A, 3_000, token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } delay_count = 0; while (!await IsInBox(token).ConfigureAwait(false)) { await Click(A, 3_000, token).ConfigureAwait(false); delay_count++; if (delay_count >= 30) { break; } if (await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) // In case we are in a Trade Evolution/PokeDex Entry and the Trade Partner quits we land on the Overworld { break; } } await Task.Delay(1_000 + Util.Rand.Next(0_700, 1_000), token).ConfigureAwait(false); await ExitTrade(Hub.Config, false, token).ConfigureAwait(false); Log("Exited Trade!"); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } // Trade was Successful! var traded = await ReadBoxPokemon(InjectBox, InjectSlot, token).ConfigureAwait(false); // Pokémon in b1s1 is same as the one they were supposed to receive (was never sent). if (SearchUtil.HashByDetails(traded) == SearchUtil.HashByDetails(pkm)) { Log("User did not complete the trade."); return(PokeTradeResult.TrainerTooSlow); } else { // As long as we got rid of our inject in b1s1, assume the trade went through. Log("User completed the trade."); poke.TradeFinished(this, traded); // Only log if we completed the trade. var counts = Hub.Counts; if (poke.Type == PokeTradeType.Random) { counts.AddCompletedDistribution(); } else if (poke.Type == PokeTradeType.Clone) { counts.AddCompletedClones(); } else { Hub.Counts.AddCompletedTrade(); } if (DumpSetting.Dump && !string.IsNullOrEmpty(DumpSetting.DumpFolder)) { var subfolder = poke.Type.ToString().ToLower(); DumpPokemon(DumpSetting.DumpFolder, subfolder, traded); // received if (poke.Type == PokeTradeType.Specific || poke.Type == PokeTradeType.Clone) { DumpPokemon(DumpSetting.DumpFolder, "traded", pkm); // sent to partner } } } return(PokeTradeResult.Success); }
private async Task <PokeTradeResult> PerformSurpriseTrade(SAV8SWSH sav, PK8 pkm, CancellationToken token) { // General Bot Strategy: // 1. Inject to b1s1 // 2. Send out Trade // 3. Clear received PKM to skip the trade animation // 4. Repeat // Inject to b1s1 if (await CheckIfSoftBanned(token).ConfigureAwait(false)) { await Unban(token).ConfigureAwait(false); } Log("Starting next Surprise Trade. Getting data..."); await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); if (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverStart); } if (await CheckIfSearchingForSurprisePartner(token).ConfigureAwait(false)) { Log("Still searching, reset."); await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); } Log("Opening Y-Comm Menu"); await Click(Y, 1_500, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } Log("Selecting Surprise Trade"); await Click(DDOWN, 0_500, token).ConfigureAwait(false); await Click(A, 2_000, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } await Task.Delay(0_750, token).ConfigureAwait(false); if (!await IsInBox(token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverPostLinkCode); } Log("Selecting Pokémon"); // Box 1 Slot 1; no movement required. await Click(A, 0_700, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } Log("Confirming..."); while (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { await Click(A, 0_800, token).ConfigureAwait(false); } if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } // Let Surprise Trade be sent out before checking if we're back to the Overworld. await Task.Delay(3_000, token).ConfigureAwait(false); if (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverReturnOverworld); } // Wait 30 Seconds for Trainer... Log("Waiting for Surprise Trade Partner..."); // Wait for an offer... var oldEC = await Connection.ReadBytesAsync(SurpriseTradeSearchOffset, 4, token).ConfigureAwait(false); var partnerFound = await ReadUntilChanged(SurpriseTradeSearchOffset, oldEC, Hub.Config.Trade.TradeWaitTime * 1_000, 0_200, false, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (!partnerFound) { await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); return(PokeTradeResult.NoTrainerFound); } // Let the game flush the results and de-register from the online surprise trade queue. await Task.Delay(7_000, token).ConfigureAwait(false); var TrainerName = await GetTradePartnerName(TradeMethod.SupriseTrade, token).ConfigureAwait(false); var SurprisePoke = await ReadSurpriseTradePokemon(token).ConfigureAwait(false); Log($"Found Surprise Trade Partner: {TrainerName}, Pokémon: {(Species)SurprisePoke.Species}"); // Clear out the received trade data; we want to skip the trade animation. // The box slot locks have been removed prior to searching. await Connection.WriteBytesAsync(BitConverter.GetBytes(SurpriseTradeSearch_Empty), SurpriseTradeSearchOffset, token).ConfigureAwait(false); await Connection.WriteBytesAsync(PokeTradeBotUtil.EMPTY_SLOT, SurpriseTradePartnerPokemonOffset, token).ConfigureAwait(false); // Let the game recognize our modifications before finishing this loop. await Task.Delay(5_000, token).ConfigureAwait(false); // Clear the Surprise Trade slot locks! We'll skip the trade animation and reuse the slot on later loops. // Write 8 bytes of FF to set both Int32's to -1. Regular locks are [Box32][Slot32] await Connection.WriteBytesAsync(BitConverter.GetBytes(ulong.MaxValue), SurpriseTradeLockBox, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { Log("Trade complete!"); } else { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); } if (DumpSetting.Dump && !string.IsNullOrEmpty(DumpSetting.DumpFolder)) { DumpPokemon(DumpSetting.DumpFolder, "surprise", SurprisePoke); } Hub.Counts.AddCompletedSurprise(); return(PokeTradeResult.Success); }
private async Task DoTrades(SAV8SWSH sav, CancellationToken token) { var type = Config.CurrentRoutineType; int waitCounter = 0; bool doDistributionNext = false; await SetCurrentBox(0, token).ConfigureAwait(false); while (!token.IsCancellationRequested && Config.NextRoutineType == type) { if (doDistributionNext && Hub.Config.Distribution.SurpriseTradeWhileIdle) { doDistributionNext = false; for (int x = 0; x < 2; ++x) { var pkmST = Hub.Ledy.Pool.GetRandomSurprise(); Log($"Surprise trade {x+1}/2 initiated. Sending: {(Species)pkmST.Species}"); await EnsureConnectedToYComm(Hub.Config, token).ConfigureAwait(false); var _ = await PerformSurpriseTrade(sav, pkmST, token).ConfigureAwait(false); } } if (!Hub.Queues.TryDequeue(type, out var detail, out var priority) && !Hub.Queues.TryDequeueLedy(out detail)) { if (waitCounter == 0) { // Updates the assets. Hub.Config.Stream.IdleAssets(this); Log("Nothing to check, waiting for new users..."); } waitCounter++; if (waitCounter % 10 == 0 && Hub.Config.AntiIdle) { await Click(B, 1_000, token).ConfigureAwait(false); } else if (waitCounter % 5 == 0) { doDistributionNext = true; } else { await Task.Delay(1_000, token).ConfigureAwait(false); } continue; } waitCounter = 0; doDistributionNext = false; string tradetype = $" ({detail.Type})"; Log($"Starting next {type}{tradetype} Bot Trade. Getting data..."); await Task.Delay(500, token).ConfigureAwait(false); // hack number 301923 for getting web to work Hub.Config.Stream.StartTrade(this, detail, Hub); Hub.Queues.StartTrade(this, detail); await EnsureConnectedToYComm(Hub.Config, token).ConfigureAwait(false); var result = await PerformLinkCodeTrade(sav, detail, token).ConfigureAwait(false); if (result != PokeTradeResult.Success) // requeue { if (result.AttemptRetry() && detail.Type != PokeTradeType.Random && !detail.IsRetry) { detail.IsRetry = true; detail.SendNotification(this, "Oops! Something happened. I'll requeue you for another attempt."); Hub.Queues.Enqueue(type, detail, Math.Min(priority, PokeTradeQueue <PK8> .Tier2)); } else { detail.SendNotification(this, $"Oops! Something happened. Canceling the trade: {result}."); detail.TradeCanceled(this, result); if (result != PokeTradeResult.IllegalTrade) // only if waited too long { doDistributionNext = true; } } } } UpdateBarrier(false); }
public static void InitializeStubs() { var sav8 = new SAV8SWSH(); SetUpSpriteCreator(sav8); }
private async Task <PokeTradeResult> PerformLinkCodeTrade(SAV8SWSH sav, PokeTradeDetail <PK8> poke, CancellationToken token) { // Update Barrier Settings UpdateBarrier(poke.IsSynchronized); poke.TradeInitialize(this); Hub.Config.Stream.EndEnterCode(this); if (await CheckIfSoftBanned(token).ConfigureAwait(false)) { await Unban(token).ConfigureAwait(false); } var pkm = poke.TradeData; if (pkm.Species != 0) { await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); } if (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverStart); } while (await CheckIfSearchingForLinkTradePartner(token).ConfigureAwait(false)) { Log("Still searching, reset bot position."); await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); } Log("Opening Y-Comm Menu"); await Click(Y, 2_000, token).ConfigureAwait(false); Log("Selecting Link Trade"); await Click(A, 1_500, token).ConfigureAwait(false); Log("Selecting Link Trade Code"); await Click(DDOWN, 500, token).ConfigureAwait(false); for (int i = 0; i < 2; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } // All other languages require an extra A press at this menu. if (GameLang != LanguageID.English && GameLang != LanguageID.Spanish) { await Click(A, 1_500, token).ConfigureAwait(false); } // Loading Screen if (poke.Type != PokeTradeType.Random) { Hub.Config.Stream.StartEnterCode(this); } await Task.Delay(Hub.Config.Timings.ExtraTimeOpenCodeEntry, token).ConfigureAwait(false); var code = poke.Code; Log($"Entering Link Trade Code: {code:0000 0000}..."); await EnterTradeCode(code, Hub.Config, token).ConfigureAwait(false); // Wait for Barrier to trigger all bots simultaneously. WaitAtBarrierIfApplicable(token); await Click(PLUS, 1_000, token).ConfigureAwait(false); Hub.Config.Stream.EndEnterCode(this); // Confirming and return to overworld. var delay_count = 0; while (!await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) { if (delay_count >= 5) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverPostLinkCode); } for (int i = 0; i < 5; i++) { await Click(A, 0_800, token).ConfigureAwait(false); } delay_count++; } poke.TradeSearching(this); await Task.Delay(0_500, token).ConfigureAwait(false); // Wait for a Trainer... Log("Waiting for trainer..."); bool partnerFound = await WaitForPokemonChanged(LinkTradePartnerPokemonOffset, Hub.Config.Trade.TradeWaitTime * 1_000, 0_200, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (!partnerFound) { await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); return(PokeTradeResult.NoTrainerFound); } // Select Pokemon // pkm already injected to b1s1 await Task.Delay(5_500, token).ConfigureAwait(false); // necessary delay to get to the box properly var TrainerName = await GetTradePartnerName(TradeMethod.LinkTrade, token).ConfigureAwait(false); Log($"Found Trading Partner: {TrainerName}..."); if (!await IsInBox(token).ConfigureAwait(false)) { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverOpenBox); } // Confirm Box 1 Slot 1 if (poke.Type == PokeTradeType.Specific || poke.Type == PokeTradeType.TradeCord || poke.Type == PokeTradeType.Giveaway) { for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } poke.SendNotification(this, $"Found Trading Partner: {TrainerName}. Waiting for a Pokémon..."); if (poke.Type == PokeTradeType.Dump) { return(await ProcessDumpTradeAsync(poke, token).ConfigureAwait(false)); } // Wait for User Input... var pk = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 25_000, 1_000, token).ConfigureAwait(false); var oldEC = await Connection.ReadBytesAsync(LinkTradePartnerPokemonOffset, 4, token).ConfigureAwait(false); if (pk == null) { if (poke.Type == PokeTradeType.Seed) { await ExitSeedCheckTrade(Hub.Config, token).ConfigureAwait(false); } else { await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); } return(PokeTradeResult.TrainerTooSlow); } if (poke.Type == PokeTradeType.Seed) { // Immediately exit, we aren't trading anything. return(await EndSeedCheckTradeAsync(poke, pk, token).ConfigureAwait(false)); } SpecialTradeType itemReq = SpecialTradeType.None; if (poke.Type == PokeTradeType.SpecialRequest) { itemReq = CheckItemRequest(ref pk, this, poke, TrainerName, sav); } if (itemReq == SpecialTradeType.FailReturn) { await ExitTrade(Hub.Config, false, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (poke.Type == PokeTradeType.SpecialRequest && itemReq == SpecialTradeType.None) { // Immediately exit, we aren't trading anything. poke.SendNotification(this, "Held item was outside the bounds of the Array, or nothing was held."); await ResetTradePosition(Hub.Config, token).ConfigureAwait(false); return(PokeTradeResult.IncorrectHeldItem); } if (poke.Type == PokeTradeType.Random) // distribution { // Allow the trade partner to do a Ledy swap. var trade = Hub.Ledy.GetLedyTrade(pk, Hub.Config.Distribution.LedySpecies); if (trade != null) { pkm = trade.Receive; poke.TradeData = pkm; poke.SendNotification(this, "Injecting the requested Pokémon."); await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); await Task.Delay(2_500, token).ConfigureAwait(false); } for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } else if (poke.Type == PokeTradeType.Clone || itemReq != SpecialTradeType.None) { // Inject the shown Pokémon. var clone = (PK8)pk.Clone(); if (itemReq != SpecialTradeType.WonderCard) { if (Hub.Config.Discord.ReturnPK8s) { poke.SendNotification(this, clone, "Here's what you showed me!"); } var la = new LegalityAnalysis(clone); if (!la.Valid) { Log($"Clone request has detected an invalid Pokémon: {(Species)clone.Species}"); if (DumpSetting.Dump) { DumpPokemon(DumpSetting.DumpFolder, "hacked", pk); } var report = la.Report(); Log(report); poke.SendNotification(this, "This Pokémon is not legal per PKHeX's legality checks. I am forbidden from cloning this. Exiting trade."); if (itemReq != SpecialTradeType.None) { poke.SendNotification(this, "SSRYour request isn't legal. Please try a different Pokémon or request."); if (!string.IsNullOrWhiteSpace(Hub.Config.Web.URIEndpoint)) { SpecialRequests.AddToPlayerLimit(TrainerName, -1); } } poke.SendNotification(this, report); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (Hub.Config.Legality.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, $"**Cloned your {(Species)clone.Species}!**\nNow press B to cancel your offer and trade me a Pokémon you don't want."); Log($"Cloned a {(Species)clone.Species}. Waiting for user to change their Pokémon..."); } if (itemReq != SpecialTradeType.WonderCard) { // Separate this out from WaitForPokemonChanged since we compare to old EC from original read. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); if (!partnerFound) { poke.SendNotification(this, "__**Change It Now Or I Am Leaving!**__"); // They get one more chance. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); } var pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 3_000, 1_000, token).ConfigureAwait(false); if (!partnerFound || pk2 == null || SearchUtil.HashByDetails(pk2) == SearchUtil.HashByDetails(pk)) { Log("Trading partner did not change their Pokémon."); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } } await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); pkm = pk; if (itemReq == SpecialTradeType.WonderCard) { poke.SendNotification(this, "SSRDistribution success!"); Log($"{(Species)clone.Species} Distribution Success!"); } else if (itemReq != SpecialTradeType.None && itemReq != SpecialTradeType.Shinify) { poke.SendNotification(this, "SSRSpecial request successful!"); Log($"{(Species)clone.Species} modification successful!"); } else if (itemReq == SpecialTradeType.Shinify) { poke.SendNotification(this, "Shinify success!"); Log($"Successfully Shinified a {(Species)clone.Species}!"); for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } } else if (poke.Type == PokeTradeType.FixOT) { var clone = (PK8)pk.Clone(); var adOT = System.Text.RegularExpressions.Regex.Match(clone.OT_Name, @"(YT$)|(YT\w*$)|(Lab$)|(\.\w*)|(TV$)|(PKHeX)|(FB:)|(SysBot)|(AuSLove)|(ShinyMart)|(Blainette)").Value != "" || System.Text.RegularExpressions.Regex.Match(clone.Nickname, @"(YT$)|(YT\w*$)|(Lab$)|(\.\w*)|(TV$)|(PKHeX)|(FB:)|(SysBot)|(AuSLove)|(ShinyMart)|(Blainette)").Value != ""; var extraInfo = $"\nBall: {(Ball)clone.Ball}\nShiny: {(clone.ShinyXor == 0 ? "Square" : clone.ShinyXor <= 16 ? "Star" : "No")}{(clone.FatefulEncounter ? "" : $"\nOT: {TrainerName}")}"; var laInit = new LegalityAnalysis(clone); if (laInit.Valid && adOT) { clone.OT_Name = clone.FatefulEncounter ? clone.OT_Name : $"{TrainerName}"; clone.PKRS_Infected = false; clone.PKRS_Cured = false; clone.PKRS_Days = 0; clone.PKRS_Strain = 0; } else if (!laInit.Valid) { Log($"FixOT request has detected an invalid Pokémon from {poke.Trainer.TrainerName}: {(Species)clone.Species}"); if (DumpSetting.Dump) { DumpPokemon(DumpSetting.DumpFolder, "hacked", clone); } poke.SendNotification(this, $"```fix\nShown Pokémon is invalid. Attempting to regenerate... \n{laInit.Report()}```"); if (clone.FatefulEncounter) { clone.SetDefaultNickname(laInit); var info = new SimpleTrainerInfo { Gender = clone.OT_Gender, Language = clone.Language, OT = TrainerName, TID = clone.TID, SID = clone.SID }; var mg = EncounterEvent.GetAllEvents().Where(x => x.Species == clone.Species && x.Form == clone.Form && x.IsShiny == clone.IsShiny && x.OT_Name == clone.OT_Name).ToList(); if (mg.Count > 0) { clone = TradeExtensions.CherishHandler(mg.First(), info); } else { clone = (PK8)AutoLegalityWrapper.GetTrainerInfo(8).GetLegal(AutoLegalityWrapper.GetTemplate(new ShowdownSet(ShowdownParsing.GetShowdownText(clone) + extraInfo)), out _); } } else { clone = (PK8)AutoLegalityWrapper.GetTrainerInfo(8).GetLegal(AutoLegalityWrapper.GetTemplate(new ShowdownSet(ShowdownParsing.GetShowdownText(clone) + extraInfo)), out _); } var laRegen = new LegalityAnalysis(clone); if (laRegen.Valid) { poke.SendNotification(this, $"```fix\nRegenerated and legalized your {(Species)clone.Species}!```"); } } else if (!adOT && laInit.Valid) { poke.SendNotification(this, "```fix\nNo ad detected in Nickname or OT, and the Pokémon is legal. Exiting trade.```"); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.Aborted); } clone = (PK8)TradeExtensions.TrashBytes(clone, new LegalityAnalysis(clone)); var la = new LegalityAnalysis(clone); if (!la.Valid) { var report = la.Report(); Log(report); poke.SendNotification(this, "This Pokémon is not legal per PKHeX's legality checks. I was unable to fix this. Exiting trade."); poke.SendNotification(this, report); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (Hub.Config.Legality.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, $"```fix\nNow confirm the trade!```"); Log($"{(!laInit.Valid ? "Legalized" : "Fixed Nickname/OT for")} {(Species)clone.Species}!"); bool changed = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 10_000, 0_200, false, token).ConfigureAwait(false); if (changed) { Log($"{poke.Trainer.TrainerName} changed the shown Pokémon ({(Species)clone.Species})"); poke.SendNotification(this, $"```fix\nSend away the originally shown Pokémon, please.```"); changed = !await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, true, token).ConfigureAwait(false); } var pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 3_000, 1_000, token).ConfigureAwait(false); if (changed || pk2 == null || SearchUtil.HashByDetails(pk2) != SearchUtil.HashByDetails(pk)) { Log("Trading partner did not wish to send away their ad-mon."); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); pkm = clone; for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } else if (poke.Type == PokeTradeType.Clone) { // Inject the shown Pokémon. var clone = (PK8)pk.Clone(); if (Hub.Config.Discord.ReturnPK8s) { poke.SendNotification(this, clone, "Here's what you showed me!"); } var la = new LegalityAnalysis(clone); if (!la.Valid) { Log($"Clone request (from {poke.Trainer.TrainerName}) has detected an invalid Pokémon: {(Species)clone.Species}."); if (DumpSetting.Dump) { DumpPokemon(DumpSetting.DumpFolder, "hacked", clone); } var report = la.Report(); Log(report); poke.SendNotification(this, "This Pokémon is not legal per PKHeX's legality checks. I am forbidden from cloning this. Exiting trade."); poke.SendNotification(this, report); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.IllegalTrade); } if (Hub.Config.Legality.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, $"**Cloned your {(Species)clone.Species}!**\nNow press B to cancel your offer and trade me a Pokémon you don't want."); Log($"Cloned a {(Species)clone.Species}. Waiting for user to change their Pokémon..."); // Separate this out from WaitForPokemonChanged since we compare to old EC from original read. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); if (!partnerFound) { poke.SendNotification(this, "**HEY CHANGE IT NOW OR I AM LEAVING!!!**"); // They get one more chance. partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, oldEC, 15_000, 0_200, false, token).ConfigureAwait(false); } var pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 3_000, 1_000, token).ConfigureAwait(false); if (!partnerFound || pk2 == null || SearchUtil.HashByDetails(pk2) == SearchUtil.HashByDetails(pk)) { Log("Trading partner did not change their Pokémon."); await ExitTrade(Hub.Config, true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); pkm = clone; for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } await Click(A, 3_000, token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } delay_count = 0; while (!await IsInBox(token).ConfigureAwait(false)) { await Click(A, 3_000, token).ConfigureAwait(false); delay_count++; if (delay_count >= 50) { break; } if (await IsOnOverworld(Hub.Config, token).ConfigureAwait(false)) // In case we are in a Trade Evolution/PokeDex Entry and the Trade Partner quits we land on the Overworld { break; } } await Task.Delay(1_000 + Util.Rand.Next(0_700, 1_000), token).ConfigureAwait(false); await ExitTrade(Hub.Config, false, token).ConfigureAwait(false); Log("Exited Trade!"); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } // Trade was Successful! var traded = await ReadBoxPokemon(InjectBox, InjectSlot, token).ConfigureAwait(false); // Pokémon in b1s1 is same as the one they were supposed to receive (was never sent). if (poke.Type != PokeTradeType.FixOT && SearchUtil.HashByDetails(traded) == SearchUtil.HashByDetails(pkm)) { Log("User did not complete the trade."); return(PokeTradeResult.TrainerTooSlow); } else { // As long as we got rid of our inject in b1s1, assume the trade went through. Log("User completed the trade."); poke.TradeFinished(this, traded); // Only log if we completed the trade. var counts = Hub.Counts; if (poke.Type == PokeTradeType.Random) { counts.AddCompletedDistribution(); } else if (poke.Type == PokeTradeType.Clone) { counts.AddCompletedClones(); } else if (poke.Type == PokeTradeType.FixOT) { counts.AddCompletedFixOTs(); } else if (poke.Type == PokeTradeType.SpecialRequest) { counts.AddCompletedSpecialRequests(); } else if (poke.Type == PokeTradeType.TradeCord) { counts.AddCompletedTradeCords(); } else if (poke.Type == PokeTradeType.Giveaway) { counts.AddCompletedGiveaways(); } else { Hub.Counts.AddCompletedTrade(); } if (DumpSetting.Dump && !string.IsNullOrEmpty(DumpSetting.DumpFolder)) { var subfolder = poke.Type.ToString().ToLower(); DumpPokemon(DumpSetting.DumpFolder, subfolder, traded); // received if (poke.Type == PokeTradeType.Specific || poke.Type == PokeTradeType.Clone || poke.Type == PokeTradeType.FixOT || poke.Type == PokeTradeType.TradeCord || poke.Type == PokeTradeType.Giveaway) { DumpPokemon(DumpSetting.DumpFolder, "traded", pkm); // sent to partner } } } return(PokeTradeResult.Success); }
private async Task <PokeTradeResult> PerformLinkCodeTrade(SAV8SWSH sav, PokeTradeDetail <PK8> poke, CancellationToken token) { // Update Barrier Settings UpdateBarrier(poke.IsSynchronized); poke.TradeInitialize(this); Hub.Config.Stream.EndEnterCode(this); if (await CheckIfSoftBanned(token).ConfigureAwait(false)) { await Unban(token).ConfigureAwait(false); } var pkm = poke.TradeData; if (pkm.Species != 0) { await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); } if (!await IsOnOverworld(token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverStart); } if (await CheckIfSearchingForLinkTradePartner(token).ConfigureAwait(false)) { Log("Still searching, reset bot position."); await ResetTradePosition(token).ConfigureAwait(false); } Log("Opening Y-Comm Menu"); await Click(Y, 2_000, token).ConfigureAwait(false); Log("Selecting Link Trade"); await Click(A, 1_500, token).ConfigureAwait(false); Log("Selecting Link Trade Code"); await Click(DDOWN, 500, token).ConfigureAwait(false); for (int i = 0; i < 2; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } // All other languages require an extra A press at this menu. if (GameLang != LanguageID.English && GameLang != LanguageID.Spanish) { await Click(A, 1_500, token).ConfigureAwait(false); } // Loading Screen await Task.Delay(1_000, token).ConfigureAwait(false); Hub.Config.Stream.StartEnterCode(this); await Task.Delay(1_000, token).ConfigureAwait(false); var code = poke.Code; Log($"Entering Link Trade Code: {code:0000}..."); await EnterTradeCode(code, token).ConfigureAwait(false); // Wait for Barrier to trigger all bots simultaneously. WaitAtBarrierIfApplicable(token); await Click(PLUS, 1_000, token).ConfigureAwait(false); // Start a Link Trade, in case of Empty Slot/Egg/Bad Pokémon we press sometimes B to return to the Overworld and skip this Slot. // Confirming... for (int i = 0; i < 4; i++) { await Click(A, 1_000, token).ConfigureAwait(false); } Hub.Config.Stream.EndEnterCode(this); // Should be on overworld by now. if (!await IsOnOverworld(token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverPostLinkCode); } // Clear the shown data offset right as we start waiting on overworld. await Connection.WriteBytesAsync(PokeTradeBotUtil.EMPTY_SLOT, LinkTradePartnerPokemonOffset, token).ConfigureAwait(false); poke.TradeSearching(this); await Task.Delay(0_500, token).ConfigureAwait(false); // Wait until search finishes // Wait 30 Seconds for Trainer... Log("Waiting for trainer ..."); bool partnerFound; const uint ofs = LinkTradePartnerPokemonOffset; if (Hub.Config.Trade.SpinTrade) { partnerFound = await SpinUntilChangedLink(30_000, token).ConfigureAwait(false); } else { partnerFound = await ReadUntilChanged(ofs, PokeTradeBotUtil.EMPTY_EC, 30_000, 0_200, token).ConfigureAwait(false); } // Wait 15 more seconds. First 30 seconds is for spin if (!partnerFound) { Log("Still no partner found.. waiting 15 more seconds"); partnerFound = await ReadUntilChanged(ofs, PokeTradeBotUtil.EMPTY_EC, 15_000, 0_200, token).ConfigureAwait(false); } if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (!partnerFound) { await ResetTradePosition(token).ConfigureAwait(false); return(PokeTradeResult.NoTrainerFound); } // Select Pokemon // pkm already injected to b1s1 var TrainerName = await GetTradePartnerName(TradeMethod.LinkTrade, token).ConfigureAwait(false); Log($"Found Trading Partner: {TrainerName} ..."); await Task.Delay(1_500, token).ConfigureAwait(false); // necessary delay to get to the box properly if (!await IsCorrectScreen(CurrentScreen_Box, token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.RecoverOpenBox); } // Confirm Box 1 Slot 1 if (poke.Type == PokeTradeType.Specific) { for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } poke.SendNotification(this, $"Found Trading Partner: {TrainerName}. Waiting for a Pokémon..."); if (poke.Type == PokeTradeType.Dump) { return(await ProcessDumpTradeAsync(poke, token).ConfigureAwait(false)); } // Wait for User Input... var pk = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 25_000, 1_000, token).ConfigureAwait(false); if (pk == null) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } if (poke.Type == PokeTradeType.Seed) { // Immediately exit, we aren't trading anything. return(await EndSeedCheckTradeAsync(poke, pk, token).ConfigureAwait(false)); } if (poke.Type == PokeTradeType.Random) // distribution { // Allow the trade partner to do a Ledy swap. var trade = Hub.Ledy.GetLedyTrade(pk, Hub.Config.Distribute.LedySpecies); pkm = trade.Receive; poke.TradeData = pkm; if (trade.Type != LedyResponseType.Random) { poke.SendNotification(this, "Injecting your requested Pokémon."); await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); await Task.Delay(2_500, token).ConfigureAwait(false); } for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } else if (poke.Type == PokeTradeType.Clone) { // Inject the shown Pokémon. var clone = (PK8)pk.Clone(); if (Hub.Config.Discord.ReturnPK8s) { poke.SendNotification(this, clone, "Here's what you showed me!"); } var la = new LegalityAnalysis(clone); if (!la.Valid && Hub.Config.VerifyLegality) { Log($"Clone request has detected an invalid Pokémon: {(Species)clone.Species}"); if (DumpSetting.Dump) { DumpPokemon(DumpSetting.DumpFolder, "hacked", clone); } var report = la.Report(); Log(report); poke.SendNotification(this, "This Pokémon is not legal per PKHeX's legality checks. I am forbidden from cloning this. Exiting trade."); poke.SendNotification(this, report); return(PokeTradeResult.IllegalTrade); } if (Hub.Config.Legality.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, $"**Cloned your {(Species)clone.Species}!**\nNow press B to cancel your offer and trade me a Pokémon you don't want."); Log($"Cloned a {(Species)clone.Species}. Waiting for user to change their Pokémon..."); // Clear the shown data offset. await Connection.WriteBytesAsync(PokeTradeBotUtil.EMPTY_SLOT, LinkTradePartnerPokemonOffset, token).ConfigureAwait(false); // Wait for User Input... var pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 10_000, 1_000, token).ConfigureAwait(false); if (pk2 == null || SearchUtil.HashByDetails(pk2) == SearchUtil.HashByDetails(pk)) { poke.SendNotification(this, "**HEY CHANGE IT NOW OR I AM LEAVING!!!**"); // They get one more chance. // Clear the shown data offset. await Connection.WriteBytesAsync(PokeTradeBotUtil.EMPTY_SLOT, LinkTradePartnerPokemonOffset, token).ConfigureAwait(false); // Wait for User Input... pk2 = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 5_000, 1_000, token).ConfigureAwait(false); if (pk2 == null || SearchUtil.HashByDetails(pk2) == SearchUtil.HashByDetails(pk)) { Log("Trading partner did not change their Pokémon."); await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } } await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); pkm = clone; for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } await Click(A, 3_000, token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } var delay_count = 0; while (!await IsCorrectScreen(CurrentScreen_Box, token).ConfigureAwait(false)) { await Click(A, 3_000, token).ConfigureAwait(false); delay_count++; if (delay_count >= 50) { break; } if (await IsOnOverworld(token).ConfigureAwait(false)) // In case we are in a Trade Evolution/PokeDex Entry and the Trade Partner quits we land on the Overworld { break; } } await Task.Delay(1_000 + Util.Rand.Next(0_700, 1_000), token).ConfigureAwait(false); await ExitTrade(false, token).ConfigureAwait(false); Log("Exited Trade!"); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } // Trade was Successful! var traded = await ReadBoxPokemon(InjectBox, InjectSlot, token).ConfigureAwait(false); // Pokémon in b1s1 is same as the one they were supposed to receive (was never sent). if (SearchUtil.HashByDetails(traded) == SearchUtil.HashByDetails(pkm)) { Log("User did not complete the trade."); return(PokeTradeResult.TrainerTooSlow); } else { // As long as we got rid of our inject in b1s1, assume the trade went through. Log("User completed the trade."); poke.TradeFinished(this, traded); // Only log if we completed the trade. var counts = Hub.Counts; if (poke.Type == PokeTradeType.Random) { counts.AddCompletedDistribution(); } else if (poke.Type == PokeTradeType.Clone) { counts.AddCompletedClones(); } else { Hub.Counts.AddCompletedTrade(); } if (DumpSetting.Dump && !string.IsNullOrEmpty(DumpSetting.DumpFolder)) { var subfolder = poke.Type.ToString().ToLower(); DumpPokemon(DumpSetting.DumpFolder, subfolder, traded); // received if (poke.Type == PokeTradeType.Specific || poke.Type == PokeTradeType.Clone) { DumpPokemon(DumpSetting.DumpFolder, "traded", pkm); // sent to partner } } } return(PokeTradeResult.Success); }
private async Task <PokeTradeResult> PerformLinkCodeTrade(SAV8SWSH sav, PokeTradeDetail <PK8> poke, CancellationToken token) { // Update Barrier Settings UpdateBarrier(poke.IsSynchronized); var code = poke.Code; if (poke.IsRandomCode) { poke.Code = code = Hub.Config.GetRandomTradeCode(); } poke.TradeInitialize(this); var pkm = poke.TradeData; if (pkm.Species != 0) { await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); } if (!await IsCorrectScreen(CurrentScreen_Overworld, token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.Recover); } Connection.Log("Open Y-Comm Menu"); await Click(Y, 2_000, token).ConfigureAwait(false); Connection.Log("Select Link Trade"); await Click(A, 1_500, token).ConfigureAwait(false); Connection.Log("Select Link Trade Code"); await Click(DDOWN, 500, token).ConfigureAwait(false); for (int i = 0; i < 2; i++) { await Click(A, 2_000, token).ConfigureAwait(false); } // Loading Screen await Task.Delay(2_000, token).ConfigureAwait(false); Connection.Log($"Entering Link Trade Code: {code} ..."); await EnterTradeCode(code, token).ConfigureAwait(false); // Wait for Barrier to trigger all bots simultaneously. WaitAtBarrierIfApplicable(token); await Click(PLUS, 1_000, token).ConfigureAwait(false); // Start a Link Trade, in case of Empty Slot/Egg/Bad Pokemon we press sometimes B to return to the Overworld and skip this Slot. // Confirming... for (int i = 0; i < 4; i++) { await Click(A, 1_000, token).ConfigureAwait(false); } poke.TradeSearching(this); await Task.Delay(Util.Rand.Next(0_350, 0_750), token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } if (!await IsCorrectScreen(CurrentScreen_Overworld, token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.Recover); } // Clear the shown data offset. await Connection.WriteBytesAsync(PokeTradeBotUtil.EMPTY_SLOT, LinkTradePartnerPokemonOffset, token).ConfigureAwait(false); // Wait 40 Seconds for Trainer... var partnerFound = await ReadUntilChanged(LinkTradePartnerPokemonOffset, PokeTradeBotUtil.EMPTY_EC, 90_000, 0_200, token).ConfigureAwait(false); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } if (!partnerFound) { await ResetTradePosition(token).ConfigureAwait(false); return(PokeTradeResult.NoTrainerFound); } // Select Pokemon // pkm already injected to b1s1 var TrainerName = await GetTradePartnerName(TradeMethod.LinkTrade, token).ConfigureAwait(false); Connection.Log($"Found Trading Partner: {TrainerName} ..."); await Task.Delay(1_500, token).ConfigureAwait(false); // necessary delay to get to the box properly if (!await IsCorrectScreen(CurrentScreen_Box, token).ConfigureAwait(false)) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.Recover); } // Confirm Box 1 Slot 1 if (poke.Type == PokeTradeType.Specific) { for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } poke.SendNotification(this, $"Found Trading Partner: {TrainerName}. Waiting for a Pokemon ..."); // Wait for User Input... var pk = await ReadUntilPresent(LinkTradePartnerPokemonOffset, 25_000, 1_000, token).ConfigureAwait(false); if (pk == null) { await ExitTrade(true, token).ConfigureAwait(false); return(PokeTradeResult.TrainerTooSlow); } if (poke.Type == PokeTradeType.Dudu) { // Immediately exit, we aren't trading anything. return(await EndDuduTradeAsync(poke, pk, token).ConfigureAwait(false)); } if (poke.Type == PokeTradeType.Random) // distribution { // Allow the trade partner to do a Ledy swap. var trade = Hub.Ledy.GetLedyTrade(pk, Hub.Config.DistributeLedySpecies); pkm = trade.Receive; if (trade.Type != LedyResponseType.Random) { poke.SendNotification(this, "Injecting your requested Pokemon."); await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(pkm, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); await Task.Delay(2_500, token).ConfigureAwait(false); } for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } else if (poke.Type == PokeTradeType.Clone) { // Inject the shown Pokemon. var clone = (PK8)pk.Clone(); if (Hub.Config.ResetHOMETracker) { clone.Tracker = 0; } poke.SendNotification(this, "Cloned. Change what you're offering!"); await Click(A, 0_800, token).ConfigureAwait(false); await SetBoxPokemon(clone, InjectBox, InjectSlot, token, sav).ConfigureAwait(false); await Task.Delay(2_500, token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 0_500, token).ConfigureAwait(false); } } await Click(A, 3_000, token).ConfigureAwait(false); for (int i = 0; i < 5; i++) { await Click(A, 1_500, token).ConfigureAwait(false); } var delay_count = 0; while (!await IsCorrectScreen(CurrentScreen_Box, token).ConfigureAwait(false)) { await Click(A, 3_000, token).ConfigureAwait(false); delay_count++; if (delay_count >= 50) { break; } if (await IsCorrectScreen(CurrentScreen_Overworld, token).ConfigureAwait(false)) // In case we are in a Trade Evolution/PokeDex Entry and the Trade Partner quits we land on the Overworld { break; } } await Task.Delay(1_000 + Util.Rand.Next(0_700, 1_000), token).ConfigureAwait(false); await ExitTrade(false, token).ConfigureAwait(false); Connection.Log("Exited Trade!"); if (token.IsCancellationRequested) { return(PokeTradeResult.Aborted); } // Trade was Successful! var traded = await ReadBoxPokemon(InjectBox, InjectSlot, token).ConfigureAwait(false); if (SearchUtil.HashByDetails(traded) == SearchUtil.HashByDetails(pk)) { Connection.Log("User traded the initially shown file."); } else if (SearchUtil.HashByDetails(traded) != SearchUtil.HashByDetails(pkm)) { Connection.Log("User did not complete the trade."); } else { Connection.Log("Recipient changed the traded Pokémon after initially showing another."); } poke.TradeFinished(this, traded); Connection.Log("Trade complete!"); var counts = Hub.Counts; if (poke.Type == PokeTradeType.Random) { counts.AddCompletedDistribution(); } else if (poke.Type == PokeTradeType.Clone) { counts.AddCompletedClones(); } else { Hub.Counts.AddCompletedTrade(); } if (DumpSetting.Dump && !string.IsNullOrEmpty(DumpSetting.DumpFolder)) { DumpPokemon(DumpSetting.DumpFolder, poke.Type.ToString().ToLower(), traded); } return(PokeTradeResult.Success); }