YAPOG  0.0.1
Yet Another Pokemon Online Game
Chat.cpp
Go to the documentation of this file.
2 
3 namespace yap
4 {
6  : chatmanager_ (new ChatManager ())
7  , chatcommand_ (new ChatCommand ())
8  , offset_ (0)
9  , index_ (0)
10  , chan_ (0)
11  , entry_ ("")
12  , output_ ("")
13  , buffer_ ()
14  , history_ ()
15  , isCommand_ (false)
16  {
17  }
18 
20  : chatmanager_ (new ChatManager ())
21  , chatcommand_ (new ChatCommand ())
22  , offset_ (0)
23  , index_ (0)
24  , chan_ (0)
25  , entry_ ("")
26  , output_ ("")
27  , buffer_ ()
28  , history_ (*(new BufferType ()))
29  , isCommand_ (false)
30  {
31  SetBuf (b);
32  }
33 
35  {
36  delete (chatmanager_);
37  chatmanager_ = nullptr;
38  delete (chatcommand_);
39  chatcommand_ = nullptr;
40  }
41 
43  {
44  BufferType* tmp = new BufferType;
45  entry_ = b;
46  String w;
47  std::stringstream ss (entry_);
48 
49  while (ss >> w)
50  tmp->Add (w);
51 
52  buffer_ = *tmp;
53  }
54 
55  void Chat::IncOff ()
56  {
57  if (history_.Count () < HISTORYMAX)
58  {
60  offset_++;
61  }
62  else
63  history_[offset_++] = entry_;
64 
65  if (offset_ == HISTORYMAX)
66  offset_ = 0;
67  index_ = offset_;
68  }
69 
71  {
74  }
75 
76  std::pair<bool, String> Chat::TestHistoryChecker ()
77  {
78  if (isCommand_ &&
79  StringHelper::CompareString (buffer_[0], String ("/up")) == 0)
80  return GetUpHistory ();
81  if (isCommand_ &&
82  StringHelper::CompareString (buffer_[0], String ("/down")) == 0)
83  return GetDownHistory ();
84 
85  return std::make_pair (false, "");
86  }
87 
89  {
91  std::pair<bool, String> upOrDown = TestHistoryChecker ();
92 
93  if (buffer_.IsEmpty ())
94  {
95  ToEcho (entry_);
96  return "";
97  }
98 
99  if (upOrDown.first)
100  return upOrDown.second;
101 
102  IncOff ();
103 
104  Check ();
105  if (isCommand_ &&
106  StringHelper::CompareString (buffer_[0], String ("/history")) == 0)
107  return GetStringHistory ();
108 
109  if (isCommand_)
110  {
112  chatcommand_->GetCmd (buffer_[0].substr (1).c_str ()));
114  }
115  else
116  ToEcho (entry_);
117  return "";
118  }
119 
121  {
123  }
124 
125  std::pair<bool, String> Chat::GetUpHistory ()
126  {
127  UInt32 size = history_.Count ();
128 
129  if (size == 0)
130  return std::make_pair (true, "");
131  if (--index_ >= size)
132  index_ = size - 1;
133 
134  return std::make_pair (true, (index_ >= 0) ? history_[index_] : "");
135  }
136 
137  std::pair<bool, String> Chat::GetDownHistory ()
138  {
139  UInt32 size = history_.Count ();
140 
141  if (size == 0)
142  return std::make_pair (true, "");
143  if (++index_ >= size)
144  index_ = 0;
145 
146  return std::make_pair (true, (index_ < size) ? history_[index_] : "");
147  }
148 
150  {
151  String stringtosend = "";
152 
153  stringtosend += "History :\r\n";
154  for (size_t i = 0; i < history_.Count () - 1; i++)
155  stringtosend += history_[i] + "\r\n";
156  stringtosend += history_[history_.Count () - 1];
157 
158  return stringtosend;
159  }
160 
162  {
163  BufferType bufftosend;
164 
165  std::cout << history_.Count () << std::endl;
166  bufftosend.Add("History :\r\n");
167  for (size_t i = 0; i < history_.Count () - 1; i++)
168  bufftosend.Add(history_[i] + "\r\n");
169  bufftosend.Add(history_[history_.Count () - 1]);
170 
171  return bufftosend;
172  }
173 
175  {
176  return chatmanager_->Cd[TabNb]->GetName ();
177  }
178 
180  {
181  return chatmanager_->Cd.Count ();
182  }
183 
185  {
186  return chatmanager_->TabNb;
187  }
188 
190  {
191  return history_;
192  }
193 
195  {
196  return isCommand_;
197  }
198 
199  void Chat::Check ()
200  {
201  isCommand_ = (buffer_.Count () > 0 && entry_[0] == '/');
202  }
203 } // namespace yap