YAPOG
0.0.1
Yet Another Pokemon Online Game
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
Chat.cpp
Go to the documentation of this file.
1
#include "
YAPOG/Game/Chat/Chat.hpp
"
2
3
namespace
yap
4
{
5
Chat::Chat
()
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
19
Chat::Chat
(
String
b)
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
34
Chat::~Chat
()
35
{
36
delete
(
chatmanager_
);
37
chatmanager_
=
nullptr
;
38
delete
(
chatcommand_
);
39
chatcommand_
=
nullptr
;
40
}
41
42
void
Chat::SetBuf
(
String
b)
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
{
59
history_
.
Add
(
entry_
);
60
offset_
++;
61
}
62
else
63
history_
[
offset_
++] =
entry_
;
64
65
if
(
offset_
==
HISTORYMAX
)
66
offset_
= 0;
67
index_
=
offset_
;
68
}
69
70
void
Chat::ToEcho
(
String
s)
71
{
72
chatcommand_
->
SetCommand
(&
ChatCommand::Echo
);
73
chatmanager_
->
Request
.
Add
(s);
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
88
String
Chat::Parse
()
89
{
90
chatmanager_
->
Request
.
Clear
();
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
{
111
chatcommand_
->
SetCommand
(
112
chatcommand_
->
GetCmd
(
buffer_
[0].substr (1).c_str ()));
113
chatmanager_
->
Request
=
buffer_
;
114
}
115
else
116
ToEcho
(
entry_
);
117
return
""
;
118
}
119
120
ResponseType
Chat::Exec
()
121
{
122
return
chatcommand_
->
ExecCmd
(
chatmanager_
);
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
149
String
Chat::GetStringHistory
()
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
161
BufferType
Chat::GetBufHistory
()
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
174
String
Chat::GetTabName
(
UInt32
TabNb)
175
{
176
return
chatmanager_
->
Cd
[TabNb]->GetName ();
177
}
178
179
UInt32
Chat::GetTabCount
()
180
{
181
return
chatmanager_
->
Cd
.
Count
();
182
}
183
184
UInt32
Chat::GetTabNb
()
185
{
186
return
chatmanager_
->
TabNb
;
187
}
188
189
BufferType
Chat::GetHistory
()
190
{
191
return
history_
;
192
}
193
194
bool
Chat::GetIsCommand
()
195
{
196
return
isCommand_
;
197
}
198
199
void
Chat::Check
()
200
{
201
isCommand_
= (
buffer_
.
Count
() > 0 &&
entry_
[0] ==
'/'
);
202
}
203
}
// namespace yap
YAPOG
src
YAPOG
Game
Chat
Chat.cpp
Generated on Mon Sep 17 2012 22:24:24 for YAPOG by
1.8.1.1