Finished code for globals, but something's not working with client/server

This commit is contained in:
2023-04-10 16:00:47 -04:00
parent ef7d0ec365
commit ac383c616f
9 changed files with 230 additions and 55 deletions

View File

@@ -1,5 +1,6 @@
#include "world.hpp"
#include "pprint.hpp"
#include "json.hpp"
#include <cassert>
void World::tangible_walkto(int64_t id, int64_t animid, float x, float y) {
@@ -219,6 +220,32 @@ void World::tangible_set_class(int64_t id, const eng::string &c) const {
LS.result();
}
void World::set_global_json(const eng::string &gvar, const eng::string &json) {
LuaVar decoded;
LuaExtStack LS(state(), decoded);
bool ok = json::decode(LS, decoded, json);
if (!ok) {
luaL_error(state(), "invalid json");
return;
}
set_global(LS, gvar, decoded);
}
eng::string World::get_global_json(const eng::string &gvar) {
LuaVar value, globaldb;
LuaExtStack LS(state(), globaldb, value);
LS.rawget(globaldb, LuaRegistry, "globaldb");
LS.rawget(value, globaldb, gvar);
eng::string out;
eng::string error = json::encode(LS, value, out, false, 10000);
if (!error.empty()) {
luaL_error(state(), "%s", error.c_str());
return "";
}
return out;
}
eng::string World::tangible_get_class(int64_t id) const {
LuaVar tangibles, tan, meta, sclass;
LuaOldStack LS(state(), tangibles, tan, meta, sclass);
@@ -232,6 +259,8 @@ eng::string World::tangible_get_class(int64_t id) const {
return result;
}
static bool worlds_identical(const UniqueWorld &w1, const UniqueWorld &w2) {
StreamBuffer sbw1, sbw2;
w1->serialize(&sbw1);
@@ -450,3 +479,32 @@ LuaDefine(unittests_world4difftanclass, "", "some unit tests") {
return 0;
}
LuaDefine(unittests_world5diffglobals, "", "some unit tests") {
UniqueWorld m(new World(WORLD_TYPE_MASTER));
UniqueWorld ss(new World(WORLD_TYPE_PREDICTIVE));
UniqueWorld cs(new World(WORLD_TYPE_PREDICTIVE));
StreamBuffer sb;
m->set_global_json("x", "3");
ss->diff_globals(m.get(), &sb);
cs->patch_globals(&sb, nullptr);
LuaAssertStrEq(L, cs->get_global_json("x"), "3");
m->set_global_json("x", "4");
m->set_global_json("x", "5");
m->set_global_json("y", "6");
ss->diff_globals(m.get(), &sb);
cs->patch_globals(&sb, nullptr);
LuaAssertStrEq(L, cs->get_global_json("x"), "5");
LuaAssertStrEq(L, cs->get_global_json("y"), "6");
cs->set_global_json("x", "2");
ss->set_global_json("x", "2");
ss->diff_globals(m.get(), &sb);
cs->patch_globals(&sb, nullptr);
LuaAssertStrEq(L, cs->get_global_json("x"), "5");
return 0;
}