| 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 | #ifndef TYGEMCONNECTION_H |
| 24 | #define TYGEMCONNECTION_H |
| 25 | #include <QtNetwork> |
| 26 | #include "networkconnection.h" |
| 27 | #include "messages.h" |
| 28 | #include <vector> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | class BoardDispatch; |
| 32 | class GameDialogDispatch; |
| 33 | class TalkDispatch; |
| 34 | |
| 35 | class TygemConnection : public NetworkConnection |
| 36 | { |
| 37 | public: |
| 38 | TygemConnection(const ConnectionCredentials credentials); |
| 39 | ~TygemConnection(); |
| 40 | /* Protocols are way similar so probably most of these don't have to be |
| 41 | * virtual FIXME */ |
| 42 | virtual void sendText(QString); |
| 43 | virtual void sendText(const char * text); |
| 44 | virtual void sendDisconnect(void); |
| 45 | virtual void sendMsg(unsigned int game_id, QString text); |
| 46 | virtual void sendMsg(PlayerListing * player, QString text); |
| 47 | virtual void sendToggle(const QString & param, bool val); |
| 48 | virtual void sendObserve(const GameListing * game); |
| 49 | virtual void sendStatsRequest(const PlayerListing *) {} |
| 50 | virtual void sendPlayersRequest(void) {} |
| 51 | virtual void sendGamesRequest(void) {} |
| 52 | |
| 53 | virtual void sendMatchInvite(const PlayerListing * opponent); |
| 54 | virtual void sendTime(BoardDispatch * boarddispatch); |
| 55 | virtual void sendMove(unsigned int game_id, class MoveRecord * move); |
| 56 | |
| 57 | //EndgameMsg should probably be protected or even private |
| 58 | enum EGVersion {pass, done_scoring, accept_count, reject_count, |
| 59 | request_count, accept_count_request, refuse_count_request, |
| 60 | request_draw, accept_draw_request, refuse_draw_request, |
| 61 | request_undo, accept_undo_request, refuse_undo_request, |
| 62 | opponent_disconnect_timer, we_resume, opponent_reconnects}; |
| 63 | virtual void sendEndgameMsg(const class GameData * game, enum EGVersion version); |
| 64 | virtual void sendStopTime(BoardDispatch * boarddispatch, enum EGVersion version); |
| 65 | |
| 66 | virtual void sendResult(class GameData * game, class GameResult * result); |
| 67 | virtual void sendMatchRequest(class MatchRequest * mr); |
| 68 | virtual void sendRematchRequest(void); |
| 69 | virtual void sendRematchAccept(void); |
| 70 | virtual void declineMatchOffer(const PlayerListing * opponent); |
| 71 | virtual void cancelMatchOffer(const PlayerListing * opponent); |
| 72 | virtual void acceptMatchOffer(const PlayerListing * opponent, class MatchRequest * mr); |
| 73 | virtual void sendRequestCount(unsigned int game_id); |
| 74 | virtual void sendAcceptCountRequest(class GameData * data); |
| 75 | virtual void sendRefuseCountRequest(class GameData * data); |
| 76 | virtual void sendRequestDraw(unsigned int game_id); |
| 77 | virtual void sendAcceptDrawRequest(class GameData * data); |
| 78 | virtual void sendRefuseDrawRequest(class GameData * data); |
| 79 | virtual void sendRejectCount(class GameData * data); |
| 80 | virtual void sendAcceptCount(class GameData * data); |
| 81 | virtual void handlePendingData(); |
| 82 | virtual void onReady(void); |
| 83 | virtual void changeServer(void); |
| 84 | virtual void createRoom(void); |
| 85 | virtual void sendCreateRoom(class RoomCreate * room); |
| 86 | virtual void sendJoinRoom(const RoomListing & room, const char * password = 0); |
| 87 | |
| 88 | virtual void addFriend(PlayerListing * player); |
| 89 | virtual void removeFriend(PlayerListing * player); |
| 90 | virtual void addBlock(PlayerListing * player); |
| 91 | virtual void removeBlock(PlayerListing * player); |
| 92 | |
| 93 | //FIXME these are unused right now: |
| 94 | virtual void requestLongInfo(PlayerListing * player); |
| 95 | virtual void requestShortInfo(PlayerListing * player); |
| 96 | |
| 97 | virtual unsigned short getRoomNumber(void); //FIXME for gamedialog unsure of necessity or reliability here |
| 98 | |
| 99 | virtual void closeTalk(PlayerListing *opponent); |
| 100 | virtual void closeBoardDispatch(unsigned int game_id); |
| 101 | |
| 102 | virtual int gd_verifyBoardSize(int v); |
| 103 | virtual QTime gd_checkMainTime(TimeSystem s, const QTime & t); |
| 104 | virtual QTime gd_checkPeriodTime(TimeSystem s, const QTime & t); |
| 105 | virtual unsigned int gd_checkPeriods(TimeSystem s, unsigned int p); |
| 106 | |
| 107 | virtual unsigned int rankToScore(QString rank); |
| 108 | virtual unsigned long getGameDialogFlags(void); |
| 109 | virtual bool supportsMultipleUndo(void) { return false; } |
| 110 | virtual bool supportsObserveOutside(void) { return false; } |
| 111 | virtual bool supportsRequestCount(void) { return false; } |
| 112 | virtual bool supportsRequestDraw(void) { return true; } |
| 113 | virtual bool clientCountsTime(void) { return false; } |
| 114 | virtual bool clientSendsTime(void) { return true; } |
| 115 | virtual bool unmarkUnmarksAllDeadStones(void) { return true; } |
| 116 | virtual bool canMarkStonesDeadinScore(void) { return false; } |
| 117 | virtual bool cantMarkOppStonesDead(void) { return true; } |
| 118 | virtual bool twoPassesEndsGame(void) { return true; } |
| 119 | virtual bool supportsServerChange(void) { return true; } |
| 120 | virtual bool supportsRematch(void) { return true; } |
| 121 | virtual bool supportsFriendList(void) { return true; } |
| 122 | virtual bool supportsBlockList(void) { return true; } |
| 123 | virtual unsigned long getPlayerListColumns(void) { return PL_NOMATCHPREFS; } |
| 124 | virtual bool supportsCreateRoom(void) { return true; } |
| 125 | virtual unsigned long getRoomStructureFlags(void) { return (RS_NOROOMLIST | RS_ONEROOMATATIME | RS_ONEGAMEPERROOM); } |
| 126 | virtual const char * getCodecString(void); |
| 127 | virtual QString getPlaceString(void); |
| 128 | virtual void timerEvent(QTimerEvent * event); |
| 129 | protected: |
| 130 | virtual int requestServerInfo(void); |
| 131 | /* this is called handleServerList in ORO, not sure why we changed |
| 132 | * the name minor FIXME */ |
| 133 | virtual void handleServerInfo(unsigned char * msg, unsigned int length); |
| 134 | int reconnectToServer(void); |
| 135 | virtual void handleGamesList(unsigned char * msg, unsigned int size); |
| 136 | |
| 137 | //FIXME secondsToDate obviously should not be here |
| 138 | void secondsToDate(unsigned short & year, unsigned char & month, unsigned char & day, unsigned char & hour, unsigned char & minute, unsigned char & second); |
| 139 | virtual QByteArray getTygemGameRecordQByteArray(class GameData * game); |
| 140 | QByteArray getTygemGameTagQByteArray(GameData * game, unsigned char white_level, unsigned char black_level, int margin); |
| 141 | unsigned char getVictoryCode(GameResult & aGameResult); |
| 142 | enum TimeFlags { BLACK_LOSES, WHITE_LOSES, GAME_ON }; |
| 143 | enum TimeFlags handleTimeChunk(BoardDispatch * boarddispatch, unsigned char chunk[8], bool black_first); |
| 144 | void fillTimeChunk(BoardDispatch * boarddispatch, unsigned char * packet); |
| 145 | /* FIXME */ |
| 146 | QTextCodec * serverCodec; |
| 147 | std::vector <class ServerItem *> serverList; |
| 148 | int current_server_index; |
| 149 | |
| 150 | int move_message_number; //FIXME can't use if more than one game |
| 151 | int move_message_number_ack; |
| 152 | int seconds_until_opp_forfeits; |
| 153 | private: |
| 154 | void sendJoin(unsigned short game_number); |
| 155 | void sendMatchMsg1(const PlayerListing * player, unsigned short game_number); |
| 156 | void sendLogin(bool response_bit = false, bool change_server = false); |
| 157 | void handleLogin(unsigned char * msg, unsigned int length); |
| 158 | void sendRequestGames(void); |
| 159 | void sendRequestPlayers(void); |
| 160 | void sendName(void); |
| 161 | void sendRequest(void); |
| 162 | void sendRequest2(void); |
| 163 | void sendFriendsBlocksRequest(void); |
| 164 | void promptResumeMatch(void); |
| 165 | void sendOpenConversation(PlayerListing * player); |
| 166 | enum MIVersion {offer, accept, decline, decline_all, modify, create, acknowledge, alreadyingame}; |
| 167 | void sendConversationReply(PlayerListing * player, enum MIVersion version); |
| 168 | void sendConversationMsg(PlayerListing * player, const char * text); |
| 169 | void sendCloseConversation(PlayerListing * player); |
| 170 | void sendPersonalChat(const PlayerListing * player, const char * text); |
| 171 | void sendServerChat(QString text); |
| 172 | void sendResume(unsigned short game_number); |
| 173 | void sendFinishObserving(unsigned short game_number); |
| 174 | void sendObserversRequest(unsigned short game_number); |
| 175 | void sendServerKeepAlive(uint32_t keepaliveIV); |
| 176 | uint32_t encodeKeepAliveIV(uint32_t a); |
| 177 | void sendMatchInvite(const PlayerListing * player, enum MIVersion version = offer, unsigned short room_number = 0xffff); |
| 178 | void sendMatchOffer(const MatchRequest & mr, enum MIVersion version = offer); |
| 179 | void sendStartGame(const MatchRequest & mr); |
| 180 | void sendInvitationSettings(bool invite); |
| 181 | void sendResumeFromDisconnect(unsigned int game_number); |
| 182 | enum OpponentDisconnectUpdate { opponent_disconnect, opponent_reconnect }; |
| 183 | void sendOpponentDisconnect(unsigned int game_number, enum OpponentDisconnectUpdate opp); |
| 184 | void sendLongOpponentDisconnect(unsigned int game_number); |
| 185 | void sendOpponentDisconnectTimer(unsigned int game_number); |
| 186 | void sendMatchResult(unsigned short game_code); |
| 187 | void sendLongMatchResult(unsigned short game_code); |
| 188 | void sendDisconnectMsg(void); |
| 189 | void encode(unsigned char * h, unsigned int cycle_size); |
| 190 | void handleMessage(QString msg); |
| 191 | void handleMessage(unsigned char * msg, unsigned int size); |
| 192 | void handleConnected(unsigned char * msg, unsigned int size); |
| 193 | void handlePlayerList(unsigned char * msg, unsigned int size); |
| 194 | void handleFriendsBlocksList(unsigned char * msg, unsigned int size); |
| 195 | void handleServerPlayerCounts(unsigned char * msg, unsigned int size); |
| 196 | QString rating_pointsToRank(unsigned int rp); |
| 197 | QString getCountryFromCode(unsigned char code); |
| 198 | int getPhase(unsigned char byte); |
| 199 | QString getRoomTag(unsigned char byte); |
| 200 | |
| 201 | void writeNotNicknameAndRank(char * p, const PlayerListing * player); |
| 202 | void writeNicknameAndCID(char * p, const PlayerListing * player); |
| 203 | PlayerListing * getOrCreatePlayerListingFromRecord(char * r); |
| 204 | |
| 205 | void handleServerAnnouncement(unsigned char * msg, unsigned int size); |
| 206 | void handleRequestKeepAlive(unsigned char * msg, unsigned int size); |
| 207 | void handleServerRoomChat(unsigned char * msg, unsigned int size); |
| 208 | void handlePersonalChat(unsigned char * msg, unsigned int size); |
| 209 | void handleOpenConversation(unsigned char * msg, unsigned int size); |
| 210 | void handleConversationReply(unsigned char * msg, unsigned int size); |
| 211 | void handleConversationMsg(unsigned char * msg, unsigned int size); |
| 212 | void handleGameChat(unsigned char * msg, unsigned int size); |
| 213 | void handleBettingMatchStart(unsigned char * msg, unsigned int size); |
| 214 | void handleTime(unsigned char * msg, unsigned int size); |
| 215 | void handleMove(unsigned char * msg, unsigned int size); |
| 216 | void handlePass(unsigned char * msg, unsigned int size, int pass_number); |
| 217 | void handleObserverList(unsigned char * msg, unsigned int size); |
| 218 | void handleBoardOpened(unsigned char * msg, unsigned int size); |
| 219 | void handleMatchOpened(unsigned char * msg, unsigned int size); |
| 220 | void handleResumeMatch(unsigned char * msg, unsigned int size); |
| 221 | QString getStatusFromCode(unsigned char code, QString rank); |
| 222 | int compareRanks(QString rankA, QString rankB); |
| 223 | void handleMatchInvite(unsigned char * msg, unsigned int size); |
| 224 | void handleMatchOffer(unsigned char * msg, unsigned int size, enum MIVersion = offer); |
| 225 | void sendCreateRoom(void); |
| 226 | void handleMatchInviteResponse(unsigned char * msg, unsigned int size); |
| 227 | void handleCreateRoomResponse(unsigned char * msg, unsigned int size); |
| 228 | //FIXME |
| 229 | void handleScoreMsg1(unsigned char * msg, unsigned int size); |
| 230 | void handleEndgameMsg(unsigned char * msg, unsigned int size); |
| 231 | void handleGameStateChange(unsigned char * msg, unsigned int size); |
| 232 | void handleCountRequest(unsigned char * msg, unsigned int size); |
| 233 | void handleCountRequestResponse(unsigned char * msg, unsigned int size); |
| 234 | void handleGameResult(unsigned char * msg, unsigned int size); |
| 235 | void handleGameResult2(unsigned char * msg, unsigned int size); |
| 236 | void handleMsg2(unsigned char * msg, unsigned int size); |
| 237 | int time_to_seconds(const QString & time); |
| 238 | |
| 239 | unsigned int http_connect_content_length; |
| 240 | bool http_response_checked; |
| 241 | /* We should really just have the player listing for ourself FIXME */ |
| 242 | unsigned short our_player_id; |
| 243 | bool opponent_is_challenger; |
| 244 | bool havenot_requested_friendsblocks; |
| 245 | |
| 246 | std::vector<PlayerListing *> decline_all_invitations; |
| 247 | QTextCodec * textCodec; |
| 248 | unsigned long encode_offset; |
| 249 | |
| 250 | std::map <PlayerListing *, QString> pendingConversationMsg; |
| 251 | |
| 252 | int matchKeepAliveTimerID, matchRequestKeepAliveTimerID; |
| 253 | int retryLoginTimerID; |
| 254 | int opponentDisconnectTimerID; |
| 255 | |
| 256 | bool received_players; |
| 257 | bool received_games; |
| 258 | bool initial_connect; |
| 259 | bool dont_validate_maintime, dont_validate_periodtime, dont_validate_periods; |
| 260 | |
| 261 | virtual void OnConnected(); |
| 262 | }; |
| 263 | #endif //TYGEMCONNECTION_H |