-
Notifications
You must be signed in to change notification settings - Fork 57
/
command_queue.cpp
294 lines (246 loc) · 7.56 KB
/
command_queue.cpp
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
292
293
294
/*
Queues #commands: handles the muti-command line format and input of parameters.
Extracted from user_menu.cpp so that others can create queues.
Each Line object has two or more fields, the field separator is "||". The
first field is the text tag, the remaining field or fields are the
associated commands.
Commands can prompt for user input. If the command contains text inside "<>",
e.g. "some text <more text> some other text" then the text inside the "<>" will
be used as a prompt and the "<...>" replaced by the text entered by the user.
You can include as many input prompts as you wish.
pjbroad/bluap January 2013
*/
/* To Do:
- block use of #suicide #reset #killme #change_pass - may be store in file
- optionally put commands into previous command buffer
- reuse entered text for a line if prompt repeated - vinoveritas suggestion
- Need line <rule> <...>
repeats 0-n times until "." entered.
Can also use for [<number>]
*/
#include <sstream>
#include "asc.h"
#include "chat.h"
#include "gl_init.h"
#include "text.h"
#include "translate.h"
#include "command_queue.hpp"
namespace CommandQueue
{
//
// Construct a command object from a command string. Parsing for
// input fields and splitting into text/input sections.
//
Command::Command(const std::string &command_text)
{
std::string::size_type from_index = 0;
std::string::size_type to_index = 0;
std::string::size_type len = 0;
std::string start_str = "<";
std::string end_str = ">";
invalid_command = false;
// loop extracting command and parameter sections
// format is "text <parameter name> text <parameter name> text" e.t.c
while ((to_index = command_text.find(start_str, from_index)) != std::string::npos)
{
if ((len = to_index-from_index) > 0)
text_segments.push_back(command_text.substr(from_index, len));
from_index = to_index + start_str.size();
if ((to_index = command_text.find(end_str, from_index)) != std::string::npos)
{
if ((len = to_index-from_index) > 0)
param_prompts.push_back(command_text.substr(from_index, len));
from_index = to_index + end_str.size();
}
else
{
text_segments.clear();
param_prompts.clear();
text_segments.push_back(command_text);
invalid_command = true;
return;
}
}
if ((len = command_text.size()-from_index) > 0)
text_segments.push_back(command_text.substr(from_index, len));
if (text_segments.empty() && param_prompts.empty())
invalid_command = true;
} // end Command::Command()
//
// Given the paramters, contruct the command to issue from
// the text sections and parameter values.
//
void Command::action(const std::vector<std::string> ¶ms) const
{
// log to the user an invalid command, formatting error
if (invalid_command)
{
LOG_TO_CONSOLE(c_red1, um_invalid_command_str);
return;
}
// append command text + parameter + text + paramter e.t.c.
std::ostringstream command_text;
for (size_t i=0; i<text_segments.size(); i++)
{
command_text << text_segments[i];
if (params.size() > i)
command_text << params[i];
}
for (size_t i=text_segments.size(); i<params.size(); i++)
command_text << params[i];
// issue the command
size_t command_len = command_text.str().size() + 1;
char temp[command_len];
safe_strncpy(temp, command_text.str().c_str(), command_len);
parse_input(temp, strlen(temp));
}
//
// Echo the command to the console, a menu window option
//
void Command::echo(void) const
{
// append command text + parameter + text + paramter e.t.c.
std::ostringstream command_text;
for (size_t i=0; i<text_segments.size(); i++)
{
command_text << text_segments[i];
if (param_prompts.size() > i)
command_text << "<" << param_prompts[i] << ">";
}
for (size_t i=text_segments.size(); i<param_prompts.size(); i++)
command_text << "<" << param_prompts[i] << ">";
LOG_TO_CONSOLE(c_grey1, command_text.str().c_str());
// log to the user an invalid command, formatting error
if (invalid_command)
LOG_TO_CONSOLE(c_red1, um_invalid_command_str);
}
//
// Initialise the command queue
//
Queue::Queue(void) : last_time(0)
{
init_ipu(&ipu, -1, MAX_TEXT_MESSAGE_LENGTH, 3, 30, cancel_handler, input_handler);
ipu.data = static_cast<void *>(this);
}
//
// If the command queue is not empty, process the next command.
//
void Queue::process(bool just_echo)
{
// if required, print all the commands to the console emptying the queue
while (just_echo && !commands.empty())
{
commands.front().echo();
commands.pop();
}
if (commands.empty())
return;
// delay consecutive commands by a small amount to avoid spamming
Uint32 curr_time = SDL_GetTicks();
if ((curr_time >= last_time) && ((curr_time - wait_time_ms) < last_time))
return;
// if the command needs parameter(s) prompt and wait for input
if (params.size() < commands.front().get_prompts().size())
{
// if the input window is already open, continue waiting for input
if (get_show_window(ipu.popup_win))
return;
// open the input window and continue waiting for input
display_popup_win(&ipu, commands.front().get_prompts()[params.size()].c_str());
centre_popup_window(&ipu);
return;
}
// we have any needed parameters so action the command and remove it form the queue
commands.front().action(params);
commands.pop();
params.clear();
last_time = curr_time;
}
//
// The input popup window cancel callback
//
void Queue::cancel(void)
{
if (commands.empty())
return;
while (!commands.empty())
commands.pop();
params.clear();
}
//
// If the user menu window is closed, clear the queue
//
void Queue::clear(void)
{
cancel();
hide_window(ipu.popup_win);
}
//
// Set the delay between executing commands on a single user menu line
//
void Queue::set_wait_time_ms(Uint32 time_ms)
{
if (time_ms > min_wait_time_ms)
wait_time_ms = time_ms;
else
wait_time_ms = min_wait_time_ms;
}
// protect the server - the minimum wait time, in milli-seconds, between executing commands
const Uint32 Queue::min_wait_time_ms = 500;
Uint32 Queue::wait_time_ms = Queue::min_wait_time_ms;
//
// construct a menu line from a text string
//
Line::Line(const std::string &line_text)
{
std::string::size_type from_index = 0;
std::string::size_type to_index = 0;
std::string delim = "||";
std::string::size_type len = 0;
std::vector<std::string> fields;
// parse the line extracting the fields separated by the delimitor
while ((to_index = line_text.find(delim, from_index)) != std::string::npos)
{
if ((len = to_index-from_index) > 0)
fields.push_back(line_text.substr(from_index, len));
from_index = to_index + delim.size();
}
if ((len = line_text.size()-from_index) > 0)
fields.push_back(line_text.substr(from_index, len));
// a line with no fields is treated as context menu separator
if (fields.empty())
{
text = "--";
return;
}
// a line must always have at least two fields, the text and a command
if (fields.size() == 1)
{
text = um_invalid_line_str;
fields.clear();
return;
}
// the first field is the menu text, remaining fields are the assiociated commands
text = fields[0];
for (size_t i=1; i<fields.size(); i++)
command_list.push_back(Command(fields[i]));
} // end Line::Line()
//
// action the selected menu options
//
void Line::action(Queue &cq) const
{
for (size_t i=0; i<command_list.size(); i++)
cq.add(command_list[i]);
}
} // end CommandQueue namespace
//
// External Interface functions
//
extern "C"
{
void set_command_queue_wait_time_ms(Uint32 wait_time_ms)
{
CommandQueue::Queue::set_wait_time_ms(wait_time_ms);
}
}