Skip to content

Commit

Permalink
12.27.24
Browse files Browse the repository at this point in the history
- Fix ReturnPKMs issue (hopefully)
- Add isMysteryMon bool to several functions
- Fix records from all coming from SWSH to their correct games.
- Update dependencies
  • Loading branch information
bdawg1989 committed Dec 27, 2024
1 parent e1813e6 commit f18ee50
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 93 deletions.
2 changes: 1 addition & 1 deletion SysBot.Base/SysBot.Base.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="LibUsbDotNet" Version="2.2.29" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
48 changes: 21 additions & 27 deletions SysBot.Base/Util/RecordUtil.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
using NLog;
using NLog.Config;
using NLog.Targets;
using System.IO;
using System;

namespace SysBot.Base;

public static class RecordUtil<T>
namespace SysBot.Base.Util
{
// ReSharper disable once StaticMemberInGenericType
private static readonly Logger Logger = new LogFactory { Configuration = GetConfig() }.GetCurrentClassLogger();

public static void Record(string message) => Logger.Log(LogLevel.Info, message);

private static LoggingConfiguration GetConfig()
public static class RecordUtil<T>
{
var config = new LoggingConfiguration();
const string dir = "records";
Directory.CreateDirectory(dir);
var name = typeof(T).Name;
var record = new FileTarget("record")
private static readonly string LogPath;

static RecordUtil()
{
FileName = Path.Combine(dir, $"{name}.txt"),
ConcurrentWrites = true,
const string dir = "records";
Directory.CreateDirectory(dir);
LogPath = Path.Combine(dir, $"{typeof(T).Name}.txt");
}

ArchiveEvery = FileArchivePeriod.None,
ArchiveNumbering = ArchiveNumberingMode.Sequence,
ArchiveFileName = Path.Combine(dir, $"{name}.{{#}}.txt"),
ArchiveAboveSize = 104857600, // 100MB (never)
MaxArchiveFiles = 14,
};
config.AddRule(LogLevel.Debug, LogLevel.Fatal, record);
return config;
public static void Record(string message)
{
try
{
File.AppendAllText(LogPath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\t{message}{Environment.NewLine}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to write to log: {ex.Message}");
}
}
}
}
36 changes: 4 additions & 32 deletions SysBot.Pokemon.Discord/Helpers/DiscordTradeNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using PKHeX.Core;
using PKHeX.Core.AutoMod;
using PKHeX.Drawing.PokeSprite;
using SysBot.Pokemon.Helpers;
using System;
using System.Collections.Generic;
using System.Drawing;
Expand Down Expand Up @@ -113,37 +112,10 @@ public void TradeFinished(PokeRoutineExecutor<T> routine, PokeTradeDetail<T> inf
{
OnFinish?.Invoke(routine);
var tradedToUser = Data.Species;
string tradedSpeciesName = result.Species != 0 ? Enum.GetName(typeof(Species), result.Species) ?? "Unknown" : "";

if (info.TotalBatchTrades > 1)
{
// Send the embed only on the first trade of the batch
if (info.BatchTradeNumber == 1)
{
var message = tradedToUser != 0 ?
(info.IsMysteryEgg ? "Enjoy your **Mystery Eggs**!" :
info.IsMysteryMon ? "Enjoy your **Mystery Pokemon**!" :
$"Enjoy your **{(Species)tradedToUser}** and other Pokémon!") :
"Batch trades started!";
EmbedHelper.SendTradeFinishedEmbedAsync(Trader, message, Data, info.IsMysteryMon, info.IsMysteryEgg).ConfigureAwait(false);
}

// send each Pokemon file as they come in
if (Hub.Config.Discord.ReturnPKMs && result.Species != 0)
Trader.SendPKMAsync(result, $"Here's the {tradedSpeciesName} you traded me!").ConfigureAwait(false);
}
else
{
// Original single trade logic
var message = tradedToUser != 0 ?
(info.IsMysteryEgg ? "Enjoy your **Mystery Egg**!" :
info.IsMysteryMon ? "Enjoy your **Mystery Pokemon**!" :
$"Enjoy your **{(Species)tradedToUser}**!") :
"Trade finished!";
EmbedHelper.SendTradeFinishedEmbedAsync(Trader, message, Data, info.IsMysteryMon, info.IsMysteryEgg).ConfigureAwait(false);
if (result.Species != 0 && Hub.Config.Discord.ReturnPKMs)
Trader.SendPKMAsync(result, $"Here's the {tradedSpeciesName} you traded me!").ConfigureAwait(false);
}
var message = tradedToUser != 0 ? $"Trade finished. Enjoy!" : "Trade finished!";
Trader.SendMessageAsync(message).ConfigureAwait(false);
if (result is not null && Hub.Config.Discord.ReturnPKMs)
Trader.SendPKMAsync(result, "Here's what you traded me!").ConfigureAwait(false);
}

public void SendNotification(PokeRoutineExecutor<T> routine, PokeTradeDetail<T> info, string message)
Expand Down
8 changes: 4 additions & 4 deletions SysBot.Pokemon.Discord/SysBot.Pokemon.Discord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

<ItemGroup>
<PackageReference Include="AnimatedGif" Version="1.0.5" />
<PackageReference Include="Discord.Net" Version="3.15.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
<PackageReference Include="Discord.Net" Version="3.17.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<ProjectReference Include="..\SysBot.Base\SysBot.Base.csproj" />
<ProjectReference Include="..\SysBot.Pokemon\SysBot.Pokemon.csproj" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion SysBot.Pokemon.WinForms/SysBot.Pokemon.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
<PackageReference Include="System.Resources.Extensions" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion SysBot.Pokemon.YouTube/SysBot.Pokemon.YouTube.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Google.Apis" Version="1.68.0" />
<PackageReference Include="Google.Apis.Auth" Version="1.68.0" />
<PackageReference Include="Google.Apis.Core" Version="1.68.0" />
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.68.0.3421" />
<PackageReference Include="Google.Apis.YouTube.v3" Version="1.68.0.3624" />
<PackageReference Include="Google.Apis.YouTubePartner.v1" Version="1.35.1.1334" />
</ItemGroup>

Expand Down
10 changes: 4 additions & 6 deletions SysBot.Pokemon/BDSP/BotTrade/PokeTradeBotBS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PKHeX.Core;
using PKHeX.Core.Searching;
using SysBot.Base;
using SysBot.Base.Util;
using SysBot.Pokemon.Helpers;
using System;
using System.IO;
Expand Down Expand Up @@ -876,7 +877,7 @@ private async Task<PokeTradeResult> PerformLinkCodeTrade(SAV8BS sav, PokeTradeDe

var tradePartner = await GetTradePartnerInfo(token).ConfigureAwait(false);
var trainerNID = GetFakeNID(tradePartner.TrainerName, tradePartner.TrainerID);
RecordUtil<PokeTradeBotSWSH>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
RecordUtil<PokeTradeBotBS>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");

var tradeCodeStorage = new TradeCodeStorage();
var existingTradeDetails = tradeCodeStorage.GetTradeDetails(poke.Trainer.ID);
Expand Down Expand Up @@ -1175,12 +1176,9 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8BS sav, PokeTradeDetai
poke.SendNotification(this, "All batch trades completed! Thank you for trading!");

// Then finish each trade with the corresponding received Pokemon
if (Hub.Config.Discord.ReturnPKMs)
foreach (var pokemon in allReceived)
{
foreach (var pokemon in allReceived)
{
poke.TradeFinished(this, pokemon); // This sends each Pokemon back to the user
}
poke.TradeFinished(this, pokemon); // This sends each Pokemon back to the user
}

// Finally do cleanup
Expand Down
2 changes: 1 addition & 1 deletion SysBot.Pokemon/Helpers/TradeBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public static class TradeBot

public const string ConfigPath = "config.json";

public const string Version = "v4.9";
public const string Version = "v5.0";
}
}
13 changes: 6 additions & 7 deletions SysBot.Pokemon/LA/BotTrade/PokeTradeBotLA.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PKHeX.Core;
using PKHeX.Core.Searching;
using SysBot.Base;
using SysBot.Base.Util;
using SysBot.Pokemon.Helpers;
using System;
using System.IO;
Expand Down Expand Up @@ -443,7 +444,7 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8LA sav, PokeTradeDetai
var tradePartner = await GetTradePartnerInfo(token).ConfigureAwait(false);
var trainerNID = await GetTradePartnerNID(TradePartnerNIDOffset, token).ConfigureAwait(false);
tradePartner.NID = trainerNID;
RecordUtil<PokeTradeBotSWSH>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
RecordUtil<PokeTradeBotLA>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");

var tradeCodeStorage = new TradeCodeStorage();
var existingTradeDetails = tradeCodeStorage.GetTradeDetails(poke.Trainer.ID);
Expand Down Expand Up @@ -561,12 +562,10 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8LA sav, PokeTradeDetai
// First send notification that trades are complete
poke.SendNotification(this, "All batch trades completed! Thank you for trading!");

if (Hub.Config.Discord.ReturnPKMs)
// Then finish each trade with the corresponding received Pokemon
foreach (var pokemon in allReceived)
{
foreach (var pokemon in allReceived)
{
poke.TradeFinished(this, pokemon); // send users traded pokemon back to them as files
}
poke.TradeFinished(this, pokemon); // This sends each Pokemon back to the user
}

// cleanup
Expand Down Expand Up @@ -675,7 +674,7 @@ private async Task<PokeTradeResult> PerformLinkCodeTrade(SAV8LA sav, PokeTradeDe
var tradePartner = await GetTradePartnerInfo(token).ConfigureAwait(false);
var trainerNID = await GetTradePartnerNID(TradePartnerNIDOffset, token).ConfigureAwait(false);
tradePartner.NID = trainerNID;
RecordUtil<PokeTradeBotSWSH>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
RecordUtil<PokeTradeBotLA>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
Log($"Found Link Trade partner: {tradePartner.TrainerName}-{tradePartner.TID7} (ID: {trainerNID})");
poke.SendNotification(this, $"Found Link Trade partner: {tradePartner.TrainerName}. **TID**: {tradePartner.TID7} **SID**: {tradePartner.SID7}. Waiting for a Pokémon...");

Expand Down
12 changes: 5 additions & 7 deletions SysBot.Pokemon/SV/BotTrade/PokeTradeBotSV.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PKHeX.Core;
using PKHeX.Core.Searching;
using SysBot.Base;
using SysBot.Base.Util;
using SysBot.Pokemon.Helpers;
using System;
using System.IO;
Expand Down Expand Up @@ -885,7 +886,7 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV9SV sav, PokeTradeDetai
var tradePartnerFullInfo = await GetTradePartnerFullInfo(token).ConfigureAwait(false);
var tradePartner = new TradePartnerSV(tradePartnerFullInfo);
var trainerNID = await GetTradePartnerNID(TradePartnerNIDOffset, token).ConfigureAwait(false);
RecordUtil<PokeTradeBotSWSH>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
RecordUtil<PokeTradeBotSV>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");

var tradeCodeStorage = new TradeCodeStorage();
var existingTradeDetails = tradeCodeStorage.GetTradeDetails(poke.Trainer.ID);
Expand Down Expand Up @@ -1009,12 +1010,9 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV9SV sav, PokeTradeDetai
poke.SendNotification(this, "All batch trades completed! Thank you for trading!");

// Then finish each trade with the corresponding received Pokemon
if (Hub.Config.Discord.ReturnPKMs)
foreach (var pokemon in allReceived)
{
foreach (var pokemon in allReceived)
{
poke.TradeFinished(this, pokemon);
}
poke.TradeFinished(this, pokemon); // This sends each Pokemon back to the user
}

// Finally do cleanup
Expand Down Expand Up @@ -1194,7 +1192,7 @@ private async Task<PokeTradeResult> PerformNonBatchTrade(SAV9SV sav, PokeTradeDe
var tradePartnerFullInfo = await GetTradePartnerFullInfo(token).ConfigureAwait(false);
var tradePartner = new TradePartnerSV(tradePartnerFullInfo);
var trainerNID = await GetTradePartnerNID(TradePartnerNIDOffset, token).ConfigureAwait(false);
RecordUtil<PokeTradeBotSWSH>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
RecordUtil<PokeTradeBotSV>.Record($"Initiating\t{trainerNID:X16}\t{tradePartner.TrainerName}\t{poke.Trainer.TrainerName}\t{poke.Trainer.ID}\t{poke.ID}\t{toSend.EncryptionConstant:X8}");
Log($"Found Link Trade partner: {tradePartner.TrainerName}-{tradePartner.TID7} (ID: {trainerNID})");
poke.SendNotification(this, $"Found Link Trade partner: {tradePartner.TrainerName}. **TID**: {tradePartner.TID7} **SID**: {tradePartner.SID7}. Waiting for a Pokémon...");

Expand Down
8 changes: 3 additions & 5 deletions SysBot.Pokemon/SWSH/BotTrade/PokeTradeBotSWSH.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PKHeX.Core;
using PKHeX.Core.Searching;
using SysBot.Base;
using SysBot.Base.Util;
using SysBot.Pokemon.Helpers;
using System;
using System.IO;
Expand Down Expand Up @@ -480,12 +481,9 @@ private async Task<PokeTradeResult> PerformBatchTrade(SAV8SWSH sav, PokeTradeDet
poke.SendNotification(this, "All batch trades completed! Thank you for trading!");

// Then finish each trade with the corresponding received Pokemon
if (Hub.Config.Discord.ReturnPKMs)
foreach (var pokemon in allReceived)
{
foreach (var pokemon in allReceived)
{
poke.TradeFinished(this, pokemon);
}
poke.TradeFinished(this, pokemon); // This sends each Pokemon back to the user
}

// Finally do cleanup
Expand Down
2 changes: 1 addition & 1 deletion SysBot.Pokemon/SysBot.Pokemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ItemGroup>
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<ProjectReference Include="..\SysBot.Base\SysBot.Base.csproj" />
</ItemGroup>

Expand Down

0 comments on commit f18ee50

Please sign in to comment.