summaryrefslogtreecommitdiffhomepage
path: root/Snapshot.cpp
blob: 5d76e73ed0a72856224b72adcd903ceb471c0965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Snapshot.h"

#include <ctime>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <iostream>

#include <nlohmann/json.hpp>
#include <raylib.h>

#include "Killmail.h"
#include "Owner.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)


Snapshot::Snapshot(const char* filename) :
    m_killmails {},
    m_teams {}
{
    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];
    int team = 0;
    for (const auto& members : dump.at("teams")) {
        for (const auto& entry : members) {
            const auto id = entry.get<long int>();
            m_teams[id] = team;
        }
        ++team;
    }
    UnloadFileText(text);
}


std::vector<Killmail>
Snapshot::killmails() const
{
    return m_killmails;
}


int
Snapshot::team(const Owner& owner) const
{
    auto find = [this](long int id) -> int {
        if (id < 0)
            return -1;
        const auto search = m_teams.find(id);
        if (search == m_teams.end())
            return -1;
        return search->second;
    };
    int id;
    if ((id = find(owner.faction)) != -1) return id;
    if ((id = find(owner.alliance)) != -1) return id;
    if ((id = find(owner.corporation)) != -1) return id;
    return -1;
}


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);
}