-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.cpp
More file actions
291 lines (263 loc) · 7.67 KB
/
database.cpp
File metadata and controls
291 lines (263 loc) · 7.67 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "database.h"
#include "common.h"
#include <string>
#include <iostream>
#include <sstream>
#include "item.h"
#include "room.h"
#include "player.h"
using namespace std;
Database::Database(){
context = redisConnect("localhost",6379);
string lastid_str = read("lastid");
if(lastid_str == "")
lastid = -1;
else
lastid = stoi(lastid_str);
cout << "DB lastid = " << lastid << endl;
// TODO: load all items, players, and rooms into vectors
for(int i = 0; i <= lastid; ++i) {
string dataFromRedis = read(to_string(i));
//cout << "At index " << i << endl;
//If the first part of the string is player, then the object stored at that index is a player, parse and load a player
if(dataFromRedis.substr(0,6) == "player") {
players.push_back(read_lastid_player(i));
} else if(dataFromRedis.substr(0,4) == "item"){
items.push_back(read_lastid_item(i));
} else if(dataFromRedis.substr(0,4) == "room"){
rooms.push_back(read_lastid_room(i));
} else {
//There should never be anything in the database that can't be parsed, if this is printed there is an error
cout << "ERROR: COULD NOT PARSE DATABASE ENTRY AT ID " << i << endl;
}
}
}
//Sets/writes a value to an assigned KEY using redisContext
void Database::write(string key, string value){
redisReply *reply;
reply = (redisReply*) redisCommand(context,"SET %s %s", key.c_str(), value.c_str());
freeReplyObject(reply);
}
//Gets/reads value of an assigned KEY using redisContext
string Database::read(string key) const{
redisReply *reply;
reply = (redisReply*) redisCommand(context,"GET %s", key.c_str());
if(reply->type == 4) {
return "";
freeReplyObject(reply);
}
string s(reply->str);
freeReplyObject(reply);
return s;
}
Item * Database::read_lastid_item(int id){
Item *i = new Item("","","",UNIQUE);
string temp = read(to_string(id));
istringstream data(temp);
string name;
string desc;
string type;
string owner;
string line;
//The first line is just item, so it isn't needed
getline(data,line);
//The name of the object
getline(data,line);
name = line.substr(6);
i->setName(name);
//Description of the object
getline(data,line);
desc = line.substr(5);
i->setDesc(desc);
getline(data,line);
owner = line.substr(6);
i->setOwner(owner);
getline(data,line);
type = line;
//The type of the item is either PERPLAYER or UNIQUE.
if(line=="PERLAYER"){
i->setType((ITEM_TYPE)0);
}else{
i->setType((ITEM_TYPE)1);
}
return i;
}
Room * Database::read_lastid_room(int id){
Room * r = new Room("","");
string temp = read(to_string(id));
istringstream data(temp);
string title;
string desc;
string line;
string start;
//The first line is just "room" disregard the line
getline(data,line);
//Line with the title of the room
getline(data,line);
title = line.substr(7);
r->setTitle(title);
//Line with the description of the room
getline(data,line);
desc = line.substr(5);
r->setDesc(desc);
//Line that states whether or not it's the starting room
getline(data,line);
start = line;
bool b;
istringstream(start) >> b;
r->setStartingRoom(b);
string north;
string south;
string east;
string west;
getline(data,line);
north = line;
getline(data,line);
south = line;
getline(data,line);
east = line;
getline(data,line);
west = line;
r->setAdjacent("north",findRoomByName(north));
r->setAdjacent("south",findRoomByName(south));
r->setAdjacent("east",findRoomByName(east));
r->setAdjacent("west",findRoomByName(west));
return r;
}
Player * Database::read_lastid_player(int id){
Player * p = new Player("","",-1);
string temp = read(to_string(id));
istringstream data(temp);
string username;
string description;
string password;
string line;
//The first line in the database is the description that they are a player, ignore the first line
getline(data,line);
//Now the username is stored in the variable line
getline(data,line);
username = line.substr(10);
p->setUsername(username);
getline(data,line);
description = line.substr(5);
p->setDescription(description);
p->setID(id);
getline(data,line);
password = line;
p->setPassword(password);
return p;
}
void Database::increment_lastid(){
lastid++;
write("lastid",to_string(lastid));
}
void Database::clearDatabase(){
redisReply *reply;
reply = (redisReply*) redisCommand(context,"FLUSHALL");
freeReplyObject(reply);
lastid = 0;
}
void Database::deleteObject(int id){
redisCommand(context,"DEL %s", to_string(id).c_str());
}
void Database::deleteRoom(int id) {
deleteObject(id);
// delete room from vector
auto it = rooms.begin();
for(; it != rooms.end(); ++it) {
if((*it)->getID() == id) break;
}
if(it != rooms.end()) {
rooms.erase(it);
}
}
void Database::deleteItem(int id) {
deleteObject(id);
// delete item from vector
auto it = items.begin();
for(; it != items.end(); ++it) {
if((*it)->getID() == id) break;
}
if(it != items.end()) {
items.erase(it);
}
}
void Database::deletePlayer(int id) {
deleteObject(id);
// delete player from vector
auto it = players.begin();
for(; it != players.end(); ++it) {
if((*it)->getID() == id) break;
}
if(it != players.end()) {
players.erase(it);
}
}
void Database::addPlayer(Player *p) {
// possibly replace known player by this id
for(unsigned int i = 0; i < players.size(); i++) {
if(players[i]->getID() == p->getID()) {
players[i] = p;
return;
}
}
players.push_back(p);
}
Player* Database::findPlayerById(int id) {
for(unsigned int i = 0; i < players.size(); i++) {
if(players[i]->getID() == id) {
return players[i];
}
}
return NULL;
}
Player* Database::findPlayerByName(string name) {
for(unsigned int i = 0; i < players.size(); i++) {
if(players[i]->getUsername() == name) {
return players[i];
}
}
return NULL;
}
void Database::addItem(Item *it) {
// possibly replace known item by this name
for(unsigned int i = 0; i < items.size(); i++) {
if(items[i]->getName() == it->getName()) {
items[i] = it;
return;
}
}
items.push_back(it);
}
Item* Database::findItemByName(string name) {
for(unsigned int i = 0; i < items.size(); i++) {
if(items[i]->getName() == name) {
return items[i];
}
}
return NULL;
}
void Database::addRoom(Room *r) {
// possibly replace known room by this title
for(unsigned int i = 0; i < rooms.size(); i++) {
if(rooms[i]->getTitle() == r->getTitle()) {
rooms[i] = r;
return;
}
}
rooms.push_back(r);
}
Room* Database::findRoomByName(string name) {
for(unsigned int i = 0; i < rooms.size(); i++) {
if(rooms[i]->getTitle() == name) {
return rooms[i];
}
}
return NULL;
}
vector<Player*> Database::getPlayers() {
return players;
}
vector<Room*> Database::getRooms(){
return rooms;
}