diff options
Diffstat (limited to 'hslist.cpp')
| -rw-r--r-- | hslist.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
| diff --git a/hslist.cpp b/hslist.cpp index d2901b4..ec99a77 100644 --- a/hslist.cpp +++ b/hslist.cpp | |||
| @@ -76,10 +76,123 @@ std::vector<Highscore> HighscoreList::getLocalHighscores() | |||
| 76 | 76 | ||
| 77 | std::vector<Highscore> HighscoreList::getGlobalHighscores() | 77 | std::vector<Highscore> HighscoreList::getGlobalHighscores() |
| 78 | { | 78 | { |
| 79 | IPaddress ipaddress; | ||
| 79 | 80 | ||
| 81 | if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) | ||
| 82 | { | ||
| 83 | printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | ||
| 84 | throw 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); | ||
| 88 | if (!tcpsock) | ||
| 89 | { | ||
| 90 | printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | ||
| 91 | throw 2; | ||
| 92 | } | ||
| 93 | |||
| 94 | char* headers = "GET /mol/hslist.php HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\n\n"; | ||
| 95 | if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) | ||
| 96 | { | ||
| 97 | printf("Connection closed by peer: %s\n", SDLNet_GetError()); | ||
| 98 | throw 3; | ||
| 99 | } | ||
| 100 | |||
| 101 | std::stringstream download(std::stringstream::in | std::stringstream::out); | ||
| 102 | char hslist[1024]; | ||
| 103 | SDLNet_TCP_Recv(tcpsock, hslist, 1024); | ||
| 104 | download << hslist; | ||
| 105 | SDLNet_TCP_Close(tcpsock); | ||
| 106 | |||
| 107 | char temps[256]; | ||
| 108 | download.getline(temps,256); | ||
| 109 | while (strlen(temps) != 1) | ||
| 110 | { | ||
| 111 | download.getline(temps,256); | ||
| 112 | } | ||
| 113 | |||
| 114 | std::vector<Highscore> temp = std::vector<Highscore>(); | ||
| 115 | int scores; | ||
| 116 | download.getline(temps, 256); | ||
| 117 | if (sscanf(temps, "%d%*c", &scores) != 1) | ||
| 118 | { | ||
| 119 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 120 | throw 4; | ||
| 121 | } | ||
| 122 | |||
| 123 | for (int i=0; i<scores; i++) | ||
| 124 | { | ||
| 125 | int namelen; | ||
| 126 | char namelens[13]; | ||
| 127 | char* name = (char*) calloc(25, sizeof(char)); | ||
| 128 | int score; | ||
| 129 | download.getline(temps, 256); | ||
| 130 | |||
| 131 | if (sscanf(temps, "%d", &namelen) != 1) | ||
| 132 | { | ||
| 133 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 134 | throw 4; | ||
| 135 | } | ||
| 136 | |||
| 137 | sprintf(namelens, "%%*d%%%dc", namelen); | ||
| 138 | |||
| 139 | if (sscanf(temps, namelens, name) != 1) | ||
| 140 | { | ||
| 141 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 142 | throw 4; | ||
| 143 | } | ||
| 144 | |||
| 145 | sprintf(namelens, "%%*d%%*%dc%%d%%*c", namelen); | ||
| 146 | |||
| 147 | if (sscanf(temps, namelens, &score) != 1) | ||
| 148 | { | ||
| 149 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 150 | throw 4; | ||
| 151 | } | ||
| 152 | |||
| 153 | Highscore h = Highscore(name, score); | ||
| 154 | h.setRank(i+1); | ||
| 155 | |||
| 156 | temp.push_back(h); | ||
| 157 | } | ||
| 158 | |||
| 159 | return temp; | ||
| 80 | } | 160 | } |
| 81 | 161 | ||
| 82 | LocalHighscoreList::LocalHighscoreList() | 162 | LocalHighscoreList::LocalHighscoreList() |
| 83 | { | 163 | { |
| 84 | this->hslist = getLocalHighscores(); | 164 | this->hslist = getLocalHighscores(); |
| 85 | } | 165 | } |
| 166 | |||
| 167 | GlobalHighscoreList::GlobalHighscoreList() | ||
| 168 | { | ||
| 169 | fail = false; | ||
| 170 | |||
| 171 | try | ||
| 172 | { | ||
| 173 | this->hslist = getGlobalHighscores(); | ||
| 174 | } catch (int e) | ||
| 175 | { | ||
| 176 | fail = true; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | SDL_Surface* GlobalHighscoreList::render() | ||
| 181 | { | ||
| 182 | if (fail) | ||
| 183 | { | ||
| 184 | SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); | ||
| 185 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); | ||
| 186 | SDL_FillRect(tmp, NULL, bgColor); | ||
| 187 | SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, bgColor); | ||
| 188 | TTF_Font* dataFont = loadFont(25); | ||
| 189 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 190 | SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error retrieving highscores", fontColor); | ||
| 191 | SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; | ||
| 192 | SDL_BlitSurface(text, NULL, tmp, &tSpace); | ||
| 193 | |||
| 194 | return tmp; | ||
| 195 | } else { | ||
| 196 | return super::render(); | ||
| 197 | } | ||
| 198 | } | ||
