Skip to content
Draft
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
Expand Up @@ -17,6 +17,7 @@
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Pokemon/AutoStory/Grid/Pokemon_GridStoryArgs.h"
#include "Pokemon_StoryStepJson.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -180,9 +181,40 @@
}


StoryPostconditions read_postconditions(const JsonValue& value, std::string_view source){
const JsonObject& root = fields(value, {"position", "items", "flags"}, source);
StoryPostconditions result;
if (const JsonValue* field = root.get_value("position")){
result.position = read_grid_pose(*field, source);
}
if (const JsonValue* field = root.get_value("items")){
result.items = read_story_items(*field, source);
}
if (const JsonValue* field = root.get_value("flags")){
result.flags = read_strings(*field, source);
}
return result;
}

StoryVerification read_verification(const JsonValue& value, std::string_view source){
const JsonObject& root = fields(value, {"steps_hash", "build", "date", "runs"}, source);
StoryVerification result;
result.steps_hash = root.get_string_throw("steps_hash");
if (const JsonValue* field = root.get_value("build")){
result.build = field->to_string_throw();
}
if (const JsonValue* field = root.get_value("date")){
result.date = field->to_string_throw();
}
if (const JsonValue* field = root.get_value("runs")){
result.runs = field->to_string_throw();
}
return result;
}

// Reads "metadata" into `checkpoint`. A missing phase is testing.
void read_metadata(const JsonValue& value, std::string_view source, StoryCheckpoint& checkpoint){
const JsonObject& root = fields(value, {"phase", "source"}, source);
const JsonObject& root = fields(value, {"phase", "verified", "source", "postconditions"}, source);
if (const JsonValue* field = root.get_value("phase")){
const std::string name = field->to_string_throw();
const std::optional<StoryPhase> phase = read_story_phase(name);
Expand All @@ -191,9 +223,15 @@
}
checkpoint.metadata.phase = *phase;
}
if (const JsonValue* field = root.get_value("verified")){
checkpoint.metadata.verified = read_verification(*field, source);
}
if (const JsonValue* field = root.get_value("source")){
checkpoint.metadata.source = field->to_string_throw();
}
if (const JsonValue* field = root.get_value("postconditions")){
checkpoint.postconditions = read_postconditions(*field, source);
}
}


Expand All @@ -213,12 +251,40 @@
return result;
}

// A gate change, {"opened": ["SilphCo_5F/door_1"], "switches": {"mansion": true}}:
// the gates a step opens, the switches it sets; both optional.
GateChange read_gate_change(const JsonValue& value, std::string_view source){
const JsonObject& root = fields(value, {"opened", "switches"}, source);
GateChange result;
if (const JsonValue* field = root.get_value("opened")){
for (std::string& key : read_strings(*field, source)){
result.opened.insert(std::move(key));
}
}
if (const JsonValue* field = root.get_value("switches")){
for (const auto& [id, on] : field->to_object_throw(std::string(source))){
result.switches[id] = on.to_boolean_throw(std::string(source));
}
}
return result;
}

// A checkpoint's "start_gates", written as a gate change from nothing open.
GateState read_gate_state(const JsonValue& value, std::string_view source){
GateState result;
apply_gate_change(result, read_gate_change(value, source));
return result;
}

// Read a step, its args parsed against its action among `actions` (the
// game's) and their fallbacks filled in. Unknown fields, kinds, actions and args, mistyped
// values and missing required args fail.
StoryStep read_story_step(const JsonValue& value, std::string_view source, std::span<const StoryActionSpec> actions){
StoryStep read_story_step(

Check warning on line 282 in SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::read_story_step( const JsonValue & value , std :: string_view source , std :: span<const StoryActionSpec> actions , const StoryArgKind * declared_pose): function_lines=34 (advisory above 25, limit 50)
const JsonValue& value, std::string_view source,
std::span<const StoryActionSpec> actions, const StoryArgKind* declared_pose
){
const JsonObject& root = fields(
value, {"id", "kind", "why", "action", "args"}, source
value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after", "gates_after", "flags_after"}, source
);
StoryStep result;
result.name = root.get_string_throw("id");
Expand All @@ -233,6 +299,18 @@
result.action = field->to_string_throw();
}
result.args = read_args(root.get_value("args"), step_action(result, actions, source), result, source);
if (const JsonValue* field = root.get_value("skip_if")){
result.skip_if = read_strings(*field, source);
}
if (const JsonValue* field = root.get_value("declared_after")){
result.pose_after = declared_pose ? DeclaredPose(declared_pose->read(*field, source)) : read_declared_pose(*field, source);
}
if (const JsonValue* field = root.get_value("gates_after")){
result.gates_after = read_gate_change(*field, source);
}
if (const JsonValue* field = root.get_value("flags_after")){
result.flags_after = read_strings(*field, source);
}
return result;
}

Expand All @@ -240,11 +318,11 @@
} // namespace


StoryCheckpoint read_story_checkpoint(

Check warning on line 321 in SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::read_story_checkpoint( const JsonValue & value , std :: string_view source , std :: span<const StoryActionSpec> actions , const StoryArgKind * declared_pose): function_lines=34 (advisory above 25, limit 50)
const JsonValue& value, std::string_view source,
std::span<const StoryActionSpec> actions, const StoryArgKind* declared_pose
){
const JsonObject& root = fields(value, {"id", "title", "start", "events", "metadata"}, source);
const JsonObject& root = fields(value, {"id", "title", "start", "start_gates", "start_flags", "events", "metadata"}, source);
StoryCheckpoint result;
result.id = root.get_string_throw("id");
if (result.id.empty()){
Expand All @@ -254,9 +332,16 @@
if (const JsonValue* start = root.get_value("start")){
result.start = declared_pose ? DeclaredPose(declared_pose->read(*start, source)) : read_declared_pose(*start, source);
}
if (const JsonValue* gates = root.get_value("start_gates")){
result.start_gates = read_gate_state(*gates, source);
}
if (const JsonValue* flags = root.get_value("start_flags")){
result.start_flags = read_strings(*flags, source);
}
std::set<std::string> names;
result.steps_json = root.get_value_throw("events").dump(-1);
for (const JsonValue& event : root.get_array_throw("events")){
StoryStep step = read_story_step(event, source, actions);
StoryStep step = read_story_step(event, source, actions, declared_pose);
if (step.name.empty() || !names.insert(step.name).second){
throw ParseException("Empty or duplicate story event id.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ namespace Pokemon{
// action among `actions` (the game's) and their fallbacks filled in.
// Unknown fields, kinds, actions and args, mistyped values and missing
// required args fail. A game whose poses are not zones passes
// `declared_pose`, which reads "start" instead.
// `declared_pose`, which reads "start" and "declared_after" instead.
// The grid games' fields: a step's "skip_if", "declared_after",
// "gates_after" and "flags_after"; the checkpoint's "start_gates" and
// "start_flags"; "metadata" "verified" and "postconditions". steps_json
// keeps the "events" array as read, dumped compactly.
StoryCheckpoint read_story_checkpoint(
const JsonValue& value, std::string_view source,
std::span<const StoryActionSpec> actions, const StoryArgKind* declared_pose = nullptr
Expand Down
Loading
Loading