source: network-game/client/Client/main.cpp@ b28e2bf

Last change on this file since b28e2bf was b28e2bf, checked in by dportnoy <dmp1488@…>, 10 years ago

Client processes server PROFILE message and displays the player's honor points and game history on the profile page

  • Property mode set to 100644
File size: 48.1 KB
Line 
1#include "../../common/Compiler.h"
2
3#if defined WINDOWS
4 #include <winsock2.h>
5 #include <ws2tcpip.h>
6#elif defined LINUX
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <cstring>
13#elif defined MAC
14 #include <netdb.h>
15#endif
16
17#include <cstdio>
18#include <cstdlib>
19//#include <cmath>
20#include <sys/types.h>
21#include <string>
22#include <iostream>
23#include <sstream>
24#include <fstream>
25#include <map>
26#include <vector>
27#include <stdexcept>
28
29#include <allegro5/allegro.h>
30#include <allegro5/allegro_font.h>
31#include <allegro5/allegro_ttf.h>
32#include <allegro5/allegro_primitives.h>
33
34#include "../../common/Common.h"
35#include "../../common/MessageContainer.h"
36#include "../../common/MessageProcessor.h"
37#include "../../common/WorldMap.h"
38#include "../../common/Player.h"
39#include "../../common/Projectile.h"
40#include "../../common/Game.h"
41#include "../../common/GameSummary.h"
42
43#include "Window.h"
44#include "TextLabel.h"
45#include "Button.h"
46#include "Textbox.h"
47#include "RadioButtonList.h"
48
49#include "GameRender.h"
50
51#include "chat.h"
52
53#ifdef WINDOWS
54 #pragma comment(lib, "ws2_32.lib")
55#endif
56
57using namespace std;
58
59void initWinSock();
60void shutdownWinSock();
61void createGui(ALLEGRO_FONT* font);
62
63void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId);
64void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
65void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
66
67int getRefreshRate(int width, int height);
68void drawMessageStatus(ALLEGRO_FONT* font);
69
70// Callback declarations
71void goToLoginScreen();
72void goToRegisterScreen();
73void registerAccount();
74void login();
75void logout();
76void quit();
77void sendChatMessage();
78void toggleDebugging();
79void goToProfileScreen();
80void goToLobbyScreen();
81void joinGame(); // for joining the game lobby
82void createGame(); // for joining the game lobby
83void joinWaitingArea();
84void joinRedTeam();
85void joinBlueTeam();
86void startGame(); // for leaving game lobby and starting the actual game
87void leaveGame();
88void closeGameSummary();
89
90const float FPS = 60;
91const int SCREEN_W = 1024;
92const int SCREEN_H = 768;
93
94enum STATE {
95 STATE_START,
96 STATE_LOBBY,
97 STATE_GAME_LOBBY,
98 STATE_GAME
99};
100
101int state;
102
103bool doexit;
104
105vector<GuiComponent*> vctComponents;
106
107Window* wndLogin;
108Window* wndRegister;
109Window* wndLobby;
110Window* wndLobbyDebug;
111Window* wndProfile;
112Window* wndGameLobby;
113Window* wndGame;
114Window* wndGameSummary;
115Window* wndCurrent;
116
117// wndLogin
118Textbox* txtUsername;
119Textbox* txtPassword;
120TextLabel* lblLoginStatus;
121
122// wndRegister
123Textbox* txtUsernameRegister;
124Textbox* txtPasswordRegister;
125RadioButtonList* rblClasses;
126TextLabel* lblRegisterStatus;
127
128// wndLobby
129Textbox* txtJoinGame;
130Textbox* txtCreateGame;
131Textbox* txtChat;
132
133int sock;
134struct sockaddr_in server, from;
135struct hostent *hp;
136NETWORK_MSG msgTo, msgFrom;
137string username;
138chat chatConsole, debugConsole;
139bool debugging;
140Game* game;
141GameSummary* gameSummary;
142Player* currentPlayer;
143
144int honorPoints;
145int numGames;
146int** gameHistory;
147
148MessageProcessor msgProcessor;
149
150int main(int argc, char **argv)
151{
152 ALLEGRO_DISPLAY *display = NULL;
153 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
154 ALLEGRO_TIMER *timer = NULL;
155 map<unsigned int, Player*> mapPlayers;
156 map<string, int> mapGames;
157 unsigned int curPlayerId = -1;
158 ofstream outputLog;
159
160 doexit = false;
161 debugging = false;
162 bool redraw = true;
163 bool fullscreen = false;
164 game = NULL;
165 gameSummary = NULL;
166
167 honorPoints = 0;
168 numGames = 0;
169 gameHistory = NULL;
170
171 state = STATE_START;
172
173 if(!al_init()) {
174 fprintf(stderr, "failed to initialize allegro!\n");
175 return -1;
176 }
177
178 outputLog.open("client.log", ios::app);
179 outputLog << "Started client on " << getCurrentDateTimeString() << endl;
180
181 if (al_init_primitives_addon())
182 cout << "Primitives initialized" << endl;
183 else
184 cout << "Primitives not initialized" << endl;
185
186 al_init_font_addon();
187 al_init_ttf_addon();
188
189 ALLEGRO_FONT* font;
190 #if defined WINDOWS
191 font = al_load_ttf_font("../pirulen.ttf", 12, 0);
192 #elif defined LINUX
193 font = al_load_ttf_font("pirulen.ttf", 12, 0);
194 #elif defined MAC
195 font = al_load_ttf_font("pirulen.ttf", 12, 0);
196 #endif
197
198 if (!font) {
199 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
200 getchar();
201 return -1;
202 }
203
204 if(!al_install_keyboard()) {
205 fprintf(stderr, "failed to initialize the keyboard!\n");
206 return -1;
207 }
208
209 if(!al_install_mouse()) {
210 fprintf(stderr, "failed to initialize the mouse!\n");
211 return -1;
212 }
213
214 timer = al_create_timer(1.0 / FPS);
215 if(!timer) {
216 fprintf(stderr, "failed to create timer!\n");
217 return -1;
218 }
219
220 int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
221 // if the computer doesn't support this resolution, just use windowed mode
222 if (refreshRate > 0 && fullscreen) {
223 al_set_new_display_flags(ALLEGRO_FULLSCREEN);
224 al_set_new_display_refresh_rate(refreshRate);
225 }
226 display = al_create_display(SCREEN_W, SCREEN_H);
227 if(!display) {
228 fprintf(stderr, "failed to create display!\n");
229 al_destroy_timer(timer);
230 return -1;
231 }
232
233 debugConsole.addLine("Debug console:");
234 debugConsole.addLine("");
235
236 createGui(font);
237
238 goToLoginScreen();
239
240 event_queue = al_create_event_queue();
241 if(!event_queue) {
242 fprintf(stderr, "failed to create event_queue!\n");
243 al_destroy_display(display);
244 al_destroy_timer(timer);
245 return -1;
246 }
247
248 al_set_target_bitmap(al_get_backbuffer(display));
249
250 al_register_event_source(event_queue, al_get_display_event_source(display));
251 al_register_event_source(event_queue, al_get_timer_event_source(timer));
252 al_register_event_source(event_queue, al_get_keyboard_event_source());
253 al_register_event_source(event_queue, al_get_mouse_event_source());
254
255 al_clear_to_color(al_map_rgb(0,0,0));
256
257 al_flip_display();
258
259 if (argc != 3) {
260 cout << "Usage: server port" << endl;
261 exit(1);
262 }
263
264 initWinSock();
265
266 sock = socket(AF_INET, SOCK_DGRAM, 0);
267 if (sock < 0)
268 error("socket");
269
270 set_nonblock(sock);
271
272 server.sin_family = AF_INET;
273 hp = gethostbyname(argv[1]);
274 if (hp == 0)
275 error("Unknown host");
276
277 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
278 server.sin_port = htons(atoi(argv[2]));
279
280 msgProcessor = MessageProcessor(sock, &outputLog);
281
282 al_start_timer(timer);
283
284 while (!doexit)
285 {
286 ALLEGRO_EVENT ev;
287
288 al_wait_for_event(event_queue, &ev);
289
290 if(wndCurrent->handleEvent(ev)) {
291 // do nothing
292 }
293 else if(ev.type == ALLEGRO_EVENT_TIMER) {
294 redraw = true;
295
296 // remove any other timer events in the queue
297 while (al_peek_next_event(event_queue, &ev) && ev.type == ALLEGRO_EVENT_TIMER) {
298 al_get_next_event(event_queue, &ev);
299 }
300 }
301 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
302 doexit = true;
303 }
304 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
305 }
306 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
307 switch(ev.keyboard.keycode) {
308 case ALLEGRO_KEY_ESCAPE:
309 doexit = true;
310 break;
311 case ALLEGRO_KEY_S: // pickup an item next to you
312 if (state == STATE_GAME) {
313 msgTo.type = MSG_TYPE_PICKUP_FLAG;
314 memcpy(msgTo.buffer, &curPlayerId, 4);
315 msgProcessor.sendMessage(&msgTo, &server);
316 }
317 break;
318 case ALLEGRO_KEY_D: // drop the current item
319 if (state == STATE_GAME) {
320 try {
321 Player* p = mapPlayers.at(curPlayerId);
322 int flagType = OBJECT_NONE;
323
324 if (p->hasBlueFlag)
325 flagType = OBJECT_BLUE_FLAG;
326 else if (p->hasRedFlag)
327 flagType = OBJECT_RED_FLAG;
328
329 if (flagType != OBJECT_NONE) {
330 msgTo.type = MSG_TYPE_DROP_FLAG;
331 memcpy(msgTo.buffer, &curPlayerId, 4);
332 msgProcessor.sendMessage(&msgTo, &server);
333 }
334 } catch (const out_of_range& ex) {}
335 }
336 break;
337 }
338 }
339 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
340 if (wndCurrent == wndGame) {
341 if (ev.mouse.button == 1) { // left click
342 msgTo.type = MSG_TYPE_PLAYER_MOVE;
343
344 POSITION pos;
345 pos.x = ev.mouse.x;
346 pos.y = ev.mouse.y;
347 pos = screenToMap(pos);
348
349 if (pos.x != -1)
350 {
351 memcpy(msgTo.buffer, &curPlayerId, 4);
352 memcpy(msgTo.buffer+4, &pos.x, 4);
353 memcpy(msgTo.buffer+8, &pos.y, 4);
354
355 msgProcessor.sendMessage(&msgTo, &server);
356 }
357 else
358 cout << "Invalid point: User did not click on the map" << endl;
359 }else if (ev.mouse.button == 2) { // right click
360 cout << "Detected a right-click" << endl;
361 map<unsigned int, Player*>::iterator it;
362
363 Player* curPlayer = mapPlayers[curPlayerId];;
364
365 cout << "Got current player" << endl;
366 cout << "current game: " << game << endl;
367
368 map<unsigned int, Player*> playersInGame = game->getPlayers();
369 Player* target;
370
371 for (it = playersInGame.begin(); it != playersInGame.end(); it++)
372 {
373 target = it->second;
374 cout << "set target" << endl;
375 if (target->team != curPlayer->team)
376 {
377 cout << "Found valid target" << endl;
378
379 POSITION cursorPos;
380 cursorPos.x = ev.mouse.x;
381 cursorPos.y = ev.mouse.y;
382 cursorPos = screenToMap(cursorPos);
383
384 float distance =posDistance(cursorPos.toFloat(), target->pos);
385
386 if (distance < 25) {
387 unsigned int targetId = target->getId();
388
389 msgTo.type = MSG_TYPE_ATTACK;
390 memcpy(msgTo.buffer, &curPlayerId, 4);
391 memcpy(msgTo.buffer+4, &targetId, 4);
392
393 msgProcessor.sendMessage(&msgTo, &server);
394 }
395 }
396 }
397 }
398 }
399 }
400
401 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
402 processMessage(msgFrom, state, chatConsole, mapPlayers, mapGames, curPlayerId);
403
404 if (redraw)
405 {
406 redraw = false;
407
408 msgProcessor.resendUnackedMessages();
409
410 if (debugging && wndCurrent == wndLobby)
411 wndLobbyDebug->draw(display);
412 else
413 wndCurrent->draw(display);
414
415 if (wndCurrent == wndLobby) {
416 if (!debugging)
417 chatConsole.draw(font, al_map_rgb(255,255,255));
418
419 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120, ALLEGRO_ALIGN_LEFT, "Current Games");
420
421 map<string, int>::iterator it;
422 int i=0;
423 ostringstream oss;
424 for (it = mapGames.begin(); it != mapGames.end(); it++) {
425 oss << it->first << " (" << it->second << " players)" << endl;
426 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
427 oss.clear();
428 oss.str("");
429 i++;
430 }
431
432 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*3/4-100, 120, ALLEGRO_ALIGN_LEFT, "Online Players");
433
434 map<unsigned int, Player*>::iterator itPlayers;
435 i=0;
436 for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++) {
437 oss << itPlayers->second->name << endl;
438 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*3/4-100, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
439 oss.clear();
440 oss.str("");
441 i++;
442 }
443 }
444 else if (wndCurrent == wndProfile)
445 {
446 ostringstream oss;
447 oss << "Honor Points: " << honorPoints << endl;
448 al_draw_text(font, al_map_rgb(0, 255, 0), 65, 90, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
449 oss.clear();
450 oss.str("");
451
452 // display records of the last 10 games
453 for (int i=0; i<numGames; i++) {
454
455 if (gameHistory[i][0] == 0)
456 oss << "DEFEAT" << endl;
457 else if (gameHistory[i][0] == 1)
458 oss << "VICTORY" << endl;
459
460 al_draw_text(font, al_map_rgb(0, 255, 0), 142, 160+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
461 oss.clear();
462 oss.str("");
463
464 oss << gameHistory[i][2] << endl;
465 al_draw_text(font, al_map_rgb(0, 255, 0), 302, 160+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
466 oss.clear();
467 oss.str("");
468
469 oss << gameHistory[i][3] << endl;
470 al_draw_text(font, al_map_rgb(0, 255, 0), 462, 160+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
471 oss.clear();
472 oss.str("");
473
474 oss << "6/11/2014, 5:12 PM" << endl;
475 al_draw_text(font, al_map_rgb(0, 255, 0), 622, 160+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
476 oss.clear();
477 oss.str("");
478
479 }
480
481 }
482 else if (wndCurrent == wndGameLobby)
483 {
484 al_draw_text(font, al_map_rgb(0, 255, 0), 200, 100, ALLEGRO_ALIGN_LEFT, "Waiting Area");
485 al_draw_text(font, al_map_rgb(0, 255, 0), 400, 100, ALLEGRO_ALIGN_LEFT, "Blue Team");
486 al_draw_text(font, al_map_rgb(0, 255, 0), 600, 100, ALLEGRO_ALIGN_LEFT, "Red Team");
487
488 int drawPosition = 0;
489
490 map<unsigned int, Player*> gamePlayers = game->getPlayers();
491 map<unsigned int, Player*>::iterator itPlayers;
492 ostringstream oss;
493 int i=0;
494 for (itPlayers = gamePlayers.begin(); itPlayers != gamePlayers.end(); itPlayers++) {
495 switch (itPlayers->second->team) {
496 case -1:
497 drawPosition = 200;
498 break;
499 case 0:
500 drawPosition = 400;
501 break;
502 case 1:
503 drawPosition = 600;
504 break;
505 }
506
507 oss << itPlayers->second->name << endl;
508 al_draw_text(font, al_map_rgb(0, 255, 0), drawPosition, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
509 oss.clear();
510 oss.str("");
511 i++;
512 }
513 }
514 else if (wndCurrent == wndGame)
515 {
516 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
517
518 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
519 map<unsigned int, Player*>::iterator it;
520
521 if (!debugging) {
522 int playerCount = 0;
523 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
524 {
525 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
526 playerCount++;
527 }
528 }
529
530 ostringstream ossScoreBlue, ossScoreRed;
531
532 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
533 ossScoreRed << "Red: " << game->getRedScore() << endl;
534
535 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
536 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
537
538 // update players
539 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
540 {
541 it->second->updateTarget(game->getPlayers());
542 }
543
544 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
545 {
546 it->second->move(game->getMap()); // ignore return value
547 }
548
549 // update projectile positions
550 map<unsigned int, Projectile>::iterator it2;
551 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
552 {
553 it2->second.move(game->getPlayers());
554 }
555
556 GameRender::drawMap(game->getMap());
557 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
558 GameRender::drawProjectiles(game->getProjectiles(), game->getPlayers());
559 }
560 else if (wndCurrent == wndGameSummary)
561 {
562 ostringstream ossBlueScore, ossRedScore;
563
564 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
565 ossRedScore << "Red Score: " << gameSummary->getRedScore();
566
567 string strWinner;
568
569 if (gameSummary->getWinner() == 0)
570 strWinner = "Blue Team Wins";
571 else if (gameSummary->getWinner() == 1)
572 strWinner = "Red Team Wins";
573 else
574 strWinner = "winner set to wrong value";
575
576 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
577 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
578 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
579 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
580 }
581
582 if (debugging) {
583 drawMessageStatus(font);
584 }
585
586 al_flip_display();
587 }
588 }
589
590 #if defined WINDOWS
591 closesocket(sock);
592 #elif defined LINUX
593 close(sock);
594 #endif
595
596 shutdownWinSock();
597
598 // delete all components
599 for (unsigned int x=0; x<vctComponents.size(); x++)
600 delete vctComponents[x];
601
602 delete wndLogin;
603 delete wndRegister;
604 delete wndLobby;
605 delete wndLobbyDebug;
606 delete wndGameLobby;
607 delete wndGame;
608 delete wndGameSummary;
609
610 // game should be deleted when the player leaves a gamw
611 if (game != NULL)
612 delete game;
613
614 if (gameSummary != NULL)
615 delete gameSummary;
616
617 map<unsigned int, Player*>::iterator it;
618
619 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
620 delete it->second;
621 }
622
623 if (gameHistory != NULL) {
624 for (int i=0; i<numGames; i++) {
625 free(gameHistory[i]);
626 }
627
628 free(gameHistory);
629 }
630
631 al_destroy_event_queue(event_queue);
632 al_destroy_display(display);
633 al_destroy_timer(timer);
634
635 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
636 outputLog.close();
637
638 return 0;
639}
640
641void initWinSock()
642{
643#if defined WINDOWS
644 WORD wVersionRequested;
645 WSADATA wsaData;
646 int wsaerr;
647
648 wVersionRequested = MAKEWORD(2, 2);
649 wsaerr = WSAStartup(wVersionRequested, &wsaData);
650
651 if (wsaerr != 0) {
652 cout << "The Winsock dll not found." << endl;
653 exit(1);
654 }else
655 cout << "The Winsock dll was found." << endl;
656#endif
657}
658
659void shutdownWinSock()
660{
661#if defined WINDOWS
662 WSACleanup();
663#endif
664}
665
666void createGui(ALLEGRO_FONT* font) {
667
668 // wndLogin
669
670 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
671 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 40, 100, 20, font)));
672 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 70, 100, 20, font)));
673 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
674 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
675 vctComponents.push_back(wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
676 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen)));
677 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login)));
678 vctComponents.push_back(wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
679 vctComponents.push_back(wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
680
681 txtUsername = (Textbox*)wndLogin->getComponent(0);
682 txtPassword = (Textbox*)wndLogin->getComponent(1);
683 lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
684
685 cout << "Created login screen" << endl;
686
687
688 // wndRegister
689
690 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
691 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 40, 100, 20, font)));
692 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 70, 100, 20, font)));
693 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
694 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
695 vctComponents.push_back(wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font)));
696 vctComponents.push_back(wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
697 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen)));
698 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount)));
699 vctComponents.push_back(wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
700 vctComponents.push_back(wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
701
702 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
703 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
704
705 rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
706 rblClasses->addRadioButton("Warrior");
707 rblClasses->addRadioButton("Ranger");
708
709 lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
710
711 cout << "Created register screen" << endl;
712
713
714 // wndLobby
715
716 txtJoinGame = new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font);
717 vctComponents.push_back(txtJoinGame);
718
719 txtCreateGame = new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font);
720 vctComponents.push_back(txtCreateGame);
721
722 wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
723 vctComponents.push_back(wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
724 vctComponents.push_back(wndLobby->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
725 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
726 wndLobby->addComponent(txtJoinGame);
727 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
728 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
729 wndLobby->addComponent(txtCreateGame);
730 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
731 vctComponents.push_back(wndLobby->addComponent(new Textbox(95, 40, 300, 20, font)));
732 vctComponents.push_back(wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage)));
733 vctComponents.push_back(wndLobby->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
734
735 txtChat = (Textbox*)wndLobby->getComponent(7);
736
737 cout << "Created lobby screen" << endl;
738
739
740 // wndLobbyDebug
741
742 wndLobbyDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
743 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
744 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
745 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
746 wndLobbyDebug->addComponent(txtJoinGame);
747 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
748 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
749 wndLobbyDebug->addComponent(txtCreateGame);
750 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
751 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
752
753 cout << "Created debug lobby screen" << endl;
754
755
756 // wndProfile
757
758 wndProfile = new Window(0, 0, SCREEN_W, SCREEN_H);
759 vctComponents.push_back(wndProfile->addComponent(new TextLabel(450, 40, 124, 20, font, "Profile", ALLEGRO_ALIGN_CENTRE)));
760 vctComponents.push_back(wndProfile->addComponent(new TextLabel(160, 120, 124, 20, font, "Game History", ALLEGRO_ALIGN_CENTRE)));
761 vctComponents.push_back(wndProfile->addComponent(new TextLabel(600, 160, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
762 vctComponents.push_back(wndProfile->addComponent(new TextLabel(80, 160, 124, 20, font, "Result", ALLEGRO_ALIGN_CENTRE)));
763 vctComponents.push_back(wndProfile->addComponent(new TextLabel(240, 160, 124, 20, font, "Blue Score", ALLEGRO_ALIGN_CENTRE)));
764 vctComponents.push_back(wndProfile->addComponent(new TextLabel(400, 160, 124, 20, font, "Red Score", ALLEGRO_ALIGN_CENTRE)));
765 vctComponents.push_back(wndProfile->addComponent(new TextLabel(560, 160, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
766 vctComponents.push_back(wndProfile->addComponent(new Button(920, 738, 80, 20, font, "Back", goToLobbyScreen)));
767
768
769 // wndGameLobby
770
771 wndGameLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
772 vctComponents.push_back(wndGameLobby->addComponent(new Button(180, 120, 160, 300, font, "", joinWaitingArea)));
773 vctComponents.push_back(wndGameLobby->addComponent(new Button(380, 120, 160, 300, font, "", joinBlueTeam)));
774 vctComponents.push_back(wndGameLobby->addComponent(new Button(580, 120, 160, 300, font, "", joinRedTeam)));
775 vctComponents.push_back(wndGameLobby->addComponent(new Button(40, 600, 120, 20, font, "Leave Game", leaveGame)));
776 vctComponents.push_back(wndGameLobby->addComponent(new Button(800, 600, 120, 20, font, "Start Game", startGame)));
777
778
779 // wndGame
780
781 wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
782 vctComponents.push_back(wndGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame)));
783
784 cout << "Created new game screen" << endl;
785
786 wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
787 vctComponents.push_back(wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary)));
788
789 cout << "Created game summary screen" << endl;
790}
791
792void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId)
793{
794 cout << "Total players in map: " << mapPlayers.size() << endl;
795
796 // this is outdated since most messages now don't contain just a text string
797 string response = string(msg.buffer);
798
799 switch(state)
800 {
801 case STATE_START:
802 {
803 cout << "In STATE_START" << endl;
804
805 switch(msg.type)
806 {
807 case MSG_TYPE_REGISTER:
808 {
809 lblRegisterStatus->setText(response);
810 break;
811 }
812 default:
813 {
814 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
815 break;
816 }
817 }
818
819 break;
820 }
821 case STATE_LOBBY:
822 {
823 cout << "In STATE_LOBBY" << endl;
824 switch(msg.type)
825 {
826 case MSG_TYPE_LOGIN:
827 {
828 if (response.compare("Player has already logged in.") == 0)
829 {
830 goToLoginScreen();
831 state = STATE_START;
832
833 lblLoginStatus->setText(response);
834 }
835 else if (response.compare("Incorrect username or password") == 0)
836 {
837 goToLoginScreen();
838 state = STATE_START;
839
840 lblLoginStatus->setText(response);
841 }
842 else
843 {
844 wndCurrent = wndLobby;
845
846 // this message should only be sent when a player first logs in so they know their id
847
848 Player* p = new Player("", "");
849 p->deserialize(msg.buffer);
850
851 if (mapPlayers.find(p->getId()) != mapPlayers.end())
852 delete mapPlayers[p->getId()];
853 mapPlayers[p->getId()] = p;
854 curPlayerId = p->getId();
855 currentPlayer = mapPlayers[curPlayerId];
856
857 cout << "Got a valid login response with the player" << endl;
858 cout << "Player id: " << curPlayerId << endl;
859 cout << "Player health: " << p->health << endl;
860 cout << "player map size: " << mapPlayers.size() << endl;
861 }
862
863 break;
864 }
865 case MSG_TYPE_LOGOUT:
866 {
867 cout << "Got a logout message" << endl;
868
869 unsigned int playerId;
870
871 // Check if it's about you or another player
872 memcpy(&playerId, msg.buffer, 4);
873 response = string(msg.buffer+4);
874
875 if (playerId == curPlayerId)
876 {
877 cout << "Got logout message for self" << endl;
878
879 if (response.compare("You have successfully logged out.") == 0)
880 {
881 cout << "Logged out" << endl;
882 state = STATE_START;
883 goToLoginScreen();
884 }
885
886 // if there was an error logging out, nothing happens
887 }
888 else
889 {
890 delete mapPlayers[playerId];
891 mapPlayers.erase(playerId);
892 }
893
894 break;
895 }
896 case MSG_TYPE_CHAT:
897 {
898 chatConsole.addLine(response);
899
900 break;
901 }
902 case MSG_TYPE_PROFILE:
903 {
904 memcpy(&honorPoints, msg.buffer, 4);
905 memcpy(&numGames, msg.buffer+4, 4);
906
907 cout << "Got records for " << numGames << " games." << endl;
908 gameHistory = (int**)malloc(numGames*sizeof(int*));
909 for (int i=0; i<numGames; i++) {
910 gameHistory[i] = (int*)malloc(4*sizeof(int));
911 cout << endl << "game record " << (i+1) << endl;
912
913 memcpy(&gameHistory[i][0], msg.buffer+8+i*16, 4);
914 memcpy(&gameHistory[i][1], msg.buffer+12+i*16, 4);
915 memcpy(&gameHistory[i][2], msg.buffer+16+i*16, 4);
916 memcpy(&gameHistory[i][3], msg.buffer+20+i*16, 4);
917
918 cout << "result: " << gameHistory[i][0] << endl;
919 cout << "team: " << gameHistory[i][1] << endl;
920 cout << "blue score: " << gameHistory[i][2] << endl;
921 cout << "red score: " << gameHistory[i][3] << endl;
922 }
923
924 wndCurrent = wndProfile;
925
926 break;
927 }
928 case MSG_TYPE_JOIN_GAME_SUCCESS:
929 {
930 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
931
932 string gameName(msg.buffer);
933
934 #if defined WINDOWS
935 game = new Game(gameName, "../../data/map.txt", &msgProcessor);
936 #elif defined LINUX
937 game = new Game(gameName, "../data/map.txt", &msgProcessor);
938 #elif defined MAC
939 game = new Game(gameName, "../data/map.txt", &msgProcessor);
940 #endif
941
942 cout << "Game name: " << gameName << endl;
943
944 state = STATE_GAME_LOBBY;
945 wndCurrent = wndGameLobby;
946
947 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
948 strcpy(msgTo.buffer, gameName.c_str());
949
950 msgProcessor.sendMessage(&msgTo, &server);
951
952 break;
953 }
954 case MSG_TYPE_JOIN_GAME_FAILURE:
955 {
956 cout << "Received a JOIN_GAME_FAILURE message" << endl;
957
958 break;
959 }
960 case MSG_TYPE_PLAYER:
961 {
962 handleMsgPlayer(msg, mapPlayers, mapGames);
963
964 break;
965 }
966 case MSG_TYPE_GAME_INFO:
967 {
968 handleMsgGameInfo(msg, mapPlayers, mapGames);
969
970 break;
971 }
972 default:
973 {
974 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
975
976 break;
977 }
978 }
979
980 break;
981 }
982 case STATE_GAME_LOBBY:
983 {
984 cout << "(STATE_GAME_LOBBY) ";
985 switch(msg.type)
986 {
987 case MSG_TYPE_START_GAME:
988 {
989 state = STATE_GAME;
990 wndCurrent = wndGame;
991
992 break;
993 }
994 default:
995 {
996 // keep these lines commented until until the correct messages are moved into the STATE_GAME_LOBBY section
997 //cout << "Received invalid message of type " << msg.type << endl;
998
999 //break;
1000 }
1001 }
1002 }
1003 case STATE_GAME:
1004 {
1005 cout << "(STATE_GAME) ";
1006 switch(msg.type)
1007 {
1008 case MSG_TYPE_SCORE:
1009 {
1010 cout << "Received SCORE message!" << endl;
1011
1012 int blueScore;
1013 memcpy(&blueScore, msg.buffer, 4);
1014 cout << "blue score: " << blueScore << endl;
1015 game->setBlueScore(blueScore);
1016
1017 int redScore;
1018 memcpy(&redScore, msg.buffer+4, 4);
1019 cout << "red score: " << redScore << endl;
1020 game->setRedScore(redScore);
1021
1022 cout << "Processed SCORE message!" << endl;
1023
1024 break;
1025 }
1026 case MSG_TYPE_FINISH_GAME:
1027 {
1028 cout << "Got a finish game message" << endl;
1029 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
1030
1031 unsigned int winner, blueScore, redScore;
1032 memcpy(&winner, msg.buffer, 4);
1033 memcpy(&blueScore, msg.buffer+4, 4);
1034 memcpy(&redScore, msg.buffer+8, 4);
1035
1036 string gameName(msg.buffer+12);
1037
1038 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
1039
1040 delete game;
1041 game = NULL;
1042 state = STATE_LOBBY;
1043 wndCurrent = wndGameSummary;
1044
1045 break;
1046 }
1047 case MSG_TYPE_LOGOUT:
1048 {
1049 cout << "Got a logout message" << endl;
1050
1051 unsigned int playerId;
1052
1053 // Check if it's about you or another player
1054 memcpy(&playerId, msg.buffer, 4);
1055 response = string(msg.buffer+4);
1056
1057 if (playerId == curPlayerId)
1058 cout << "Received MSG_TYPE_LOGOUT for self in STATE_GAME. This shouldn't happen." << endl;
1059 else {
1060 delete mapPlayers[playerId];
1061 mapPlayers.erase(playerId);
1062 }
1063
1064 break;
1065 }
1066 case MSG_TYPE_PLAYER_JOIN_GAME:
1067 {
1068 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
1069
1070 Player p("", "");
1071 p.deserialize(msg.buffer);
1072 cout << "Deserialized player" << endl;
1073 cout << "player team: " << p.team << endl;
1074 cout << "current player team: " << currentPlayer->team << endl;
1075 p.timeLastUpdated = getCurrentMillis();
1076 p.isChasing = false;
1077 if (p.health <= 0)
1078 p.isDead = true;
1079 else
1080 p.isDead = false;
1081
1082 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1083 *(mapPlayers[p.getId()]) = p;
1084 else
1085 mapPlayers[p.getId()] = new Player(p);
1086
1087 game->addPlayer(mapPlayers[p.getId()], false);
1088
1089 break;
1090 }
1091 case MSG_TYPE_LEAVE_GAME:
1092 {
1093 cout << "Received a LEAVE_GAME message" << endl;
1094
1095 string gameName(msg.buffer+4);
1096 unsigned int playerId;
1097
1098 memcpy(&playerId, msg.buffer, 4);
1099
1100 game->removePlayer(playerId);
1101
1102 break;
1103 }
1104 case MSG_TYPE_PLAYER_MOVE:
1105 {
1106 cout << "Received PLAYER_MOVE message" << endl;
1107
1108 unsigned int id;
1109 int x, y;
1110
1111 memcpy(&id, msg.buffer, 4);
1112 memcpy(&x, msg.buffer+4, 4);
1113 memcpy(&y, msg.buffer+8, 4);
1114
1115 cout << "id: " << id << endl;
1116
1117 mapPlayers[id]->target.x = x;
1118 mapPlayers[id]->target.y = y;
1119
1120 mapPlayers[id]->isChasing = false;
1121 mapPlayers[id]->setTargetPlayer(0);
1122
1123 break;
1124 }
1125 case MSG_TYPE_OBJECT:
1126 {
1127 cout << "Received object message in STATE_GAME" << endl;
1128
1129 WorldMap::Object o(0, OBJECT_NONE, 0, 0);
1130 o.deserialize(msg.buffer);
1131 cout << "object id: " << o.id << endl;
1132 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1133
1134 break;
1135 }
1136 case MSG_TYPE_REMOVE_OBJECT:
1137 {
1138 cout << "Received REMOVE_OBJECT message!" << endl;
1139
1140 int id;
1141 memcpy(&id, msg.buffer, 4);
1142
1143 cout << "Removing object with id " << id << endl;
1144
1145 if (!game->getMap()->removeObject(id))
1146 cout << "Did not remove the object" << endl;
1147
1148 break;
1149 }
1150 case MSG_TYPE_ATTACK:
1151 {
1152 cout << "Received START_ATTACK message" << endl;
1153
1154 unsigned int id, targetId;
1155 memcpy(&id, msg.buffer, 4);
1156 memcpy(&targetId, msg.buffer+4, 4);
1157
1158 cout << "source id: " << id << endl;
1159 cout << "target id: " << targetId << endl;
1160
1161 // need to check the target exists in the current game
1162 Player* source = game->getPlayers()[id];
1163 source->setTargetPlayer(targetId);
1164 source->isChasing = true;
1165
1166 break;
1167 }
1168 case MSG_TYPE_PROJECTILE:
1169 {
1170 cout << "Received a PROJECTILE message" << endl;
1171
1172 unsigned int projId, x, y, targetId;
1173
1174 memcpy(&projId, msg.buffer, 4);
1175 memcpy(&x, msg.buffer+4, 4);
1176 memcpy(&y, msg.buffer+8, 4);
1177 memcpy(&targetId, msg.buffer+12, 4);
1178
1179 cout << "projId: " << projId << endl;
1180 cout << "x: " << x << endl;
1181 cout << "y: " << y << endl;
1182 cout << "Target: " << targetId << endl;
1183
1184 Projectile proj(x, y, targetId, 0);
1185 proj.setId(projId);
1186
1187 game->addProjectile(proj);
1188
1189 break;
1190 }
1191 case MSG_TYPE_REMOVE_PROJECTILE:
1192 {
1193 cout << "Received a REMOVE_PROJECTILE message" << endl;
1194
1195 unsigned int id;
1196 memcpy(&id, msg.buffer, 4);
1197
1198 game->removeProjectile(id);
1199
1200 break;
1201 }
1202 case MSG_TYPE_PLAYER:
1203 {
1204 handleMsgPlayer(msg, mapPlayers, mapGames);
1205
1206 break;
1207 }
1208 case MSG_TYPE_GAME_INFO:
1209 {
1210 handleMsgGameInfo(msg, mapPlayers, mapGames);
1211
1212 break;
1213 }
1214 default:
1215 {
1216 cout << "Received invalid message of type " << msg.type << endl;
1217
1218 break;
1219 }
1220 }
1221
1222 break;
1223 }
1224 default:
1225 {
1226 cout << "The state has an invalid value: " << state << endl;
1227
1228 break;
1229 }
1230 }
1231}
1232
1233int getRefreshRate(int width, int height)
1234{
1235 int numRefreshRates = al_get_num_display_modes();
1236 ALLEGRO_DISPLAY_MODE displayMode;
1237
1238 for(int i=0; i<numRefreshRates; i++) {
1239 al_get_display_mode(i, &displayMode);
1240
1241 if (displayMode.width == width && displayMode.height == height)
1242 return displayMode.refresh_rate;
1243 }
1244
1245 return 0;
1246}
1247
1248void drawMessageStatus(ALLEGRO_FONT* font)
1249{
1250 int clientMsgOffset = 5;
1251 int serverMsgOffset = 950;
1252
1253 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1254 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1255 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1256
1257 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1258
1259 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1260 int id, type;
1261 bool acked;
1262 ostringstream ossId, ossAcked;
1263
1264 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1265
1266 int msgCount = 0;
1267 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1268 map<unsigned long, MessageContainer> playerMessage = it->second;
1269 map<unsigned long, MessageContainer>::iterator it2;
1270 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1271
1272 id = it->first;
1273 ossId.str("");;
1274 ossId << id;
1275
1276 type = it2->second.getMessage()->type;
1277 string typeStr = MessageContainer::getMsgTypeString(type);
1278
1279 acked = it2->second.getAcked();
1280 ossAcked.str("");;
1281 ossAcked << boolalpha << acked;
1282
1283 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1284 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1285 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1286
1287 msgCount++;
1288 }
1289 }
1290
1291 if (msgProcessor.getAckedMessages().size() > 0) {
1292 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1293 map<unsigned int, unsigned long long>::iterator it3;
1294
1295 msgCount = 0;
1296 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1297 ossId.str("");;
1298 ossId << it3->first;
1299
1300 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1301
1302 msgCount++;
1303 }
1304 }
1305}
1306
1307// message handling functions
1308
1309void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1310 cout << "Received MSG_TYPE_PLAYER" << endl;
1311
1312 Player p("", "");
1313 p.deserialize(msg.buffer);
1314 p.timeLastUpdated = getCurrentMillis();
1315 p.isChasing = false;
1316 if (p.health <= 0)
1317 p.isDead = true;
1318 else
1319 p.isDead = false;
1320
1321 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1322 *(mapPlayers[p.getId()]) = p;
1323 else
1324 mapPlayers[p.getId()] = new Player(p);
1325}
1326
1327void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1328 cout << "Received a GAME_INFO message" << endl;
1329
1330 string gameName(msg.buffer+4);
1331 int numPlayers;
1332
1333 memcpy(&numPlayers, msg.buffer, 4);
1334
1335 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
1336
1337 if (numPlayers > 0)
1338 mapGames[gameName] = numPlayers;
1339 else
1340 mapGames.erase(gameName);
1341}
1342
1343// Callback definitions
1344
1345void goToRegisterScreen()
1346{
1347 txtUsernameRegister->clear();
1348 txtPasswordRegister->clear();
1349 lblRegisterStatus->setText("");
1350 rblClasses->setSelectedButton(-1);
1351
1352 wndCurrent = wndRegister;
1353}
1354
1355void goToLoginScreen()
1356{
1357 txtUsername->clear();
1358 txtPassword->clear();
1359 lblLoginStatus->setText("");
1360
1361 wndCurrent = wndLogin;
1362}
1363
1364// maybe need a goToGameScreen function as well and add state changes to these functions as well
1365
1366void registerAccount()
1367{
1368 string username = txtUsernameRegister->getStr();
1369 string password = txtPasswordRegister->getStr();
1370
1371 txtUsernameRegister->clear();
1372 txtPasswordRegister->clear();
1373 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1374
1375 Player::PlayerClass playerClass;
1376
1377 switch (rblClasses->getSelectedButton()) {
1378 case 0:
1379 playerClass = Player::CLASS_WARRIOR;
1380 break;
1381 case 1:
1382 playerClass = Player::CLASS_RANGER;
1383 break;
1384 default:
1385 cout << "Invalid class selection" << endl;
1386 playerClass = Player::CLASS_NONE;
1387 break;
1388 }
1389
1390 msgTo.type = MSG_TYPE_REGISTER;
1391
1392 strcpy(msgTo.buffer, username.c_str());
1393 strcpy(msgTo.buffer+username.size()+1, password.c_str());
1394 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
1395
1396 msgProcessor.sendMessage(&msgTo, &server);
1397}
1398
1399void login()
1400{
1401 string strUsername = txtUsername->getStr();
1402 string strPassword = txtPassword->getStr();
1403 username = strUsername;
1404
1405 txtUsername->clear();
1406 txtPassword->clear();
1407
1408 msgTo.type = MSG_TYPE_LOGIN;
1409
1410 strcpy(msgTo.buffer, strUsername.c_str());
1411 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1412
1413 msgProcessor.sendMessage(&msgTo, &server);
1414
1415 state = STATE_LOBBY;
1416}
1417
1418void logout()
1419{
1420 switch(state) {
1421 case STATE_LOBBY:
1422 txtJoinGame->clear();
1423 txtCreateGame->clear();
1424 break;
1425 default:
1426 cout << "Logout called from invalid state: " << state << endl;
1427 break;
1428 }
1429
1430 msgTo.type = MSG_TYPE_LOGOUT;
1431
1432 strcpy(msgTo.buffer, username.c_str());
1433
1434 msgProcessor.sendMessage(&msgTo, &server);
1435}
1436
1437void quit()
1438{
1439 doexit = true;
1440}
1441
1442void sendChatMessage()
1443{
1444 string msg = txtChat->getStr();
1445 txtChat->clear();
1446
1447 msgTo.type = MSG_TYPE_CHAT;
1448 strcpy(msgTo.buffer, msg.c_str());
1449
1450 msgProcessor.sendMessage(&msgTo, &server);
1451}
1452
1453void toggleDebugging()
1454{
1455 debugging = !debugging;
1456}
1457
1458void goToProfileScreen()
1459{
1460 msgTo.type = MSG_TYPE_PROFILE;
1461
1462 msgProcessor.sendMessage(&msgTo, &server);
1463}
1464
1465void goToLobbyScreen()
1466{
1467 wndCurrent = wndLobby;
1468}
1469
1470void joinGame()
1471{
1472 cout << "Joining game" << endl;
1473
1474 string msg = txtJoinGame->getStr();
1475 txtJoinGame->clear();
1476
1477 msgTo.type = MSG_TYPE_JOIN_GAME;
1478 strcpy(msgTo.buffer, msg.c_str());
1479
1480 msgProcessor.sendMessage(&msgTo, &server);
1481}
1482
1483void createGame()
1484{
1485 cout << "Creating game" << endl;
1486
1487 string msg = txtCreateGame->getStr();
1488 txtCreateGame->clear();
1489
1490 cout << "Sending message: " << msg.c_str() << endl;
1491
1492 msgTo.type = MSG_TYPE_CREATE_GAME;
1493 strcpy(msgTo.buffer, msg.c_str());
1494
1495 msgProcessor.sendMessage(&msgTo, &server);
1496 cout << "Sent CREATE_GAME message" << endl;
1497}
1498
1499void joinWaitingArea() {
1500 cout << "joining waiting area" << endl;
1501 currentPlayer->team = -1;
1502
1503 msgTo.type = MSG_TYPE_JOIN_TEAM;
1504 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1505
1506 msgProcessor.sendMessage(&msgTo, &server);
1507}
1508
1509void joinBlueTeam() {
1510 cout << "joining blue team" << endl;
1511 currentPlayer->team = 0;
1512
1513 msgTo.type = MSG_TYPE_JOIN_TEAM;
1514 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1515
1516 msgProcessor.sendMessage(&msgTo, &server);
1517}
1518
1519void joinRedTeam() {
1520 cout << "joining red team" << endl;
1521 currentPlayer->team = 1;
1522
1523 msgTo.type = MSG_TYPE_JOIN_TEAM;
1524 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1525
1526 msgProcessor.sendMessage(&msgTo, &server);
1527}
1528
1529void startGame() {
1530 msgTo.type = MSG_TYPE_START_GAME;
1531
1532 msgProcessor.sendMessage(&msgTo, &server);
1533}
1534
1535void leaveGame()
1536{
1537 cout << "Leaving game" << endl;
1538
1539 delete game;
1540 game = NULL;
1541
1542 state = STATE_LOBBY;
1543 wndCurrent = wndLobby;
1544
1545 msgTo.type = MSG_TYPE_LEAVE_GAME;
1546
1547 msgProcessor.sendMessage(&msgTo, &server);
1548}
1549
1550void closeGameSummary()
1551{
1552 delete gameSummary;
1553 gameSummary = NULL;
1554 wndCurrent = wndLobby;
1555 cout << "Processed button actions" << endl;
1556}
Note: See TracBrowser for help on using the repository browser.