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

Last change on this file since 84f9797 was 8df0c49, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

Client makefile shows warnings

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