1 | #include <cstdlib>
|
---|
2 | #include <cstdio>
|
---|
3 | #include <unistd.h>
|
---|
4 | #include <string>
|
---|
5 | #include <iostream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <cstring>
|
---|
8 | #include <cmath>
|
---|
9 |
|
---|
10 | #include <vector>
|
---|
11 | #include <map>
|
---|
12 |
|
---|
13 | #include <sys/time.h>
|
---|
14 |
|
---|
15 | #include <sys/socket.h>
|
---|
16 | #include <netdb.h>
|
---|
17 | #include <netinet/in.h>
|
---|
18 | #include <arpa/inet.h>
|
---|
19 |
|
---|
20 | #include <crypt.h>
|
---|
21 |
|
---|
22 | /*
|
---|
23 | #include <openssl/bio.h>
|
---|
24 | #include <openssl/ssl.h>
|
---|
25 | #include <openssl/err.h>
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "../common/Compiler.h"
|
---|
29 | #include "../common/Common.h"
|
---|
30 | #include "../common/Message.h"
|
---|
31 | #include "../common/WorldMap.h"
|
---|
32 | #include "../common/Player.h"
|
---|
33 | #include "../common/Projectile.h"
|
---|
34 |
|
---|
35 | #include "DataAccess.h"
|
---|
36 |
|
---|
37 | using namespace std;
|
---|
38 |
|
---|
39 | // from used to be const. Removed that so I could take a reference
|
---|
40 | // and use it to send messages
|
---|
41 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
|
---|
42 |
|
---|
43 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
---|
44 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
|
---|
45 |
|
---|
46 | // this should probably go somewhere in the common folder
|
---|
47 | void error(const char *msg)
|
---|
48 | {
|
---|
49 | perror(msg);
|
---|
50 | exit(0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | Player *findPlayerByName(map<unsigned int, Player> &m, string name)
|
---|
54 | {
|
---|
55 | map<unsigned int, Player>::iterator it;
|
---|
56 |
|
---|
57 | for (it = m.begin(); it != m.end(); it++)
|
---|
58 | {
|
---|
59 | if ( it->second.name.compare(name) == 0 )
|
---|
60 | return &(it->second);
|
---|
61 | }
|
---|
62 |
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 |
|
---|
66 | Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
|
---|
67 | {
|
---|
68 | map<unsigned int, Player>::iterator it;
|
---|
69 |
|
---|
70 | for (it = m.begin(); it != m.end(); it++)
|
---|
71 | {
|
---|
72 | if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
73 | it->second.addr.sin_port == addr.sin_port )
|
---|
74 | return &(it->second);
|
---|
75 | }
|
---|
76 |
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int main(int argc, char *argv[])
|
---|
81 | {
|
---|
82 | int sock, length, n;
|
---|
83 | struct sockaddr_in server;
|
---|
84 | struct sockaddr_in from; // info of client sending the message
|
---|
85 | NETWORK_MSG clientMsg, serverMsg;
|
---|
86 | map<unsigned int, Player> mapPlayers;
|
---|
87 | map<unsigned int, Projectile> mapProjectiles;
|
---|
88 | unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
|
---|
89 | int scoreBlue, scoreRed;
|
---|
90 |
|
---|
91 | scoreBlue = 0;
|
---|
92 | scoreRed = 0;
|
---|
93 |
|
---|
94 | //SSL_load_error_strings();
|
---|
95 | //ERR_load_BIO_strings();
|
---|
96 | //OpenSSL_add_all_algorithms();
|
---|
97 |
|
---|
98 | if (argc < 2) {
|
---|
99 | cerr << "ERROR, no port provided" << endl;
|
---|
100 | exit(1);
|
---|
101 | }
|
---|
102 |
|
---|
103 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
---|
104 |
|
---|
105 | // add some items to the map. They will be sent out
|
---|
106 | // to players when they login
|
---|
107 | for (int y=0; y<gameMap->height; y++) {
|
---|
108 | for (int x=0; x<gameMap->width; x++) {
|
---|
109 | switch (gameMap->getStructure(x, y)) {
|
---|
110 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
111 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
112 | break;
|
---|
113 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
114 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
121 | if (sock < 0) error("Opening socket");
|
---|
122 | length = sizeof(server);
|
---|
123 | bzero(&server,length);
|
---|
124 | server.sin_family=AF_INET;
|
---|
125 | server.sin_port=htons(atoi(argv[1]));
|
---|
126 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
127 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
128 | error("binding");
|
---|
129 |
|
---|
130 | set_nonblock(sock);
|
---|
131 |
|
---|
132 | bool broadcastResponse;
|
---|
133 | timespec ts;
|
---|
134 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
---|
135 | while (true) {
|
---|
136 |
|
---|
137 | usleep(5000);
|
---|
138 |
|
---|
139 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
140 | // make the number smaller so millis can fit in an int
|
---|
141 | ts.tv_sec -= 1368000000;
|
---|
142 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
143 |
|
---|
144 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
|
---|
145 | timeLastUpdated = curTime;
|
---|
146 |
|
---|
147 | // move all players
|
---|
148 | // maybe put this in a separate method
|
---|
149 | map<unsigned int, Player>::iterator it;
|
---|
150 | FLOAT_POSITION oldPos;
|
---|
151 | bool broadcastMove = false;
|
---|
152 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
153 | oldPos = it->second.pos;
|
---|
154 | if (it->second.move(gameMap)) {
|
---|
155 |
|
---|
156 | // check if the move needs to be canceled
|
---|
157 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
158 | case WorldMap::TERRAIN_NONE:
|
---|
159 | case WorldMap::TERRAIN_OCEAN:
|
---|
160 | case WorldMap::TERRAIN_ROCK:
|
---|
161 | {
|
---|
162 | it->second.pos = oldPos;
|
---|
163 | it->second.target.x = it->second.pos.x;
|
---|
164 | it->second.target.y = it->second.pos.y;
|
---|
165 | broadcastMove = true;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | default:
|
---|
169 | // if there are no obstacles, do nothing
|
---|
170 | break;
|
---|
171 | }
|
---|
172 |
|
---|
173 | WorldMap::ObjectType flagType;
|
---|
174 | POSITION pos;
|
---|
175 | bool flagTurnedIn = false;
|
---|
176 | bool flagReturned = false;
|
---|
177 | bool ownFlagAtBase = false;
|
---|
178 |
|
---|
179 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
180 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
181 | {
|
---|
182 | if (it->second.team == 0 && it->second.hasRedFlag)
|
---|
183 | {
|
---|
184 | // check that your flag is at your base
|
---|
185 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
186 |
|
---|
187 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
188 | vector<WorldMap::Object>::iterator itObjects;
|
---|
189 |
|
---|
190 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
191 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
---|
192 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
193 | ownFlagAtBase = true;
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (ownFlagAtBase) {
|
---|
200 | it->second.hasRedFlag = false;
|
---|
201 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
202 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
203 | flagTurnedIn = true;
|
---|
204 | scoreBlue++;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
211 | {
|
---|
212 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
---|
213 | {
|
---|
214 | // check that your flag is at your base
|
---|
215 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
216 |
|
---|
217 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
218 | vector<WorldMap::Object>::iterator itObjects;
|
---|
219 |
|
---|
220 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
221 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
---|
222 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
223 | ownFlagAtBase = true;
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (ownFlagAtBase) {
|
---|
230 | it->second.hasBlueFlag = false;
|
---|
231 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
232 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
233 | flagTurnedIn = true;
|
---|
234 | scoreRed++;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (flagTurnedIn) {
|
---|
243 | // send an OBJECT message to add the flag back to its spawn point
|
---|
244 | pos.x = pos.x*25+12;
|
---|
245 | pos.y = pos.y*25+12;
|
---|
246 | gameMap->addObject(flagType, pos.x, pos.y);
|
---|
247 |
|
---|
248 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
249 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
250 |
|
---|
251 | map<unsigned int, Player>::iterator it2;
|
---|
252 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
253 | {
|
---|
254 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
255 | error("sendMessage");
|
---|
256 | }
|
---|
257 |
|
---|
258 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
259 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
260 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
261 |
|
---|
262 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
263 | {
|
---|
264 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
265 | error("sendMessage");
|
---|
266 | }
|
---|
267 |
|
---|
268 | // this means a PLAYER message will be sent
|
---|
269 | broadcastMove = true;
|
---|
270 | }
|
---|
271 |
|
---|
272 | // go through all objects and check if the player is close to one and if its their flag
|
---|
273 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
274 | vector<WorldMap::Object>::iterator itObjects;
|
---|
275 | POSITION structPos;
|
---|
276 |
|
---|
277 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
278 | POSITION pos = itObjects->pos;
|
---|
279 |
|
---|
280 | if (posDistance(it->second.pos, pos.toFloat()) < 10) {
|
---|
281 | if (it->second.team == 0 &&
|
---|
282 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
---|
283 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
284 | flagReturned = true;
|
---|
285 | break;
|
---|
286 | } else if (it->second.team == 1 &&
|
---|
287 | itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
---|
288 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
289 | flagReturned = true;
|
---|
290 | break;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | if (flagReturned) {
|
---|
296 | itObjects->pos.x = structPos.x*25+12;
|
---|
297 | itObjects->pos.y = structPos.y*25+12;
|
---|
298 |
|
---|
299 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
300 | itObjects->serialize(serverMsg.buffer);
|
---|
301 |
|
---|
302 | map<unsigned int, Player>::iterator it2;
|
---|
303 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
304 | {
|
---|
305 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
306 | error("sendMessage");
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (broadcastMove) {
|
---|
311 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
312 | it->second.serialize(serverMsg.buffer);
|
---|
313 |
|
---|
314 | cout << "about to broadcast move" << endl;
|
---|
315 | map<unsigned int, Player>::iterator it2;
|
---|
316 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
317 | {
|
---|
318 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
319 | error("sendMessage");
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | // check if the player's attack animation is complete
|
---|
325 | if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
|
---|
326 | it->second.isAttacking = false;
|
---|
327 |
|
---|
328 | //send everyone an ATTACK message
|
---|
329 | cout << "about to broadcast attack" << endl;
|
---|
330 |
|
---|
331 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
332 | memcpy(serverMsg.buffer, &it->second.id, 4);
|
---|
333 | memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
|
---|
334 |
|
---|
335 | map<unsigned int, Player>::iterator it2;
|
---|
336 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
337 | {
|
---|
338 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
339 | error("sendMessage");
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (it->second.attackType == Player::ATTACK_MELEE) {
|
---|
343 | Player* target = &mapPlayers[it->second.targetPlayer];
|
---|
344 |
|
---|
345 | target->health -= it->second.damage;
|
---|
346 | if (target->health < 0)
|
---|
347 | target->health = 0;
|
---|
348 |
|
---|
349 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
350 | target->serialize(serverMsg.buffer);
|
---|
351 | }else if (it->second.attackType == Player::ATTACK_RANGED) {
|
---|
352 | Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
|
---|
353 | proj.id = unusedProjectileId;
|
---|
354 | updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
|
---|
355 | mapProjectiles[proj.id] = proj;
|
---|
356 |
|
---|
357 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
358 | memcpy(serverMsg.buffer, &it->second.pos.x, 4);
|
---|
359 | memcpy(serverMsg.buffer+4, &it->second.pos.y, 4);
|
---|
360 | memcpy(serverMsg.buffer+8, &it->second.targetPlayer, 4);
|
---|
361 | }else {
|
---|
362 | cout << "Invalid attack type: " << it->second.attackType << endl;
|
---|
363 | }
|
---|
364 |
|
---|
365 | // broadcast either a PLAYER or PROJECTILE message
|
---|
366 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
367 | {
|
---|
368 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
369 | error("sendMessage");
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | // move all projectiles
|
---|
375 | map<unsigned int, Projectile>::iterator itProj;
|
---|
376 | for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
|
---|
377 | if (itProj->second.move(mapPlayers)) {
|
---|
378 | // send a REMOVE_PROJECTILE message
|
---|
379 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
380 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
381 | mapProjectiles.erase(itProj->second.id);
|
---|
382 |
|
---|
383 | map<unsigned int, Player>::iterator it2;
|
---|
384 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
385 | {
|
---|
386 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
387 | error("sendMessage");
|
---|
388 | }
|
---|
389 |
|
---|
390 | // send a PLAYER message after dealing damage
|
---|
391 | Player* target = &mapPlayers[it->second.targetPlayer];
|
---|
392 |
|
---|
393 | target->health -= itProj->second.damage;
|
---|
394 | if (target->health < 0)
|
---|
395 | target->health = 0;
|
---|
396 |
|
---|
397 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
398 | target->serialize(serverMsg.buffer);
|
---|
399 |
|
---|
400 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
401 | {
|
---|
402 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
403 | error("sendMessage");
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
410 |
|
---|
411 | if (n >= 0) {
|
---|
412 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
|
---|
413 |
|
---|
414 | if (broadcastResponse)
|
---|
415 | {
|
---|
416 | cout << "Should be broadcasting the message" << endl;
|
---|
417 |
|
---|
418 | map<unsigned int, Player>::iterator it;
|
---|
419 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
420 | {
|
---|
421 | cout << "Sent message back to " << it->second.name << endl;
|
---|
422 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
423 | error("sendMessage");
|
---|
424 | }
|
---|
425 | }
|
---|
426 | else
|
---|
427 | {
|
---|
428 | cout << "Should be sending back the message" << endl;
|
---|
429 |
|
---|
430 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
431 | error("sendMessage");
|
---|
432 | }
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | return 0;
|
---|
437 | }
|
---|
438 |
|
---|
439 | bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)
|
---|
440 | {
|
---|
441 | DataAccess da;
|
---|
442 |
|
---|
443 | cout << "Received message" << endl;
|
---|
444 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
445 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
446 |
|
---|
447 | // maybe we should make a message class and have this be a member
|
---|
448 | bool broadcastResponse = false;
|
---|
449 |
|
---|
450 | // Check that if an invalid message is sent, the client will correctly
|
---|
451 | // receive and display the response. Maybe make a special error msg type
|
---|
452 | switch(clientMsg.type)
|
---|
453 | {
|
---|
454 | case MSG_TYPE_REGISTER:
|
---|
455 | {
|
---|
456 | string username(clientMsg.buffer);
|
---|
457 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
458 |
|
---|
459 | cout << "username: " << username << endl;
|
---|
460 | cout << "password: " << password << endl;
|
---|
461 |
|
---|
462 | int error = da.insertPlayer(username, password);
|
---|
463 |
|
---|
464 | if (!error)
|
---|
465 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
466 | else
|
---|
467 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
468 |
|
---|
469 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
470 |
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | case MSG_TYPE_LOGIN:
|
---|
474 | {
|
---|
475 | cout << "Got login message" << endl;
|
---|
476 |
|
---|
477 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
478 |
|
---|
479 | string username(clientMsg.buffer);
|
---|
480 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
481 |
|
---|
482 | Player* p = da.getPlayer(username);
|
---|
483 |
|
---|
484 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
485 | {
|
---|
486 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
487 | }
|
---|
488 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
489 | {
|
---|
490 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
495 |
|
---|
496 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
497 | p->id = unusedPlayerId;
|
---|
498 | cout << "new player id: " << p->id << endl;
|
---|
499 | p->setAddr(from);
|
---|
500 |
|
---|
501 | // choose a random team (either 0 or 1)
|
---|
502 | p->team = rand() % 2;
|
---|
503 |
|
---|
504 | // choose a random class
|
---|
505 | int intClass = rand() % 2;
|
---|
506 | switch (intClass) {
|
---|
507 | case 0:
|
---|
508 | p->setClass(Player::CLASS_WARRIOR);
|
---|
509 | break;
|
---|
510 | case 1:
|
---|
511 | p->setClass(Player::CLASS_RANGER);
|
---|
512 | break;
|
---|
513 | }
|
---|
514 |
|
---|
515 | // tell the new player about all the existing players
|
---|
516 | cout << "Sending other players to new player" << endl;
|
---|
517 |
|
---|
518 | map<unsigned int, Player>::iterator it;
|
---|
519 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
520 | {
|
---|
521 | it->second.serialize(serverMsg.buffer);
|
---|
522 |
|
---|
523 | cout << "sending info about " << it->second.name << endl;
|
---|
524 | cout << "sending id " << it->second.id << endl;
|
---|
525 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
526 | error("sendMessage");
|
---|
527 | }
|
---|
528 |
|
---|
529 | // tell the new player about all map objects
|
---|
530 | // (currently just the flags)
|
---|
531 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
532 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
533 | vector<WorldMap::Object>::iterator itObjects;
|
---|
534 | cout << "sending items" << endl;
|
---|
535 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
536 | itObjects->serialize(serverMsg.buffer);
|
---|
537 | cout << "sending item id " << itObjects->id << endl;
|
---|
538 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
539 | error("sendMessage");
|
---|
540 | }
|
---|
541 |
|
---|
542 | // send the current score
|
---|
543 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
544 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
545 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
546 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
547 | error("sendMessage");
|
---|
548 |
|
---|
549 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
550 | p->serialize(serverMsg.buffer);
|
---|
551 | cout << "Should be broadcasting the message" << endl;
|
---|
552 |
|
---|
553 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
554 | {
|
---|
555 | cout << "Sent message back to " << it->second.name << endl;
|
---|
556 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
557 | error("sendMessage");
|
---|
558 | }
|
---|
559 |
|
---|
560 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
561 | mapPlayers[unusedPlayerId] = *p;
|
---|
562 | }
|
---|
563 |
|
---|
564 | delete(p);
|
---|
565 |
|
---|
566 | break;
|
---|
567 | }
|
---|
568 | case MSG_TYPE_LOGOUT:
|
---|
569 | {
|
---|
570 | string name(clientMsg.buffer);
|
---|
571 | cout << "Player logging out: " << name << endl;
|
---|
572 |
|
---|
573 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
574 |
|
---|
575 | if (p == NULL)
|
---|
576 | {
|
---|
577 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
578 | cout << "Player not logged in" << endl;
|
---|
579 | }
|
---|
580 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
581 | p->addr.sin_port != from.sin_port )
|
---|
582 | {
|
---|
583 | strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
---|
584 | cout << "Player logged in using a different connection" << endl;
|
---|
585 | }
|
---|
586 | else
|
---|
587 | {
|
---|
588 | if (p->id < unusedPlayerId)
|
---|
589 | unusedPlayerId = p->id;
|
---|
590 | mapPlayers.erase(p->id);
|
---|
591 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
592 | }
|
---|
593 |
|
---|
594 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
595 |
|
---|
596 | break;
|
---|
597 | }
|
---|
598 | case MSG_TYPE_CHAT:
|
---|
599 | {
|
---|
600 | cout << "Got a chat message" << endl;
|
---|
601 |
|
---|
602 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
603 |
|
---|
604 | if (p == NULL)
|
---|
605 | {
|
---|
606 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
607 | }
|
---|
608 | else
|
---|
609 | {
|
---|
610 | broadcastResponse = true;
|
---|
611 |
|
---|
612 | ostringstream oss;
|
---|
613 | oss << p->name << ": " << clientMsg.buffer;
|
---|
614 |
|
---|
615 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
616 | }
|
---|
617 |
|
---|
618 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
619 |
|
---|
620 | break;
|
---|
621 | }
|
---|
622 | case MSG_TYPE_PLAYER_MOVE:
|
---|
623 | {
|
---|
624 | cout << "PLAYER_MOVE" << endl;
|
---|
625 |
|
---|
626 | int id, x, y;
|
---|
627 |
|
---|
628 | memcpy(&id, clientMsg.buffer, 4);
|
---|
629 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
630 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
631 |
|
---|
632 | cout << "x: " << x << endl;
|
---|
633 | cout << "y: " << y << endl;
|
---|
634 | cout << "id: " << id << endl;
|
---|
635 |
|
---|
636 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
637 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
638 | {
|
---|
639 | // we need to make sure the player can move here
|
---|
640 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
641 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
642 | {
|
---|
643 | cout << "valid terrain" << endl;
|
---|
644 |
|
---|
645 | mapPlayers[id].target.x = x;
|
---|
646 | mapPlayers[id].target.y = y;
|
---|
647 |
|
---|
648 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
649 |
|
---|
650 | memcpy(serverMsg.buffer, &id, 4);
|
---|
651 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
652 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
653 |
|
---|
654 | broadcastResponse = true;
|
---|
655 | }
|
---|
656 | else
|
---|
657 | cout << "Bad terrain detected" << endl;
|
---|
658 | }
|
---|
659 | else // nned to send back a message indicating failure
|
---|
660 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
661 |
|
---|
662 | break;
|
---|
663 | }
|
---|
664 | case MSG_TYPE_PICKUP_FLAG:
|
---|
665 | {
|
---|
666 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
667 | cout << "PICKUP_FLAG" << endl;
|
---|
668 |
|
---|
669 | int id;
|
---|
670 |
|
---|
671 | memcpy(&id, clientMsg.buffer, 4);
|
---|
672 | cout << "id: " << id << endl;
|
---|
673 |
|
---|
674 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
675 | vector<WorldMap::Object>::iterator itObjects;
|
---|
676 |
|
---|
677 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
678 | POSITION pos = itObjects->pos;
|
---|
679 | bool gotFlag = false;
|
---|
680 |
|
---|
681 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
---|
682 | switch (itObjects->type) {
|
---|
683 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
684 | if (mapPlayers[id].team == 1) {
|
---|
685 | gotFlag = true;
|
---|
686 | mapPlayers[id].hasBlueFlag = true;
|
---|
687 | broadcastResponse = true;
|
---|
688 | }
|
---|
689 | break;
|
---|
690 | case WorldMap::OBJECT_RED_FLAG:
|
---|
691 | if (mapPlayers[id].team == 0) {
|
---|
692 | gotFlag = true;
|
---|
693 | mapPlayers[id].hasRedFlag = true;
|
---|
694 | broadcastResponse = true;
|
---|
695 | }
|
---|
696 | break;
|
---|
697 | }
|
---|
698 |
|
---|
699 | if (gotFlag) {
|
---|
700 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
701 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
702 |
|
---|
703 | map<unsigned int, Player>::iterator it;
|
---|
704 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
705 | {
|
---|
706 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
707 | error("sendMessage");
|
---|
708 | }
|
---|
709 |
|
---|
710 | // remove the object from the server-side map
|
---|
711 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
712 | itObjects = vctObjects->erase(itObjects);
|
---|
713 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (!gotFlag)
|
---|
718 | itObjects++;
|
---|
719 | }
|
---|
720 |
|
---|
721 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
722 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
723 |
|
---|
724 | break;
|
---|
725 | }
|
---|
726 | case MSG_TYPE_DROP_FLAG:
|
---|
727 | {
|
---|
728 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
729 | cout << "DROP_FLAG" << endl;
|
---|
730 |
|
---|
731 | int id;
|
---|
732 |
|
---|
733 | memcpy(&id, clientMsg.buffer, 4);
|
---|
734 | cout << "id: " << id << endl;
|
---|
735 |
|
---|
736 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
737 | if (mapPlayers[id].hasBlueFlag)
|
---|
738 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
739 | else if (mapPlayers[id].hasRedFlag)
|
---|
740 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
741 |
|
---|
742 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
743 |
|
---|
744 | // need to send the OBJECT message too
|
---|
745 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
746 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
747 |
|
---|
748 | map<unsigned int, Player>::iterator it;
|
---|
749 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
750 | {
|
---|
751 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
752 | error("sendMessage");
|
---|
753 | }
|
---|
754 |
|
---|
755 | mapPlayers[id].hasBlueFlag = false;
|
---|
756 | mapPlayers[id].hasRedFlag = false;
|
---|
757 |
|
---|
758 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
759 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
760 |
|
---|
761 | broadcastResponse = true;
|
---|
762 |
|
---|
763 | break;
|
---|
764 | }
|
---|
765 | case MSG_TYPE_START_ATTACK:
|
---|
766 | {
|
---|
767 | cout << "Received a START_ATTACK message" << endl;
|
---|
768 |
|
---|
769 | int id, targetId;
|
---|
770 |
|
---|
771 | memcpy(&id, clientMsg.buffer, 4);
|
---|
772 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
773 |
|
---|
774 | Player* source = &mapPlayers[id];
|
---|
775 | source->timeAttackStarted = getCurrentMillis();
|
---|
776 | source->targetPlayer = targetId;
|
---|
777 |
|
---|
778 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
779 | memcpy(serverMsg.buffer, &id, 4);
|
---|
780 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
781 | broadcastResponse = true;
|
---|
782 |
|
---|
783 | break;
|
---|
784 | }
|
---|
785 | case MSG_TYPE_ATTACK:
|
---|
786 | {
|
---|
787 | cout << "Received am ATTACK message" << endl;
|
---|
788 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
789 |
|
---|
790 | break;
|
---|
791 | }
|
---|
792 | default:
|
---|
793 | {
|
---|
794 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
795 |
|
---|
796 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
797 |
|
---|
798 | break;
|
---|
799 | }
|
---|
800 | }
|
---|
801 |
|
---|
802 | return broadcastResponse;
|
---|
803 | }
|
---|
804 |
|
---|
805 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
806 | {
|
---|
807 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
808 | id++;
|
---|
809 | }
|
---|
810 |
|
---|
811 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
---|
812 | {
|
---|
813 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
---|
814 | id++;
|
---|
815 | }
|
---|