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 | cout << "Attack animation is complete" << endl;
|
---|
328 |
|
---|
329 | //send everyone an ATTACK message
|
---|
330 | cout << "about to broadcast attack" << endl;
|
---|
331 |
|
---|
332 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
333 | memcpy(serverMsg.buffer, &it->second.id, 4);
|
---|
334 | memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
|
---|
335 |
|
---|
336 | map<unsigned int, Player>::iterator it2;
|
---|
337 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
338 | {
|
---|
339 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
340 | error("sendMessage");
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (it->second.attackType == Player::ATTACK_MELEE) {
|
---|
344 | Player* target = &mapPlayers[it->second.targetPlayer];
|
---|
345 |
|
---|
346 | target->health -= it->second.damage;
|
---|
347 | if (target->health < 0)
|
---|
348 | target->health = 0;
|
---|
349 |
|
---|
350 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
351 | target->serialize(serverMsg.buffer);
|
---|
352 | }else if (it->second.attackType == Player::ATTACK_RANGED) {
|
---|
353 | Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
|
---|
354 | proj.id = unusedProjectileId;
|
---|
355 | updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
|
---|
356 | mapProjectiles[proj.id] = proj;
|
---|
357 |
|
---|
358 | int x = it->second.pos.x;
|
---|
359 | int y = it->second.pos.y;
|
---|
360 |
|
---|
361 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
362 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
363 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
364 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
365 | memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
|
---|
366 | }else {
|
---|
367 | cout << "Invalid attack type: " << it->second.attackType << endl;
|
---|
368 | }
|
---|
369 |
|
---|
370 | // broadcast either a PLAYER or PROJECTILE message
|
---|
371 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
372 | {
|
---|
373 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
374 | error("sendMessage");
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | // move all projectiles
|
---|
380 | map<unsigned int, Projectile>::iterator itProj;
|
---|
381 | for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
|
---|
382 | if (itProj->second.move(mapPlayers)) {
|
---|
383 | // send a REMOVE_PROJECTILE message
|
---|
384 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
385 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
386 | mapProjectiles.erase(itProj->second.id);
|
---|
387 |
|
---|
388 | map<unsigned int, Player>::iterator it2;
|
---|
389 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
390 | {
|
---|
391 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
392 | error("sendMessage");
|
---|
393 | }
|
---|
394 |
|
---|
395 | // send a PLAYER message after dealing damage
|
---|
396 | Player* target = &mapPlayers[itProj->second.target];
|
---|
397 |
|
---|
398 | target->health -= itProj->second.damage;
|
---|
399 | if (target->health < 0)
|
---|
400 | target->health = 0;
|
---|
401 |
|
---|
402 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
403 | target->serialize(serverMsg.buffer);
|
---|
404 |
|
---|
405 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
406 | {
|
---|
407 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
408 | error("sendMessage");
|
---|
409 | }
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
415 |
|
---|
416 | if (n >= 0) {
|
---|
417 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
|
---|
418 |
|
---|
419 | if (broadcastResponse)
|
---|
420 | {
|
---|
421 | cout << "Should be broadcasting the message" << endl;
|
---|
422 |
|
---|
423 | map<unsigned int, Player>::iterator it;
|
---|
424 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
425 | {
|
---|
426 | cout << "Sent message back to " << it->second.name << endl;
|
---|
427 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
428 | error("sendMessage");
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | cout << "Should be sending back the message" << endl;
|
---|
434 |
|
---|
435 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
436 | error("sendMessage");
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | return 0;
|
---|
442 | }
|
---|
443 |
|
---|
444 | 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)
|
---|
445 | {
|
---|
446 | DataAccess da;
|
---|
447 |
|
---|
448 | cout << "Received message" << endl;
|
---|
449 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
450 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
451 |
|
---|
452 | // maybe we should make a message class and have this be a member
|
---|
453 | bool broadcastResponse = false;
|
---|
454 |
|
---|
455 | // Check that if an invalid message is sent, the client will correctly
|
---|
456 | // receive and display the response. Maybe make a special error msg type
|
---|
457 | switch(clientMsg.type)
|
---|
458 | {
|
---|
459 | case MSG_TYPE_REGISTER:
|
---|
460 | {
|
---|
461 | string username(clientMsg.buffer);
|
---|
462 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
463 |
|
---|
464 | cout << "username: " << username << endl;
|
---|
465 | cout << "password: " << password << endl;
|
---|
466 |
|
---|
467 | int error = da.insertPlayer(username, password);
|
---|
468 |
|
---|
469 | if (!error)
|
---|
470 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
471 | else
|
---|
472 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
473 |
|
---|
474 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
475 |
|
---|
476 | break;
|
---|
477 | }
|
---|
478 | case MSG_TYPE_LOGIN:
|
---|
479 | {
|
---|
480 | cout << "Got login message" << endl;
|
---|
481 |
|
---|
482 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
483 |
|
---|
484 | string username(clientMsg.buffer);
|
---|
485 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
486 |
|
---|
487 | Player* p = da.getPlayer(username);
|
---|
488 |
|
---|
489 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
490 | {
|
---|
491 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
492 | }
|
---|
493 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
494 | {
|
---|
495 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
500 |
|
---|
501 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
502 | p->id = unusedPlayerId;
|
---|
503 | cout << "new player id: " << p->id << endl;
|
---|
504 | p->setAddr(from);
|
---|
505 |
|
---|
506 | // choose a random team (either 0 or 1)
|
---|
507 | p->team = rand() % 2;
|
---|
508 |
|
---|
509 | // choose a random class
|
---|
510 | int intClass = rand() % 2;
|
---|
511 | switch (intClass) {
|
---|
512 | case 0:
|
---|
513 | p->setClass(Player::CLASS_WARRIOR);
|
---|
514 | break;
|
---|
515 | case 1:
|
---|
516 | p->setClass(Player::CLASS_RANGER);
|
---|
517 | break;
|
---|
518 | }
|
---|
519 |
|
---|
520 | // tell the new player about all the existing players
|
---|
521 | cout << "Sending other players to new player" << endl;
|
---|
522 |
|
---|
523 | map<unsigned int, Player>::iterator it;
|
---|
524 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
525 | {
|
---|
526 | it->second.serialize(serverMsg.buffer);
|
---|
527 |
|
---|
528 | cout << "sending info about " << it->second.name << endl;
|
---|
529 | cout << "sending id " << it->second.id << endl;
|
---|
530 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
531 | error("sendMessage");
|
---|
532 | }
|
---|
533 |
|
---|
534 | // tell the new player about all map objects
|
---|
535 | // (currently just the flags)
|
---|
536 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
537 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
538 | vector<WorldMap::Object>::iterator itObjects;
|
---|
539 | cout << "sending items" << endl;
|
---|
540 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
541 | itObjects->serialize(serverMsg.buffer);
|
---|
542 | cout << "sending item id " << itObjects->id << endl;
|
---|
543 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
544 | error("sendMessage");
|
---|
545 | }
|
---|
546 |
|
---|
547 | // send the current score
|
---|
548 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
549 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
550 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
551 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
552 | error("sendMessage");
|
---|
553 |
|
---|
554 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
555 | p->serialize(serverMsg.buffer);
|
---|
556 | cout << "Should be broadcasting the message" << endl;
|
---|
557 |
|
---|
558 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
559 | {
|
---|
560 | cout << "Sent message back to " << it->second.name << endl;
|
---|
561 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
562 | error("sendMessage");
|
---|
563 | }
|
---|
564 |
|
---|
565 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
566 | mapPlayers[unusedPlayerId] = *p;
|
---|
567 | }
|
---|
568 |
|
---|
569 | delete(p);
|
---|
570 |
|
---|
571 | break;
|
---|
572 | }
|
---|
573 | case MSG_TYPE_LOGOUT:
|
---|
574 | {
|
---|
575 | string name(clientMsg.buffer);
|
---|
576 | cout << "Player logging out: " << name << endl;
|
---|
577 |
|
---|
578 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
579 |
|
---|
580 | if (p == NULL)
|
---|
581 | {
|
---|
582 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
583 | cout << "Player not logged in" << endl;
|
---|
584 | }
|
---|
585 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
586 | p->addr.sin_port != from.sin_port )
|
---|
587 | {
|
---|
588 | 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.");
|
---|
589 | cout << "Player logged in using a different connection" << endl;
|
---|
590 | }
|
---|
591 | else
|
---|
592 | {
|
---|
593 | if (p->id < unusedPlayerId)
|
---|
594 | unusedPlayerId = p->id;
|
---|
595 | mapPlayers.erase(p->id);
|
---|
596 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
597 | }
|
---|
598 |
|
---|
599 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
600 |
|
---|
601 | break;
|
---|
602 | }
|
---|
603 | case MSG_TYPE_CHAT:
|
---|
604 | {
|
---|
605 | cout << "Got a chat message" << endl;
|
---|
606 |
|
---|
607 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
608 |
|
---|
609 | if (p == NULL)
|
---|
610 | {
|
---|
611 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
612 | }
|
---|
613 | else
|
---|
614 | {
|
---|
615 | broadcastResponse = true;
|
---|
616 |
|
---|
617 | ostringstream oss;
|
---|
618 | oss << p->name << ": " << clientMsg.buffer;
|
---|
619 |
|
---|
620 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
621 | }
|
---|
622 |
|
---|
623 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
624 |
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | case MSG_TYPE_PLAYER_MOVE:
|
---|
628 | {
|
---|
629 | cout << "PLAYER_MOVE" << endl;
|
---|
630 |
|
---|
631 | int id, x, y;
|
---|
632 |
|
---|
633 | memcpy(&id, clientMsg.buffer, 4);
|
---|
634 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
635 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
636 |
|
---|
637 | cout << "x: " << x << endl;
|
---|
638 | cout << "y: " << y << endl;
|
---|
639 | cout << "id: " << id << endl;
|
---|
640 |
|
---|
641 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
642 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
643 | {
|
---|
644 | // we need to make sure the player can move here
|
---|
645 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
646 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
647 | {
|
---|
648 | cout << "valid terrain" << endl;
|
---|
649 |
|
---|
650 | mapPlayers[id].target.x = x;
|
---|
651 | mapPlayers[id].target.y = y;
|
---|
652 |
|
---|
653 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
654 |
|
---|
655 | memcpy(serverMsg.buffer, &id, 4);
|
---|
656 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
657 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
658 |
|
---|
659 | broadcastResponse = true;
|
---|
660 | }
|
---|
661 | else
|
---|
662 | cout << "Bad terrain detected" << endl;
|
---|
663 | }
|
---|
664 | else // nned to send back a message indicating failure
|
---|
665 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
666 |
|
---|
667 | break;
|
---|
668 | }
|
---|
669 | case MSG_TYPE_PICKUP_FLAG:
|
---|
670 | {
|
---|
671 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
672 | cout << "PICKUP_FLAG" << endl;
|
---|
673 |
|
---|
674 | int id;
|
---|
675 |
|
---|
676 | memcpy(&id, clientMsg.buffer, 4);
|
---|
677 | cout << "id: " << id << endl;
|
---|
678 |
|
---|
679 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
680 | vector<WorldMap::Object>::iterator itObjects;
|
---|
681 |
|
---|
682 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
683 | POSITION pos = itObjects->pos;
|
---|
684 | bool gotFlag = false;
|
---|
685 |
|
---|
686 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
---|
687 | switch (itObjects->type) {
|
---|
688 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
689 | if (mapPlayers[id].team == 1) {
|
---|
690 | gotFlag = true;
|
---|
691 | mapPlayers[id].hasBlueFlag = true;
|
---|
692 | broadcastResponse = true;
|
---|
693 | }
|
---|
694 | break;
|
---|
695 | case WorldMap::OBJECT_RED_FLAG:
|
---|
696 | if (mapPlayers[id].team == 0) {
|
---|
697 | gotFlag = true;
|
---|
698 | mapPlayers[id].hasRedFlag = true;
|
---|
699 | broadcastResponse = true;
|
---|
700 | }
|
---|
701 | break;
|
---|
702 | }
|
---|
703 |
|
---|
704 | if (gotFlag) {
|
---|
705 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
706 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
707 |
|
---|
708 | map<unsigned int, Player>::iterator it;
|
---|
709 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
710 | {
|
---|
711 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
712 | error("sendMessage");
|
---|
713 | }
|
---|
714 |
|
---|
715 | // remove the object from the server-side map
|
---|
716 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
717 | itObjects = vctObjects->erase(itObjects);
|
---|
718 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | if (!gotFlag)
|
---|
723 | itObjects++;
|
---|
724 | }
|
---|
725 |
|
---|
726 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
727 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
728 |
|
---|
729 | break;
|
---|
730 | }
|
---|
731 | case MSG_TYPE_DROP_FLAG:
|
---|
732 | {
|
---|
733 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
734 | cout << "DROP_FLAG" << endl;
|
---|
735 |
|
---|
736 | int id;
|
---|
737 |
|
---|
738 | memcpy(&id, clientMsg.buffer, 4);
|
---|
739 | cout << "id: " << id << endl;
|
---|
740 |
|
---|
741 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
742 | if (mapPlayers[id].hasBlueFlag)
|
---|
743 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
744 | else if (mapPlayers[id].hasRedFlag)
|
---|
745 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
746 |
|
---|
747 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
748 |
|
---|
749 | // need to send the OBJECT message too
|
---|
750 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
751 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
752 |
|
---|
753 | map<unsigned int, Player>::iterator it;
|
---|
754 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
755 | {
|
---|
756 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
757 | error("sendMessage");
|
---|
758 | }
|
---|
759 |
|
---|
760 | mapPlayers[id].hasBlueFlag = false;
|
---|
761 | mapPlayers[id].hasRedFlag = false;
|
---|
762 |
|
---|
763 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
764 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
765 |
|
---|
766 | broadcastResponse = true;
|
---|
767 |
|
---|
768 | break;
|
---|
769 | }
|
---|
770 | case MSG_TYPE_START_ATTACK:
|
---|
771 | {
|
---|
772 | cout << "Received a START_ATTACK message" << endl;
|
---|
773 |
|
---|
774 | int id, targetId;
|
---|
775 |
|
---|
776 | memcpy(&id, clientMsg.buffer, 4);
|
---|
777 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
778 |
|
---|
779 | Player* source = &mapPlayers[id];
|
---|
780 | source->timeAttackStarted = getCurrentMillis();
|
---|
781 | source->targetPlayer = targetId;
|
---|
782 | source->isAttacking = true;
|
---|
783 |
|
---|
784 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
785 | memcpy(serverMsg.buffer, &id, 4);
|
---|
786 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
787 | broadcastResponse = true;
|
---|
788 |
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | case MSG_TYPE_ATTACK:
|
---|
792 | {
|
---|
793 | cout << "Received am ATTACK message" << endl;
|
---|
794 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
795 |
|
---|
796 | break;
|
---|
797 | }
|
---|
798 | default:
|
---|
799 | {
|
---|
800 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
801 |
|
---|
802 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
803 |
|
---|
804 | break;
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | return broadcastResponse;
|
---|
809 | }
|
---|
810 |
|
---|
811 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
812 | {
|
---|
813 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
814 | id++;
|
---|
815 | }
|
---|
816 |
|
---|
817 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
---|
818 | {
|
---|
819 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
---|
820 | id++;
|
---|
821 | }
|
---|