-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction_parser.cpp
More file actions
148 lines (144 loc) · 5.28 KB
/
action_parser.cpp
File metadata and controls
148 lines (144 loc) · 5.28 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
#include "action_parser.h"
#include "database.h"
#include "game_server.h"
#include "common.h"
#include <iostream>
#include "room.h"
#include "item.h"
using namespace std;
bool ActionParser::handleInput(Player *p, string command)
{
if(command == "help")
{
server->printToUser(p, "Commands available:");
server->printToUser(p, " -go [dir] (north, east, south, west)");
server->printToUser(p, " -check (Check what items are in the room)");
server->printToUser(p, " -inventory (See what items you have)");
server->printToUser(p, " -get [object name](Pick up items)");
server->printToUser(p, " -drop [object name] (Drop an item from your backpack)");
server->printToUser(p, " -quit");
return false;
} else if(command.find("inventory ") == 0)
{
if(p->getInventory().empty()) {
//check if player has items in their container
server->printToUser(p, "It looks like your backpack is empty :(");
} else
{
server->printToUser(p, "Your items: ");
//loop through items in the player's container
set<Item*>::iterator it;
auto inventory = p->getInventory();
for(it = inventory.begin(); it != inventory.end(); it++)
{
server->printToUser(p, "- " + (*it)->getName());
}
}
return true;
} else if(command.find("go ") == 0)
{
server->printToUser(p, "Direction to go: " + command.substr(3));
//check direction
if(p->getRoom() != NULL)
{
Room *adj = p->getRoom()->getAdjacent(command.substr(3));
//get the container and if the container is empty
if(adj == NULL)
{
server->printToUser(p, p->getUsername() + " tried to go a dead end in dir " + command.substr(3));
server->printToUser(p, "That's a dead end");
//server->printToUser(p, output) << endl;
} else
{
server->printToUser(p, "SUCCESS");
if(p->getRoom()->roomhaveReq() && p->reqisPassed())
{
p->setRoom(adj);
server->printToUser(p, "You're in " + p->getRoom()->getTitle());
server->printToUser(p, p->getRoom()->getDesc());
//server->printToUser(p, p->getRoom()->getDesc());
} else if (!p->getRoom()->roomhaveReq()){
p->setRoom(adj);
} else {
server->printToUser(p, "Cannot go this way.");
}
}
return true;
} else
{
server->printToUser(p, "That's not a direction");
server->printToUser(p, "Type 'help' for commands");
return true;
}
} else if(command.find("check ") == 0)
{
server->printToUser(p, "In " + p->getRoom()->getTitle());
server->printToUser(p, p->getRoom()->getDesc());
//check if there are items in container
if(p->getRoom()->getItems().empty())
{
server->printToUser(p, "There are no items in here ");
return true;
} else
{
server->printToUser(p, "Items in this room: ");
//loop through items in the room
set<Item*>::iterator it;
auto items = p->getRoom()->getItems();
for(it = items.begin(); it != items.end(); it++)
{
server->printToUser(p, "- " + (*it)->getName());
}
return true;
}
} else if(command.find("get ") == 0)
{
//loop through items in room
auto items = p->getRoom()->getItems();
set<Item*>::iterator it;
for(it = items.begin(); it != items.end(); it++)
{
//TODO change findItemOnFloor
if(p->getRoom()->findItemOnFloor("command") != NULL)
{
server->printToUser(p, "You grabbed a ");
p->addItemToInventory(*it);
p->getRoom()->dropItemFromFloor(*it);
return true;
}
}
return true;
} else if(command.find("drop ") == 0)
{
bool in = false;
// Pass in item name: if(p->isItemInInventory() == false)
//TODO put something in there after user types command
if(p->isItemInInventory("command") == false)
{
//TODO fix what the user puts in for item in command
server->printToUser(p, "There is no command in your inventory");
// server->printToUser(p, "There is no " + item->getName() + " in your inventory");
return true;
} else
{
set<Item*>::iterator it;
auto inventory = p->getInventory();
for(it = inventory.begin(); it != inventory.end(); it++)
{
if((*it)->getName() == "command")
{
server->printToUser(p, "You dropped a ");
p->getRoom()->addItemToFloor(*it);
p->dropItem((*it)->getName());
}
}
in = true;
return true;
}
if(!in)
{
server->printToUser(p, "You don't have a BLANK in your backpack.");
}
}
return false;
}