Naive implementation of MemoryObject.

Implement GameObject.cpuGetUsed().
This commit is contained in:
Kirill Kirilenko 2021-06-13 21:46:44 +03:00
parent 48806408de
commit 07c0b0ac80
3 changed files with 23 additions and 1 deletions

View file

@ -3,12 +3,17 @@
#include "Object.hpp" #include "Object.hpp"
#include <string_view>
namespace Screeps { namespace Screeps {
class MemoryObject : public Object class MemoryObject : public Object
{ {
public: public:
MemoryObject(); MemoryObject();
JSON operator[](const std::string_view& key);
void set(const std::string_view& key, const JSON& value);
}; };
extern MemoryObject Memory; extern MemoryObject Memory;

View file

@ -17,7 +17,7 @@ convertObjectToMap(const JS::Value& object)
{ {
std::map<std::string, T> map; std::map<std::string, T> map;
for (const auto& pair : JS::jsObjectToMap(object)) for (const auto& pair : JS::jsObjectToMap(object))
map.emplace(pair.first, T{pair.second}); map.insert(std::make_pair(pair.first, T{pair.second}));
return map; return map;
} }
@ -107,6 +107,11 @@ int GameObject::time() const
return value()["time"].as<int>(); return value()["time"].as<int>();
} }
double GameObject::cpuGetUsed()
{
return value()["cpu"].call<double>("getUsed");
}
int GameObject::cpuGeneratePixel() int GameObject::cpuGeneratePixel()
{ {
return value()["cpu"].call<int>("generatePixel"); return value()["cpu"].call<int>("generatePixel");

View file

@ -1,9 +1,21 @@
#include "Memory.hpp" #include "Memory.hpp"
#include <Screeps/JSON.hpp>
namespace Screeps { namespace Screeps {
MemoryObject Memory; MemoryObject Memory;
MemoryObject::MemoryObject() = default; MemoryObject::MemoryObject() = default;
JSON MemoryObject::operator[](const std::string_view& key)
{
return JS::toJSON(value()[key.data()]);
}
void MemoryObject::set(const std::string_view& key, const JSON& value)
{
this->value().set(key.data(), JS::fromJSON(value));
}
} // namespace Screeps } // namespace Screeps