Support playable Modern data worlds in the editor (by Codex)

This commit is contained in:
Codex committed 2026-10-25 08:23:02 +01:00
1 parent 3dbaea294a
commit 7de6f644f2
5 files changed
+228 -6

No files matched your search

+118
View File
@@ -34,6 +34,123 @@ Json update(const char *table, const char *id, Json set) {
Json row(Session &session, const char *table, const char *id) {
return session.execute("get", {{"table", table}, {"id", id}})["record"];
}
bool has_error(const World &world, const char *code = nullptr) {
for (const auto &diagnostic : world.validate())
if (diagnostic["severity"] == "error" && (!code || diagnostic["code"] == code))
return true;
return false;
}
void modern_roundtrip(const fs::path &root) {
auto world = World::blank("Synthetic Modern archive");
world.manifest["requires"].push_back("runtime:modern-v1");
world.manifest["x-editor"]["playable"] = true;
world.manifest["x-future"] = {{"fraction", 1.125}, {"unknown", Json::array({1, "ä", nullptr})}};
world.rows("nations").push_back({{"id", "nation:test"}, {"name", "Test"}});
const Json camp = {{"id", "resort:test"}, {"map", "map:test"}, {"x", nullptr}, {"y", nullptr},
{"name", {{"default", "Camp"}}}, {"place", "Place"}, {"country", "nation:test"},
{"base_price", 485000}, {"quality", Json::array({0, 6, 4, 4})},
{"x-future", {{"preserve", true}}}};
world.rows("rules").push_back({{"id", "rule:training-camps"}, {"type", "training_camps"},
{"maps", Json::array({{{"id", "map:test"}, {"name", "Map"}}})},
{"resorts", Json::array({camp})}});
world.rows("staff").push_back({{"id", "staff:test"}, {"role", "referee"}, {"name", "Test"}});
check(!has_error(world), "Modern optional GUI images, null coordinates and absent referee grades accepted");
check(world.statistics()["playable"] == true, "Modern playable flag reported accurately");
check(world.rows("finance").empty(), "Modern roundtrip does not require fictional finances");
auto unknown_birthday = world;
unknown_birthday.rows("players").push_back({{"id", "player:unknown"},
{"name", {{"first", "Test"}, {"last", "Player"}}},
{"age_at_start", {{"on", "2026-07-01"}, {"years", 16}}},
{"attributes", Json::object()}});
check(!has_error(unknown_birthday), "Modern missing born retains age fallback");
unknown_birthday.rows("players")[0]["born"] = nullptr;
check(has_error(unknown_birthday, "invalid_date"), "Modern born null rejected like the engine reader");
unknown_birthday.manifest["requires"] = {"editor:world-v1"};
unknown_birthday.manifest["x-editor"]["playable"] = false;
check(!has_error(unknown_birthday), "editor-only nullable born remains accepted");
auto invalid = [&](auto change, const char *code) {
auto copy = world;
change(copy);
check(has_error(copy, code), "Modern validation reports relevant field error");
rejects("validation_failed", [&] { copy.save(root / "invalid-modern.obw"); });
};
invalid([](World &w) { w.manifest["requires"] = {"editor:world-v1"}; }, "invalid_manifest");
invalid([](World &w) { w.manifest["requires"] = {"runtime:modern-v1"}; }, "invalid_manifest");
invalid([](World &w) { w.manifest["requires"].push_back("runtime:classic-v1"); }, "invalid_manifest");
invalid([](World &w) { w.manifest["x-editor"]["playable"] = "true"; }, "invalid_manifest");
invalid([](World &w) { w.manifest["x-editor"] = nullptr; }, "invalid_manifest");
auto bad_camp = [&](const char *field, Json value) {
invalid([&](World &w) { w.rows("rules").back()["resorts"][0][field] = value; },
"invalid_training_camps");
};
bad_camp("map", "map:missing");
bad_camp("country", "nation:missing");
bad_camp("image", "asset:missing");
bad_camp("base_price", -1);
bad_camp("quality", Json::array({1, 2, 3, 7}));
bad_camp("quality", Json::array({1, 2, 3, 1.5}));
bad_camp("x", 1.5);
invalid([](World &w) { w.rows("rules").back()["resorts"][0].erase("x"); }, "invalid_training_camps");
invalid([](World &w) { auto &r = w.rows("rules").back(); r["maps"].push_back(r["maps"][0]); },
"invalid_training_camps");
invalid([](World &w) { auto &r = w.rows("rules").back(); r["resorts"].push_back(r["resorts"][0]); },
"invalid_training_camps");
invalid([](World &w) { w.rows("rules").back()["maps"] = Json::array(); }, "invalid_training_camps");
invalid([](World &w) { w.rows("rules").back()["maps"][0]["width"] = 10; }, "invalid_training_camps");
invalid([](World &w) {
auto &r = w.rows("rules").back();
r["maps"][0]["width"] = 10; r["maps"][0]["height"] = 20;
}, "invalid_training_camps");
invalid([](World &w) {
auto &r = w.rows("rules").back();
r["maps"][0]["width"] = 10; r["maps"][0]["height"] = 20;
r["resorts"][0]["x"] = 10; r["resorts"][0]["y"] = 0;
}, "invalid_training_camps");
for (const auto *field : {"strictness", "perception"})
for (const auto &value : {Json(-1), Json(4), Json(1.5)})
invalid([&](World &w) { w.rows("staff")[0]["attributes"][field] = value; }, "invalid_referee");
world.rows("assets").push_back({{"key", "asset:camp-test"},
{"files", {{"svg", "assets/camp-test.svg"}}}});
world.store->set("assets/camp-test.svg", std::make_shared<const std::string>(images::silhouette()));
auto &catalogue = world.rows("rules").back();
catalogue["maps"][0]["width"] = 10;
catalogue["maps"][0]["height"] = 20;
catalogue["resorts"][0]["x"] = 9;
catalogue["resorts"][0]["y"] = 19;
catalogue["resorts"][0]["image"] = "asset:camp-test";
check(!has_error(world), "Modern in-bounds coordinates and an explicit world image accepted");
auto full = world;
for (int i = 1; i < 255; ++i)
full.rows("staff").push_back({{"id", "staff:" + std::to_string(i)}, {"role", "referee"},
{"name", "Synthetic"}, {"attributes", {{"strictness", 0}, {"perception", 3}}}});
check(!has_error(full), "255 Modern referees accepted");
full.rows("staff").push_back({{"id", "staff:overflow"}, {"role", "referee"}, {"name", "Synthetic"}});
check(has_error(full, "invalid_referee"), "256 Modern referees rejected");
auto legacy = world;
legacy.manifest["requires"] = {"editor:world-v1"};
legacy.manifest["x-editor"]["playable"] = false;
legacy.rows("rules").back().erase("maps");
legacy.rows("staff")[0]["attributes"]["strictness"] = 7;
check(!has_error(legacy), "editor-only worlds retain previous camp/referee validation");
const auto opaque = std::make_shared<const std::string>("opaque future data\0bytes", 24);
world.store->set("x-future/opaque.bin", opaque);
world.save(root / "modern.obw");
auto opened = World::open(root / "modern.obw");
opened.save(root / "modern-roundtrip.obw");
auto reopened = World::open(root / "modern-roundtrip.obw");
check(reopened.manifest == world.manifest, "Modern manifest semantic roundtrip");
check(reopened.rows("rules") == world.rows("rules"), "Modern camp fields and extensions preserved");
check(reopened.rows("staff") == world.rows("staff"), "Modern referee defaults not materialized");
check(reopened.store->read("x-future/opaque.bin") == *opaque, "Modern opaque entry byte preservation");
auto manifest = opened.manifest;
manifest["requires"] = {"editor:world-v1"};
opened.store->save(root / "forbidden-playable.obw", {{"manifest.json", manifest.dump(2)}});
rejects("unsupported_version", [&] { World::open(root / "forbidden-playable.obw"); });
manifest = opened.manifest;
manifest["min_reader"] = "1.2";
opened.store->save(root / "future-reader.obw", {{"manifest.json", manifest.dump(2)}});
rejects("unsupported_version", [&] { World::open(root / "future-reader.obw"); });
}
} // namespace
int main() {
auto root = fs::temp_directory_path() / new_id("editor-test-");
@@ -46,6 +163,7 @@ int main() {
}
} cleanup{root};
try {
modern_roundtrip(root);
Session s;
s.execute("demo");
const auto generation = s.execute("info")["world_generation"];