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