[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
|
---|
| 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);
|
---|
[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;
|
---|
[e084950] | 86 |
|
---|
[edfd1d0] | 87 | //SSL_load_error_strings();
|
---|
| 88 | //ERR_load_BIO_strings();
|
---|
| 89 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 90 |
|
---|
| 91 | if (argc < 2) {
|
---|
[73f75c1] | 92 | cerr << "ERROR, no port provided" << endl;
|
---|
| 93 | exit(1);
|
---|
[e3535b3] | 94 | }
|
---|
[60017fc] | 95 |
|
---|
[7b43385] | 96 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
---|
[6e66ffd] | 97 |
|
---|
| 98 | // add some items to the map. They will be sent out
|
---|
| 99 | // to players when they login
|
---|
[5f868c0] | 100 | for (int y=0; y<gameMap->height; y++) {
|
---|
| 101 | for (int x=0; x<gameMap->width; x++) {
|
---|
| 102 | switch (gameMap->getStructure(x, y)) {
|
---|
| 103 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
| 104 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
| 105 | break;
|
---|
| 106 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
| 107 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[6e66ffd] | 112 |
|
---|
[371ce29] | 113 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[e3535b3] | 114 | if (sock < 0) error("Opening socket");
|
---|
| 115 | length = sizeof(server);
|
---|
| 116 | bzero(&server,length);
|
---|
| 117 | server.sin_family=AF_INET;
|
---|
| 118 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 119 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 120 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 121 | error("binding");
|
---|
[73f75c1] | 122 |
|
---|
[371ce29] | 123 | set_nonblock(sock);
|
---|
| 124 |
|
---|
[da692b9] | 125 | bool broadcastResponse;
|
---|
[d211210] | 126 | timespec ts;
|
---|
[430c80e] | 127 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
---|
[cb1f288] | 128 | while (true) {
|
---|
[371ce29] | 129 |
|
---|
| 130 | usleep(5000);
|
---|
| 131 |
|
---|
[d211210] | 132 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
[430c80e] | 133 | // make the number smaller so millis can fit in an int
|
---|
[d69eb32] | 134 | ts.tv_sec -= 1368000000;
|
---|
[430c80e] | 135 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
[d211210] | 136 |
|
---|
[430c80e] | 137 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
|
---|
[d211210] | 138 | timeLastUpdated = curTime;
|
---|
| 139 |
|
---|
| 140 | // maybe put this in a separate method
|
---|
[23559e7] | 141 | map<unsigned int, Player>::iterator it;
|
---|
| 142 | FLOAT_POSITION oldPos;
|
---|
| 143 | bool broadcastMove = false;
|
---|
[d211210] | 144 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
[23559e7] | 145 | oldPos = it->second.pos;
|
---|
| 146 | if (it->second.move(gameMap)) {
|
---|
| 147 |
|
---|
| 148 | // check if the move needs to be canceled
|
---|
| 149 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
| 150 | case WorldMap::TERRAIN_NONE:
|
---|
| 151 | case WorldMap::TERRAIN_OCEAN:
|
---|
| 152 | case WorldMap::TERRAIN_ROCK:
|
---|
[e4c60ba] | 153 | {
|
---|
[23559e7] | 154 | it->second.pos = oldPos;
|
---|
| 155 | it->second.target.x = it->second.pos.x;
|
---|
| 156 | it->second.target.y = it->second.pos.y;
|
---|
| 157 | broadcastMove = true;
|
---|
| 158 | break;
|
---|
[e4c60ba] | 159 | }
|
---|
[23559e7] | 160 | default:
|
---|
| 161 | // if there are no obstacles, do nothing
|
---|
| 162 | break;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[e4c60ba] | 165 | WorldMap::ObjectType flagType;
|
---|
| 166 | POSITION pos;
|
---|
[7553db9] | 167 | bool flagTurnedIn = false;
|
---|
| 168 |
|
---|
[e4c60ba] | 169 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
---|
| 170 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
| 171 | {
|
---|
[7553db9] | 172 | if (it->second.team == 0 && it->second.hasRedFlag)
|
---|
| 173 | {
|
---|
[e4c60ba] | 174 | it->second.hasRedFlag = false;
|
---|
| 175 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 176 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
[7553db9] | 177 | flagTurnedIn = true;
|
---|
[e4c60ba] | 178 | }
|
---|
[7553db9] | 179 |
|
---|
| 180 | break;
|
---|
[e4c60ba] | 181 | }
|
---|
| 182 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
| 183 | {
|
---|
[7553db9] | 184 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
---|
| 185 | {
|
---|
[e4c60ba] | 186 | it->second.hasBlueFlag = false;
|
---|
| 187 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 188 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
[7553db9] | 189 | flagTurnedIn = true;
|
---|
[e4c60ba] | 190 | }
|
---|
| 191 |
|
---|
[7553db9] | 192 | break;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[e4c60ba] | 195 |
|
---|
[7553db9] | 196 | if (flagTurnedIn) {
|
---|
| 197 | // send an OBJECT message to add the flag back to its spawn point
|
---|
| 198 | pos.x = pos.x*25+12;
|
---|
| 199 | pos.y = pos.y*25+12;
|
---|
| 200 | gameMap->addObject(flagType, pos.x, pos.y);
|
---|
[e4c60ba] | 201 |
|
---|
[7553db9] | 202 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
| 203 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
[e4c60ba] | 204 |
|
---|
[7553db9] | 205 | map<unsigned int, Player>::iterator it2;
|
---|
| 206 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
| 207 | {
|
---|
| 208 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
| 209 | error("sendMessage");
|
---|
[e4c60ba] | 210 | }
|
---|
[7553db9] | 211 |
|
---|
| 212 | // this means a PLAYER message will be sent
|
---|
| 213 | broadcastMove = true;
|
---|
[e4c60ba] | 214 | }
|
---|
| 215 |
|
---|
[e487381] | 216 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
[b07eeac] | 217 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 218 |
|
---|
[e487381] | 219 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
[b07eeac] | 220 | POSITION pos = itObjects->pos;
|
---|
[7553db9] | 221 | bool gotFlag = false;
|
---|
| 222 |
|
---|
[b07eeac] | 223 | if (posDistance(it->second.pos, pos.toFloat()) < 10) {
|
---|
| 224 | switch (itObjects->type) {
|
---|
| 225 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
[7553db9] | 226 | if (it->second.team == 1) {
|
---|
| 227 | gotFlag = true;
|
---|
| 228 | it->second.hasBlueFlag = true;
|
---|
| 229 | broadcastMove = true;
|
---|
| 230 | }
|
---|
[b07eeac] | 231 | break;
|
---|
| 232 | case WorldMap::OBJECT_RED_FLAG:
|
---|
[7553db9] | 233 | if (it->second.team == 0) {
|
---|
| 234 | gotFlag = true;
|
---|
| 235 | it->second.hasRedFlag = true;
|
---|
| 236 | broadcastMove = true;
|
---|
| 237 | }
|
---|
[b07eeac] | 238 | break;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[7553db9] | 241 | if (gotFlag) {
|
---|
| 242 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
| 243 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
| 244 |
|
---|
| 245 | map<unsigned int, Player>::iterator it2;
|
---|
| 246 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
| 247 | {
|
---|
| 248 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
| 249 | error("sendMessage");
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | // remove the object from the server-side map
|
---|
| 253 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
| 254 | itObjects = vctObjects->erase(itObjects);
|
---|
| 255 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
[b07eeac] | 256 | }
|
---|
| 257 | }
|
---|
[7553db9] | 258 |
|
---|
[e330873] | 259 | if (!gotFlag)
|
---|
| 260 | itObjects++;
|
---|
[23559e7] | 261 | }
|
---|
| 262 |
|
---|
| 263 | if (broadcastMove) {
|
---|
| 264 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 265 | it->second.serialize(serverMsg.buffer);
|
---|
| 266 |
|
---|
| 267 | cout << "about to broadcast move" << endl;
|
---|
[b07eeac] | 268 | map<unsigned int, Player>::iterator it2;
|
---|
[23559e7] | 269 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
| 270 | {
|
---|
| 271 | if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
---|
| 272 | error("sendMessage");
|
---|
| 273 | }
|
---|
[d211210] | 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[e084950] | 279 | n = receiveMessage(&clientMsg, sock, &from);
|
---|
[8e540f4] | 280 |
|
---|
[371ce29] | 281 | if (n >= 0) {
|
---|
[d211210] | 282 | broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
|
---|
[371ce29] | 283 |
|
---|
[b128109] | 284 | // probably replace this with a function that prints based on the
|
---|
| 285 | // message type
|
---|
[371ce29] | 286 | cout << "msg: " << serverMsg.buffer << endl;
|
---|
[da692b9] | 287 | if (broadcastResponse)
|
---|
[3b1efcc] | 288 | {
|
---|
[da692b9] | 289 | cout << "Should be broadcasting the message" << endl;
|
---|
| 290 |
|
---|
[01d0d00] | 291 | map<unsigned int, Player>::iterator it;
|
---|
| 292 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
[3b1efcc] | 293 | {
|
---|
[d211210] | 294 | cout << "Sent message back to " << it->second.name << endl;
|
---|
[01d0d00] | 295 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[3b1efcc] | 296 | error("sendMessage");
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | else
|
---|
| 300 | {
|
---|
[da692b9] | 301 | cout << "Should be sending back the message" << endl;
|
---|
| 302 |
|
---|
[3b1efcc] | 303 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 304 | error("sendMessage");
|
---|
| 305 | }
|
---|
[7b43385] | 306 | }
|
---|
[8e540f4] | 307 | }
|
---|
[371ce29] | 308 |
|
---|
[8e540f4] | 309 | return 0;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[d211210] | 312 | 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)
|
---|
[8e540f4] | 313 | {
|
---|
[41ad8ed] | 314 | DataAccess da;
|
---|
| 315 |
|
---|
[b8cb03f] | 316 | cout << "Received message" << endl;
|
---|
[8e540f4] | 317 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 318 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 319 |
|
---|
[da692b9] | 320 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 321 | bool broadcastResponse = false;
|
---|
| 322 |
|
---|
[8e540f4] | 323 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 324 | // receive and display the response. Maybe make a special error msg type
|
---|
| 325 | switch(clientMsg.type)
|
---|
| 326 | {
|
---|
| 327 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 328 | {
|
---|
[8e540f4] | 329 | string username(clientMsg.buffer);
|
---|
| 330 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[d2b411a] | 331 |
|
---|
[8e540f4] | 332 | cout << "username: " << username << endl;
|
---|
| 333 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 334 |
|
---|
[3b1efcc] | 335 | int error = da.insertPlayer(username, password);
|
---|
[41ad8ed] | 336 |
|
---|
[3b1efcc] | 337 | if (!error)
|
---|
| 338 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 339 | else
|
---|
| 340 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 341 |
|
---|
| 342 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 343 |
|
---|
[8e540f4] | 344 | break;
|
---|
| 345 | }
|
---|
| 346 | case MSG_TYPE_LOGIN:
|
---|
| 347 | {
|
---|
[60017fc] | 348 | cout << "Got login message" << endl;
|
---|
| 349 |
|
---|
[d211210] | 350 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 351 |
|
---|
[8e540f4] | 352 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 353 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 354 |
|
---|
[41ad8ed] | 355 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 356 |
|
---|
[b128109] | 357 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 358 | {
|
---|
| 359 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 360 | }
|
---|
[01d0d00] | 361 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 362 | {
|
---|
| 363 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 364 | }
|
---|
| 365 | else
|
---|
[8e540f4] | 366 | {
|
---|
[d211210] | 367 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 368 |
|
---|
[1106210] | 369 | updateUnusedId(unusedId, mapPlayers);
|
---|
[01d0d00] | 370 | p->id = unusedId;
|
---|
[d211210] | 371 | cout << "new player id: " << p->id << endl;
|
---|
[df79cfd] | 372 | p->setAddr(from);
|
---|
| 373 |
|
---|
| 374 | // choose a random team (either 0 or 1)
|
---|
| 375 | p->team = rand() % 2;
|
---|
[d211210] | 376 |
|
---|
| 377 | // tell the new player about all the existing players
|
---|
| 378 | cout << "Sending other players to new player" << endl;
|
---|
| 379 |
|
---|
| 380 | map<unsigned int, Player>::iterator it;
|
---|
| 381 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 382 | {
|
---|
| 383 | it->second.serialize(serverMsg.buffer);
|
---|
| 384 |
|
---|
| 385 | cout << "sending info about " << it->second.name << endl;
|
---|
[5f868c0] | 386 | cout << "sending id " << it->second.id << endl;
|
---|
| 387 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 388 | error("sendMessage");
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | // tell the new player about all map objects
|
---|
| 392 | // (currently just the flags)
|
---|
| 393 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
[e487381] | 394 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
[5f868c0] | 395 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 396 | cout << "sending items" << endl;
|
---|
[e487381] | 397 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
[5f868c0] | 398 | itObjects->serialize(serverMsg.buffer);
|
---|
| 399 | cout << "sending item id " << itObjects->id << endl;
|
---|
[d211210] | 400 | if ( sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
| 401 | error("sendMessage");
|
---|
| 402 | }
|
---|
[59061f6] | 403 |
|
---|
[594d2e9] | 404 | p->serialize(serverMsg.buffer);
|
---|
[d211210] | 405 | cout << "Should be broadcasting the message" << endl;
|
---|
| 406 |
|
---|
| 407 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 408 | {
|
---|
| 409 | cout << "Sent message back to " << it->second.name << endl;
|
---|
| 410 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
| 411 | error("sendMessage");
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 415 | mapPlayers[unusedId] = *p;
|
---|
[07028b9] | 416 | }
|
---|
| 417 |
|
---|
[41ad8ed] | 418 | delete(p);
|
---|
[07028b9] | 419 |
|
---|
[8e540f4] | 420 | break;
|
---|
| 421 | }
|
---|
| 422 | case MSG_TYPE_LOGOUT:
|
---|
| 423 | {
|
---|
| 424 | string name(clientMsg.buffer);
|
---|
| 425 | cout << "Player logging out: " << name << endl;
|
---|
| 426 |
|
---|
[01d0d00] | 427 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 428 |
|
---|
[8e540f4] | 429 | if (p == NULL)
|
---|
| 430 | {
|
---|
| 431 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 432 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 433 | }
|
---|
[01d0d00] | 434 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 435 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 436 | {
|
---|
[8e540f4] | 437 | 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] | 438 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 439 | }
|
---|
[8e540f4] | 440 | else
|
---|
[2488852] | 441 | {
|
---|
[01d0d00] | 442 | if (p->id < unusedId)
|
---|
| 443 | unusedId = p->id;
|
---|
| 444 | mapPlayers.erase(p->id);
|
---|
[41ad8ed] | 445 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8e540f4] | 446 | }
|
---|
[07028b9] | 447 |
|
---|
[d211210] | 448 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[8a3ef42] | 449 |
|
---|
[8e540f4] | 450 | break;
|
---|
| 451 | }
|
---|
| 452 | case MSG_TYPE_CHAT:
|
---|
| 453 | {
|
---|
[da692b9] | 454 | cout << "Got a chat message" << endl;
|
---|
| 455 |
|
---|
[01d0d00] | 456 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 457 |
|
---|
[8e540f4] | 458 | if (p == NULL)
|
---|
| 459 | {
|
---|
| 460 | 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] | 461 | }
|
---|
[8e540f4] | 462 | else
|
---|
| 463 | {
|
---|
[3b1efcc] | 464 | broadcastResponse = true;
|
---|
| 465 |
|
---|
[b128109] | 466 | ostringstream oss;
|
---|
| 467 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 468 |
|
---|
[b128109] | 469 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 470 | }
|
---|
| 471 |
|
---|
| 472 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 473 |
|
---|
| 474 | break;
|
---|
[e084950] | 475 | }
|
---|
[b128109] | 476 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 477 | {
|
---|
| 478 | cout << "PLAYER_MOVE" << endl;
|
---|
| 479 |
|
---|
| 480 | int id, x, y;
|
---|
| 481 |
|
---|
| 482 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 483 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
| 484 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
[7b43385] | 485 |
|
---|
[b128109] | 486 | cout << "x: " << x << endl;
|
---|
| 487 | cout << "y: " << y << endl;
|
---|
| 488 | cout << "id: " << id << endl;
|
---|
[7b43385] | 489 |
|
---|
[b128109] | 490 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
| 491 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
| 492 | {
|
---|
[60017fc] | 493 | // we need to make sure the player can move here
|
---|
| 494 | if (0 <= x && x < 300 && 0 <= y && y < 300 &&
|
---|
| 495 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
| 496 | {
|
---|
[7b43385] | 497 | cout << "valid terrain" << endl;
|
---|
| 498 |
|
---|
[60017fc] | 499 | mapPlayers[id].target.x = x;
|
---|
| 500 | mapPlayers[id].target.y = y;
|
---|
| 501 |
|
---|
| 502 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 503 |
|
---|
| 504 | memcpy(serverMsg.buffer, &id, 4);
|
---|
[d211210] | 505 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
| 506 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
[60017fc] | 507 |
|
---|
| 508 | broadcastResponse = true;
|
---|
| 509 | }
|
---|
| 510 | else
|
---|
| 511 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 512 | }
|
---|
| 513 | else // nned to send back a message indicating failure
|
---|
| 514 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
| 515 |
|
---|
| 516 | break;
|
---|
| 517 | }
|
---|
[5299436] | 518 | case MSG_TYPE_PICKUP_FLAG:
|
---|
| 519 | {
|
---|
| 520 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 521 | cout << "PICKUP_FLAG" << endl;
|
---|
| 522 |
|
---|
| 523 | int id;
|
---|
| 524 |
|
---|
| 525 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 526 | cout << "id: " << id << endl;
|
---|
| 527 |
|
---|
| 528 | break;
|
---|
| 529 | }
|
---|
[e487381] | 530 | case MSG_TYPE_DROP_FLAG:
|
---|
| 531 | {
|
---|
| 532 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 533 | cout << "DROP_FLAG" << endl;
|
---|
| 534 |
|
---|
| 535 | int id;
|
---|
| 536 |
|
---|
| 537 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 538 | cout << "id: " << id << endl;
|
---|
| 539 |
|
---|
| 540 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
| 541 | if (mapPlayers[id].hasBlueFlag)
|
---|
| 542 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 543 | else if (mapPlayers[id].hasRedFlag)
|
---|
| 544 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 545 |
|
---|
| 546 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
| 547 |
|
---|
| 548 | // need to send the OBJECT message too
|
---|
| 549 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
| 550 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
| 551 |
|
---|
| 552 | map<unsigned int, Player>::iterator it;
|
---|
| 553 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 554 | {
|
---|
| 555 | if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
| 556 | error("sendMessage");
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | mapPlayers[id].hasBlueFlag = false;
|
---|
| 560 | mapPlayers[id].hasRedFlag = false;
|
---|
| 561 |
|
---|
| 562 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 563 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
| 564 |
|
---|
| 565 | map<unsigned int, Player>::iterator it2;
|
---|
| 566 | broadcastResponse = true;
|
---|
| 567 |
|
---|
| 568 | break;
|
---|
| 569 | }
|
---|
[8e540f4] | 570 | default:
|
---|
| 571 | {
|
---|
| 572 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 573 |
|
---|
[8e540f4] | 574 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 575 |
|
---|
[8e540f4] | 576 | break;
|
---|
| 577 | }
|
---|
[e3535b3] | 578 | }
|
---|
[da692b9] | 579 |
|
---|
| 580 | return broadcastResponse;
|
---|
[e3535b3] | 581 | }
|
---|
[da692b9] | 582 |
|
---|
[1106210] | 583 | void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
[01d0d00] | 584 | {
|
---|
[1106210] | 585 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 586 | id++;
|
---|
[01d0d00] | 587 | }
|
---|