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 <string_view>
namespace Screeps {
class MemoryObject : public Object
{
public:
MemoryObject();
JSON operator[](const std::string_view& key);
void set(const std::string_view& key, const JSON& value);
};
extern MemoryObject Memory;

View file

@ -17,7 +17,7 @@ convertObjectToMap(const JS::Value& object)
{
std::map<std::string, T> map;
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;
}
@ -107,6 +107,11 @@ int GameObject::time() const
return value()["time"].as<int>();
}
double GameObject::cpuGetUsed()
{
return value()["cpu"].call<double>("getUsed");
}
int GameObject::cpuGeneratePixel()
{
return value()["cpu"].call<int>("generatePixel");

View file

@ -1,9 +1,21 @@
#include "Memory.hpp"
#include <Screeps/JSON.hpp>
namespace Screeps {
MemoryObject Memory;
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