Added more tests to ordered list, and fixed bug when inserting at end of list as client

This commit is contained in:
Yousef Khadadeh 2026-01-05 22:01:54 +03:00
parent 06079f80cd
commit df7ec9cb99
3 changed files with 63 additions and 27 deletions

View file

@ -15,8 +15,10 @@
#ifdef ENABLE_NETOBJ_ORDERED_LIST_VERBOSE_DEBUG
#define NETOBJ_ORDERED_LIST_VERBOSE_DEBUG(data, idMap) netobj_ordered_list_debug_check(data, idMap)
#define NETOBJ_CHECK_INSERT_ORDERED(data, newObjs) netobj_check_insert_is_ordered(data, newObjs)
#else
#define NETOBJ_ORDERED_LIST_VERBOSE_DEBUG(data, idMap)
#define NETOBJ_CHECK_INSERT_ORDERED(data, newObjs)
#endif
namespace NetworkingObjects {
@ -78,6 +80,28 @@ namespace NetworkingObjects {
}
std::cout << "[netobj_ordered_list_debug_check] Debug print end" << std::endl;
}
template <typename T> void netobj_check_insert_is_ordered(const std::list<NetObjOrderedListObjectInfo<T>>& l, std::vector<std::pair<NetObjOrderedListIterator<T>, NetObjOwnerPtr<T>>>& newObjs) {
std::cout << "[netobj_check_insert_is_ordered] Insert list order check" << std::endl;
uint32_t lastPos = 0;
bool isEnd = false;
for(auto& [it, obj] : newObjs) {
if(it == l.end()) {
std::cout << "[netobj_check_insert_is_ordered] Insert at: " << l.size() << " (end)" << std::endl;
isEnd = true;
}
else {
std::cout << "[netobj_check_insert_is_ordered] Insert at: " << it->pos << std::endl;
if(isEnd)
throw std::runtime_error("[netobj_check_insert_is_ordered] Insert list not ordered! (Failure at end)");
if(lastPos > it->pos)
throw std::runtime_error("[netobj_check_insert_is_ordered] Insert list not ordered!");
lastPos = it->pos;
isEnd = false;
}
}
std::cout << "[netobj_check_insert_is_ordered] Insert list is ordered" << std::endl;
}
#endif
template <typename T> void refresh_positions_complete(std::list<NetObjOrderedListObjectInfo<T>>& l) {
@ -311,6 +335,8 @@ namespace NetworkingObjects {
if(newObjs.empty())
return {};
NETOBJ_CHECK_INSERT_ORDERED(data, newObjs);
std::vector<NetworkingObjects::NetObjID> idPositionList;
for(auto& [itToInsertAt, newObj] : newObjs) {
if(itToInsertAt == data.begin())
@ -465,6 +491,8 @@ namespace NetworkingObjects {
if(newObjs.empty())
return {};
NETOBJ_CHECK_INSERT_ORDERED(clientData, newObjs);
std::vector<NetObjOrderedListIterator<T>> toRet;
for(auto& [itToInsertAt, newObj] : newObjs) {
auto newObjIt = clientData.emplace(itToInsertAt, std::move(newObj), itToInsertAt == clientData.end() ? clientData.size() : itToInsertAt->pos); // All position values will be wrong except the first one, which is all that matters
@ -648,8 +676,11 @@ namespace NetworkingObjects {
if(!elemData.foundInClient)
continue;
if(firstInsertedIt == clientData.end()) {
firstInsertedActualPos = elemData.clientInsertPosIt->pos;
firstInsertedIt = elemData.clientObjectIt;
if(elemData.clientInsertPosIt != clientData.end())
firstInsertedActualPos = elemData.clientInsertPosIt->pos;
else
firstInsertedActualPos = clientData.size();
}
clientData.splice(elemData.clientInsertPosIt, referredToObjectData, elemData.clientObjectIt);
clientIdToDataMap.emplace(elemData.clientObjectIt->obj.get_net_id(), elemData.clientObjectIt);

View file

@ -507,34 +507,38 @@ void World::scale_up(const WorldScalar& scaleUpAmount) {
#ifdef ENABLE_ORDERED_LIST_TEST
void World::list_debug_test_update() {
if(std::chrono::steady_clock::now() < (listDebugTestTimeStart + std::chrono::minutes(1))) {
bool isInsert;
if(listDebugTest->size() > 70)
isInsert = false;
else if(listDebugTest->size() < 20)
isInsert = true;
else
isInsert = Random::get().real_range(0.0f, 1.0f) > 0.5f;
if(nextSendTime < std::chrono::steady_clock::now() - std::chrono::milliseconds(300)) {
nextSendTime = std::chrono::steady_clock::now();
using namespace NetworkingObjects;
if(isInsert) {
std::vector<std::pair<NetObjOrderedListIterator<uint16_t>, NetObjOwnerPtr<uint16_t>>> toInsert;
std::vector<uint32_t> randomIndices;
uint32_t insertAmount = Random::get().int_range(5, 25);
for(uint32_t i = 0; i < insertAmount; i++)
randomIndices.emplace_back(Random::get().int_range<uint32_t>(0, listDebugTest->size() + 10));
randomIndices[1] = randomIndices[0];
std::sort(randomIndices.begin(), randomIndices.end());
for(uint32_t index : randomIndices)
toInsert.emplace_back(listDebugTest->at(index), netObjMan.make_obj_direct<uint16_t>(Random::get().int_range<uint32_t>(10, 100)));
listDebugTest->insert_ordered_list_and_send_create(listDebugTest, toInsert);
}
else {
std::vector<NetObjOrderedListIterator<uint16_t>> toErase;
for(uint32_t i = 0; i < listDebugTest->size(); i++) {
if(Random::get().real_range(0.0f, 1.0f) > 0.8f)
toErase.emplace_back(listDebugTest->at(i));
bool isInsert;
if(listDebugTest->size() > 70)
isInsert = false;
else if(listDebugTest->size() < 20)
isInsert = true;
else
isInsert = Random::get().real_range(0.0f, 1.0f) > 0.5f;
using namespace NetworkingObjects;
if(isInsert) {
std::vector<std::pair<NetObjOrderedListIterator<uint16_t>, NetObjOwnerPtr<uint16_t>>> toInsert;
std::vector<uint32_t> randomIndices;
uint32_t insertAmount = Random::get().int_range(5, 25);
for(uint32_t i = 0; i < insertAmount; i++)
randomIndices.emplace_back(Random::get().int_range<uint32_t>(0, listDebugTest->size() + 10));
randomIndices[1] = randomIndices[0];
std::sort(randomIndices.begin(), randomIndices.end());
for(uint32_t index : randomIndices)
toInsert.emplace_back(listDebugTest->at(index), netObjMan.make_obj_direct<uint16_t>(Random::get().int_range<uint32_t>(10, 100)));
listDebugTest->insert_ordered_list_and_send_create(listDebugTest, toInsert);
}
else {
std::vector<NetObjOrderedListIterator<uint16_t>> toErase;
for(uint32_t i = 0; i < listDebugTest->size(); i++) {
if(Random::get().real_range(0.0f, 1.0f) > 0.8f)
toErase.emplace_back(listDebugTest->at(i));
}
listDebugTest->erase_list(listDebugTest, toErase);
}
listDebugTest->erase_list(listDebugTest, toErase);
}
}
if(netServer && netServer->get_client_list().empty())

View file

@ -120,5 +120,6 @@ class World {
NetworkingObjects::NetObjOwnerPtr<NetworkingObjects::NetObjOrderedList<uint16_t>> listDebugTest;
void list_debug_test_update();
std::chrono::steady_clock::time_point listDebugTestTimeStart = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point nextSendTime = std::chrono::steady_clock::now();
#endif
};