source: network-game/server/server.cpp@ 626e5b0

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

Players pick up flags when they get close to the flag objects, not the structres. When a flag is picked up, a REMOVE_OBJECT message is sent

  • Property mode set to 100644
File size: 14.9 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:
153 it->second.pos = oldPos;
154 it->second.target.x = it->second.pos.x;
155 it->second.target.y = it->second.pos.y;
156 broadcastMove = true;
157 break;
158 default:
159 // if there are no obstacles, do nothing
160 break;
161 }
162
[b07eeac]163 vector<WorldMap::Object> vctObjects = gameMap->getObjects();
164 vector<WorldMap::Object>::iterator itObjects;
165
166 for (itObjects = vctObjects.begin(); itObjects != vctObjects.end(); itObjects++) {
167 POSITION pos = itObjects->pos;
168 if (posDistance(it->second.pos, pos.toFloat()) < 10) {
169 switch (itObjects->type) {
170 case WorldMap::OBJECT_BLUE_FLAG:
171 cout << "Got blue flag" << endl;
172 it->second.hasBlueFlag = true;
173 broadcastMove = true;
174 break;
175 case WorldMap::OBJECT_RED_FLAG:
176 cout << "Got red flag" << endl;
177 it->second.hasRedFlag = true;
178 broadcastMove = true;
179 break;
180 }
181
182 // send a MSG_TYPE_REMOVE_OBJECT message
183 serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
184 memcpy(serverMsg.buffer, &itObjects->id, 4);
185
186 map<unsigned int, Player>::iterator it2;
187 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
188 {
189 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
190 error("sendMessage");
191 }
192 }
[23559e7]193 }
194
195 if (broadcastMove) {
196 serverMsg.type = MSG_TYPE_PLAYER;
197 it->second.serialize(serverMsg.buffer);
198
199 cout << "about to broadcast move" << endl;
[b07eeac]200 map<unsigned int, Player>::iterator it2;
[23559e7]201 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
202 {
203 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
204 error("sendMessage");
205 }
[d211210]206 }
207 }
208 }
209 }
210
[e084950]211 n = receiveMessage(&clientMsg, sock, &from);
[8e540f4]212
[371ce29]213 if (n >= 0) {
214 cout << "Got a message" << endl;
[8e540f4]215
[d211210]216 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
[371ce29]217
[b128109]218 // probably replace this with a function that prints based on the
219 // message type
[371ce29]220 cout << "msg: " << serverMsg.buffer << endl;
[b128109]221 cout << "broadcastResponse: " << broadcastResponse << endl;
[da692b9]222 if (broadcastResponse)
[3b1efcc]223 {
[da692b9]224 cout << "Should be broadcasting the message" << endl;
225
[01d0d00]226 map<unsigned int, Player>::iterator it;
227 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]228 {
[d211210]229 cout << "Sent message back to " << it->second.name << endl;
[01d0d00]230 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
[3b1efcc]231 error("sendMessage");
232 }
233 }
234 else
235 {
[da692b9]236 cout << "Should be sending back the message" << endl;
237
[3b1efcc]238 if ( sendMessage(&serverMsg, sock, &from) < 0 )
239 error("sendMessage");
240 }
[7b43385]241 }
[8e540f4]242 }
[371ce29]243
[8e540f4]244 return 0;
245}
246
[d211210]247bool 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]248{
[41ad8ed]249 DataAccess da;
250
[8e540f4]251 cout << "MSG: type: " << clientMsg.type << endl;
252 cout << "MSG contents: " << clientMsg.buffer << endl;
253
[da692b9]254 // maybe we should make a message class and have this be a member
[3b1efcc]255 bool broadcastResponse = false;
256
[8e540f4]257 // Check that if an invalid message is sent, the client will correctly
258 // receive and display the response. Maybe make a special error msg type
259 switch(clientMsg.type)
260 {
261 case MSG_TYPE_REGISTER:
[d2b411a]262 {
[8e540f4]263 string username(clientMsg.buffer);
264 string password(strchr(clientMsg.buffer, '\0')+1);
[d2b411a]265
[8e540f4]266 cout << "username: " << username << endl;
267 cout << "password: " << password << endl;
[d2b411a]268
[3b1efcc]269 int error = da.insertPlayer(username, password);
[41ad8ed]270
[3b1efcc]271 if (!error)
272 strcpy(serverMsg.buffer, "Registration successful.");
273 else
274 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[8e540f4]275
276 serverMsg.type = MSG_TYPE_REGISTER;
[d2b411a]277
[8e540f4]278 break;
279 }
280 case MSG_TYPE_LOGIN:
281 {
[60017fc]282 cout << "Got login message" << endl;
283
[d211210]284 serverMsg.type = MSG_TYPE_LOGIN;
285
[8e540f4]286 string username(clientMsg.buffer);
[41ad8ed]287 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]288
[41ad8ed]289 Player* p = da.getPlayer(username);
[d2b411a]290
[b128109]291 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]292 {
293 strcpy(serverMsg.buffer, "Incorrect username or password");
294 }
[01d0d00]295 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]296 {
297 strcpy(serverMsg.buffer, "Player has already logged in.");
298 }
299 else
[8e540f4]300 {
[d211210]301 serverMsg.type = MSG_TYPE_PLAYER;
302
[01d0d00]303 p->setAddr(from);
[1106210]304 updateUnusedId(unusedId, mapPlayers);
[01d0d00]305 p->id = unusedId;
[d211210]306 cout << "new player id: " << p->id << endl;
307
308 // tell the new player about all the existing players
309 cout << "Sending other players to new player" << endl;
310
311 map<unsigned int, Player>::iterator it;
312 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
313 {
314 it->second.serialize(serverMsg.buffer);
315
316 cout << "sending info about " << it->second.name << endl;
[5f868c0]317 cout << "sending id " << it->second.id << endl;
318 if ( sendMessage(&serverMsg, sock, &from) < 0 )
319 error("sendMessage");
320 }
321
322 // tell the new player about all map objects
323 // (currently just the flags)
324 serverMsg.type = MSG_TYPE_OBJECT;
325 vector<WorldMap::Object> vctObjects = gameMap->getObjects();
326 vector<WorldMap::Object>::iterator itObjects;
327 cout << "sending items" << endl;
328 for (itObjects = vctObjects.begin(); itObjects != vctObjects.end(); itObjects++) {
329 itObjects->serialize(serverMsg.buffer);
330 cout << "sending item id " << itObjects->id << endl;
[d211210]331 if ( sendMessage(&serverMsg, sock, &from) < 0 )
332 error("sendMessage");
333 }
[59061f6]334
[594d2e9]335 p->serialize(serverMsg.buffer);
[d211210]336 cout << "Should be broadcasting the message" << endl;
337
338 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
339 {
340 cout << "Sent message back to " << it->second.name << endl;
341 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
342 error("sendMessage");
343 }
344
345 serverMsg.type = MSG_TYPE_LOGIN;
346 mapPlayers[unusedId] = *p;
[07028b9]347 }
348
[41ad8ed]349 delete(p);
[07028b9]350
[8e540f4]351 break;
352 }
353 case MSG_TYPE_LOGOUT:
354 {
355 string name(clientMsg.buffer);
356 cout << "Player logging out: " << name << endl;
357
[01d0d00]358 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]359
[8e540f4]360 if (p == NULL)
361 {
362 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]363 cout << "Player not logged in" << endl;
[07028b9]364 }
[01d0d00]365 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
366 p->addr.sin_port != from.sin_port )
[07028b9]367 {
[8e540f4]368 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]369 cout << "Player logged in using a different connection" << endl;
[2488852]370 }
[8e540f4]371 else
[2488852]372 {
[01d0d00]373 if (p->id < unusedId)
374 unusedId = p->id;
375 mapPlayers.erase(p->id);
[41ad8ed]376 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8a3ef42]377 cout << "Player logged out successfuly" << endl;
[8e540f4]378 }
[07028b9]379
[d211210]380 serverMsg.type = MSG_TYPE_LOGOUT;
[8a3ef42]381
[8e540f4]382 break;
383 }
384 case MSG_TYPE_CHAT:
385 {
[da692b9]386 cout << "Got a chat message" << endl;
387
[01d0d00]388 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]389
[8e540f4]390 if (p == NULL)
391 {
392 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]393 }
[8e540f4]394 else
395 {
[3b1efcc]396 broadcastResponse = true;
397
[b128109]398 ostringstream oss;
399 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]400
[b128109]401 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]402 }
403
404 serverMsg.type = MSG_TYPE_CHAT;
405
406 break;
[e084950]407 }
[b128109]408 case MSG_TYPE_PLAYER_MOVE:
409 {
410 istringstream iss;
411 iss.str(clientMsg.buffer);
412
413 cout << "PLAYER_MOVE" << endl;
414
415 int id, x, y;
416
417 memcpy(&id, clientMsg.buffer, 4);
418 memcpy(&x, clientMsg.buffer+4, 4);
419 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]420
[b128109]421 cout << "x: " << x << endl;
422 cout << "y: " << y << endl;
423 cout << "id: " << id << endl;
[7b43385]424
[b128109]425 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
426 mapPlayers[id].addr.sin_port == from.sin_port )
427 {
[60017fc]428 // we need to make sure the player can move here
429 if (0 <= x && x < 300 && 0 <= y && y < 300 &&
430 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
431 {
[7b43385]432 cout << "valid terrain" << endl;
433
[60017fc]434 mapPlayers[id].target.x = x;
435 mapPlayers[id].target.y = y;
436
437 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
438
439 memcpy(serverMsg.buffer, &id, 4);
[d211210]440 memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
441 memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
[60017fc]442
443 broadcastResponse = true;
444 }
445 else
446 cout << "Bad terrain detected" << endl;
[b128109]447 }
448 else // nned to send back a message indicating failure
449 cout << "Player id (" << id << ") doesn't match sender" << endl;
450
451 break;
452 }
[8e540f4]453 default:
454 {
455 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]456
[8e540f4]457 serverMsg.type = MSG_TYPE_CHAT;
[e084950]458
[8e540f4]459 break;
460 }
[e3535b3]461 }
[da692b9]462
[60017fc]463 cout << "Got to the end of the switch" << endl;
464
[da692b9]465 return broadcastResponse;
[e3535b3]466}
[da692b9]467
[1106210]468void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]469{
[1106210]470 while (mapPlayers.find(id) != mapPlayers.end())
471 id++;
[01d0d00]472}
Note: See TracBrowser for help on using the repository browser.