Debian Codegraph @

plugins/dm.conversation/ConversationCommand.cpp

raw
1 #include "ConversationCommand.h"
2
3 #include "string/convert.h"
4 #include "string/replace.h"
5
6 #include "ConversationCommandLibrary.h"
7
8 namespace conversation {
9
10 ConversationCommand::ConversationCommand() :
11 type(-1), // invalid id
12 actor(-1),
13 waitUntilFinished(true)
14 {}
15
16 std::string ConversationCommand::getArgument(int index) const {
17 ArgumentMap::const_iterator i = arguments.find(index);
18
19 return (i != arguments.end()) ? i->second : "";
20 }
21
22 std::string ConversationCommand::getSentence() const {
23 // Get the command description for this type
24 try {
25 const ConversationCommandInfo& cmdInfo =
26 ConversationCommandLibrary::Instance().findCommandInfo(type);
27
28 // Get the sentence and fill in the placeholders, if any
29 std::string sentence = cmdInfo.sentence;
30
31 int counter = 1;
32
33 for (ConversationCommandInfo::ArgumentInfoList::const_iterator i = cmdInfo.arguments.begin();
34 i != cmdInfo.arguments.end(); ++i, ++counter)
35 {
36 std::string needle = "[arg" + string::to_string(counter) + "]";
37 std::string replacement = getArgument(counter);
38
39 // Check for a bool
40 /*if (i->second.type == "b") {
41 replacement = (i->second.value.empty()) ? "no" : "yes";
42 }*/
43
44 string::replace_all(sentence, needle, replacement);
45 }
46
47 return sentence;
48 }
49 catch (std::runtime_error&) {
50 return "Unrecognised command.";
51 }
52 }
53
54 } // namespace conversation