Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* BBQ Failure Snapshots
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <sstream>
#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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* BBQ Failure Snapshots
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonSV_BBQSnapshot_H
#define PokemonAutomation_PokemonSV_BBQSnapshot_H

#include <string>
#include <string_view>
#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 <timestamp>-<quest>-<reason>.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* BBQ Snapshot Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <vector>
#include <utility>
#include <QFile>
#include <QTemporaryDir>
#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<std::string> 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{

Check warning on line 66 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::Test_BBQSnapshot::run( Logger & , CancellableScope &) const: function_lines=34 (advisory above 25, limit 50)
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_BBQSnapshot>(test_case);
}
}

} // namespace PokemonSV
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Pokemon SV BBQ Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=829 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -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"
Expand Down Expand Up @@ -818,6 +819,7 @@
add_tests_BBQFarmerRounds(database);
add_tests_TeraEncounterAction(database);
add_tests_BBQDiagnosticLogs(database);
add_tests_BBQSnapshot(database);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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);
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Blueberry Quests

Check failure on line 1 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

<file>: file_lines=1467, limit=1000, allowed 1463
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -443,7 +444,7 @@
return plan.to_do;
}

bool process_and_do_quest(

Check failure on line 447 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::process_and_do_quest( ProgramEnvironment & env , ConsoleHandle & console , ProControllerContext & context , BBQOption & BBQ_OPTIONS , BBQuests current_quest , uint8_t & eggs_hatched): function_lines=138, limit=50, allowed 135
ProgramEnvironment& env,
ConsoleHandle& console, ProControllerContext& context,
BBQOption& BBQ_OPTIONS,
Expand Down Expand Up @@ -570,7 +571,10 @@
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;
Expand Down
4 changes: 4 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading