diff options
author | Aki <please@ignore.pl> | 2022-06-04 13:14:20 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-06-04 13:14:20 +0200 |
commit | c60ec37a5b99cd1c9f3bd543c0a78d571b69bf95 (patch) | |
tree | cc227399f4bd665063bcc8e3d9d2d1f51dc9a767 /Snapshot.cpp | |
parent | af781837df4a4fb1df63103cdeb6a3dfbab7b9d4 (diff) | |
download | derelict-c60ec37a5b99cd1c9f3bd543c0a78d571b69bf95.zip derelict-c60ec37a5b99cd1c9f3bd543c0a78d571b69bf95.tar.gz derelict-c60ec37a5b99cd1c9f3bd543c0a78d571b69bf95.tar.bz2 |
Renamed DumpSource to Snapshot
Diffstat (limited to 'Snapshot.cpp')
-rw-r--r-- | Snapshot.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/Snapshot.cpp b/Snapshot.cpp new file mode 100644 index 0000000..57f81db --- /dev/null +++ b/Snapshot.cpp @@ -0,0 +1,75 @@ +#include "DumpSource.h" + +#include <ctime> +#include <sstream> +#include <string> +#include <unordered_map> +#include <vector> + +#include <nlohmann/json.hpp> +#include <raylib.h> + +#include "Killmail.h" + + +using json = nlohmann::json; +using tm = std::tm; + + +void from_json(const json& j, tm& d); +void from_json(const json& j, Killmail& km); + + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(LongVector3, x, y, z) + + +DumpSource::DumpSource(const char* filename) : + m_killmails {} +{ + if (!FileExists(filename)) + throw "File does not exist"; + char* text = LoadFileText(filename); + auto dump = json::parse(text); + std::unordered_map<long int, long int> group_lookup; + for (const auto& item : dump["types"]) { + const auto type_id = item["type_id"].get<long int>(); + item["group_id"].get_to(group_lookup[type_id]); + } + dump.at("killmails").get_to(m_killmails); + for (auto& km : m_killmails) + km.group = group_lookup[km.ship]; + UnloadFileText(text); +} + + +std::vector<Killmail> +DumpSource::killmails() const +{ + return m_killmails; +} + + +void +from_json(const json& j, tm& d) +{ + std::istringstream str(j.get<std::string>()); + str >> std::get_time(&d, "%Y-%m-%dT%H:%M:%SZ"); +} + + +void +from_json(const json& j, Killmail& km) +{ + j.at("solar_system_id").get_to(km.location); + j.at("victim").at("ship_type_id").get_to(km.ship); + j.at("victim").at("position").get_to(km.position); + if (j.at("victim").contains("character_id")) + j.at("victim").at("character_id").get_to(km.owner.character); + j.at("victim").at("corporation_id").get_to(km.owner.corporation); + if (j.at("victim").contains("alliance_id")) + j.at("victim").at("alliance_id").get_to(km.owner.alliance); + if (j.at("victim").contains("faction_id")) + j.at("victim").at("faction_id").get_to(km.owner.faction); + auto calendar = j.at("killmail_time").get<tm>(); + km.time = std::mktime(&calendar); +} |