aboutsummaryrefslogtreecommitdiff
path: root/frontend/GameData.cpp
blob: f2817377681ea27e0865e7ec4f8b4e60dd944584 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <memory>

#include "backend/Enemy.h"
#include "backend/EnemyFactory.h"
#include "backend/Exception.h"
#include "backend/LocationFactory.h"
#include "backend/Object.h"
#include "backend/ObjectFactory.h"
#include "backend/RNG.h"
#include "backend/Range.h"
#include "backend/print.h"

#include "GameData.h"

using namespace std;

GameData & GameData::get_instance() {
	static GameData instance;
	return instance;
}

unique_ptr<Enemy> GameData::create_enemy(const string & name) const {
	static DBStatement query = this->db.prepare(R"(
		select
			naam,
			omschrijving,
			minimumobjecten,
			maximumobjecten,
			levenspunten,
			aanvalskans,
			minimumschade,
			maximumschade
		from Vijanden
		where lower(naam) = lower(?)
		limit 1
	)");
	query.reset()
		.bind(name)
	;

	try {
		auto row = query.row();
		auto enemy = unique_ptr<Enemy>{ EnemyFactory::create_enemy(row.col<const char *>(0), row.col<const char *>(1)) };
		int object_count = RNG::get().rand_int(Range<int> { row.col<int>(2), row.col<int>(3) });
		for (const string & name : this->random_objects(object_count))
			enemy->add_hidden_object(this->create_object(name).release());
		enemy->set_health(row.col<int>(4));
		enemy->set_attack(static_cast<float>(row.col<int>(5)) / 100);
		enemy->set_damage({ row.col<int>(6), row.col<int>(7) });
		return enemy;
	} catch (Exception & e) {
		printf("Fout bij aanmaken van vijand: %s\n", e.what());
		return unique_ptr<Enemy>{ EnemyFactory::create_enemy(name.c_str()) };
	}
}

unique_ptr<Object> GameData::create_object(const string & name) const {
	static DBStatement query = this->db.prepare(R"(
		select
			omschrijving,
			type,
			minimumwaarde,
			maximumwaarde,
			bescherming
		from Objecten
		where lower(naam) = lower(?)
		limit 1
	)");
	query.reset()
		.bind(name)
	;

	try {
		auto row = query.row();
		return unique_ptr<Object>(ObjectFactory::create_object({
			.name = name.c_str(),
			.description = row.col<const char *>(0, nullptr),
			.type = row.col<const char *>(1, nullptr),
			.value = { row.col<int>(2), row.col<int>(3) },
			.protection = row.col<unsigned int>(4),
		}));
	} catch (Exception & e) {
		return unique_ptr<Object>{ ObjectFactory::create_object(name.c_str()) };
	}
}

unique_ptr<Location> GameData::create_location(const string & name) const{
	static DBStatement query = this->db.prepare(R"(
		select
			naam,
			beschrijving
		from Locaties
		where lower(naam) = lower(?)
		limit 1
	)");
	query.reset()
		.bind(name)
	;

	try {
		auto row = query.row();
		return unique_ptr<Location>{ LocationFactory::create_location(row.col<const char *>(0), row.col<const char *>(1)) };
	} catch (...) {
		return unique_ptr<Location>{ LocationFactory::create_location(name.c_str()) };
	}
}

void GameData::leaderbord_add(const string & name, unsigned int gold) {
	static DBStatement stmt = this->db.prepare(R"(
		insert into Leaderboard (naam, goudstukken)
		values (?, ?)
	)");
	stmt.reset()
		.bind(name)
		.bind(gold)
	;
	stmt.execute();
}

void GameData::leaderbord_print() const {
	static DBStatement query = this->db.prepare(R"(
		select naam, goudstukken
		from Leaderboard
		order by goudstukken desc
		limit 10
	)");
	query.reset();
	printf("\033[1;4m");
	lprtf("%3s  %-20s  %4s", "#", "Naam", "Goud");
	printf("\033[0m");
	lprtf("\n");
	unsigned int i = 1;
	for (DBQueryRow & row : query.rows()) {
		lprtf("%3d  %-20s  %4d\n", i++, row.col<const char *>(0), row.col<int>(1));
	}
}

vector<string> GameData::random_names(const string & table, unsigned count) const {
	if (count == 0) return {};
	try {
		// NOTE: Parameter placeholders cannot be used for database identifiers
		// (i.e. the table name in this case), which makes this function vulnerable
		// to SQL injection if the table argument contains user-controllable data.
		String query_str = String::fmt("select naam from %s order by random() limit ?", table.c_str());
		DBStatement query = this->db.prepare(query_str.c_str());
		query.reset()
			.bind(count)
		;

		vector<string> names = {};
		for (DBQueryRow & row : query.rows()) {
			names.push_back(row.col<const char *>(0));
		}
		return names;
	} catch (Exception & e) {
		throw Exception("genereren van %d willekeurige namen uit tabel %s: %s", count, table.c_str(), e.what());
	}
}

vector<string> GameData::random_locations(unsigned count) const {
	return this->random_names("Locaties", count);
}

vector<string> GameData::random_objects(unsigned count) const {
	return this->random_names("Objecten", count);
}

vector<string> GameData::random_enemies(unsigned count) const {
	return this->random_names("Vijanden", count);
}

string GameData::random_location() const {
	return this->random_locations(1)[0];
}

string GameData::random_object() const {
	return this->random_objects(1)[0];
}

string GameData::random_enemy() const {
	return this->random_enemies(1)[0];
}