diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp new file mode 100644 index 0000000000..8821055279 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp @@ -0,0 +1,56 @@ +/* BBQ Failure Snapshots + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "PokemonSV_BBQSnapshot.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +std::string bbq_snapshot_directory(){ + return DEBUG_PATH() + "BBQ/"; +} + +std::string save_bbq_snapshot( + Logger& logger, const ImageViewRGB32& frame, + const std::string& directory, BBQuests quest, std::string_view reason +){ + const std::string& slug = BBQuests_enum_to_string(quest); + std::ostringstream path; + path << directory << now_to_filestring() << '-' + << (slug.empty() ? "unknown" : slug) << '-' << reason << ".png"; + const std::string filename = path.str(); + + const bool saved = frame.save(filename); + std::ostringstream out; + out << "BBQ event=snapshot saved=" << saved << " path=" << filename; + logger.log(out.str()); + return saved ? filename : std::string(); +} + + +void record_bbq_battle_entry_timeout(ConsoleHandle& console, BBQuests quest){ + BBQLogger(console.logger()).battle_entry_timeout(quest); + save_bbq_snapshot(console.logger(), console.video().snapshot(), bbq_snapshot_directory(), quest, "battle-entry-timeout"); +} + +void record_bbq_attempt_result( + ConsoleHandle& console, BBQAttemptLog& attempt, BBQuests quest, BBQAttemptOutcome outcome +){ + if (outcome == BBQAttemptOutcome::StillOnBoard){ + save_bbq_snapshot(console.logger(), console.video().snapshot(), bbq_snapshot_directory(), quest, "verification-failed"); + } + attempt.result(outcome == BBQAttemptOutcome::Completed); +} + +} // namespace PokemonSV +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h new file mode 100644 index 0000000000..ff2e5af219 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h @@ -0,0 +1,54 @@ +/* BBQ Failure Snapshots + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQSnapshot_H +#define PokemonAutomation_PokemonSV_BBQSnapshot_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "PokemonSV_BBQLogger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Where BBQ failure screenshots are saved: DEBUG_PATH()/BBQ/. +std::string bbq_snapshot_directory(); + +// Save a screenshot of a failed quest attempt into `directory` (created if +// missing) as --.png, and log the outcome as a +// "BBQ event=snapshot" line. Unlike dump_image(), this stays on the local +// machine and sends nothing to telemetry. +// Returns the path written, or an empty string if the image was not saved. +std::string save_bbq_snapshot( + Logger& logger, const ImageViewRGB32& frame, + const std::string& directory, BBQuests quest, std::string_view reason +); + + +// Board verification decides whether the attempt completed; capturing evidence does not. +enum class BBQAttemptOutcome{ + Completed, + StillOnBoard, +}; + +// Log a battle-entry timeout and save its current frame locally; no controller input. +void record_bbq_battle_entry_timeout(ConsoleHandle& console, BBQuests quest); + +// Save the verification-failure frame when needed, then log the attempt result. +// The caller retains all retry/counting decisions; this performs no controller input. +void record_bbq_attempt_result( + ConsoleHandle& console, BBQAttemptLog& attempt, BBQuests quest, BBQAttemptOutcome outcome +); + +} // namespace PokemonSV +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.cpp new file mode 100644 index 0000000000..b3fd3f6876 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.cpp @@ -0,0 +1,131 @@ +/* BBQ Snapshot Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonSV_BBQSnapshot.h" +#include "PokemonSV_BBQSnapshot_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ +namespace{ + +// Capture evidence through the real logging interface without controller input. +class SnapshotTestLogger : public Logger{ +public: + virtual void log(const std::string& message, Color) override{ + messages.push_back(message); + } + std::vector messages; +}; + +// State of the screenshot destination before the attempted write. +enum class SnapshotDestination{ + MissingDirectory, + BlockedByFile, +}; + +// A quest failure and the destination state to exercise. +struct SnapshotInput{ + std::string name; + SnapshotDestination destination; + BBQuests quest; + std::string reason; +}; + +// The path contract and outcome prefix expected in the one emitted log line. +struct SnapshotExpectation{ + bool saved; + std::string suffix; + std::string log_prefix; +}; + +// Each row keeps its input and expectation together. +struct SnapshotCase{ + SnapshotInput input; + SnapshotExpectation expectation; +}; + +// Check real local image output in an automatically removed temporary directory. +class Test_BBQSnapshot : public UnitTest{ +public: + explicit Test_BBQSnapshot(SnapshotCase test_case) + : UnitTest("PokemonSV::BBQSnapshot - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + QTemporaryDir temp; + if (!temp.isValid()){ + return "Could not create the snapshot test directory."; + } + const std::string base = temp.path().toStdString() + "/BBQ"; + if (m_case.input.destination == SnapshotDestination::BlockedByFile){ + QFile block(QString::fromStdString(base)); + if (!block.open(QIODevice::WriteOnly)){ + return "Could not create the blocking-file fixture."; + } + block.write("not a folder"); + } + const std::string directory = base + "/"; + ImageRGB32 frame(4, 4); + frame.fill(0xff00ff00); + SnapshotTestLogger logger; + const std::string path = save_bbq_snapshot(logger, frame, directory, m_case.input.quest, m_case.input.reason); + if (path.empty() == m_case.expectation.saved){ + return "Expected saved=" + std::to_string(m_case.expectation.saved) + "; read path [" + path + "]."; + } + if (logger.messages.size() != 1){ + return "Expected exactly one snapshot log; read " + std::to_string(logger.messages.size()) + "."; + } + const std::string prefix = m_case.expectation.log_prefix + directory; + const std::string& message = logger.messages[0]; + if (!message.starts_with(prefix) || !message.ends_with(m_case.expectation.suffix)){ + return "Expected snapshot log prefix [" + prefix + "] and suffix [" + m_case.expectation.suffix + "]; read [" + message + "]."; + } + if (m_case.expectation.saved && (!message.ends_with(path) || !Filesystem::is_regular_file(Filesystem::Path(path)))){ + return "Expected the logged file to exist at the returned path [" + path + "]."; + } + return true; + } + +private: + const SnapshotCase m_case; +}; + +// Both original screenshot cases: a new folder and a destination blocked by a file. +const SnapshotCase SNAPSHOT_CASES[] = { + { + .input = {.name = "saves into a new folder", .destination = SnapshotDestination::MissingDirectory, + .quest = BBQuests::make_tm, .reason = "verification-failed"}, + .expectation = {.saved = true, .suffix = "-make-tm-verification-failed.png", + .log_prefix = "BBQ event=snapshot saved=1 path="}, + }, + { + .input = {.name = "reports a failed save", .destination = SnapshotDestination::BlockedByFile, + .quest = BBQuests::make_tm, .reason = "verification-failed"}, + .expectation = {.saved = false, .suffix = "-make-tm-verification-failed.png", + .log_prefix = "BBQ event=snapshot saved=0 path="}, + }, +}; + +} // namespace + +void add_tests_BBQSnapshot(UnitTestDatabase& database){ + for (const SnapshotCase& test_case : SNAPSHOT_CASES){ + database.add(test_case); + } +} + +} // namespace PokemonSV +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.h new file mode 100644 index 0000000000..475244dd6c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.h @@ -0,0 +1,22 @@ +/* BBQ Snapshot Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQSnapshot_Tests_H +#define PokemonAutomation_PokemonSV_BBQSnapshot_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + +// Registers local screenshot-write and failure-reporting cases without a console. +void add_tests_BBQSnapshot(UnitTestDatabase& database); + +} // namespace PokemonSV +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 2230619401..f4c438c685 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -12,6 +12,7 @@ #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" #include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" #include "PokemonSV_BBQLogger_Tests.h" +#include "PokemonSV_BBQSnapshot_Tests.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQSoloFarmer.h" #include "PokemonSV_BBQTests.h" @@ -818,6 +819,7 @@ void add_tests_BBQ(UnitTestDatabase& database){ add_tests_BBQFarmerRounds(database); add_tests_TeraEncounterAction(database); add_tests_BBQDiagnosticLogs(database); + add_tests_BBQSnapshot(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp index a77219fc37..de89e53043 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp @@ -25,7 +25,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" #include "VideoGames/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" -#include "PokemonSV_BBQLogger.h" +#include "PokemonSV_BBQSnapshot.h" #include "PokemonSV_BlueberryCatchPhoto.h" namespace PokemonAutomation{ @@ -916,7 +916,7 @@ void quest_catch( { battle_menu } ); if (ret2 != 0){ - BBQLogger(console.logger()).battle_entry_timeout(current_quest); + record_bbq_battle_entry_timeout(console, current_quest); } }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index afe39c8692..b4bc376dea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -44,6 +44,7 @@ #include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" #include "PokemonSV_BBQLogger.h" +#include "PokemonSV_BBQSnapshot.h" #include "PokemonSV_BlueberryQuests.h" @@ -570,7 +571,10 @@ bool process_and_do_quest( quest_completed = true; } - attempt_log.result(/*completed=*/quest_completed); + record_bbq_attempt_result( + console, attempt_log, current_quest, + quest_completed ? BBQAttemptOutcome::Completed : BBQAttemptOutcome::StillOnBoard + ); if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ BBQLogger(console.logger()).retry_exhausted(current_quest); break; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 011038f97e..43e42e1905 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2645,6 +2645,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp