more unit testing of diff xmit

This commit is contained in:
2021-08-03 12:40:12 -04:00
parent 202c5a24ba
commit 3fa782383e
4 changed files with 104 additions and 24 deletions

View File

@@ -126,14 +126,16 @@ Tangible *World::tangible_get(lua_State *L, int idx) {
return result;
}
void World::tangible_delete(lua_State *L, int64_t id) {
void World::tangible_delete(int64_t id) {
lua_State *L = state();
LuaVar tangibles, database;
LuaStack LS(L, tangibles, database);
// Fetch the C++ side of the tangible.
auto iter = tangibles_.find(id);
if (iter == tangibles_.end()) {
luaL_error(L, "Not a tangible: %lld", id);
LS.result();
return; // Nothing to delete.
}
// Fetch the lua side of the tangible.
@@ -145,10 +147,10 @@ void World::tangible_delete(lua_State *L, int64_t id) {
LS.clearmetatable(database);
LS.cleartable(database);
// Remove the lua portion from the table.
// Remove the lua portion from the tangibles table.
LS.rawset(tangibles, id, LuaNil);
// Remove the C++ portion from the table.
// Remove the C++ portion from the tangibles table.
tangibles_.erase(iter);
LS.result();
}
@@ -166,6 +168,13 @@ void World::tangible_walkto(int64_t id, int64_t animid, float x, float y) {
}
std::string World::tangible_anim_debug_string(int64_t id) const {
const Tangible *t = tangible_get(id);
if (t == 0) return "no such tangible";
return t->anim_queue_.steps_debug_string();
}
std::string World::tangible_ids_debug_string() const {
util::IdVector idv;
for (const auto &pair : tangibles_) {
@@ -664,7 +673,7 @@ void World::patch_visible_animations(StreamBuffer *sb) {
count = sb->read_int32();
for (int i = 0; i < count; i++) {
int64_t id = sb->read_int64();
tangible_delete(state(), id);
tangible_delete(id);
}
// Receive update messages
@@ -727,11 +736,12 @@ LuaDefine(tangible_delete, "c") {
LuaStack LS(L, tanobj);
World *w = World::fetch_global_pointer(L);
Tangible *tan = w->tangible_get(L, tanobj.index());
assert(tan != nullptr); // this should be checked above.
if (tan->is_an_actor()) {
luaL_error(L, "Cannot delete a player using tangible.delete, use tangible.redirect instead.");
return 0;
}
w->tangible_delete(L, tan->id());
w->tangible_delete(tan->id());
return LS.result();
}
@@ -811,7 +821,7 @@ LuaDefine(tangible_redirect, "c") {
w->redirects_[tan1->id()] = tan2->id();
}
if (bulldoze) {
w->tangible_delete(L, tan1->id());
w->tangible_delete(tan1->id());
}
return LS.result();
}
@@ -843,14 +853,65 @@ LuaDefine(unittests_world, "c") {
ss.reset(new World(util::WORLD_TYPE_S_SYNC));
cs.reset(new World(util::WORLD_TYPE_C_SYNC));
// Create some tangibles, and add some animations.
m->tangible_make(0, 123, false);
m->tangible_make(0, 345, false);
m->tangible_walkto(123, 770, 3, 4);
m->tangible_walkto(345, 771, 6, 2);
LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123,345");
LuaAssertStrEq(L, m->tangible_anim_debug_string(123),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=770 action=walkto x=3 y=4; ");
LuaAssertStrEq(L, m->tangible_anim_debug_string(345),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=771 action=walkto x=6 y=2; ");
// Now difference transmit all that to the client.
ss->difference_transmit(123, m.get(), &sb);
cs->apply_differences(&sb);
LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123,345");
LuaAssertStrEq(L, ss->tangible_anim_debug_string(123),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=770 action=walkto x=3 y=4; ");
LuaAssertStrEq(L, ss->tangible_anim_debug_string(345),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=771 action=walkto x=6 y=2; ");
LuaAssert(L, worlds_identical(ss, cs));
// Now add some more animation records to the master.
m->tangible_walkto(123, 772, 7, 3);
m->tangible_walkto(345, 773, 2, 5);
LuaAssertStrEq(L, m->tangible_anim_debug_string(123),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=770 action=walkto x=3 y=4; "
"id=772 action=walkto x=7 y=3; ");
LuaAssertStrEq(L, m->tangible_anim_debug_string(345),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=771 action=walkto x=6 y=2; "
"id=773 action=walkto x=2 y=5; ");
// Now difference transmit all that to the client again.
ss->difference_transmit(123, m.get(), &sb);
cs->apply_differences(&sb);
LuaAssertStrEq(L, ss->tangible_anim_debug_string(123),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=770 action=walkto x=3 y=4; "
"id=772 action=walkto x=7 y=3; ");
LuaAssertStrEq(L, ss->tangible_anim_debug_string(345),
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
"id=771 action=walkto x=6 y=2; "
"id=773 action=walkto x=2 y=5; ");
LuaAssert(L, worlds_identical(ss, cs));
// Delete tangible 345.
m->tangible_delete(345);
LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123");
// And difference transmit
ss->difference_transmit(123, m.get(), &sb);
cs->apply_differences(&sb);
LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123");
LuaAssert(L, worlds_identical(ss, cs));
return 0;
}