Skip to content
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* BBQ Points Ledger
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "PokemonSV_BBQPointsLedger.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


void BBQPointsLedger::record(int read, WallClock when){
if (read < 0){
return;
}
if (starting_balance < 0){
starting_balance = read;
first_read = when;
}else if (read > balance){
earned += read - balance;
}else{
spent += balance - read;
}
balance = read;
last_read = when;
}

int64_t BBQPointsLedger::net_per_hour() const{
const int64_t span_ms = std::chrono::duration_cast<std::chrono::milliseconds>(last_read - first_read).count();
if (starting_balance < 0 || span_ms <= 0){
return 0;
}
return net() * 3600000 / span_ms;
}


}

Check failure on line 39 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonSV
}

Check failure on line 40 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace NintendoSwitch
}

Check failure on line 41 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* BBQ Points Ledger
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonSV_BBQPointsLedger_H
#define PokemonAutomation_PokemonSV_BBQPointsLedger_H

#include <stdint.h>
#include "Common/Cpp/Time.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSV{


// BP accounting from successive balance reads of the quest panel.
// A rise between two reads is income (quest rewards) and a drop is an
// expense (rerolls), so income and expense are only split correctly when
// they don't fall between the same two reads. The net is always exact.
struct BBQPointsLedger{

Check failure on line 22 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::BBQPointsLedger: public_data=6, limit=0, allowed 0; make data private, or split it into a plain struct without methods
int starting_balance = -1; // -1 until the first successful read.
int balance = -1;
WallClock first_read;
WallClock last_read;
uint64_t earned = 0;
uint64_t spent = 0;

// Record a balance read at "when". Negative reads (OCR failures) are ignored.
void record(int read, WallClock when);

int64_t net() const{
return (int64_t)earned - (int64_t)spent;
}

// Net BP per hour between the first and the latest read. 0 until two reads
// are at least a millisecond apart.
int64_t net_per_hour() const;
};


}

Check failure on line 43 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonSV
}

Check failure on line 44 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace NintendoSwitch
}

Check failure on line 45 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <set>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/PrettyPrint.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/ProgramStats/StatsTracking.h"
#include "CommonTools/StartupChecks/StartProgramChecks.h"
Expand All @@ -21,6 +22,7 @@
#include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h"
//#include "VideoGames/PokemonSV/PokemonSV_Settings.h"
#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h"
#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h"
#include "PokemonSV_BBQSoloFarmer.h"

namespace PokemonAutomation{
Expand All @@ -44,19 +46,50 @@
}
)
{}
struct BBQSoloFarmer_Descriptor::Stats : public StatsTracker{

Check failure on line 49 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::Stats: public_data=7, limit=0, allowed 0; make data private, or split it into a plain struct without methods
Stats()
: questsCompleted(m_stats["Quests Completed"])
, saves(m_stats["Saves"])
, errors(m_stats["Errors"])
, bp_earned(m_stats["BP Earned"])
, bp_spent(m_stats["BP Spent"])
{
m_display_order.emplace_back("Quests Completed");
m_display_order.emplace_back("Saves");
m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO);
m_display_order.emplace_back("BP Earned");
m_display_order.emplace_back("BP Spent");
}

void update_BP(const BBQPointsLedger& ledger){
bp_earned.store(ledger.earned, std::memory_order_relaxed);
bp_spent.store(ledger.spent, std::memory_order_relaxed);
bp_per_hour.store(ledger.net_per_hour(), std::memory_order_relaxed);
has_bp_per_hour.store(true, std::memory_order_relaxed);
}

// Net and BP/hour are derived, so they are shown but not saved. Past runs
// have no BP/hour since their time span isn't saved.
virtual std::string to_str(PrintMode mode) const override{
std::string str = StatsTracker::to_str(mode);
if (mode == SAVE_TO_STATS_FILE){
return str;
}
const int64_t net = (int64_t)bp_earned.load(std::memory_order_relaxed) - (int64_t)bp_spent.load(std::memory_order_relaxed);
str += " - Net BP: " + tostr_u_commas(net);
if (has_bp_per_hour.load(std::memory_order_relaxed)){
str += " - BP/hour: " + tostr_u_commas(bp_per_hour.load(std::memory_order_relaxed));
}
return str;
}

std::atomic<uint64_t>& questsCompleted;
std::atomic<uint64_t>& saves;
std::atomic<uint64_t>& errors;
std::atomic<uint64_t>& bp_earned;
std::atomic<uint64_t>& bp_spent;
std::atomic<int64_t> bp_per_hour{0};
std::atomic<bool> has_bp_per_hour{false};
};
std::unique_ptr<StatsTracker> BBQSoloFarmer_Descriptor::make_stats() const{
return std::unique_ptr<StatsTracker>(new Stats());
Expand Down Expand Up @@ -232,7 +265,7 @@

namespace{

class BBQSoloFarmerGameActions : public BBQGameActions{

Check warning on line 268 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::BBQSoloFarmerGameActions: public_methods=7 (advisory above 5, limit 7)
public:
BBQSoloFarmerGameActions(
SingleSwitchProgramEnvironment& env, ProControllerContext& context,
Expand All @@ -245,24 +278,32 @@
{}

virtual std::vector<BBQuests> read_quests() override{
return PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options);
int BP = -1;
std::vector<BBQuests> quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options, &BP);
record_BP(BP);
return quests;
}
virtual std::vector<BBQuests> process_quest_list(
const std::vector<BBQuests>& quests, uint8_t eggs_hatched
) override{
return PokemonSV::process_quest_list(m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched);
std::vector<BBQuests> to_do = PokemonSV::process_quest_list(
m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched, &m_bp_ledger
);
update_BP_stats();
return to_do;
}
virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{
return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched);
}
virtual void reroll_quest(BBQuests quest) override{
std::vector<BBQuests> quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options);
std::vector<BBQuests> quests = read_quests();
auto iter = std::find(quests.begin(), quests.end(), quest);
if (iter == quests.end()){
m_env.log("Failed quest is no longer on the panel. Not rerolling.");
return;
}
reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin());
reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin(), &m_bp_ledger);
update_BP_stats();
}
virtual void quest_completed() override{
m_stats.questsCompleted++;
Expand Down Expand Up @@ -290,17 +331,33 @@
send_program_status_notification(m_env, m_status_update);
}

//BP is read from the quest panel screenshots the program already takes.
void record_BP(int BP){
m_bp_ledger.record(BP, current_time());
update_BP_stats();
}

private:
void update_BP_stats(){
m_env.log(
"BP: " + std::to_string(m_bp_ledger.balance) + " (start " + std::to_string(m_bp_ledger.starting_balance) +
", earned " + std::to_string(m_bp_ledger.earned) + ", spent " + std::to_string(m_bp_ledger.spent) + ")"
);
m_stats.update_BP(m_bp_ledger);
m_env.update_stats();
}

SingleSwitchProgramEnvironment& m_env;
ProControllerContext& m_context;
BBQOption& m_options;
BBQSoloFarmer_Descriptor::Stats& m_stats;
EventNotificationOption& m_status_update;
BBQPointsLedger m_bp_ledger;
};

}

void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){

Check warning on line 360 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::BBQSoloFarmer::program( SingleSwitchProgramEnvironment & env , ProControllerContext & context): function_lines=50 (advisory above 25, limit 50)
StartProgramChecks::check_performance_class_wired_or_wireless(context);
assert_16_9_720p_min(env.logger(), env.console);

Expand Down Expand Up @@ -330,6 +387,9 @@

BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE);
run_bbq_rounds(env.logger(), BBQ_OPTIONS, game);

//No quest panel is read after the last batch, so read the balance once more.
game.record_BP(read_BP(env.program_info(), env.console, context));
env.update_stats();

if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Pokemon SV BBQ Tests

Check failure 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

<file>: file_lines=1137, limit=1000, allowed 1032
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -13,6 +13,7 @@
#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h"
#include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h"
#include "PokemonSV_BlueberryQuests.h"
#include "PokemonSV_BBQPointsLedger.h"
#include "PokemonSV_BBQSoloFarmer.h"
#include "PokemonSV_BBQTests.h"

Expand Down Expand Up @@ -1008,6 +1009,109 @@



namespace{

// Balance reads in order, each at a minute offset from the first, and the
// ledger they must produce. A negative read is an OCR failure.
struct BBQPointsLedgerScenario{
struct Read{
int balance;
int minute;
};
std::string name;
std::vector<Read> reads = {};
int expected_start = -1;
uint64_t expected_earned = 0;
uint64_t expected_spent = 0;
int64_t expected_net = 0;
int64_t expected_per_hour = 0;
};

class Test_BBQPointsLedger : public UnitTest{
public:
Test_BBQPointsLedger(BBQPointsLedgerScenario scenario)
: UnitTest("PokemonSV::BBQPointsLedger - " + scenario.name)
, m_scenario(std::move(scenario))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const WallClock start = current_time();
BBQPointsLedger ledger;
for (const BBQPointsLedgerScenario::Read& read : m_scenario.reads){
ledger.record(read.balance, start + std::chrono::minutes(read.minute));
}
const std::pair<const char*, std::pair<int64_t, int64_t>> checks[] = {
{"starting balance", {m_scenario.expected_start, ledger.starting_balance}},
{"earned", {(int64_t)m_scenario.expected_earned, (int64_t)ledger.earned}},
{"spent", {(int64_t)m_scenario.expected_spent, (int64_t)ledger.spent}},
{"net", {m_scenario.expected_net, ledger.net()}},
{"net per hour", {m_scenario.expected_per_hour, ledger.net_per_hour()}},
};
for (const auto& [field, values] : checks){
if (values.first != values.second){
return std::string("Expected ") + field + " " + std::to_string(values.first)
+ ", got " + std::to_string(values.second);
}
}
return true;
}

private:
const BBQPointsLedgerScenario m_scenario;
};

}

Check failure on line 1063 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace

static void add_tests_BBQPointsLedger(UnitTestDatabase& database){

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests_BBQPointsLedger( UnitTestDatabase & database): function_lines=47 (advisory above 25, limit 50)
const BBQPointsLedgerScenario scenarios[] = {
{
.name = "no reads",
},
{
.name = "one read sets the start",
.reads = {{6750, 0}},
.expected_start = 6750,
},
{
// A quest reward, then a reroll.
.name = "rises are earned and drops are spent",
.reads = {{6750, 0}, {6780, 10}, {6730, 20}},
.expected_start = 6750,
.expected_earned = 30,
.expected_spent = 50,
.expected_net = -20,
.expected_per_hour = -60,
},
{
.name = "an unchanged balance adds nothing",
.reads = {{6750, 0}, {6750, 30}},
.expected_start = 6750,
},
{
.name = "OCR failures are skipped",
.reads = {{6750, 0}, {-1, 5}, {6800, 30}},
.expected_start = 6750,
.expected_earned = 50,
.expected_net = 50,
.expected_per_hour = 100,
},
{
// The hour starts at the first successful read.
.name = "a failed first read does not set the start",
.reads = {{-1, 0}, {6750, 10}, {6760, 70}},
.expected_start = 6750,
.expected_earned = 10,
.expected_net = 10,
.expected_per_hour = 10,
},
};
for (const BBQPointsLedgerScenario& scenario : scenarios){
database.add<Test_BBQPointsLedger>(scenario);
}
}



void add_tests_BBQ(UnitTestDatabase& database){
database.add<Test_BBQuestsSlugs>();

Expand All @@ -1022,6 +1126,7 @@
add_tests_BBQReroll(database);
add_tests_BBQuestPlan(database);
add_tests_BBQFarmerRounds(database);
add_tests_BBQPointsLedger(database);
add_tests_TeraEncounterAction(database);
}

Expand Down
Loading
Loading