File: ConversationCommand.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (54 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (5)
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
#include "ConversationCommand.h"

#include "string/convert.h"
#include "string/replace.h"

#include "ConversationCommandLibrary.h"

namespace conversation {

ConversationCommand::ConversationCommand() :
	type(-1), // invalid id
	actor(-1),
	waitUntilFinished(true)
{}

std::string ConversationCommand::getArgument(int index) const {
	ArgumentMap::const_iterator i = arguments.find(index);

	return (i != arguments.end()) ? i->second : "";
}

std::string ConversationCommand::getSentence() const {
	// Get the command description for this type
	try {
		const ConversationCommandInfo& cmdInfo =
			ConversationCommandLibrary::Instance().findCommandInfo(type);

		// Get the sentence and fill in the placeholders, if any
		std::string sentence = cmdInfo.sentence;

		int counter = 1;

		for (ConversationCommandInfo::ArgumentInfoList::const_iterator i = cmdInfo.arguments.begin();
			 i != cmdInfo.arguments.end(); ++i, ++counter)
		{
			std::string needle = "[arg" + string::to_string(counter) + "]";
			std::string replacement = getArgument(counter);

			// Check for a bool
			/*if (i->second.type == "b") {
				replacement = (i->second.value.empty()) ? "no" : "yes";
			}*/

			string::replace_all(sentence, needle, replacement);
		}

		return sentence;
	}
	catch (std::runtime_error&) {
		return "Unrecognised command.";
	}
}

} // namespace conversation