Debian Codegraph @

src/network/tomconnection.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include "tomconnection.h"
24 #include "room.h"
25 #include "playergamelistings.h"
26 #include "gamedata.h"
27 #include "serverlistdialog.h"
28 #include "codecwarndialog.h"
29 /* Note that Tom either has additional checks on this or simply that one cannot play against
30 * the same username meaning that sendMatchOffers may not be received. */
31
32
33 TomConnection::TomConnection(const ConnectionCredentials credentials)
34 : TygemConnection(credentials)
35 {
36 serverCodec = QTextCodec::codecForName(getCodecString());
37 if(!serverCodec)
38 {
39 new CodecWarnDialog(getCodecString());
40 serverCodec = QTextCodec::codecForLocale();
41 }
42 }
43
44 const char * TomConnection::getCodecString(void)
45 {
46 return "GB2312";
47 }
48
49 QString TomConnection::getPlaceString(void)
50 {
51 return "Tom: " + serverList[current_server_index]->name;
52 }
53
54 int TomConnection::requestServerInfo(void)
55 {
56 qDebug("Requesting Tom Server Info");
57 if(!openConnection(hostname, port, NOT_MAIN_CONNECTION))
58 {
59 qDebug("Can't get server info");
60 return -1;
61 }
62 unsigned int length = 0x94;
63 unsigned char * packet = new unsigned char[length]; //term 0x00
64 snprintf((char *)packet, length,
65 "GET /service/files/livebaduk3.cfg HTTP/1.1\r\n" \
66 "User-Agent: Tygem HTTP\r\n" \
67 "Host: duiyi.sports.tom.com\r\n" \
68 "Connection: Keep-Alive\r\n" \
69 "Cache-Control: no-cache\r\n\r\n");
70 #ifdef RE_DEBUG
71 for(int i = 0; i < length; i++)
72 printf("%02x", packet[i]);
73 printf("\n");
74 #endif //RE_DEBUG
75 if(write((const char *)packet, length) < 0)
76 {
77 qWarning("*** failed sending server info request");
78 return -1;
79 }
80 delete[] packet;
81
82 setState(Info);
83 return 0;
84 }
85
86 /* We need to write a tygem parser that doesn't need to be different for tom FIXME */
87 void TomConnection::handleServerInfo(unsigned char * msg, unsigned int length)
88 {
89 char * p = (char *)msg;
90 int i, j;
91 ServerItem * si;
92
93 #ifdef RE_DEBUG
94 for(i = 0; i < length; i++)
95 printf("%c", p[i]);
96 printf("\n");
97 #endif //RE_DEBUG
98
99 while(p < ((char *)msg + length))
100 {
101 while(p < ((char *)msg + length - 1) && p[0] != '\n')
102 p++;
103 p++;
104 //FIXME, check for size
105 if(strncmp(p, "/LIVE ", 6) == 0)
106 {
107 //same as for /SERVER but with only 2 [], name and ip
108 }
109 else if(strncmp(p, "/LECTURE ", 9) == 0)
110 {
111 //same as for /SERVER but with only 2 [], name and ip
112 }
113 else if(strncmp(p, "/SERVER ", 8) == 0 ||
114 strncmp(p, "/_SERVER ", 9) == 0) //whats the "_" FIXME
115 {
116 //TOM has only 2 [], name and ip
117 if(p[1] == '_')
118 p++;
119 p += 8;
120 unsigned char ipaddr[16];
121 unsigned char name[20];
122 i = 0;
123 while(i < 2 && p < ((char *)msg + length))
124 {
125 if(*p != '[')
126 {
127 qDebug("Server info parse error");
128 closeConnection();
129 return;
130 }
131 p++;
132 si = new ServerItem();
133 unsigned char * dest;
134 if(i == 1)
135 dest = ipaddr;
136 else if(i == 0)
137 dest = name;
138 j = 0;
139 while(*p != ']' && p < ((char *)msg + length))
140 {
141 if((i == 1 && j < 15) || (i == 0 && j < 19))
142 dest[j++] = *p;
143 p++;
144 }
145 if(i == 0 || i == 1)
146 {
147 dest[j++] = 0x00;
148 }
149 p += 2;
150 if(i == 1)
151 {
152 strncpy(si->ipaddress, (const char *)ipaddr, 16);
153 si->name = serverCodec->toUnicode((char *)name, strlen((char *)name));
154 qDebug("Server: %s", ipaddr);
155 serverList.push_back(si);
156 }
157 i++;
158 if(i == 7)
159 p--; //no space
160 }
161 }
162 else if(strncmp(p, "/ENCRYPT ", 9) == 0)
163 {
164 //FIXME
165 //[encode] [on]
166 }
167 }
168 /* We close here because this first time, its going to close
169 * anyway, and if we don't close here, we'll get an error
170 * and lose the object */
171 setState(Reconnecting);
172 closeConnection(false);
173
174 if(reconnectToServer() < 0)
175 {
176 qDebug("User canceled");
177 closeConnection(false);
178 setState(Canceled);
179 //if(dispatch)
180 // dispatch->onError(); //not great... FIXME
181 return;
182 }
183 //sendLogin();
184 }
185
186 QByteArray TomConnection::getTygemGameRecordQByteArray(GameData * game)
187 {
188 Room * room = getDefaultRoom();
189 const PlayerListing * black, * white;
190 int black_ordinal, white_ordinal;
191 char black_qualifier, white_qualifier;
192 QByteArray white_qualifier_string, black_qualifier_string;
193 unsigned char black_level, white_level;
194 unsigned short year;
195 unsigned char month, day, hour, minute, second;
196 secondsToDate(year, month, day, hour, minute, second);
197
198 int margin = 0;
199
200 black = room->getPlayerListing(game->black_name);
201 white = room->getPlayerListing(game->white_name);
202 sscanf(black->rank.toLatin1().constData(), "%d%c", &black_ordinal, &black_qualifier);
203 if(black_qualifier == 'k')
204 {
205 black_level = 0x12 - black_ordinal;
206 black_qualifier_string.append("\xbc\xb6");
207 }
208 else if(black_qualifier == 'p')
209 black_level = black_ordinal + 0x1a;
210 else
211 {
212 black_level = black_ordinal + 0x11;
213 //double check that this isn't the pro tag?
214 black_qualifier_string.append("\xb6\xce");
215 }
216 sscanf(white->rank.toLatin1().constData(), "%d%c", &white_ordinal, &white_qualifier);
217 if(white_qualifier == 'k')
218 {
219 white_level = 0x12 - white_ordinal;
220 white_qualifier_string.append("\xbc\xb6");
221 }
222 else if(white_qualifier == 'p')
223 white_level = white_ordinal + 0x1a;
224 else
225 {
226 white_level = white_ordinal + 0x11;
227 white_qualifier_string.append("\xb6\xce");
228 }
229
230 QByteArray string;
231 /* GIBOKIND and GAMELECNAME and maybe some others are missing
232 * from Tom record, so there's a question of whether that's only
233 * in the 0672s and is left out of the records or whether that's
234 * just not part of Tom. I do get the feeling that, except for
235 * a few particular entries, we can be pretty easy going with
236 * this */
237 //string += "\\[GIBOKIND=China\\]\r\n"; //I don't see this in file, necessary?
238 string += "\\[TYPE=0\\]\r\n";
239 string += "\\[GAMECONDITION=";
240 string.append("\xc8\xce\xcf\xc8");
241 //c8c3cfc8 seems suffient for a finished
242 //game
243 //I think cbc0bbee313ab7d6 is possibly
244 //some unfinished game or something
245 string += "\\]\r\n";
246 string += "\\[GAMETIME=";
247 string += "\\]\r\n";
248 string += "\\[GAMERESULT="; //3f3332353f3f //w + 325 margin ONLY, no color
249 //badac6e5cab1bce4caa4 B + T
250 //b0d7c6e5d6d0c5cccaa4 W + R
251 //bada38d4d6332f34d7d3caa4 B + margin?
252 switch(game->fullresult->result)
253 {
254 case GameResult::DRAW:
255 //string += QString(0xb9ab) + QString(0xbdc2) + QString(0xbace);
256 break;
257 case GameResult::FORFEIT:
258 //c8e6 20 bdc3 b0a3 20 c3ca b0fa bdc2
259
260 break;
261 case GameResult::TIME:
262 break;
263 case GameResult::RESIGN:
264 if(game->fullresult->winner_color == stoneWhite)
265 {
266 //string += QString(0xb9e9);
267 }
268 else
269 {
270 //string += QString(0xc8e6);
271 }
272 //string += " " + QString(0xbad2) + QString(0xb0e8) + QString(0xbdc2);
273 break;
274 case GameResult::SCORE:
275 {
276 float fmargin = game->fullresult->winner_score = game->fullresult->loser_score;
277 margin = (int)fmargin;
278 if(fmargin > (float)margin)
279 margin = (margin * 10) + 5;
280 else
281 margin *= 10;
282 qDebug("FIXME margin: %d\n", margin);
283 string += QByteArray::number(margin);
284 //string += QString(0x3f) + " " + QByteArray::number(margin) +
285 // QString(0x3f3f) + " " + QString(0x3f);
286 }
287 break;
288 default:
289 break;
290 }
291 string += "\\]\r\n";
292 string += "\\[GAMEZIPSU=" + QByteArray::number(margin) + "\\]\r\n";
293 string += "\\[GAMEDUM=0\\]\r\n";
294 string += "\\[GAMEGONGJE=0\\]\r\n";
295 string += "\\[GAMETOTALNUM="; //0xd7dc: 3335cafd" 35?
296 string += " " + QByteArray::number(game->moves);
297 string += "\\]\r\n";
298 string += "\\[SZAUDIO=0\\]\r\n";
299 string += "\\[GAMENAME=";
300 string.append("\xd3\xd1\xd2\xea\xb6\xd4\xbe\xd6\\]\r\n");
301 //cdfd bdb5 bcb6 b6d4 bed6 5c5d //a broadcast game I saw
302 /* Be careful here also, no idea what this says, we should
303 * probably leave it blank */
304 string += "\\[GAMEDATE=" + QByteArray::number(year) + "-";
305 string.append("\xc4\xea");
306 string += QByteArray::number(month) +
307 string.append("xd4\xc2");
308 string += QByteArray::number(day);
309 string.append("\xc8\xd5");
310 string += " "; /*+ QString(0xcfc2) +
311 QString(0xcee7) */
312 string += " " + QByteArray::number(hour % 12) +
313 (minute < 10 ? ":0" : ":") + QByteArray::number(minute) + "\\]\r\n";
314 /* Note that its very likely that one of the above strings contains a PM versus
315 * AM */
316 const unsigned char tom_game_place_array[] = {0x54, 0x6f, 0x6d, 0xb6, 0xd4, 0xde, 0xc4};
317 QByteArray tom_game_place((const char *)tom_game_place_array, 7);
318 string += "\\[GAMEPLACE=" + tom_game_place + "\\]\r\n";
319 string += "\\[GAMELECNAME=\\]\r\n";
320 /* FIXME, note that this is very likely going to be different on the korean
321 * server. This is for eweiqi really */
322 string += "\\[GAMEWHITENAME=" + serverCodec->fromUnicode(white->notnickname) + "("
323 + QByteArray::number(white_ordinal)
324 + white_qualifier_string + ")\\]\r\n";
325 string += "\\[GAMEWHITELEVEL=" + QByteArray::number(white_level)
326 + white_qualifier_string + "\\]\r\n";
327 string += "\\[GAMEWHITENICK=" + serverCodec->fromUnicode(white->name) + "\\]\r\n";
328 string += "\\[GAMEWHITECOUNTRY=" + QByteArray::number(white->country_id) + "\\]\r\n";
329 string += "\\[GAMEWAVATA=1\\]\r\n";
330 string += "\\[GAMEWIMAGE=\\]\r\n";
331 string += "\\[GAMEBLACKNAME=" + serverCodec->fromUnicode(black->notnickname) + "("
332 + QByteArray::number(black_ordinal)
333 + black_qualifier_string + ")\\]\r\n";
334 string += "\\[GAMEBLACKLEVEL=" + QByteArray::number(black_level)
335 + black_qualifier_string + "\\]\r\n";
336 string += "\\[GAMEBLACKNICK=" + serverCodec->fromUnicode(black->name) + "\\]\r\n";
337 string += "\\[GAMEBLACKCOUNTRY=" + QByteArray::number(black->country_id) + "\\]\r\n";
338 string += "\\[GAMEBAVATA=1\\]\r\n";
339 string += "\\[GAMEBIMAGE=\\]\r\n";
340 string += "\\[GAMECOMMENT=\\]\r\n";
341 //country = 2 = gbkind = gibokind
342 string += "\\[GAMEINFOMAIN=GBKIND:2,GTYPE:0,GCDT:1,GTIME:" +
343 QByteArray::number(game->maintime) + "-" +
344 QByteArray::number(game->periodtime) + "-" +
345 QByteArray::number(game->stones_periods) + ",GRLT:0";
346 string += ",ZIPSU:" + QByteArray::number(margin) +
347 ",DUM:0,GONGJE:0,TCNT:" +
348 QByteArray::number(game->moves);
349 string += ",AUSZ:0\\]\r\n";
350 string += "\\[GAMEINFOSUB=GNAMEF:0,GPLCF:0,GNAME:";
351 string += "GDATE:" + QByteArray::number(year) + "- " + QByteArray::number(month)
352 + "- " + QByteArray::number(day) + "-" + QByteArray::number(hour)
353 + "-" + QByteArray::number(minute) + "-" + QByteArray::number(second);
354 string += ",GPLC:";
355 string += ",GCMT:\\]\r\n";
356 string += "\\[WUSERINFO=WID:" + serverCodec->fromUnicode(white->notnickname) + ",WLV:" + QByteArray::number(white_level) +
357 ",WNICK:" + serverCodec->fromUnicode(white->name) + ",WNCD:" + QByteArray::number(white->country_id) +
358 ",WAID:60001,WIMG:\\]\r\n";
359 string += "\\[BUSERINFO=BID:" + serverCodec->fromUnicode(black->notnickname) + ",BLV:" + QByteArray::number(black_level) +
360 ",BNICK:" + serverCodec->fromUnicode(black->name) + ",BNCD:" + QByteArray::number(black->country_id) +
361 ",BAID:60001,BIMG:\\]\r\n";
362 /* Here, I'm thinking S0 is black wins, S1 is white wins
363 * then again, there's also W1 indicating white win with W0 as white loss */
364 /* I've also seen W4 as indicating W + R */
365 /* I've also seen S1 W7 as B + T also with Z0 indicating that's time remaining
366 * or something */
367 /* T is likely time settings, 30 seconds, 3 periods, 1200 total time maybe (20min)*/
368 string += getTygemGameTagQByteArray(game, white_level, black_level, margin);
369 //tag is simple of earlier info
370
371 return string;
372 }