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

Last change on this file since e1af80c was 635ad9b, checked in by dportnoy <dmp1488@…>, 11 years ago

Correction to GameSummary constructor

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