YAPOG  0.0.1
Yet Another Pokemon Online Game
ChatCommand.cpp
Go to the documentation of this file.
3 
4 namespace yap
5 {
7  : tab_ (new MyCmdType[NBCMDS + NBCMDSLOC])
8  , command_ (&ChatCommand::Unknown)
9  {
10  MyCmdType tabs[] =
11  {
12  {"help", &ChatCommand::Help, nullptr},
13  {"trade", &ChatCommand::Trade, nullptr},
14  {"echo", &ChatCommand::Echo, nullptr}
15  };
16  MyCmdType tabsloc[] =
17  {
22  };
23 
24  for (unsigned i = 0; i < NBCMDSLOC; i++)
25  tab_[i] = tabsloc[i];
26 
27  for (unsigned i = 0; i < NBCMDS; i++)
28  tab_[NBCMDSLOC + i] = tabs[i];
29  }
30 
32  {
33  delete[] tab_;
34  tab_ = nullptr;
35  }
36 
37  std::pair<
38  std::pair<bool, UInt32>,
39  std::pair<UInt32, BufferType>> MyPair (bool b,
40  UInt32 fct,
41  UInt32 chan,
42  BufferType bt)
43  {
44  return std::make_pair (
45  std::make_pair (b, fct),
46  std::make_pair (chan, bt));
47  }
48 
50  {
51  BufferType bt;
52  bt.Add ("Argument is missing.");
53 
54  return MyPair (false, NBTOTCMDS + 1, 0, bt);
55  }
56 
58  {
59  BufferType bt;
60  bt.Add ("Too much argument.");
61 
62  return MyPair (false, NBTOTCMDS + 1, 0, bt);
63  }
64 
66  {
67  BufferType bt;
68 
69  if (b.Count () > nbarg)
70  return TooMuchArg ();
71  if (b.Count () < nbarg)
72  return ArgMiss ();
73 
74  bt.Add ("Test argument failed.");
75 
76  return MyPair (false, NBTOTCMDS + 1, 0, bt);
77  }
78 
80  {
81  DisplayType dt;
82  BufferType bt;
83 
84  bt.Add ("Command list: ");
85  bt.Add ("/help\t\t: to display command list");
86  bt.Add ("/trade x\t: to trade with the player x");
87 
88  return MyPair (false, NBTOTCMDS + 1, 1, bt);
89 
90  return dt;
91  }
92 
94  {
95  DisplayType toret;
96  UInt32 channb = StringHelper::Parse<int> (b[b.Count () - 1]);
97  BufferType bt;
98 
99  b.RemoveBack ();
100  if (b.Count () > 0)
101  {
102  if (b[0].compare("/echo") == 0)
103  {
104  if (b.Count () == 1)
105  return ArgMiss ();
106  else
107  for (size_t i = 1; i < b.Count (); i++)
108  bt.Add (b[i]);
109  }
110  else
111  bt.Add (b[0]);
112  }
113 
114  toret = MyPair (false, NBTOTCMDS + 1, channb, bt);
115 
116  return toret;
117  }
118 
120  {
121  BufferType bt;
122 
123  bt.Add ("Command " + s[0].substr (1) + " not exist.");
124 
125  return MyPair (false, NBTOTCMDS + 1, 0, bt);
126  }
127 
129  {
130  BufferType bt;
131  name.RemoveBack ();
132 
133  if (name.Count () != 2)
134  return TestArg (name, 2);
135 
136  bt.Add ("Trying to trade with " + name[1] + ".");
137 
138  return MyPair (false, NBTOTCMDS + 1, 0, bt);
139  }
140 
142  {
143  BufferType bt;
144  b.RemoveBack ();
145 
146  if (b.Count () != 2)
147  return TestArg (b, 2);
148 
149  bt.Add (b[1]);
150 
151  return MyPair (true, 0, 0, bt);
152  }
153 
155  {
156  BufferType bt;
157  b.RemoveBack ();
158 
159  if (b.Count () != 2)
160  return TestArg (b, 2);
161 
162  bt.Add (b[1]);
163 
164  return MyPair (true, 1, 0, bt);
165  }
166 
168  {
169  BufferType bt;
170  b.RemoveBack ();
171 
172  if (b.Count () != 2)
173  return TestArg (b, 2);
174 
175  bt.Add ("Channel " + b[1] + " has been added.");
176  bt.Add (b[1]);
177 
178  return MyPair (true, 2, 0, bt);
179  }
180 
182  {
183  BufferType bt;
184  b.RemoveBack ();
185 
186  if (b.Count () != 2)
187  return TestArg (b, 2);
188 
189  bt.Add ("Channel : " + b[1] + " has been removed.");
190  bt.Add (b[1]);
191 
192  return MyPair (true, 3, 0, bt);
193  }
194 
196  {
197  command_ = cmd;
198  }
199 
200  func ChatCommand::GetCmd (const char *pString)
201  {
202  String s (pString);
203 
204  for (unsigned i = 0; i < NBCMDS + NBCMDSLOC; i++)
205  if (StringHelper::CompareString(tab_[i].PtrString, s) == 0)
206  return tab_[i].PtrFunc;
207 
208  return &ChatCommand::Unknown;
209  }
210 
212  BufferType* b,
213  ChatManagerType* cm,
214  ChatDisplayer* cd)
215  {
216  BufferType bt;
217 
218  if (b->Count() > 1)
219  bt.Add ("Too much argument.");
220  if (b->Count() == 1)
221  {
222  String chanNb = (*b)[0];
223 
224  if (StringFilter::IsNumeric (chanNb))
225  {
226  std::istringstream buf (chanNb);
227  UInt32 tmp = 0;
228  buf >> tmp;
229  if (tmp >= 0 && tmp < cm->Count)
230  {
231  cm->TabNb = tmp;
232  b->RemoveBack ();
233  }
234  else
235  bt.Add (tmp + " is not an tab number.");
236  }
237  else
238  bt.Add ("Bad argument.");
239  }
240 
241  return bt;
242  }
243 
245  BufferType* b,
246  ChatManagerType* cm,
247  ChatDisplayer* cd)
248  {
249  BufferType bt;
250 
251  if (b->Count() > 1)
252  bt.Add ("Too much argument.");
253  if (b->Count() == 1)
254  {
255  String chanNb = (*b)[0];
256 
257  if (StringFilter::IsNumeric (chanNb))
258  {
259  std::istringstream buf (chanNb);
260  UInt32 tmp = 0;
261  buf >> tmp;
262  if (tmp >= 0 && tmp < cd->GetChanNb ())
263  {
264  (*channb) = 0;
265  cm->ChanNb = tmp;
266  b->RemoveBack ();
267  }
268  else
269  bt.Add (tmp + " is not an chan number.");
270  }
271  else
272  bt.Add ("Bad argument.");
273  }
274 
275  return bt;
276  }
277 
279  BufferType* b,
280  ChatManagerType* cm,
281  ChatDisplayer* cd)
282  {
283  BufferType bt;
284 
285  if (b->Count() > 2)
286  bt.Add ("Too much argument.");
287  if (b->Count() == 2)
288  {
289  String chanNb = (*b)[1];
290 
291  if (StringFilter::IsNumeric (chanNb))
292  {
293  std::istringstream buf (chanNb);
294  UInt32 tmp = 0;
295  buf >> tmp;
296  if (tmp >= 0 && tmp < cd->GetChanNb())
297  {
298  cd->AddChan (tmp);
299  b->RemoveBack ();
300  }
301  else
302  bt.Add (tmp + " is not an chan number.");
303  }
304  else
305  bt.Add ("Bad argument.");
306  }
307 
308  return bt;
309  }
310 
311 
313  BufferType* b,
314  ChatManagerType* cm,
315  ChatDisplayer* cd)
316  {
317  BufferType bt;
318 
319  if (b->Count() > 2)
320  bt.Add ("Too much argument.");
321  if (b->Count() == 2)
322  {
323  String chanNb = (*b)[1];
324 
325  if (StringFilter::IsNumeric (chanNb))
326  {
327  std::istringstream buf (chanNb);
328  UInt32 tmp = 0;
329  buf >> tmp;
330  if (tmp >= 0 && tmp < cd->GetChanNb())
331  {
332  cd->RmChan (tmp);
333  b->RemoveBack ();
334  }
335  else
336  bt.Add (tmp + " is not an chan number.");
337  }
338  else
339  bt.Add ("Bad argument.");
340  }
341 
342  return bt;
343  }
344 
346  {
347  BufferType bt;
348 
350 
351  DisplayType response (
352  cm->Request.IsEmpty () ?
353  MyPair (false, NBTOTCMDS + 1, 0, bt) :
354  (this->*command_) (cm->Request));
355 
356  ChatDisplayer* cd = cm->Cd[cm->TabNb];
357 
358  if (response.first.first)
359  bt = (this->*(tab_[response.first.second].PtrFuncloc))
360  (&response.second.first, &response.second.second, cm, cd);
361  if (bt.Count () == 0)
362  {
363  for (UInt32 i = 0; i < cm->Count; i++)
364  cm->Cd[i]->AddToChan (response.second.first, response.second.second);
365 
366  // Switch Tab detected.
367  if (response.first.second == 0)
368  {
370  return cm->Cd[cm->TabNb]->DisplayTab ();
371  }
372  }
373  else
374  cd->AddToChan (0, bt);
376  return cd->Display ();
377  }
378 } // namespace yap