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

Last change on this file since 8554263 was 68d94de, checked in by dportnoy <dmp1488@…>, 11 years ago

MessageProcessor now takes a socket and optional output log file as constructor arguments instead of accepting them as parameters in each method

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