-
Notifications
You must be signed in to change notification settings - Fork 9
/
spiredb.h
84 lines (74 loc) · 2.03 KB
/
spiredb.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
#ifndef SPIREDB_H_
#define SPIREDB_H_
#include <deque>
#include <string>
#define PQXX_HIDE_EXP_OPTIONAL
#include <pqxx/connection>
#include <pqxx/transaction>
#include "network.h"
#include "spirecore.h"
#include "spirelayout.h"
#include "types.h"
struct HttpMessage;
class SpireDB
{
private:
struct LiveQuery
{
std::string upgrades;
unsigned floors;
Number budget;
Core core;
std::string core_type;
Number core_budget;
};
struct RecentQuery: LiveQuery
{
bool income;
bool towers;
std::string client;
std::chrono::steady_clock::time_point time;
unsigned work_given_count;
};
enum WorkType
{
INCOMPLETE,
UNDERPERFORMING,
ADD_FLOOR,
INCREASE_BUDGET,
INCREASE_BUDGET_TOWERS,
DECREASE_BUDGET
};
Network network;
std::mutex database_mutex;
pqxx::connection *pq_conn;
bool force_update;
std::map<Network::ConnectionTag, LiveQuery> live_queries;
std::mutex recent_mutex;
std::deque<RecentQuery> recent_queries;
Random random;
std::mutex work_mutex;
std::string current_work;
unsigned work_given_count;
static const unsigned current_version;
public:
SpireDB(int, char **);
~SpireDB();
int main();
private:
void update_data();
void serve(Network::ConnectionTag, const std::string &);
void serve_http(Network::ConnectionTag, const std::string &);
void serve_http_file(const std::string &, HttpMessage &);
std::string query(Network::ConnectionTag, const std::vector<std::string> &, bool = false);
Layout query_layout(pqxx::transaction_base &, unsigned, const TrapUpgrades &, Number, const Core *, Number, bool, bool);
Core query_core(pqxx::transaction_base &, unsigned);
std::string submit(Network::ConnectionTag, const std::vector<std::string> &);
int submit_layout(Network::ConnectionTag, pqxx::transaction_base &, const Layout &);
int check_better_layout(pqxx::transaction_base &, const Layout &, bool, bool);
void check_live_queries(Network::ConnectionTag, const Layout &);
static int compare_layouts(const Layout &, const Layout &, bool, bool);
void select_random_work();
std::string get_work();
};
#endif