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