Skip to content

Commit

Permalink
Merge pull request #1007 from OutpostUniverse/refactorRobotIdString
Browse files Browse the repository at this point in the history
Refactor robot id string serialization
  • Loading branch information
DanRStevens authored Jun 26, 2021
2 parents e45b6f9 + 2b2ac1b commit 8195a6f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
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

0 comments on commit 8195a6f

Please sign in to comment.