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 | map<unsigned int, Player>::iterator it;
|
---|
148 |
|
---|
149 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
150 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
151 | //Player* p = &it->second;
|
---|
152 | it->second.updateTarget(mapPlayers);
|
---|
153 |
|
---|
154 | /*
|
---|
155 | if (p->isChasing) {
|
---|
156 | p->target.x = mapPlayers[p->targetPlayer].pos.x;
|
---|
157 | p->target.y = mapPlayers[p->targetPlayer].pos.y;
|
---|
158 |
|
---|
159 | if (posDistance(p->pos, p->target.toFloat()) <= p->range) {
|
---|
160 | p->target.x = p->pos.x;
|
---|
161 | p->target.y = p->pos.y;
|
---|
162 |
|
---|
163 | p->isChasing = false;
|
---|
164 | p->isAttacking = true;
|
---|
165 | p->timeAttackStarted = getCurrentMillis();
|
---|
166 | }
|
---|
167 | }
|
---|
168 | */
|
---|
169 | }
|
---|
170 |
|
---|
171 | // move all players
|
---|
172 | // maybe put this in a separate method
|
---|
173 | FLOAT_POSITION oldPos;
|
---|
174 | bool broadcastMove = false;
|
---|
175 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
176 | cout << "Starting new for loop iteration" << endl;
|
---|
177 | oldPos = it->second.pos;
|
---|
178 | if (it->second.move(gameMap)) {
|
---|
179 |
|
---|
180 | // check if the move needs to be canceled
|
---|
181 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
182 | case WorldMap::TERRAIN_NONE:
|
---|
183 | case WorldMap::TERRAIN_OCEAN:
|
---|
184 | case WorldMap::TERRAIN_ROCK:
|
---|
185 | {
|
---|
186 | it->second.pos = oldPos;
|
---|
187 | it->second.target.x = it->second.pos.x;
|
---|
188 | it->second.target.y = it->second.pos.y;
|
---|
189 | broadcastMove = true;
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | default:
|
---|
193 | // if there are no obstacles, do nothing
|
---|
194 | break;
|
---|
195 | }
|
---|
196 |
|
---|
197 | WorldMap::ObjectType flagType;
|
---|
198 | POSITION pos;
|
---|
199 | bool flagTurnedIn = false;
|
---|
200 | bool flagReturned = false;
|
---|
201 | bool ownFlagAtBase = false;
|
---|
202 |
|
---|
203 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
204 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
205 | {
|
---|
206 | if (it->second.team == 0 && it->second.hasRedFlag)
|
---|
207 | {
|
---|
208 | // check that your flag is at your base
|
---|
209 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
210 |
|
---|
211 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
212 | vector<WorldMap::Object>::iterator itObjects;
|
---|
213 |
|
---|
214 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
215 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
---|
216 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
217 | ownFlagAtBase = true;
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (ownFlagAtBase) {
|
---|
224 | it->second.hasRedFlag = false;
|
---|
225 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
226 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
227 | flagTurnedIn = true;
|
---|
228 | scoreBlue++;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
235 | {
|
---|
236 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
---|
237 | {
|
---|
238 | // check that your flag is at your base
|
---|
239 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
240 |
|
---|
241 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
242 | vector<WorldMap::Object>::iterator itObjects;
|
---|
243 |
|
---|
244 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
245 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
---|
246 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
247 | ownFlagAtBase = true;
|
---|
248 | break;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (ownFlagAtBase) {
|
---|
254 | it->second.hasBlueFlag = false;
|
---|
255 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
256 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
257 | flagTurnedIn = true;
|
---|
258 | scoreRed++;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | break;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (flagTurnedIn) {
|
---|
267 | // send an OBJECT message to add the flag back to its spawn point
|
---|
268 | pos.x = pos.x*25+12;
|
---|
269 | pos.y = pos.y*25+12;
|
---|
270 | gameMap->addObject(flagType, pos.x, pos.y);
|
---|
271 |
|
---|
272 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
273 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
274 |
|
---|
275 | map<unsigned int, Player>::iterator it2;
|
---|
276 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
277 | {
|
---|
278 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
279 | error("sendMessage");
|
---|
280 | }
|
---|
281 |
|
---|
282 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
283 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
284 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
285 |
|
---|
286 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
287 | {
|
---|
288 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
289 | error("sendMessage");
|
---|
290 | }
|
---|
291 |
|
---|
292 | // this means a PLAYER message will be sent
|
---|
293 | broadcastMove = true;
|
---|
294 | }
|
---|
295 |
|
---|
296 | // go through all objects and check if the player is close to one and if its their flag
|
---|
297 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
298 | vector<WorldMap::Object>::iterator itObjects;
|
---|
299 | POSITION structPos;
|
---|
300 |
|
---|
301 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
302 | POSITION pos = itObjects->pos;
|
---|
303 |
|
---|
304 | if (posDistance(it->second.pos, pos.toFloat()) < 10) {
|
---|
305 | if (it->second.team == 0 &&
|
---|
306 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
---|
307 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
308 | flagReturned = true;
|
---|
309 | break;
|
---|
310 | } else if (it->second.team == 1 &&
|
---|
311 | itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
---|
312 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
313 | flagReturned = true;
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (flagReturned) {
|
---|
320 | itObjects->pos.x = structPos.x*25+12;
|
---|
321 | itObjects->pos.y = structPos.y*25+12;
|
---|
322 |
|
---|
323 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
324 | itObjects->serialize(serverMsg.buffer);
|
---|
325 |
|
---|
326 | map<unsigned int, Player>::iterator it2;
|
---|
327 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
328 | {
|
---|
329 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
330 | error("sendMessage");
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (broadcastMove) {
|
---|
335 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
336 | it->second.serialize(serverMsg.buffer);
|
---|
337 |
|
---|
338 | cout << "about to broadcast move" << endl;
|
---|
339 | map<unsigned int, Player>::iterator it2;
|
---|
340 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
341 | {
|
---|
342 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
343 | error("sendMessage");
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | // check if the player's attack animation is complete
|
---|
349 | if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
|
---|
350 | it->second.isAttacking = false;
|
---|
351 | cout << "Attack animation is complete" << endl;
|
---|
352 |
|
---|
353 | //send everyone an ATTACK message
|
---|
354 | cout << "about to broadcast attack" << endl;
|
---|
355 |
|
---|
356 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
357 | memcpy(serverMsg.buffer, &it->second.id, 4);
|
---|
358 | memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
|
---|
359 |
|
---|
360 | map<unsigned int, Player>::iterator it2;
|
---|
361 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
362 | {
|
---|
363 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
364 | error("sendMessage");
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (it->second.attackType == Player::ATTACK_MELEE) {
|
---|
368 | cout << "Melee attack" << endl;
|
---|
369 |
|
---|
370 | Player* target = &mapPlayers[it->second.targetPlayer];
|
---|
371 |
|
---|
372 | target->health -= it->second.damage;
|
---|
373 | if (target->health < 0)
|
---|
374 | target->health = 0;
|
---|
375 |
|
---|
376 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
377 | target->serialize(serverMsg.buffer);
|
---|
378 | }else if (it->second.attackType == Player::ATTACK_RANGED) {
|
---|
379 | cout << "Ranged attack" << endl;
|
---|
380 |
|
---|
381 | Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
|
---|
382 | proj.id = unusedProjectileId;
|
---|
383 | updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
|
---|
384 | mapProjectiles[proj.id] = proj;
|
---|
385 |
|
---|
386 | int x = it->second.pos.x;
|
---|
387 | int y = it->second.pos.y;
|
---|
388 |
|
---|
389 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
390 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
391 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
392 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
393 | memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
|
---|
394 | }else {
|
---|
395 | cout << "Invalid attack type: " << it->second.attackType << endl;
|
---|
396 | }
|
---|
397 |
|
---|
398 | // broadcast either a PLAYER or PROJECTILE message
|
---|
399 | cout << "Broadcasting player or projectile message" << endl;
|
---|
400 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
401 | {
|
---|
402 | if (sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
403 | error("sendMessage");
|
---|
404 | }
|
---|
405 | cout << "Done broadcasting" << endl;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | cout << "Done with the for loop" << endl;
|
---|
410 |
|
---|
411 | // move all projectiles
|
---|
412 | cout << "Moving projectiles" << endl;
|
---|
413 | map<unsigned int, Projectile>::iterator itProj;
|
---|
414 | for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
|
---|
415 | if (itProj->second.move(mapPlayers)) {
|
---|
416 | // send a REMOVE_PROJECTILE message
|
---|
417 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
418 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
419 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
420 | mapProjectiles.erase(itProj->second.id);
|
---|
421 |
|
---|
422 | map<unsigned int, Player>::iterator it2;
|
---|
423 | cout << "Broadcasting REMOVE_PROJECTILE" << endl;
|
---|
424 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
425 | {
|
---|
426 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
427 | error("sendMessage");
|
---|
428 | }
|
---|
429 |
|
---|
430 | cout << "send a PLAYER message after dealing damage" << endl;
|
---|
431 | // send a PLAYER message after dealing damage
|
---|
432 | Player* target = &mapPlayers[itProj->second.target];
|
---|
433 |
|
---|
434 | target->health -= itProj->second.damage;
|
---|
435 | if (target->health < 0)
|
---|
436 | target->health = 0;
|
---|
437 |
|
---|
438 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
439 | target->serialize(serverMsg.buffer);
|
---|
440 |
|
---|
441 | cout << "Sending a PLAYER message" << endl;
|
---|
442 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
443 | {
|
---|
444 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
445 | error("sendMessage");
|
---|
446 | }
|
---|
447 | }
|
---|
448 | }
|
---|
449 | }
|
---|
450 | cout << "Done moving projectiles" << endl;
|
---|
451 |
|
---|
452 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
453 |
|
---|
454 | if (n >= 0) {
|
---|
455 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
|
---|
456 |
|
---|
457 | if (broadcastResponse)
|
---|
458 | {
|
---|
459 | cout << "Should be broadcasting the message" << endl;
|
---|
460 |
|
---|
461 | map<unsigned int, Player>::iterator it;
|
---|
462 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
463 | {
|
---|
464 | cout << "Sent message back to " << it->second.name << endl;
|
---|
465 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
466 | error("sendMessage");
|
---|
467 | }
|
---|
468 | }
|
---|
469 | else
|
---|
470 | {
|
---|
471 | cout << "Should be sending back the message" << endl;
|
---|
472 |
|
---|
473 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
474 | error("sendMessage");
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | return 0;
|
---|
480 | }
|
---|
481 |
|
---|
482 | 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)
|
---|
483 | {
|
---|
484 | DataAccess da;
|
---|
485 |
|
---|
486 | cout << "Received message" << endl;
|
---|
487 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
488 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
489 |
|
---|
490 | // maybe we should make a message class and have this be a member
|
---|
491 | bool broadcastResponse = false;
|
---|
492 |
|
---|
493 | // Check that if an invalid message is sent, the client will correctly
|
---|
494 | // receive and display the response. Maybe make a special error msg type
|
---|
495 | switch(clientMsg.type)
|
---|
496 | {
|
---|
497 | case MSG_TYPE_REGISTER:
|
---|
498 | {
|
---|
499 | string username(clientMsg.buffer);
|
---|
500 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
501 |
|
---|
502 | cout << "username: " << username << endl;
|
---|
503 | cout << "password: " << password << endl;
|
---|
504 |
|
---|
505 | int error = da.insertPlayer(username, password);
|
---|
506 |
|
---|
507 | if (!error)
|
---|
508 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
509 | else
|
---|
510 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
511 |
|
---|
512 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
513 |
|
---|
514 | break;
|
---|
515 | }
|
---|
516 | case MSG_TYPE_LOGIN:
|
---|
517 | {
|
---|
518 | cout << "Got login message" << endl;
|
---|
519 |
|
---|
520 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
521 |
|
---|
522 | string username(clientMsg.buffer);
|
---|
523 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
524 |
|
---|
525 | Player* p = da.getPlayer(username);
|
---|
526 |
|
---|
527 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
528 | {
|
---|
529 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
530 | }
|
---|
531 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
532 | {
|
---|
533 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
534 | }
|
---|
535 | else
|
---|
536 | {
|
---|
537 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
538 |
|
---|
539 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
540 | p->id = unusedPlayerId;
|
---|
541 | cout << "new player id: " << p->id << endl;
|
---|
542 | p->setAddr(from);
|
---|
543 |
|
---|
544 | // choose a random team (either 0 or 1)
|
---|
545 | p->team = rand() % 2;
|
---|
546 |
|
---|
547 | // choose a random class
|
---|
548 | int intClass = rand() % 2;
|
---|
549 | switch (intClass) {
|
---|
550 | case 0:
|
---|
551 | p->setClass(Player::CLASS_WARRIOR);
|
---|
552 | break;
|
---|
553 | case 1:
|
---|
554 | p->setClass(Player::CLASS_RANGER);
|
---|
555 | break;
|
---|
556 | }
|
---|
557 |
|
---|
558 | // tell the new player about all the existing players
|
---|
559 | cout << "Sending other players to new player" << endl;
|
---|
560 |
|
---|
561 | map<unsigned int, Player>::iterator it;
|
---|
562 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
563 | {
|
---|
564 | it->second.serialize(serverMsg.buffer);
|
---|
565 |
|
---|
566 | cout << "sending info about " << it->second.name << endl;
|
---|
567 | cout << "sending id " << it->second.id << endl;
|
---|
568 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
569 | error("sendMessage");
|
---|
570 | }
|
---|
571 |
|
---|
572 | // tell the new player about all map objects
|
---|
573 | // (currently just the flags)
|
---|
574 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
575 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
576 | vector<WorldMap::Object>::iterator itObjects;
|
---|
577 | cout << "sending items" << endl;
|
---|
578 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
579 | itObjects->serialize(serverMsg.buffer);
|
---|
580 | cout << "sending item id " << itObjects->id << endl;
|
---|
581 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
582 | error("sendMessage");
|
---|
583 | }
|
---|
584 |
|
---|
585 | // send the current score
|
---|
586 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
587 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
588 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
589 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
590 | error("sendMessage");
|
---|
591 |
|
---|
592 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
593 | p->serialize(serverMsg.buffer);
|
---|
594 | cout << "Should be broadcasting the message" << endl;
|
---|
595 |
|
---|
596 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
597 | {
|
---|
598 | cout << "Sent message back to " << it->second.name << endl;
|
---|
599 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
600 | error("sendMessage");
|
---|
601 | }
|
---|
602 |
|
---|
603 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
604 | mapPlayers[unusedPlayerId] = *p;
|
---|
605 | }
|
---|
606 |
|
---|
607 | delete(p);
|
---|
608 |
|
---|
609 | break;
|
---|
610 | }
|
---|
611 | case MSG_TYPE_LOGOUT:
|
---|
612 | {
|
---|
613 | string name(clientMsg.buffer);
|
---|
614 | cout << "Player logging out: " << name << endl;
|
---|
615 |
|
---|
616 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
617 |
|
---|
618 | if (p == NULL)
|
---|
619 | {
|
---|
620 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
621 | cout << "Player not logged in" << endl;
|
---|
622 | }
|
---|
623 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
624 | p->addr.sin_port != from.sin_port )
|
---|
625 | {
|
---|
626 | 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.");
|
---|
627 | cout << "Player logged in using a different connection" << endl;
|
---|
628 | }
|
---|
629 | else
|
---|
630 | {
|
---|
631 | if (p->id < unusedPlayerId)
|
---|
632 | unusedPlayerId = p->id;
|
---|
633 | mapPlayers.erase(p->id);
|
---|
634 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
635 | }
|
---|
636 |
|
---|
637 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
638 |
|
---|
639 | break;
|
---|
640 | }
|
---|
641 | case MSG_TYPE_CHAT:
|
---|
642 | {
|
---|
643 | cout << "Got a chat message" << endl;
|
---|
644 |
|
---|
645 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
646 |
|
---|
647 | if (p == NULL)
|
---|
648 | {
|
---|
649 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
650 | }
|
---|
651 | else
|
---|
652 | {
|
---|
653 | broadcastResponse = true;
|
---|
654 |
|
---|
655 | ostringstream oss;
|
---|
656 | oss << p->name << ": " << clientMsg.buffer;
|
---|
657 |
|
---|
658 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
659 | }
|
---|
660 |
|
---|
661 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
662 |
|
---|
663 | break;
|
---|
664 | }
|
---|
665 | case MSG_TYPE_PLAYER_MOVE:
|
---|
666 | {
|
---|
667 | cout << "PLAYER_MOVE" << endl;
|
---|
668 |
|
---|
669 | int id, x, y;
|
---|
670 |
|
---|
671 | memcpy(&id, clientMsg.buffer, 4);
|
---|
672 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
673 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
674 |
|
---|
675 | cout << "x: " << x << endl;
|
---|
676 | cout << "y: " << y << endl;
|
---|
677 | cout << "id: " << id << endl;
|
---|
678 |
|
---|
679 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
680 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
681 | {
|
---|
682 | // we need to make sure the player can move here
|
---|
683 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
684 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
685 | {
|
---|
686 | cout << "valid terrain" << endl;
|
---|
687 |
|
---|
688 | mapPlayers[id].target.x = x;
|
---|
689 | mapPlayers[id].target.y = y;
|
---|
690 |
|
---|
691 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
692 |
|
---|
693 | memcpy(serverMsg.buffer, &id, 4);
|
---|
694 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
695 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
696 |
|
---|
697 | broadcastResponse = true;
|
---|
698 | }
|
---|
699 | else
|
---|
700 | cout << "Bad terrain detected" << endl;
|
---|
701 | }
|
---|
702 | else // nned to send back a message indicating failure
|
---|
703 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
704 |
|
---|
705 | break;
|
---|
706 | }
|
---|
707 | case MSG_TYPE_PICKUP_FLAG:
|
---|
708 | {
|
---|
709 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
710 | cout << "PICKUP_FLAG" << endl;
|
---|
711 |
|
---|
712 | int id;
|
---|
713 |
|
---|
714 | memcpy(&id, clientMsg.buffer, 4);
|
---|
715 | cout << "id: " << id << endl;
|
---|
716 |
|
---|
717 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
718 | vector<WorldMap::Object>::iterator itObjects;
|
---|
719 |
|
---|
720 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
721 | POSITION pos = itObjects->pos;
|
---|
722 | bool gotFlag = false;
|
---|
723 |
|
---|
724 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
---|
725 | switch (itObjects->type) {
|
---|
726 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
727 | if (mapPlayers[id].team == 1) {
|
---|
728 | gotFlag = true;
|
---|
729 | mapPlayers[id].hasBlueFlag = true;
|
---|
730 | broadcastResponse = true;
|
---|
731 | }
|
---|
732 | break;
|
---|
733 | case WorldMap::OBJECT_RED_FLAG:
|
---|
734 | if (mapPlayers[id].team == 0) {
|
---|
735 | gotFlag = true;
|
---|
736 | mapPlayers[id].hasRedFlag = true;
|
---|
737 | broadcastResponse = true;
|
---|
738 | }
|
---|
739 | break;
|
---|
740 | }
|
---|
741 |
|
---|
742 | if (gotFlag) {
|
---|
743 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
744 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
745 |
|
---|
746 | map<unsigned int, Player>::iterator it;
|
---|
747 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
748 | {
|
---|
749 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
750 | error("sendMessage");
|
---|
751 | }
|
---|
752 |
|
---|
753 | // remove the object from the server-side map
|
---|
754 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
755 | itObjects = vctObjects->erase(itObjects);
|
---|
756 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
757 | }
|
---|
758 | }
|
---|
759 |
|
---|
760 | if (!gotFlag)
|
---|
761 | itObjects++;
|
---|
762 | }
|
---|
763 |
|
---|
764 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
765 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
766 |
|
---|
767 | break;
|
---|
768 | }
|
---|
769 | case MSG_TYPE_DROP_FLAG:
|
---|
770 | {
|
---|
771 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
772 | cout << "DROP_FLAG" << endl;
|
---|
773 |
|
---|
774 | int id;
|
---|
775 |
|
---|
776 | memcpy(&id, clientMsg.buffer, 4);
|
---|
777 | cout << "id: " << id << endl;
|
---|
778 |
|
---|
779 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
780 | if (mapPlayers[id].hasBlueFlag)
|
---|
781 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
782 | else if (mapPlayers[id].hasRedFlag)
|
---|
783 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
784 |
|
---|
785 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
786 |
|
---|
787 | // need to send the OBJECT message too
|
---|
788 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
789 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
790 |
|
---|
791 | map<unsigned int, Player>::iterator it;
|
---|
792 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
793 | {
|
---|
794 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
795 | error("sendMessage");
|
---|
796 | }
|
---|
797 |
|
---|
798 | mapPlayers[id].hasBlueFlag = false;
|
---|
799 | mapPlayers[id].hasRedFlag = false;
|
---|
800 |
|
---|
801 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
802 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
803 |
|
---|
804 | broadcastResponse = true;
|
---|
805 |
|
---|
806 | break;
|
---|
807 | }
|
---|
808 | case MSG_TYPE_START_ATTACK:
|
---|
809 | {
|
---|
810 | cout << "Received a START_ATTACK message" << endl;
|
---|
811 |
|
---|
812 | int id, targetId;
|
---|
813 |
|
---|
814 | memcpy(&id, clientMsg.buffer, 4);
|
---|
815 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
816 |
|
---|
817 | Player* source = &mapPlayers[id];
|
---|
818 | source->targetPlayer = targetId;
|
---|
819 | source->isChasing = true;
|
---|
820 |
|
---|
821 | // this is irrelevant since the client doesn't even listen for START_ATTACK messages
|
---|
822 | // actually, the client should not ignore this and should instead perform the same movement
|
---|
823 | // algorithm on its end (following the target player until in range) that the server does.
|
---|
824 | // Once the attacker is in range, the client should stop movement and wait for messages
|
---|
825 | // from the server
|
---|
826 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
827 | memcpy(serverMsg.buffer, &id, 4);
|
---|
828 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
829 | broadcastResponse = true;
|
---|
830 |
|
---|
831 | break;
|
---|
832 | }
|
---|
833 | case MSG_TYPE_ATTACK:
|
---|
834 | {
|
---|
835 | cout << "Received am ATTACK message" << endl;
|
---|
836 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
837 |
|
---|
838 | break;
|
---|
839 | }
|
---|
840 | default:
|
---|
841 | {
|
---|
842 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
843 |
|
---|
844 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
845 |
|
---|
846 | break;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | return broadcastResponse;
|
---|
851 | }
|
---|
852 |
|
---|
853 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
854 | {
|
---|
855 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
856 | id++;
|
---|
857 | }
|
---|
858 |
|
---|
859 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
---|
860 | {
|
---|
861 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
---|
862 | id++;
|
---|
863 | }
|
---|