event parsing and dispatching
Some checks are pending
ContinuousIntegration / macos (push) Waiting to run
ContinuousIntegration / windows (push) Waiting to run
ContinuousIntegration / linux (push) Successful in 22s

This commit is contained in:
Green Sky 2024-06-11 10:37:20 +02:00
parent 70317ab4db
commit 6f6894419a
No known key found for this signature in database
4 changed files with 176 additions and 13 deletions

View File

@ -17,10 +17,159 @@ void FactorioLogParser::onFileEvent(const std::string& path, const filewatch::Ev
// on mod (?), read line, parse // on mod (?), read line, parse
std::string line;
const auto parse_res = log_parse_line(line);
if (parse_res.has_value()) {
dispatchRaw(parse_res.value().event, parse_res.value().params);
}
}
void FactorioLogParser::dispatchRaw(std::string_view event, std::string_view params) {
if (event == "JOIN") {
throwJoin(params);
} else if (event == "LEAVE") {
throwLeave(params);
} else if (event == "CHAT") {
throwChat(params);
} else if (event == "DIED") {
throwDied(params);
} else if (event == "EVOLUTION") {
throwEvolution(params);
} else if (event == "RESEARCH STARTED") {
throwResearchStarted(params);
} else if (event == "RESEARCH FINISHED") {
throwResearchFinished(params);
} else if (event == "RESEARCH CANCELLED") {
throwResearchCancelled(params);
} else {
std::cerr << "FLP error: unknown event parsed: '" << event << "'\n";
}
}
void FactorioLogParser::throwJoin(std::string_view params) {
if (params == "<server>") {
return;
}
dispatch( dispatch(
FactorioLogParser_Event::join, FactorioLogParser_Event::join,
FactorioLog::Events::Join{ FactorioLog::Events::Join{
"Player" params
}
);
}
void FactorioLogParser::throwLeave(std::string_view params) {
const auto user = params.substr(0, params.find_first_of(' '));
if (user.empty() || user.size() == params.size()) {
std::cerr << "FLP error: invalid LEAVE params: '" << params << "'\n";
return;
}
if (user == "<server>") {
return;
}
auto reason = params;
reason.remove_prefix(user.size()+1);
dispatch(
FactorioLogParser_Event::leave,
FactorioLog::Events::Leave{
user,
reason
}
);
}
void FactorioLogParser::throwChat(std::string_view params) {
const auto user = params.substr(0, params.find_first_of(':'));
if (user.empty() || user.size() == params.size() || user.size() + 2 > params.size()) {
std::cerr << "FLP error: invalid CHAT params: '" << params << "'\n";
return;
}
if (user == "<server>") {
return;
}
auto message = params;
message.remove_prefix(user.size()+2); // ": "
if (message.empty()) {
std::cerr << "FLP error: empty message? '" << params << "'\n";
return;
}
dispatch(
FactorioLogParser_Event::chat,
FactorioLog::Events::Chat{
user,
message
}
);
}
void FactorioLogParser::throwDied(std::string_view params) {
const auto user = params.substr(0, params.find_first_of(' '));
if (user.empty() || user.size() == params.size()) {
std::cerr << "FLP error: invalid DIED params: '" << params << "'\n";
return;
}
if (user == "<server>") {
return;
}
auto reason = params;
reason.remove_prefix(user.size()+1);
dispatch(
FactorioLogParser_Event::died,
FactorioLog::Events::Died{
user,
reason
}
);
}
void FactorioLogParser::throwEvolution(std::string_view params) {
if (params.empty()) {
return; // ???
}
dispatch(
FactorioLogParser_Event::evolution,
FactorioLog::Events::Evolution{
params
}
);
}
void FactorioLogParser::throwResearchStarted(std::string_view params) {
dispatch(
FactorioLogParser_Event::research_started,
FactorioLog::Events::ResearchStarted{
params
}
);
}
void FactorioLogParser::throwResearchFinished(std::string_view params) {
dispatch(
FactorioLogParser_Event::research_finished,
FactorioLog::Events::ResearchFinished{
params
}
);
}
void FactorioLogParser::throwResearchCancelled(std::string_view params) {
dispatch(
FactorioLogParser_Event::research_cancelled,
FactorioLog::Events::ResearchCancelled{
params
} }
); );
} }

View File

@ -5,42 +5,44 @@
#include <FileWatch.hpp> #include <FileWatch.hpp>
#include <string> #include <string>
#include <string_view>
namespace FactorioLog::Events { namespace FactorioLog::Events {
// TODO: string views?
struct Join { struct Join {
std::string player_name; std::string_view player_name;
}; };
struct Leave { struct Leave {
std::string player_name; std::string_view player_name;
std::string_view reason;
}; };
struct Chat { struct Chat {
std::string player_name; std::string_view player_name;
std::string message; std::string_view message;
}; };
struct Died { struct Died {
std::string player_name; std::string_view player_name;
std::string_view reason;
}; };
struct Evolution { struct Evolution {
// ? // ?
std::string evo; std::string_view evo;
}; };
struct ResearchStarted { struct ResearchStarted {
std::string name; std::string_view name;
}; };
struct ResearchFinished { struct ResearchFinished {
std::string name; std::string_view name;
}; };
struct ResearchCancelled { struct ResearchCancelled {
std::string name; std::string_view name;
}; };
} // FactorioLog::Events } // FactorioLog::Events
@ -83,5 +85,17 @@ class FactorioLogParser : public FactorioLogParserEventProviderI {
protected: protected:
void onFileEvent(const std::string& path, const filewatch::Event change_type); void onFileEvent(const std::string& path, const filewatch::Event change_type);
protected:
void dispatchRaw(std::string_view event, std::string_view params);
void throwJoin(std::string_view params);
void throwLeave(std::string_view params);
void throwChat(std::string_view params);
void throwDied(std::string_view params);
void throwEvolution(std::string_view params);
void throwResearchStarted(std::string_view params);
void throwResearchFinished(std::string_view params);
void throwResearchCancelled(std::string_view params);
}; };

View File

@ -5,7 +5,7 @@
struct LPLRes { struct LPLRes {
std::string_view event; std::string_view event;
std::string_view info; std::string_view params;
}; };
std::optional<LPLRes> log_parse_line(std::string_view line); std::optional<LPLRes> log_parse_line(std::string_view line);

View File

@ -8,7 +8,7 @@ int main(void) {
const auto parse_res = log_parse_line(" 442.539 Script @__Factorio-Event-Logger__/logger.lua:65: [RESEARCH CANCELLED] worker-robots-speed"); const auto parse_res = log_parse_line(" 442.539 Script @__Factorio-Event-Logger__/logger.lua:65: [RESEARCH CANCELLED] worker-robots-speed");
assert(parse_res.has_value()); assert(parse_res.has_value());
assert(parse_res.value().event == "RESEARCH CANCELLED"); assert(parse_res.value().event == "RESEARCH CANCELLED");
assert(parse_res.value().info == "worker-robots-speed"); assert(parse_res.value().params == "worker-robots-speed");
} }
{ {