source: network-game/server/server.cpp@ e4c60ba

Last change on this file since e4c60ba was e4c60ba, checked in by dportnoy <dmp1488@…>, 12 years ago

Players can turn in flags they have picked up to their own flag sites

  • Property mode set to 100644
File size: 18.4 KB
RevLine 
[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]36using 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
40bool 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]42void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
[8e540f4]43
[73f75c1]44// this should probably go somewhere in the common folder
[e3535b3]45void error(const char *msg)
46{
47 perror(msg);
48 exit(0);
49}
50
[01d0d00]51Player *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]64Player *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]78int 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;
167 switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
168 case WorldMap::STRUCTURE_BLUE_FLAG:
169 {
170 if (!it->second.team == 0 || !it->second.hasRedFlag)
171 break;
172 else {
173 it->second.hasRedFlag = false;
174 flagType = WorldMap::OBJECT_RED_FLAG;
175 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
176 }
177 }
178 case WorldMap::STRUCTURE_RED_FLAG:
179 {
180 if (!it->second.team == 1 || !it->second.hasBlueFlag)
181 break;
182 else {
183 it->second.hasBlueFlag = false;
184 flagType = WorldMap::OBJECT_BLUE_FLAG;
185 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
186 }
187
188 // all code from here to the break is executed for both cases of the switch
189
190 // send an OBJECT message to add the flag back to its spawn point
191 pos.x = pos.x*25+12;
192 pos.y = pos.y*25+12;
193 gameMap->addObject(flagType, pos.x, pos.y);
194
195 serverMsg.type = MSG_TYPE_OBJECT;
196 gameMap->getObjects()->back().serialize(serverMsg.buffer);
197
198 map<unsigned int, Player>::iterator it2;
199 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
200 {
201 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
202 error("sendMessage");
203 }
204
205 // this means a PLAYER message will be send
206 broadcastMove = true;
207
208 break;
209 }
210 default:
211 break;
212 }
213
[e487381]214 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
[b07eeac]215 vector<WorldMap::Object>::iterator itObjects;
216
[e487381]217 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
[b07eeac]218 POSITION pos = itObjects->pos;
219 if (posDistance(it->second.pos, pos.toFloat()) < 10) {
220 switch (itObjects->type) {
221 case WorldMap::OBJECT_BLUE_FLAG:
222 cout << "Got blue flag" << endl;
223 it->second.hasBlueFlag = true;
224 broadcastMove = true;
225 break;
226 case WorldMap::OBJECT_RED_FLAG:
227 cout << "Got red flag" << endl;
228 it->second.hasRedFlag = true;
229 broadcastMove = true;
230 break;
231 }
232
233 serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
234 memcpy(serverMsg.buffer, &itObjects->id, 4);
235
236 map<unsigned int, Player>::iterator it2;
237 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
238 {
239 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
240 error("sendMessage");
241 }
[e487381]242
[b8cb03f]243 // remove the object from the server-side map
[e487381]244 cout << "size before: " << gameMap->getObjects()->size() << endl;
245 itObjects = vctObjects->erase(itObjects);
246 cout << "size after: " << gameMap->getObjects()->size() << endl;
[b07eeac]247 }
[e487381]248 else
249 itObjects++;
[23559e7]250 }
251
252 if (broadcastMove) {
253 serverMsg.type = MSG_TYPE_PLAYER;
254 it->second.serialize(serverMsg.buffer);
255
256 cout << "about to broadcast move" << endl;
[b07eeac]257 map<unsigned int, Player>::iterator it2;
[23559e7]258 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
259 {
260 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
261 error("sendMessage");
262 }
[d211210]263 }
264 }
265 }
266 }
267
[e084950]268 n = receiveMessage(&clientMsg, sock, &from);
[8e540f4]269
[371ce29]270 if (n >= 0) {
[d211210]271 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
[371ce29]272
[b128109]273 // probably replace this with a function that prints based on the
274 // message type
[371ce29]275 cout << "msg: " << serverMsg.buffer << endl;
[da692b9]276 if (broadcastResponse)
[3b1efcc]277 {
[da692b9]278 cout << "Should be broadcasting the message" << endl;
279
[01d0d00]280 map<unsigned int, Player>::iterator it;
281 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]282 {
[d211210]283 cout << "Sent message back to " << it->second.name << endl;
[01d0d00]284 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
[3b1efcc]285 error("sendMessage");
286 }
287 }
288 else
289 {
[da692b9]290 cout << "Should be sending back the message" << endl;
291
[3b1efcc]292 if ( sendMessage(&serverMsg, sock, &from) < 0 )
293 error("sendMessage");
294 }
[7b43385]295 }
[8e540f4]296 }
[371ce29]297
[8e540f4]298 return 0;
299}
300
[d211210]301bool 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]302{
[41ad8ed]303 DataAccess da;
304
[b8cb03f]305 cout << "Received message" << endl;
[8e540f4]306 cout << "MSG: type: " << clientMsg.type << endl;
307 cout << "MSG contents: " << clientMsg.buffer << endl;
308
[da692b9]309 // maybe we should make a message class and have this be a member
[3b1efcc]310 bool broadcastResponse = false;
311
[8e540f4]312 // Check that if an invalid message is sent, the client will correctly
313 // receive and display the response. Maybe make a special error msg type
314 switch(clientMsg.type)
315 {
316 case MSG_TYPE_REGISTER:
[d2b411a]317 {
[8e540f4]318 string username(clientMsg.buffer);
319 string password(strchr(clientMsg.buffer, '\0')+1);
[d2b411a]320
[8e540f4]321 cout << "username: " << username << endl;
322 cout << "password: " << password << endl;
[d2b411a]323
[3b1efcc]324 int error = da.insertPlayer(username, password);
[41ad8ed]325
[3b1efcc]326 if (!error)
327 strcpy(serverMsg.buffer, "Registration successful.");
328 else
329 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[8e540f4]330
331 serverMsg.type = MSG_TYPE_REGISTER;
[d2b411a]332
[8e540f4]333 break;
334 }
335 case MSG_TYPE_LOGIN:
336 {
[60017fc]337 cout << "Got login message" << endl;
338
[d211210]339 serverMsg.type = MSG_TYPE_LOGIN;
340
[8e540f4]341 string username(clientMsg.buffer);
[41ad8ed]342 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]343
[41ad8ed]344 Player* p = da.getPlayer(username);
[d2b411a]345
[b128109]346 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]347 {
348 strcpy(serverMsg.buffer, "Incorrect username or password");
349 }
[01d0d00]350 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]351 {
352 strcpy(serverMsg.buffer, "Player has already logged in.");
353 }
354 else
[8e540f4]355 {
[d211210]356 serverMsg.type = MSG_TYPE_PLAYER;
357
[1106210]358 updateUnusedId(unusedId, mapPlayers);
[01d0d00]359 p->id = unusedId;
[d211210]360 cout << "new player id: " << p->id << endl;
[df79cfd]361 p->setAddr(from);
362
363 // choose a random team (either 0 or 1)
364 p->team = rand() % 2;
[d211210]365
366 // tell the new player about all the existing players
367 cout << "Sending other players to new player" << endl;
368
369 map<unsigned int, Player>::iterator it;
370 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
371 {
372 it->second.serialize(serverMsg.buffer);
373
374 cout << "sending info about " << it->second.name << endl;
[5f868c0]375 cout << "sending id " << it->second.id << endl;
376 if ( sendMessage(&serverMsg, sock, &from) < 0 )
377 error("sendMessage");
378 }
379
380 // tell the new player about all map objects
381 // (currently just the flags)
382 serverMsg.type = MSG_TYPE_OBJECT;
[e487381]383 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
[5f868c0]384 vector<WorldMap::Object>::iterator itObjects;
385 cout << "sending items" << endl;
[e487381]386 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
[5f868c0]387 itObjects->serialize(serverMsg.buffer);
388 cout << "sending item id " << itObjects->id << endl;
[d211210]389 if ( sendMessage(&serverMsg, sock, &from) < 0 )
390 error("sendMessage");
391 }
[59061f6]392
[594d2e9]393 p->serialize(serverMsg.buffer);
[d211210]394 cout << "Should be broadcasting the message" << endl;
395
396 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
397 {
398 cout << "Sent message back to " << it->second.name << endl;
399 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
400 error("sendMessage");
401 }
402
403 serverMsg.type = MSG_TYPE_LOGIN;
404 mapPlayers[unusedId] = *p;
[07028b9]405 }
406
[41ad8ed]407 delete(p);
[07028b9]408
[8e540f4]409 break;
410 }
411 case MSG_TYPE_LOGOUT:
412 {
413 string name(clientMsg.buffer);
414 cout << "Player logging out: " << name << endl;
415
[01d0d00]416 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]417
[8e540f4]418 if (p == NULL)
419 {
420 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]421 cout << "Player not logged in" << endl;
[07028b9]422 }
[01d0d00]423 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
424 p->addr.sin_port != from.sin_port )
[07028b9]425 {
[8e540f4]426 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]427 cout << "Player logged in using a different connection" << endl;
[2488852]428 }
[8e540f4]429 else
[2488852]430 {
[01d0d00]431 if (p->id < unusedId)
432 unusedId = p->id;
433 mapPlayers.erase(p->id);
[41ad8ed]434 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8e540f4]435 }
[07028b9]436
[d211210]437 serverMsg.type = MSG_TYPE_LOGOUT;
[8a3ef42]438
[8e540f4]439 break;
440 }
441 case MSG_TYPE_CHAT:
442 {
[da692b9]443 cout << "Got a chat message" << endl;
444
[01d0d00]445 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]446
[8e540f4]447 if (p == NULL)
448 {
449 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]450 }
[8e540f4]451 else
452 {
[3b1efcc]453 broadcastResponse = true;
454
[b128109]455 ostringstream oss;
456 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]457
[b128109]458 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]459 }
460
461 serverMsg.type = MSG_TYPE_CHAT;
462
463 break;
[e084950]464 }
[b128109]465 case MSG_TYPE_PLAYER_MOVE:
466 {
467 cout << "PLAYER_MOVE" << endl;
468
469 int id, x, y;
470
471 memcpy(&id, clientMsg.buffer, 4);
472 memcpy(&x, clientMsg.buffer+4, 4);
473 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]474
[b128109]475 cout << "x: " << x << endl;
476 cout << "y: " << y << endl;
477 cout << "id: " << id << endl;
[7b43385]478
[b128109]479 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
480 mapPlayers[id].addr.sin_port == from.sin_port )
481 {
[60017fc]482 // we need to make sure the player can move here
483 if (0 <= x && x < 300 && 0 <= y && y < 300 &&
484 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
485 {
[7b43385]486 cout << "valid terrain" << endl;
487
[60017fc]488 mapPlayers[id].target.x = x;
489 mapPlayers[id].target.y = y;
490
491 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
492
493 memcpy(serverMsg.buffer, &id, 4);
[d211210]494 memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
495 memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
[60017fc]496
497 broadcastResponse = true;
498 }
499 else
500 cout << "Bad terrain detected" << endl;
[b128109]501 }
502 else // nned to send back a message indicating failure
503 cout << "Player id (" << id << ") doesn't match sender" << endl;
504
505 break;
506 }
[e487381]507 case MSG_TYPE_DROP_FLAG:
508 {
509 // may want to check the id matches the sender, just like for PLAYER_NOVE
510 cout << "DROP_FLAG" << endl;
511
512 int id;
513
514 memcpy(&id, clientMsg.buffer, 4);
515 cout << "id: " << id << endl;
516
517 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
518 if (mapPlayers[id].hasBlueFlag)
519 flagType = WorldMap::OBJECT_BLUE_FLAG;
520 else if (mapPlayers[id].hasRedFlag)
521 flagType = WorldMap::OBJECT_RED_FLAG;
522
523 gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
524
525 // need to send the OBJECT message too
526 serverMsg.type = MSG_TYPE_OBJECT;
527 gameMap->getObjects()->back().serialize(serverMsg.buffer);
528
529 map<unsigned int, Player>::iterator it;
530 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
531 {
532 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
533 error("sendMessage");
534 }
535
536 mapPlayers[id].hasBlueFlag = false;
537 mapPlayers[id].hasRedFlag = false;
538
539 serverMsg.type = MSG_TYPE_PLAYER;
540 mapPlayers[id].serialize(serverMsg.buffer);
541
542 map<unsigned int, Player>::iterator it2;
543 broadcastResponse = true;
544
545 break;
546 }
[8e540f4]547 default:
548 {
549 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]550
[8e540f4]551 serverMsg.type = MSG_TYPE_CHAT;
[e084950]552
[8e540f4]553 break;
554 }
[e3535b3]555 }
[da692b9]556
557 return broadcastResponse;
[e3535b3]558}
[da692b9]559
[1106210]560void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]561{
[1106210]562 while (mapPlayers.find(id) != mapPlayers.end())
563 id++;
[01d0d00]564}
Note: See TracBrowser for help on using the repository browser.