summary refs log tree commit diff stats
path: root/hssubmit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hssubmit.cpp')
-rw-r--r--hssubmit.cpp226
1 files changed, 226 insertions, 0 deletions
diff --git a/hssubmit.cpp b/hssubmit.cpp new file mode 100644 index 0000000..690749a --- /dev/null +++ b/hssubmit.cpp
@@ -0,0 +1,226 @@
1#include "includes.h"
2
3SubmitHighscoreListState::SubmitHighscoreListState(char* hsname, int level)
4{
5 LOADIMAGE(pointer,pointer)
6
7 this->hsname = hsname;
8 this->level = level;
9 this->scoreSent = false;
10 this->selection = 0;
11
12 list = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0);
13 Uint32 bgColor = SDL_MapRGB(list->format, 255, 255, 255);
14 SDL_FillRect(list, NULL, bgColor);
15 SDL_SetColorKey(list, SDL_SRCCOLORKEY, bgColor);
16 TTF_Font* dataFont = loadFont(25);
17 SDL_Color fontColor = {0, 0, 0, 0};
18 SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor);
19 SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
20 SDL_BlitSurface(text, NULL, list, &aSpace);
21
22 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
23 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
24 SDL_BlitSurface(title, NULL, list, &tSpace);
25
26 SDL_CreateThread(&LoadHighscoreList, this);
27}
28
29int SubmitHighscoreListState::LoadHighscoreList(void* pParam)
30{
31 SubmitHighscoreList* lhl = new SubmitHighscoreList(((SubmitHighscoreListState*)pParam)->hsname, ((SubmitHighscoreListState*)pParam)->level);
32 ((SubmitHighscoreListState*)pParam)->list = lhl->render();
33
34 SDL_Color fontColor = {0, 0, 0, 0};
35 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
36 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
37 SDL_BlitSurface(title, NULL, ((SubmitHighscoreListState*)pParam)->list, &tSpace);
38
39 if (lhl->hasFailed())
40 {
41 LOADIMAGE(((SubmitHighscoreListState*)pParam)->options,hlo_passartm)
42 } else {
43 LOADIMAGE(((SubmitHighscoreListState*)pParam)->options,hlo_paartm)
44 }
45
46 SDL_Rect oSpace = {0, 440, ((SubmitHighscoreListState*)pParam)->options->w, ((SubmitHighscoreListState*)pParam)->options->h};
47 SDL_BlitSurface(((SubmitHighscoreListState*)pParam)->options, NULL, ((SubmitHighscoreListState*)pParam)->list, &oSpace);
48
49 ((SubmitHighscoreListState*)pParam)->fail = lhl->hasFailed();
50 ((SubmitHighscoreListState*)pParam)->newpos = lhl->getNewPos();
51 ((SubmitHighscoreListState*)pParam)->scoreSent = true;
52}
53
54void SubmitHighscoreListState::input(SDL_keysym key)
55{
56 if (scoreSent)
57 {
58 if ((key.sym == SDLK_LEFT) && (selection != 0))
59 {
60 selection--;
61 } else if ((key.sym == SDLK_RIGHT) && (selection != (fail?2:1)))
62 {
63 selection++;
64 } else if (key.sym == SDLK_RETURN)
65 {
66 if (fail)
67 {
68 switch (selection)
69 {
70 case 0:
71 changeState(new GameState());
72
73 break;
74 case 1:
75 changeState(new SubmitHighscoreListState(hsname, level));
76
77 break;
78 case 2:
79 changeState(new TitleState());
80
81 break;
82 }
83 } else {
84 switch (selection)
85 {
86 case 0:
87 changeState(new GameState());
88
89 break;
90 case 1:
91 changeState(new TitleState());
92
93 break;
94 }
95 }
96 }
97 }
98}
99
100void SubmitHighscoreListState::render(SDL_Surface* screen)
101{
102 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
103
104 if (scoreSent)
105 {
106 SDL_Rect pSpace;
107 if (fail)
108 {
109 pSpace.x = (selection==0?13:(selection==1?138:284));
110 pSpace.y = 448;
111 } else {
112 pSpace.x = (selection==0?52:225);
113 pSpace.y = 447;
114 }
115 pSpace.w = pointer->w;
116 pSpace.h = pointer->h;
117
118 SDL_BlitSurface(pointer, NULL, screen, &pSpace);
119
120 SDL_Rect eSpace = {0, newpos*40, 480, 40};
121 SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0));
122 }
123
124 SDL_BlitSurface(list, NULL, screen, NULL);
125}
126
127SubmitHighscoreListState::SubmitHighscoreList::SubmitHighscoreList(char* hsname, int level)
128{
129 fail = false;
130
131 try
132 {
133 IPaddress ipaddress;
134
135 if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1)
136 {
137 printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError());
138 throw 1;
139 }
140
141 TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress);
142 if (!tcpsock)
143 {
144 printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError());
145 throw 2;
146 }
147
148 char body[256];
149 sprintf(body, "name=%s&level=%d", hsname, level);
150 char headers[256];
151 sprintf(headers, "POST /mol/hslist.php?add HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\nContent-Type: application/x-www-form-urlencoded\nContent-Length: %d\n\n%s\n", strlen(body), body);
152 if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers))
153 {
154 printf("Connection closed by peer: %s\n", SDLNet_GetError());
155 throw 3;
156 }
157
158 std::stringstream download(std::stringstream::in | std::stringstream::out);
159 char hslist[1024];
160 SDLNet_TCP_Recv(tcpsock, hslist, 1024);
161 download << hslist;
162 SDLNet_TCP_Close(tcpsock);
163
164 char temps[256];
165 download.getline(temps,256);
166 while (strlen(temps) != 1)
167 {
168 download.getline(temps,256);
169 }
170
171 int rank;
172 download.getline(temps, 256);
173 if (sscanf(temps, "%d%*c", &rank) != 1)
174 {
175 printf("Recieved data is of an invalid format: %s\n", temps);
176 throw 4;
177 }
178
179 this->hslist = getGlobalHighscores();
180
181 if (this->hslist.empty())
182 {
183 printf("Global Highscore List cannot be empty after adding a score to it.\n");
184 throw 5;
185 }
186
187 if (rank > 10)
188 {
189 Highscore temph(hsname, level);
190 temph.setRank(rank);
191
192 this->hslist[9] = temph;
193 this->newpos = 10;
194 } else {
195 std::vector<Highscore>::iterator it;
196 bool found = false;
197
198 for (it=this->hslist.begin(); it<this->hslist.end(); it++)
199 {
200 Highscore h = *it;
201
202 if (!found)
203 {
204 if ((strcmp(h.getName(),hsname) == 0) && (h.getLevel() == level))
205 {
206 this->newpos = h.getRank();
207 found = true;
208 }
209 }
210 }
211 }
212 } catch (int e)
213 {
214 fail = true;
215 }
216}
217
218int SubmitHighscoreListState::SubmitHighscoreList::getNewPos()
219{
220 return newpos;
221}
222
223bool SubmitHighscoreListState::SubmitHighscoreList::hasFailed()
224{
225 return fail;
226}