Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor robot id string serialization #1007

Merged
merged 6 commits into from
Jun 26, 2021
Merged
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
12 changes: 6 additions & 6 deletions OPHD/States/MapViewStateIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,16 @@ static void loadResorucesFromXmlElement(NAS2D::Xml::XmlElement* element, Storabl
}


static void readRccRobots(NAS2D::Xml::XmlAttribute* attr, Structure& structure, RobotPool& pool)
static void readRccRobots(std::string robotIds, RobotCommand& robotCommand, RobotPool& pool)
{
if (!attr) { return; }

for (const auto& string : NAS2D::split(attr->value(), ','))
for (const auto& string : NAS2D::split(robotIds, ','))
{
const auto robotId = NAS2D::stringTo<int>(string);
for (auto* robot : pool.robots())
{
if (robot->id() == robotId)
{
static_cast<RobotCommand*>(&structure)->addRobot(robot);
robotCommand.addRobot(robot);
break;
}
}
Expand Down Expand Up @@ -501,7 +499,9 @@ void MapViewState::readStructures(Xml::XmlElement* element)
auto robotsElement = structureElement->firstChildElement("robots");
if (robotsElement)
{
readRccRobots(robotsElement->firstAttribute(), structure, mRobotPool);
const auto robotIds = robotsElement->attribute("robots");
auto& robotCommand = *static_cast<RobotCommand*>(&structure);
readRccRobots(robotIds, robotCommand, mRobotPool);
}
}

Expand Down
12 changes: 5 additions & 7 deletions OPHD/StructureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "States/MapViewStateHelper.h" // <-- For removeRefinedResources()

#include <NAS2D/ParserHelper.h>
#include <NAS2D/StringUtils.h>
#include <NAS2D/ContainerUtils.h>

#include <algorithm>
#include <sstream>
Expand Down Expand Up @@ -65,15 +67,11 @@ namespace
{
const auto& robots = static_cast<RobotCommand*>(structure)->robots();

std::stringstream str;
for (std::size_t i = 0; i < robots.size(); ++i)
{
str << robots[i]->id();
if (i != robots.size() - 1) { str << ","; } // kind of a kludge
}
const auto robotToIdString = [](const Robot* robot){ return NAS2D::stringFrom(robot->id()); };
const auto idsString = NAS2D::join(NAS2D::mapToVector(robots, robotToIdString), ",");

structureElement->linkEndChild(
NAS2D::dictionaryToAttributes("robots", {{{"robots", str.str()}}})
NAS2D::dictionaryToAttributes("robots", {{{"robots", idsString}}})
);
}

Expand Down