summary refs log tree commit diff stats
path: root/hslist.cpp
blob: 3d9f19a7bb05e54f835444da9ef8e3c2876e81e2 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "hslist.h"
#include <SDL_ttf.h>
#include <SDL_net.h>
#include <sstream>
#include <fstream>
#include "util.h"
#include "titlestate.h"

SDL_Surface* HighscoreList::render()
{
	SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
	Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255);
	SDL_FillRect(tmp, NULL, bgColor);
	SDL_SetColorKey(tmp, SDL_TRUE, bgColor);
	TTF_Font* posFont = loadFont(40);
	TTF_Font* dataFont = loadFont(25);
	SDL_Color fontColor = {0, 0, 0, 0};

	for (int i=0; i<hslist.size(); i++)
	{
		Highscore h = hslist[i];

		int posw, posh;
        char pos[3]; // 2 max characters in rank plus the colon at the end
        sprintf(pos, "%d:", h.getRank());
		TTF_SizeText(posFont, pos, &posw, &posh);
		SDL_Rect posSpace = {0, (i+1)*40, posw, posh};
		SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace);

		int namew, nameh;
		char name[26]; // 25 max characters in username plus the space at the beginning
		sprintf(name, " %s", h.getName());
		TTF_SizeText(dataFont, name, &namew, &nameh);
		SDL_Rect nameSpace = {posw, (i+1)*40+((posh/2)-(nameh/2)), namew, nameh};
		SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, tmp, &nameSpace);

		int lvlw, lvlh;
		char lvl[10]; // 10 max characters in level (based off the fact that 2^32-1 is 10 characters long, and is the highest int)
		sprintf(lvl, "%d", h.getLevel());
		TTF_SizeText(dataFont, lvl, &lvlw, &lvlh);
		SDL_Rect lvlSpace = {480-lvlw, (i+1)*40+((posh/2)-(nameh/2)), lvlw, lvlh};
		SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, &lvlSpace);
	}

	return tmp;
}

std::vector<Highscore> HighscoreList::getLocalHighscores()
{
	std::vector<Highscore> temp = std::vector<Highscore>();

	std::ifstream exists(getDataFile());
	if (exists)
	{
		FILE* hslist = fopen(getDataFile(), "r");
		int scores;
		fscanf(hslist, "%d%*c", &scores);

		for (int i=0; i<scores; i++)
		{
			int namelen;
			char namelens[4];
			char* name = (char*) calloc(25, sizeof(char));
			int score;

			fscanf(hslist, "%d", &namelen);
			sprintf(namelens, "%%%dc", namelen);
			fscanf(hslist, namelens, name);
			fscanf(hslist, "%d%*c", &score);

			Highscore h = Highscore(name, score);
			h.setRank(i+1);

			temp.push_back(h);
		}

		fclose(hslist);
	}

	return temp;
}

std::vector<Highscore> HighscoreList::getGlobalHighscores()
{
	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;
	}

	const char* headers = "GET /mol/hslist.php HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v3.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\n\n";
	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);
	}

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

	for (int i=0; i<scores; i++)
	{
		int namelen;
		char namelens[13];
		char* name = (char*) calloc(25, sizeof(char));
		int score;
		download.getline(temps, 256);

		if (sscanf(temps, "%d", &namelen) != 1)
		{
			printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps);
			throw 4;
		}

		sprintf(namelens, "%%*d%%%dc", namelen);

		if (sscanf(temps, namelens, name) != 1)
		{
			printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps);
			throw 4;
		}

		sprintf(namelens, "%%*d%%*%dc%%d", namelen);

		if (sscanf(temps, namelens, &score) != 1)
		{
			printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps);
			throw 4;
		}

		Highscore h = Highscore(name, score);
		h.setRank(i+1);

		temp.push_back(h);
	}

	return temp;
}

LocalHighscoreList::LocalHighscoreList()
{
	this->hslist = getLocalHighscores();
}

GlobalHighscoreList::GlobalHighscoreList()
{
	fail = false;

	try
	{
		this->hslist = getGlobalHighscores();
	} catch (int e)
	{
		fail = true;
	}
}

SDL_Surface* GlobalHighscoreList::render()
{
	if (fail)
	{
		SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
		Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255);
		SDL_FillRect(tmp, NULL, bgColor);
		SDL_SetColorKey(tmp, SDL_TRUE, bgColor);
		TTF_Font* dataFont = loadFont(25);
		SDL_Color fontColor = {0, 0, 0, 0};
		SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error retrieving highscores", fontColor);
		SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
		SDL_BlitSurface(text, NULL, tmp, &tSpace);

		return tmp;
	} else {
		return super::render();
	}
}

State* ChooseHighscoreListState::operator() (SDL_Renderer* renderer)
{
	SDL_Texture* background = loadImage(renderer, "resources/chl.bmp");
	SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
	int selection = 0;
	SDL_Event e;
	
	for (;;)
	{
		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer, background, NULL, NULL);
		applyTexture(renderer, pointer, 127, selection==0?306:(selection==1?336:396));
		SDL_RenderPresent(renderer);
		
		while (SDL_PollEvent(&e))
		{
			if (e.type == SDL_QUIT)
			{
				return NULL;
			} else if (e.type == SDL_KEYDOWN)
			{
				if ((e.key.keysym.sym == SDLK_UP) && (selection != 0))
				{
					selection--;
				} else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 2))
				{
					selection++;
				} else if (e.key.keysym.sym == SDLK_RETURN)
				{
					switch (selection)
					{
						case 0: return new DisplayLocalHighscoreListState();
						case 1: return new DisplayGlobalHighscoreListState();
						case 2: return new TitleState();
					}
				}
			}
		}
	}
}

State* DisplayLocalHighscoreListState::operator() (SDL_Renderer* renderer)
{
	SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
	
	LocalHighscoreList* lhl = new LocalHighscoreList();
	SDL_Surface* list_s = 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, list_s, &tSpace);
	SDL_FreeSurface(title);

	SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
	SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
	SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
	SDL_FreeSurface(options_s);
	
	SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s);
	SDL_FreeSurface(list_s);
	
	SDL_Event e;
	
	for (;;)
	{
		SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer, list, NULL, NULL);
		applyTexture(renderer, pointer, 137, 449);
		SDL_RenderPresent(renderer);
		
		while (SDL_PollEvent(&e))
		{
			if (e.type == SDL_QUIT)
			{
				return NULL;
			} else if (e.type == SDL_KEYDOWN)
			{
				if (e.key.keysym.sym == SDLK_RETURN)
				{
					return new ChooseHighscoreListState();
				}
			}
		}
	}
}

State* DisplayGlobalHighscoreListState::operator() (SDL_Renderer* renderer)
{
	SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
	
	// Display loading message
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_RenderClear(renderer);
	
	SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
	Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255);
	SDL_FillRect(list_s, NULL, bgColor);
	SDL_SetColorKey(list_s, SDL_TRUE, bgColor);
	TTF_Font* dataFont = loadFont(25);
	SDL_Color fontColor = {0, 0, 0, 0};
	SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor);
	SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
	SDL_BlitSurface(text, NULL, list_s, &aSpace);
	SDL_FreeSurface(text);

	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_s, &tSpace);
	SDL_FreeSurface(title);

	SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
	SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
	SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
	SDL_FreeSurface(options_s);
	
	list = SDL_CreateTextureFromSurface(renderer, list_s);
	SDL_FreeSurface(list_s);
	
	m = SDL_CreateMutex();
	
	// Start downloading scores
	SDL_CreateThread(&LoadHighscoreList, "LoadHighscoreList", this);
	
	// Parse keyboard events
	SDL_Event e;
	
	for (;;)
	{
		if (SDL_LockMutex(m) == 0)
		{
			if (lhl != NULL)
			{
				SDL_Surface* list_s = 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, list_s, &tSpace);
				SDL_FreeSurface(title);

				SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
				SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
				SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
				SDL_FreeSurface(options_s);
	
				list = SDL_CreateTextureFromSurface(renderer, list_s);
				SDL_FreeSurface(list_s);
			
				lhl = NULL;
			}
			
			SDL_UnlockMutex(m);
		}
		
		SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
		SDL_RenderClear(renderer);
		SDL_RenderCopy(renderer, list, NULL, NULL);
		applyTexture(renderer, pointer, 137, 449);
		SDL_RenderPresent(renderer);
		
		while (SDL_PollEvent(&e))
		{
			if (e.type == SDL_QUIT)
			{
				SDL_DestroyMutex(m);
				
				return NULL;
			} else if (e.type == SDL_KEYDOWN)
			{
				if (e.key.keysym.sym == SDLK_RETURN)
				{
					SDL_DestroyMutex(m);
					
					return new ChooseHighscoreListState();
				}
			}
		}
	}
}

int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam)
{
	DisplayGlobalHighscoreListState* parent = ((DisplayGlobalHighscoreListState*)pParam);
	if (SDL_LockMutex(parent->m) == 0)
	{
		parent->lhl = new GlobalHighscoreList();
		
		SDL_UnlockMutex(parent->m);
	} else {
		printf("Couldn't lock mutex: %s\n", SDL_GetError());
	}
}