aboutsummaryrefslogtreecommitdiff
path: root/frontend/GameData.cpp
blob: edcd8542d45099113a707dbeca875cca18396379 (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
#include <memory>
#include <unordered_map>

#include "backend/Enemy.h"
#include "backend/EnemyFactory.h"
#include "backend/LocationFactory.h"
#include "backend/Object.h"
#include "backend/ObjectFactory.h"

#include "GameData.h"

using namespace std;

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

GameData::GameData() {
	this->db = make_unique<DB>("kerkersendraken.db");
}

Enemy * GameData::create_enemy(const string & name) {
	Enemy * enemy = EnemyFactory::create_enemy();
	// TODO: fill fields
	return enemy;
}

static const unordered_map<string, ObjectType> type_map = {
	{ "teleportatiedrank", ObjectType::CONSUMABLE },
	{ "ervaringsdrank", ObjectType::CONSUMABLE },
	{ "levenselixer", ObjectType::CONSUMABLE },
	{ "wapenrusting", ObjectType::ARMOR },
	{ "wapen", ObjectType::WEAPON },
	{ "goudstukken", ObjectType::GOLD },
};

Object * GameData::create_object(const string & name) {
	static DBStatement query = this->db->prepare(R"(
		select
			type,
			omschrijving,
			minimumwaarde,
			maximumwaarde,
			bescherming
		from Objecten
		where lower(naam) = lower(?)
		limit 1
	)");
	query.reset()
		.bind(name)
	;
	
	try {
		auto row = query.row();
		string type = row.col<const char *>(0);
		if (!type_map.contains(type)) throw std::exception();
		return ObjectFactory::create_object({
			.name = name.c_str(),
			.description = row.col<const char *>(1),
			.type = type_map.at(type),
			.min_value = row.col<int>(2),
			.max_value = row.col<int>(3),
			.protection = row.col<int>(4),
		});
	} catch (...) {
		return ObjectFactory::create_object(name.c_str());
	}
}

Location * GameData::create_location(const string & name) {
	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 LocationFactory::create_location(row.col<const char *>(0), row.col<const char *>(1));
	} catch (...) {
		return 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();
}

vector<string> GameData::random_locations(unsigned count) {
	static DBStatement query = this->db->prepare(R"(
		select naam
		from Locaties
		order by random()
		limit ?
	)");
	query.reset()
		.bind(count)
	;

	vector<string> names = {};
	for (DBQueryRow & row : query.rows()) {
		names.push_back(row.col<const char *>(0));
	}
	return names;
}