| 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 "eweiqiconnection.h" |
| 24 | #include "codecwarndialog.h" |
| 25 | #include "playergamelistings.h" |
| 26 | #include "gamedata.h" |
| 27 | #include "room.h" |
| 28 | #include "serverlistdialog.h" |
| 29 | |
| 30 | EWeiQiConnection::EWeiQiConnection(const ConnectionCredentials credentials) |
| 31 | : TygemConnection(credentials) |
| 32 | { |
| 33 | serverCodec = QTextCodec::codecForName(getCodecString()); |
| 34 | if(!serverCodec) |
| 35 | { |
| 36 | new CodecWarnDialog(getCodecString()); |
| 37 | serverCodec = QTextCodec::codecForLocale(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const char * EWeiQiConnection::getCodecString(void) |
| 42 | { |
| 43 | return "GB2312"; |
| 44 | } |
| 45 | |
| 46 | QString EWeiQiConnection::getPlaceString(void) |
| 47 | { |
| 48 | return "EWeiqi: " + serverList[current_server_index]->name; |
| 49 | } |
| 50 | |
| 51 | int EWeiQiConnection::requestServerInfo(void) |
| 52 | { |
| 53 | qDebug("Requesting eWeiQi Server Info"); |
| 54 | if(!openConnection(hostname, port, NOT_MAIN_CONNECTION)) |
| 55 | { |
| 56 | qDebug("Can't get server info"); |
| 57 | return -1; |
| 58 | } |
| 59 | unsigned int length = 0x96; |
| 60 | unsigned char * packet = new unsigned char[length]; //term 0x00 |
| 61 | snprintf((char *)packet, length, |
| 62 | "GET /service/down_china/livebaduk3.cfg HTTP/1.1\r\n" \ |
| 63 | "User-Agent: Tygem HTTP\r\n" \ |
| 64 | "Host: service.tygem.com\r\n" \ |
| 65 | "Connection: Keep-Alive\r\n" \ |
| 66 | "Cache-Control: no-cache\r\n\r\n"); |
| 67 | #ifdef RE_DEBUG |
| 68 | for(int i = 0; i < length; i++) |
| 69 | printf("%02x", packet[i]); |
| 70 | printf("\n"); |
| 71 | #endif //RE_DEBUG |
| 72 | if(write((const char *)packet, length) < 0) |
| 73 | { |
| 74 | qWarning("*** failed sending server info request"); |
| 75 | delete[] packet; |
| 76 | return -1; |
| 77 | } |
| 78 | delete[] packet; |
| 79 | |
| 80 | setState(Info); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | QByteArray EWeiQiConnection::getTygemGameRecordQByteArray(GameData * game) |
| 85 | { |
| 86 | Room * room = getDefaultRoom(); |
| 87 | const PlayerListing * black, * white; |
| 88 | int black_ordinal, white_ordinal; |
| 89 | char black_qualifier, white_qualifier; |
| 90 | QByteArray white_qualifier_string, black_qualifier_string; |
| 91 | unsigned char black_level, white_level; |
| 92 | unsigned short year; |
| 93 | unsigned char month, day, hour, minute, second; |
| 94 | secondsToDate(year, month, day, hour, minute, second); |
| 95 | |
| 96 | int margin = 0; |
| 97 | |
| 98 | black = room->getPlayerListing(game->black_name); |
| 99 | white = room->getPlayerListing(game->white_name); |
| 100 | sscanf(black->rank.toLatin1().constData(), "%d%c", &black_ordinal, &black_qualifier); |
| 101 | if(black_qualifier == 'k') |
| 102 | { |
| 103 | black_level = 0x12 - black_ordinal; |
| 104 | black_qualifier_string.append("\xbc\xb6"); |
| 105 | } |
| 106 | else if(black_qualifier == 'p') |
| 107 | black_level = black_ordinal + 0x1a; |
| 108 | else |
| 109 | { |
| 110 | black_level = black_ordinal + 0x11; |
| 111 | black_qualifier_string.append("\xb6\xce"); |
| 112 | } |
| 113 | sscanf(white->rank.toLatin1().constData(), "%d%c", &white_ordinal, &white_qualifier); |
| 114 | if(white_qualifier == 'k') |
| 115 | { |
| 116 | white_level = 0x12 - white_ordinal; |
| 117 | white_qualifier_string.append("\xbc\xb6"); |
| 118 | } |
| 119 | else if(white_qualifier == 'p') |
| 120 | white_level = white_ordinal + 0x1a; |
| 121 | else |
| 122 | { |
| 123 | white_level = white_ordinal + 0x11; |
| 124 | white_qualifier_string.append("\xb6\xce"); |
| 125 | } |
| 126 | |
| 127 | QByteArray string; |
| 128 | string += "\\[GIBOKIND=China\\]\r\n"; |
| 129 | string += "\\[TYPE=0\\]\r\n"; |
| 130 | string += "\\[GAMECONDITION="; |
| 131 | string.append("\xc8\xce\xcf\xc8"); |
| 132 | //c8c3cfc8 seems suffient for a finished |
| 133 | //game |
| 134 | //I think cbc0bbee313ab7d6 is possibly |
| 135 | //some unfinished game or something |
| 136 | string += "\\]\r\n"; |
| 137 | string += "\\[GAMETIME="; |
| 138 | string += "\\]\r\n"; |
| 139 | string += "\\[GAMERESULT="; //3f3332353f3f //w + 325 margin ONLY, no color |
| 140 | //badac6e5cab1bce4caa4 B + T |
| 141 | //b0d7c6e5d6d0c5cccaa4 W + R |
| 142 | switch(game->fullresult->result) |
| 143 | { |
| 144 | case GameResult::DRAW: |
| 145 | //string += QString(0xb9ab) + QString(0xbdc2) + QString(0xbace); |
| 146 | break; |
| 147 | case GameResult::FORFEIT: |
| 148 | //c8e6 20 bdc3 b0a3 20 c3ca b0fa bdc2 |
| 149 | |
| 150 | break; |
| 151 | case GameResult::TIME: |
| 152 | break; |
| 153 | case GameResult::RESIGN: |
| 154 | if(game->fullresult->winner_color == stoneWhite) |
| 155 | { |
| 156 | //string += QString(0xb9e9); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | //string += QString(0xc8e6); |
| 161 | } |
| 162 | //string += " " + QString(0xbad2) + QString(0xb0e8) + QString(0xbdc2); |
| 163 | break; |
| 164 | case GameResult::SCORE: |
| 165 | { |
| 166 | float fmargin = game->fullresult->winner_score = game->fullresult->loser_score; |
| 167 | margin = (int)fmargin; |
| 168 | if(fmargin > (float)margin) |
| 169 | margin = (margin * 10) + 5; |
| 170 | else |
| 171 | margin *= 10; |
| 172 | qDebug("FIXME margin: %d\n", margin); |
| 173 | //3f20 margin 3f3f203f //black wins |
| 174 | string += QByteArray::number(margin); |
| 175 | //string += QString(0x3f) + " " + QByteArray::number(margin) + |
| 176 | // QString(0x3f3f) + " " + QString(0x3f); |
| 177 | } |
| 178 | break; |
| 179 | default: |
| 180 | break; |
| 181 | } |
| 182 | string += "\\]\r\n"; |
| 183 | string += "\\[GAMEZIPSU=" + QByteArray::number(margin) + "\\]\r\n"; |
| 184 | string += "\\[GAMEDUM=0\\]\r\n"; |
| 185 | string += "\\[GAMEGONGJE=0\\]\r\n"; |
| 186 | string += "\\[GAMETOTALNUM="; //0xd7dc: 3335cafd" 35? |
| 187 | string += "\\[SZAUDIO=0\\]\r\n"; |
| 188 | string += "\\[GAMENAME="; |
| 189 | string.append("\xd3\xd1\xd2\xea\xb6\xd4\xbe\xd6\\]\r\n"); |
| 190 | /* Be careful here also, no idea what this says, we should |
| 191 | * probably leave it blank */ |
| 192 | string += "\\[GAMEDATE=" + QByteArray::number(year) + "-"; |
| 193 | string.append("\xc4\xea"); |
| 194 | string += QByteArray::number(month); |
| 195 | string.append("\xd4\xc2"); |
| 196 | string += QByteArray::number(day); |
| 197 | string.append("\xc8\xd5 \xcf\xc2\xce\xe7"); |
| 198 | string += " " + QByteArray::number(hour % 12) + |
| 199 | (minute < 10 ? ":0" : ":") + QByteArray::number(minute) + "\\]\r\n"; |
| 200 | /* Note that its very likely that one of the above strings contains a PM versus |
| 201 | * AM */ |
| 202 | const char eWeiQi_game_place_array[] = {static_cast<const char>(0xde),static_cast<const char>(0xc4), |
| 203 | static_cast<const char>(0xb3),static_cast<const char>(0xc7), |
| 204 | static_cast<const char>(0x54),static_cast<const char>(0x59), |
| 205 | static_cast<const char>(0x47),static_cast<const char>(0x45), |
| 206 | static_cast<const char>(0x4d),static_cast<const char>(0xb6), |
| 207 | static_cast<const char>(0xd4),static_cast<const char>(0xde), |
| 208 | static_cast<const char>(0xc4)}; |
| 209 | QByteArray eWeiQi_game_place(eWeiQi_game_place_array, 13); |
| 210 | string += "\\[GAMEPLACE=" + eWeiQi_game_place + "\\]\r\n"; |
| 211 | string += "\\[GAMELECNAME=\\]\r\n"; |
| 212 | /* FIXME, note that this is very likely going to be different on the korean |
| 213 | * server. This is for eweiqi really */ |
| 214 | string += "\\[GAMEWHITENAME=" + serverCodec->fromUnicode(white->notnickname) + "(" |
| 215 | + QByteArray::number(white_ordinal) |
| 216 | + white_qualifier_string + ")\\]\r\n"; |
| 217 | string += "\\[GAMEWHITELEVEL=" + QByteArray::number(white_level) |
| 218 | + white_qualifier_string + "\\]\r\n"; |
| 219 | string += "\\[GAMEWHITENICK=" + serverCodec->fromUnicode(white->name) + "\\]\r\n"; |
| 220 | string += "\\[GAMEWHITECOUNTRY=" + QByteArray::number(white->country_id) + "\\]\r\n"; |
| 221 | string += "\\[GAMEWAVATA=1\\]\r\n"; |
| 222 | string += "\\[GAMEWIMAGE=\\]\r\n"; |
| 223 | string += "\\[GAMEBLACKNAME=" + serverCodec->fromUnicode(black->notnickname) + "(" |
| 224 | + QByteArray::number(black_ordinal) |
| 225 | + black_qualifier_string + ")\\]\r\n"; |
| 226 | string += "\\[GAMEBLACKLEVEL=" + QByteArray::number(black_level) |
| 227 | + black_qualifier_string + "\\]\r\n"; |
| 228 | string += "\\[GAMEBLACKNICK=" + serverCodec->fromUnicode(black->name) + "\\]\r\n"; |
| 229 | string += "\\[GAMEBLACKCOUNTRY=" + QByteArray::number(black->country_id) + "\\]\r\n"; |
| 230 | string += "\\[GAMEBAVATA=1\\]\r\n"; |
| 231 | string += "\\[GAMEBIMAGE=\\]\r\n"; |
| 232 | string += "\\[GAMECOMMENT=\\]\r\n"; |
| 233 | //country = 2 = gbkind = gibokind |
| 234 | string += "\\[GAMEINFOMAIN=GBKIND:2,GTYPE:0,GCDT:1,GTIME:" + |
| 235 | QByteArray::number(game->maintime) + "-" + |
| 236 | QByteArray::number(game->periodtime) + "-" + |
| 237 | QByteArray::number(game->stones_periods) + ",GRLT:0"; |
| 238 | string += ",ZIPSU:" + QByteArray::number(margin) + |
| 239 | ",DUM:0,GONGJE:0,TCNT:" + |
| 240 | QByteArray::number(game->moves); |
| 241 | string += ",AUSZ:0\\]\r\n"; |
| 242 | string += "\\[GAMEINFOSUB=GNAMEF:0,GPLCF:0,GNAME:"; |
| 243 | string += "GDATE:" + QByteArray::number(year) + "- " + QByteArray::number(month) |
| 244 | + "- " + QByteArray::number(day) + "-" + QByteArray::number(hour) |
| 245 | + "-" + QByteArray::number(minute) + "-" + QByteArray::number(second); |
| 246 | string += ",GPLC:"; |
| 247 | string += ",GCMT:\\]\r\n"; |
| 248 | string += "\\[WUSERINFO=WID:" + serverCodec->fromUnicode(white->notnickname) + ",WLV:" + QByteArray::number(white_level) + |
| 249 | ",WNICK:" + serverCodec->fromUnicode(white->name) + ",WNCD:" + QByteArray::number(white->country_id) + |
| 250 | ",WAID:60001,WIMG:\\]\r\n"; |
| 251 | string += "\\[BUSERINFO=BID:" + serverCodec->fromUnicode(black->notnickname) + ",BLV:" + QByteArray::number(black_level) + |
| 252 | ",BNICK:" + serverCodec->fromUnicode(black->name) + ",BNCD:" + QByteArray::number(black->country_id) + |
| 253 | ",BAID:60001,BIMG:\\]\r\n"; |
| 254 | /* Here, I'm thinking S0 is black wins, S1 is white wins |
| 255 | * then again, there's also W1 indicating white win with W0 as white loss */ |
| 256 | /* I've also seen W4 as indicating W + R */ |
| 257 | /* I've also seen S1 W7 as B + T also with Z0 indicating that's time remaining |
| 258 | * or something */ |
| 259 | /* T is likely time settings, 30 seconds, 3 periods, 1200 total time maybe (20min)*/ |
| 260 | string += getTygemGameTagQByteArray(game, white_level, black_level, margin); |
| 261 | |
| 262 | return string; |
| 263 | } |