summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--hslist.cpp314
-rw-r--r--hslist.h35
2 files changed, 347 insertions, 2 deletions
diff --git a/hslist.cpp b/hslist.cpp index 3e11e41..9a12dac 100644 --- a/hslist.cpp +++ b/hslist.cpp
@@ -231,6 +231,87 @@ GlobalHighscoreList::GlobalHighscoreList()
231 } 231 }
232} 232}
233 233
234GlobalHighscoreList::GlobalHighscoreList(Highscore* h)
235{
236 fail = false;
237
238 try
239 {
240 IPaddress ipaddress;
241
242 if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1)
243 {
244 printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError());
245 throw 1;
246 }
247
248 TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress);
249 if (!tcpsock)
250 {
251 printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError());
252 throw 2;
253 }
254
255 char body[256];
256 sprintf(body, "name=%s&level=%d", h->getName(), h->getLevel());
257 char headers[256];
258 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", (int) strlen(body), body);
259 if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers))
260 {
261 printf("Connection closed by peer: %s\n", SDLNet_GetError());
262 throw 3;
263 }
264
265 std::stringstream download(std::stringstream::in | std::stringstream::out);
266 char hslist[1024];
267 SDLNet_TCP_Recv(tcpsock, hslist, 1024);
268 download << hslist;
269 SDLNet_TCP_Close(tcpsock);
270
271 char temps[256];
272 download.getline(temps,256);
273 while (strlen(temps) != 1)
274 {
275 download.getline(temps,256);
276 }
277
278 int rank;
279 download.getline(temps, 256);
280 if (sscanf(temps, "%d%*c", &rank) != 1)
281 {
282 printf("Recieved data is of an invalid format: %s\n", temps);
283 throw 4;
284 }
285
286 this->hslist = getGlobalHighscores();
287
288 if (this->hslist.empty())
289 {
290 printf("Global Highscore List cannot be empty after adding a score to it.\n");
291 throw 5;
292 }
293
294 if (rank > 10)
295 {
296 h->setRank(rank);
297
298 this->hslist[9] = h;
299 } else {
300 this->hslist.push_back(h);
301 std::sort(this->hslist.begin(), this->hslist.end(), hslist_comp_i);
302 resetRanks(this->hslist);
303
304 if (this->hslist.size() > 10)
305 {
306 this->hslist.resize(10);
307 }
308 }
309 } catch (int e)
310 {
311 fail = true;
312 }
313}
314
234SDL_Surface* GlobalHighscoreList::render() 315SDL_Surface* GlobalHighscoreList::render()
235{ 316{
236 if (fail) 317 if (fail)
@@ -251,6 +332,11 @@ SDL_Surface* GlobalHighscoreList::render()
251 } 332 }
252} 333}
253 334
335bool GlobalHighscoreList::didFail()
336{
337 return fail;
338}
339
254State* ChooseHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer) 340State* ChooseHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer)
255{ 341{
256 SDL_Texture* background = loadImage(renderer, "resources/chl.bmp"); 342 SDL_Texture* background = loadImage(renderer, "resources/chl.bmp");
@@ -390,7 +476,6 @@ State* DisplayAndReturnLocalHighscoreListState::operator() (SDL_Window* window,
390 case 1: return new TitleState(); 476 case 1: return new TitleState();
391 } 477 }
392 } 478 }
393
394 } 479 }
395 } 480 }
396 } 481 }
@@ -680,7 +765,7 @@ State* NewHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer
680 switch (selection) 765 switch (selection)
681 { 766 {
682 case 0: return new GameState(); 767 case 0: return new GameState();
683 //case 1: return new SubmitHighscoreListState(hsname, level); 768 case 1: return new SubmitHighscoreState(h);
684 case 2: return new TitleState(); 769 case 2: return new TitleState();
685 } 770 }
686 } 771 }
@@ -688,3 +773,228 @@ State* NewHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer
688 } 773 }
689 } 774 }
690} 775}
776
777SubmitHighscoreState::SubmitHighscoreState(Highscore* h)
778{
779 this->h = h;
780}
781
782State* SubmitHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer)
783{
784 SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
785 Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255);
786 SDL_FillRect(list_s, NULL, bgColor);
787 SDL_SetColorKey(list_s, SDL_TRUE, bgColor);
788 TTF_Font* dataFont = loadFont(25);
789 SDL_Color fontColor = {0, 0, 0, 0};
790 SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor);
791 SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
792 SDL_BlitSurface(text, NULL, list_s, &aSpace);
793 SDL_FreeSurface(text);
794
795 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
796 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
797 SDL_BlitSurface(title, NULL, list_s, &tSpace);
798 SDL_FreeSurface(title);
799
800 SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s);
801 SDL_FreeSurface(list_s);
802
803 // Start submitting score
804 m = SDL_CreateMutex();
805 SDL_CreateThread(&SubmitHighscore, "SubmitHighscore", this);
806
807 SDL_Event e;
808
809 for (;;)
810 {
811 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
812 SDL_RenderClear(renderer);
813 SDL_RenderCopy(renderer, list, NULL, NULL);
814 SDL_RenderPresent(renderer);
815
816 if (SDL_LockMutex(m) == 0)
817 {
818 if (lhl != NULL)
819 {
820 SDL_UnlockMutex(m);
821 SDL_DestroyMutex(m);
822
823 if (lhl->didFail())
824 {
825 return new FailedSubmittingHighscoreState(h);
826 } else {
827 return new SubmittedHighscoreState(lhl, h);
828 }
829 } else {
830 SDL_UnlockMutex(m);
831 }
832 }
833
834 while (SDL_PollEvent(&e))
835 {
836 if (e.type == SDL_QUIT)
837 {
838 SDL_DestroyMutex(m);
839
840 return NULL;
841 }
842 }
843 }
844}
845
846int SubmitHighscoreState::SubmitHighscore(void* pParam)
847{
848 SubmitHighscoreState* parent = (SubmitHighscoreState*) pParam;
849 if (SDL_LockMutex(parent->m) == 0)
850 {
851 parent->lhl = new GlobalHighscoreList(parent->h);
852
853 SDL_UnlockMutex(parent->m);
854 } else {
855 printf("Could not lock mutex: %s\n", SDL_GetError());
856 }
857}
858
859FailedSubmittingHighscoreState::FailedSubmittingHighscoreState(Highscore* h)
860{
861 this->h = h;
862}
863
864State* FailedSubmittingHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer)
865{
866 SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
867 Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255);
868 SDL_FillRect(list_s, NULL, bgColor);
869 SDL_SetColorKey(list_s, SDL_TRUE, bgColor);
870 TTF_Font* dataFont = loadFont(25);
871 SDL_Color fontColor = {0, 0, 0, 0};
872 SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error submitting highscores", fontColor);
873 SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
874 SDL_BlitSurface(text, NULL, list_s, &tSpace);
875 SDL_FreeSurface(text);
876
877 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
878 SDL_Rect aSpace = {240-(title->w/2), 0, title->w, title->h};
879 SDL_BlitSurface(title, NULL, list_s, &aSpace);
880 SDL_FreeSurface(title);
881
882 SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.bmp");
883 SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
884 SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
885 SDL_FreeSurface(options_s);
886
887 SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s);
888 SDL_FreeSurface(list_s);
889
890 SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
891 int selection = 0;
892 SDL_Event e;
893
894 for (;;)
895 {
896 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
897 SDL_RenderClear(renderer);
898 SDL_RenderCopy(renderer, list, NULL, NULL);
899 applyTexture(renderer, pointer, selection==0?13:(selection==1?138:284), 448);
900 SDL_RenderPresent(renderer);
901
902 while (SDL_PollEvent(&e))
903 {
904 if (e.type == SDL_QUIT)
905 {
906 return NULL;
907 } else if (e.type == SDL_KEYDOWN)
908 {
909 if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0))
910 {
911 selection--;
912 } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2))
913 {
914 selection++;
915 } else if (e.key.keysym.sym == SDLK_RETURN)
916 {
917 switch (selection)
918 {
919 case 0: return new GameState();
920 case 1: return new SubmitHighscoreState(h);
921 case 2: return new TitleState();
922 }
923 }
924 }
925 }
926 }
927}
928
929SubmittedHighscoreState::SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h)
930{
931 this->lhl = lhl;
932 this->h = h;
933}
934
935State* SubmittedHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer)
936{
937 SDL_Surface* list_s = lhl->render();
938
939 SDL_Color fontColor = {0, 0, 0, 0};
940 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
941 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
942 SDL_BlitSurface(title, NULL, list_s, &tSpace);
943 SDL_FreeSurface(title);
944
945 SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_paartm.bmp");
946 SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
947 SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
948 SDL_FreeSurface(options_s);
949
950 SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s);
951 SDL_FreeSurface(list_s);
952
953 SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
954 int selection = 0;
955 SDL_Event e;
956
957 int newpos = h->getRank();
958 if (newpos > 10)
959 {
960 newpos = 10;
961 }
962
963 for (;;)
964 {
965 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
966 SDL_RenderClear(renderer);
967
968 SDL_Rect eSpace = {0, newpos*40, 480, 40};
969 SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
970 SDL_RenderFillRect(renderer, &eSpace);
971
972 SDL_RenderCopy(renderer, list, NULL, NULL);
973 applyTexture(renderer, pointer, selection==0?52:225, 447);
974 SDL_RenderPresent(renderer);
975
976 while (SDL_PollEvent(&e))
977 {
978 if (e.type == SDL_QUIT)
979 {
980 return NULL;
981 } else if (e.type == SDL_KEYDOWN)
982 {
983 if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0))
984 {
985 selection--;
986 } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1))
987 {
988 selection++;
989 } else if (e.key.keysym.sym == SDLK_RETURN)
990 {
991 switch (selection)
992 {
993 case 0: return new GameState();
994 case 1: return new TitleState();
995 }
996 }
997 }
998 }
999 }
1000}
diff --git a/hslist.h b/hslist.h index 8d450af..844bf37 100644 --- a/hslist.h +++ b/hslist.h
@@ -32,7 +32,9 @@ class LocalHighscoreList : public HighscoreList {
32class GlobalHighscoreList : public HighscoreList { 32class GlobalHighscoreList : public HighscoreList {
33 public: 33 public:
34 GlobalHighscoreList(); 34 GlobalHighscoreList();
35 GlobalHighscoreList(Highscore* h);
35 SDL_Surface* render(); 36 SDL_Surface* render();
37 bool didFail();
36 38
37 private: 39 private:
38 typedef HighscoreList super; 40 typedef HighscoreList super;
@@ -91,4 +93,37 @@ class NewHighscoreState : public State {
91 Highscore* h; 93 Highscore* h;
92}; 94};
93 95
96class SubmitHighscoreState : public State {
97 public:
98 SubmitHighscoreState(Highscore* h);
99 State* operator() (SDL_Window* window, SDL_Renderer* renderer);
100
101 protected:
102 Highscore* h;
103 SDL_mutex* m;
104 GlobalHighscoreList* lhl;
105
106 private:
107 static int SubmitHighscore(void* pParam);
108};
109
110class FailedSubmittingHighscoreState : public State {
111 public:
112 FailedSubmittingHighscoreState(Highscore* h);
113 State* operator() (SDL_Window* window, SDL_Renderer* renderer);
114
115 private:
116 Highscore* h;
117};
118
119class SubmittedHighscoreState : public State {
120 public:
121 SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h);
122 State* operator() (SDL_Window* window, SDL_Renderer* renderer);
123
124 private:
125 GlobalHighscoreList* lhl;
126 Highscore* h;
127};
128
94#endif 129#endif