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