-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistics.h
113 lines (97 loc) · 2.37 KB
/
Statistics.h
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
106
107
108
109
110
111
112
113
#pragma once
#include "VyHub.h"
#include "Api.h"
#include "User.h"
#include <API/ARK/Ark.h>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <json.hpp>
#pragma comment(lib, "AsaApi.lib")
namespace Statistic
{
inline std::string checkDefintion()
{
if (VyHub::playtimeDefinitionId != "")
{
return VyHub::playtimeDefinitionId;
}
else
{
std::unique_ptr<Entity::Definition> definition = NULL;
definition = Api::getPlaytimeDefinition();
if (definition)
{
// Definition available
VyHub::playtimeDefinitionId = definition->id;
return VyHub::playtimeDefinitionId;
}
else
{
// No definition available, needs to be created
Log::GetLog()->error("Could not find playtime definition, creating...");
std::string new_id = Api::createPlaytimeDefinition();
if (new_id != "")
{
VyHub::playtimeDefinitionId = new_id;
return new_id;
}
else
{
Log::GetLog()->error("Could not create playtime definition, please check your configuration");
return "";
}
}
}
}
inline void sendPlayerTime()
{
std::string definitionId = checkDefintion();
if (definitionId != "")
{
const std::string config_path = AsaApi::Tools::GetCurrentDir() + "/ArkApi/Plugins/VyHub/playtime.json";
std::ifstream file{ config_path };
if (!file.is_open())
{
Log::GetLog()->error("Can't open playtime.json");
throw std::runtime_error("Can't open playtime.json");
}
nlohmann::json j;
file >> j;
file.close();
for (auto& entry : j.items())
{
std::string playerID = entry.key();
std::unique_ptr<Entity::VyHubUser> user = User::getUser(playerID);
if (!user)
{
continue;
}
double hours = std::round((entry.value().get<double>() / 60) * 100.0) / 100.0;
if (hours < 0.1)
{
continue;
}
std::string serverbundleID = static_cast<std::string>(VyHub::config["serverbundle_id"]);
nlohmann::json values = {
{"definition_id", definitionId},
{"user_id", user->id},
{"serverbundle_id", serverbundleID},
{"value", hours}
};
boolean r = Api::updatePlaytimeDefinition(values);
if (!r)
{
// Something went wrong.
continue;
}
// Reset player time
j[playerID] = 0.0;
}
// Save the modified JSON back to the file
std::ofstream o(config_path);
o << std::setw(4) << j << std::endl;
}
}
}