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

Last change on this file since 778d0c9 was 3e44a59, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

The client shows a game summary screen when the current game cfinishes

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