summary refs log tree commit diff stats
path: root/hssubmit.cpp
blob: 690749a425ad5cdf9e04ea67d6df0bb339bac4c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "includes.h"

SubmitHighscoreListState::SubmitHighscoreListState(char* hsname, int level)
{
	LOADIMAGE(pointer,pointer)

	this->hsname = hsname;
	this->level = level;
	this->scoreSent = false;
	this->selection = 0;

	list = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0);
	Uint32 bgColor = SDL_MapRGB(list->format, 255, 255, 255);
	SDL_FillRect(list, NULL, bgColor);
	SDL_SetColorKey(list, SDL_SRCCOLORKEY, bgColor);
	TTF_Font* dataFont = loadFont(25);
	SDL_Color fontColor = {0, 0, 0, 0};
	SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor);
	SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
	SDL_BlitSurface(text, NULL, list, &aSpace);

	SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
	SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
	SDL_BlitSurface(title, NULL, list, &tSpace);

	SDL_CreateThread(&LoadHighscoreList, this);
}

int SubmitHighscoreListState::LoadHighscoreList(void* pParam)
{
	SubmitHighscoreList* lhl = new SubmitHighscoreList(((SubmitHighscoreListState*)pParam)->hsname, ((SubmitHighscoreListState*)pParam)->level);
	((SubmitHighscoreListState*)pParam)->list = lhl->render();

	SDL_Color fontColor = {0, 0, 0, 0};
	SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
	SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
	SDL_BlitSurface(title, NULL, ((SubmitHighscoreListState*)pParam)->list, &tSpace);

	if (lhl->hasFailed())
	{
		LOADIMAGE(((SubmitHighscoreListState*)pParam)->options,hlo_passartm)
	} else {
		LOADIMAGE(((SubmitHighscoreListState*)pParam)->options,hlo_paartm)
	}

	SDL_Rect oSpace = {0, 440, ((SubmitHighscoreListState*)pParam)->options->w, ((SubmitHighscoreListState*)pParam)->options->h};
	SDL_BlitSurface(((SubmitHighscoreListState*)pParam)->options, NULL, ((SubmitHighscoreListState*)pParam)->list, &oSpace);

	((SubmitHighscoreListState*)pParam)->fail = lhl->hasFailed();
	((SubmitHighscoreListState*)pParam)->newpos = lhl->getNewPos();
	((SubmitHighscoreListState*)pParam)->scoreSent = true;
}

void SubmitHighscoreListState::input(SDL_keysym key)
{
	if (scoreSent)
	{
		if ((key.sym == SDLK_LEFT) && (selection != 0))
		{
		        selection--;
		} else if ((key.sym == SDLK_RIGHT) && (selection != (fail?2:1)))
		{
		        selection++;
		} else if (key.sym == SDLK_RETURN)
		{
			if (fail)
			{
				switch (selection)
				{
					case 0:
						changeState(new GameState());

				                break;
					case 1:
						changeState(new SubmitHighscoreListState(hsname, level));

						break;
					case 2:
						changeState(new TitleState());

						break;
				}
			} else {
				switch (selection)
				{
				        case 0:
				                changeState(new GameState());

				                break;
				        case 1:
				                changeState(new TitleState());

				                break;
				}
			}
		}
	}
}

void SubmitHighscoreListState::render(SDL_Surface* screen)
{
	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));

	if (scoreSent)
	{
		SDL_Rect pSpace;
		if (fail)
		{
			pSpace.x = (selection==0?13:(selection==1?138:284));
			pSpace.y = 448;
		} else {
		        pSpace.x = (selection==0?52:225);
		        pSpace.y = 447;
		}
		pSpace.w = pointer->w;
		pSpace.h = pointer->h;

		SDL_BlitSurface(pointer, NULL, screen, &pSpace);

		SDL_Rect eSpace = {0, newpos*40, 480, 40};
		SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0));
	}

	SDL_BlitSurface(list, NULL, screen, NULL);
}

SubmitHighscoreListState::SubmitHighscoreList::SubmitHighscoreList(char* hsname, int level)
{
	fail = false;

	try
	{
		IPaddress ipaddress;

		if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1)
		{
			printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError());
			throw 1;
		}

		TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress);
		if (!tcpsock)
		{
			printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError());
			throw 2;
		}

		char body[256];
		sprintf(body, "name=%s&level=%d", hsname, level);
		char headers[256];
		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);
		if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers))
		{
			printf("Connection closed by peer: %s\n", SDLNet_GetError());
			throw 3;
		}

		std::stringstream download(std::stringstream::in | std::stringstream::out);
		char hslist[1024];
		SDLNet_TCP_Recv(tcpsock, hslist, 1024);
		download << hslist;
		SDLNet_TCP_Close(tcpsock);

		char temps[256];
		download.getline(temps,256);
		while (strlen(temps) != 1)
		{
			download.getline(temps,256);
		}

		int rank;
		download.getline(temps, 256);
		if (sscanf(temps, "%d%*c", &rank) != 1)
		{
			printf("Recieved data is of an invalid format: %s\n", temps);
			throw 4;
		}

		this->hslist = getGlobalHighscores();

		if (this->hslist.empty())
		{
			printf("Global Highscore List cannot be empty after adding a score to it.\n");
			throw 5;
		}

		if (rank > 10)
		{
			Highscore temph(hsname, level);
			temph.setRank(rank);

			this->hslist[9] = temph;
			this->newpos = 10;
		} else {
			std::vector<Highscore>::iterator it;
			bool found = false;

			for (it=this->hslist.begin(); it<this->hslist.end(); it++)
			{
				Highscore h = *it;

				if (!found)
				{
					if ((strcmp(h.getName(),hsname) == 0) && (h.getLevel() == level))
					{
						this->newpos = h.getRank();
						found = true;
					}
				}
			}
		}
	} catch (int e)
	{
		fail = true;
	}
}

int SubmitHighscoreListState::SubmitHighscoreList::getNewPos()
{
	return newpos;
}

bool SubmitHighscoreListState::SubmitHighscoreList::hasFailed()
{
	return fail;
}