-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_speak_parser.cpp
More file actions
211 lines (178 loc) · 7.04 KB
/
build_speak_parser.cpp
File metadata and controls
211 lines (178 loc) · 7.04 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
#include<iostream>
#include<sstream>
#include "database.h"
#include "common.h"
#include "game_server.h"
#include "build_speak_parser.h"
using namespace std;
bool BuildParser::handleInput(Player *p, string command)
{
//command patterns
regex createPattern1 ("create (.+) (.+) desc (.*)");
regex connectPattern ("connect room (.+) to (.+) via (.+)");
regex putPattern ("put item (.+) in room (.+)");
regex setItemTypePattern ("set item (.+) (.+)");
regex setStartingRoomPattern ("set room (.+) starting");
regex setNotStartingRoomPattern("set room (.+) not starting");
regex deleteRoomPattern("delete room (.+)");
smatch m;
if(command == "help"){
server->printToUser(p, "Room building commands: \n");
server->printToUser(p, "To create a room/item(will be unique by default) - create (room/item) <name> desc <description>");
server->printToUser(p, "To connect rooms - connect room <roomname> to <roomname> via (south, north, west, east)");
server->printToUser(p, "To put item - put item <itemname> in room <roomname>");
server->printToUser(p, "To set item type - set item <itemname> (unique, perplayer)");
server->printToUser(p, "To set starting room - set room <roomname> starting");
server->printToUser(p, "To set room not starting room- set room <roomname> not starting");
return false;
server->printToUser(p, "To delete a room - delete room <Roomname>");
}
if(regex_match(command, m, setStartingRoomPattern)){
string name = m[1];
if(db->findRoomByName(name)){
db->findRoomByName(name)->setStartingRoom(true);
ostringstream os;
os << "You set the starting room: " << name;
server->printToUser(p, os.str());
return true;
}else{
server->printToUser(p, "Room not found!");
return true;
}
}
if(regex_match(command, m, setNotStartingRoomPattern)){
string name = m[1];
if(db->findRoomByName(name)){
db->findRoomByName(name)->setStartingRoom(false);
ostringstream os;
os << "You set the room: " << name << " not starting room";
server->printToUser(p, os.str());
return true;
}else
{
server->printToUser(p, "Room not found!");
return true;
}
}
//Creation commands(items and rooms):
//Format 1: create room/item called X and described by Y
if (regex_match(command, m ,createPattern1)) {
cout << "CREATING ROOM " << endl;
string name = m[2];
string description = m[3];
if (p->getCanBuild()) {
if (m[1] == "room") {
if(db->findRoomByName(name) == NULL){
Room* tempR = new Room(name, description);
tempR->save();
ostringstream os1;
os1 << "You created a room: " << name;
server->printToUser (p, os1.str());
ostringstream os2;
os2 << "Description for this room: " << description;
server->printToUser (p, os2.str());
return true;
}else{
server->printToUser(p, "Room already exists!");
return true;
}
}
else if (m[1] == "item") {
if(db->findItemByName(name) == NULL){
Item* tempI = new Item(name, description,p->getUsername(), UNIQUE);
tempI->save();
ostringstream os1, os2;
os1 << "You created an Item: " << name;
os2 << "Description for this item: " << description;
server->printToUser(p, os1.str());
server->printToUser(p, os2.str());
return true;
}else{
server->printToUser(p, "Item already exists!");
return false;
}
}
}
else {
server->printToUser(p, "You cannot build for now!");
return true;
}
}
//Put item command
//Format: put item X in room Y
if (regex_match(command, m, putPattern)) {
string item = m[1];
string room = m[2];
//Add item to room and print
if(db->findItemByName(item) != NULL && db->findRoomByName(room) != NULL){
db->findRoomByName(room)->addItemToFloor(db->findItemByName(item));
ostringstream os;
os << "You put item " << item << " into room " << room;
server->printToUser(p, os.str());
return true;
}else{
server->printToUser(p, "There is no such item or room");
return true;
}
}
//Connect command
//Format: connect room X to Y via DIR
if (regex_match(command, m, connectPattern)) {
string room1 = m[1];
string room2 = m[2];
string dir = m[3];
if(dir != "north" && dir != "south" && dir != "east" && dir != "west"){
server->printToUser(p, "Please enter a valid direction(north, east, south, west)");
return true;
}else{
if(db->findRoomByName(room1) != NULL && db->findRoomByName(room2) != NULL){
db->findRoomByName(room1)->setAdjacent(dir, db->findRoomByName(room2));
ostringstream os;
os <<"You connected room " << room1 << " and room " << room2 << " via " << dir;
server->printToUser(p, os.str());
return true;
}else{
server->printToUser(p, "Room doesn't exist!");
return true;
}
}
}
if(regex_match(command, m, setItemTypePattern)){
string item = m[1];
string type = m[2];
ITEM_TYPE t;
if(type == "unique"){
t = UNIQUE;
}else if(type == "perplayer"){
t = PERPLAYER;
}else{
server->printToUser(p, "Please enter a valid item type(unique,perplayer)");
return true;
}
if(db->findItemByName(item) != NULL){
if(db->findItemByName(item)->getType() == t){
ostringstream os;
os << "It's already "<< type << "!";
server->printToUser(p, os.str());
return true;
}else{
db->findItemByName(item)->setType(t);
ostringstream os;
os << "You set the type: " << type;
server->printToUser(p,os.str());
return true;
}
}else{
server->printToUser(p,"No such item!");
return true;
}
}
if(regex_match(command, m, deleteRoomPattern)){
string room = m[2];
string roomName;
if(db->findRoomByName(roomName) != NULL){
db->deleteRoom(db->findRoomByName(roomName)->getID());
}
}
return false;
}