source: network-game/server/server.cpp@ 227baaa

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

Smooth player movement now works, albeit poorly.

  • Property mode set to 100644
File size: 10.8 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
[73f75c1]13#include <sys/socket.h>
[371ce29]14#include <netdb.h>
[73f75c1]15#include <netinet/in.h>
16#include <arpa/inet.h>
17
[b128109]18#include <crypt.h>
19
[edfd1d0]20/*
[e3535b3]21#include <openssl/bio.h>
22#include <openssl/ssl.h>
23#include <openssl/err.h>
[edfd1d0]24*/
[e3535b3]25
[b53c6b3]26#include "../common/Compiler.h"
[3b1efcc]27#include "../common/Common.h"
[edfd1d0]28#include "../common/Message.h"
[60017fc]29#include "../common/WorldMap.h"
[edfd1d0]30#include "../common/Player.h"
[b53c6b3]31
[36082e8]32#include "DataAccess.h"
[d2b411a]33
[e3535b3]34using namespace std;
35
[60017fc]36bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
[01d0d00]37
[1106210]38void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
[8e540f4]39
[73f75c1]40// this should probably go somewhere in the common folder
[e3535b3]41void error(const char *msg)
42{
43 perror(msg);
44 exit(0);
45}
46
[01d0d00]47Player *findPlayerByName(map<unsigned int, Player> &m, string name)
[2488852]48{
[01d0d00]49 map<unsigned int, Player>::iterator it;
[2488852]50
[01d0d00]51 for (it = m.begin(); it != m.end(); it++)
[2488852]52 {
[01d0d00]53 if ( it->second.name.compare(name) == 0 )
54 return &(it->second);
[2488852]55 }
56
57 return NULL;
58}
59
[01d0d00]60Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
[73f75c1]61{
[01d0d00]62 map<unsigned int, Player>::iterator it;
[73f75c1]63
[01d0d00]64 for (it = m.begin(); it != m.end(); it++)
[73f75c1]65 {
[01d0d00]66 if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
67 it->second.addr.sin_port == addr.sin_port )
68 return &(it->second);
[73f75c1]69 }
70
71 return NULL;
72}
73
[01d0d00]74void broadcastPlayerPositions(map<unsigned int, Player> &m, int sock)
[edfd1d0]75{
[01d0d00]76 map<unsigned int, Player>::iterator it, it2;
[edfd1d0]77 NETWORK_MSG serverMsg;
78
79 serverMsg.type = MSG_TYPE_PLAYER;
80
[01d0d00]81 for (it = m.begin(); it != m.end(); it++)
[edfd1d0]82 {
[01d0d00]83 it->second.serialize(serverMsg.buffer);
[edfd1d0]84
[01d0d00]85 for (it2 = m.begin(); it2 != m.end(); it2++)
[edfd1d0]86 {
[01d0d00]87 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
[edfd1d0]88 error("sendMessage");
89 }
90 }
91}
92
[e3535b3]93int main(int argc, char *argv[])
94{
95 int sock, length, n;
96 struct sockaddr_in server;
[3b1efcc]97 struct sockaddr_in from; // info of client sending the message
[e084950]98 NETWORK_MSG clientMsg, serverMsg;
[01d0d00]99 map<unsigned int, Player> mapPlayers;
[1106210]100 unsigned int unusedId = 1;
[e084950]101
[edfd1d0]102 //SSL_load_error_strings();
103 //ERR_load_BIO_strings();
104 //OpenSSL_add_all_algorithms();
[e3535b3]105
106 if (argc < 2) {
[73f75c1]107 cerr << "ERROR, no port provided" << endl;
108 exit(1);
[e3535b3]109 }
[60017fc]110
[7b43385]111 WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
[41ad8ed]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;
[cb1f288]126 while (true) {
[371ce29]127
128 usleep(5000);
129
[e084950]130 n = receiveMessage(&clientMsg, sock, &from);
[8e540f4]131
[371ce29]132 if (n >= 0) {
133 cout << "Got a message" << endl;
[8e540f4]134
[60017fc]135 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
[371ce29]136
[b128109]137 // probably replace this with a function that prints based on the
138 // message type
[371ce29]139 cout << "msg: " << serverMsg.buffer << endl;
[b128109]140 cout << "broadcastResponse: " << broadcastResponse << endl;
[da692b9]141 if (broadcastResponse)
[3b1efcc]142 {
[da692b9]143 cout << "Should be broadcasting the message" << endl;
144
[01d0d00]145 map<unsigned int, Player>::iterator it;
146 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]147 {
[01d0d00]148 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
[3b1efcc]149 error("sendMessage");
150 }
151 }
152 else
153 {
[da692b9]154 cout << "Should be sending back the message" << endl;
155
[3b1efcc]156 if ( sendMessage(&serverMsg, sock, &from) < 0 )
157 error("sendMessage");
158 }
[7b43385]159 }
[8e540f4]160
[7b43385]161 // update player positions
162 map<unsigned int, Player>::iterator it;
163 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
164 {
165 it->second.move();
[edfd1d0]166 }
[7b43385]167
168 broadcastPlayerPositions(mapPlayers, sock);
[8e540f4]169 }
[371ce29]170
[8e540f4]171 return 0;
172}
173
[60017fc]174bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
[8e540f4]175{
[41ad8ed]176 DataAccess da;
177
[8e540f4]178 cout << "MSG: type: " << clientMsg.type << endl;
179 cout << "MSG contents: " << clientMsg.buffer << endl;
180
[da692b9]181 // maybe we should make a message class and have this be a member
[3b1efcc]182 bool broadcastResponse = false;
183
[8e540f4]184 // Check that if an invalid message is sent, the client will correctly
185 // receive and display the response. Maybe make a special error msg type
186 switch(clientMsg.type)
187 {
188 case MSG_TYPE_REGISTER:
[d2b411a]189 {
[8e540f4]190 string username(clientMsg.buffer);
191 string password(strchr(clientMsg.buffer, '\0')+1);
[d2b411a]192
[8e540f4]193 cout << "username: " << username << endl;
194 cout << "password: " << password << endl;
[d2b411a]195
[3b1efcc]196 int error = da.insertPlayer(username, password);
[41ad8ed]197
[3b1efcc]198 if (!error)
199 strcpy(serverMsg.buffer, "Registration successful.");
200 else
201 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[8e540f4]202
203 serverMsg.type = MSG_TYPE_REGISTER;
[d2b411a]204
[8e540f4]205 break;
206 }
207 case MSG_TYPE_LOGIN:
208 {
[60017fc]209 cout << "Got login message" << endl;
210
[8e540f4]211 string username(clientMsg.buffer);
[41ad8ed]212 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]213
[41ad8ed]214 Player* p = da.getPlayer(username);
[d2b411a]215
[b128109]216 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]217 {
218 strcpy(serverMsg.buffer, "Incorrect username or password");
219 }
[01d0d00]220 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]221 {
222 strcpy(serverMsg.buffer, "Player has already logged in.");
223 }
224 else
[8e540f4]225 {
[01d0d00]226 p->setAddr(from);
[1106210]227 updateUnusedId(unusedId, mapPlayers);
[01d0d00]228 p->id = unusedId;
229 mapPlayers[unusedId] = *p;
[59061f6]230
[594d2e9]231 // sendd back the new player info to the user
232 p->serialize(serverMsg.buffer);
[07028b9]233 }
234
[8e540f4]235 serverMsg.type = MSG_TYPE_LOGIN;
[41ad8ed]236
237 delete(p);
[07028b9]238
[8e540f4]239 break;
240 }
241 case MSG_TYPE_LOGOUT:
242 {
243 string name(clientMsg.buffer);
244 cout << "Player logging out: " << name << endl;
245
[01d0d00]246 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]247
[8e540f4]248 if (p == NULL)
249 {
250 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]251 cout << "Player not logged in" << endl;
[07028b9]252 }
[01d0d00]253 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
254 p->addr.sin_port != from.sin_port )
[07028b9]255 {
[8e540f4]256 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]257 cout << "Player logged in using a different connection" << endl;
[2488852]258 }
[8e540f4]259 else
[2488852]260 {
[01d0d00]261 if (p->id < unusedId)
262 unusedId = p->id;
263 mapPlayers.erase(p->id);
[41ad8ed]264 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8a3ef42]265 cout << "Player logged out successfuly" << endl;
[8e540f4]266 }
[07028b9]267
[8a3ef42]268 // should really be serverMsg.type = MSG_TYPE_LOGOUT;
269 serverMsg.type = MSG_TYPE_LOGIN;
270
[8e540f4]271 break;
272 }
273 case MSG_TYPE_CHAT:
274 {
[da692b9]275 cout << "Got a chat message" << endl;
276
[01d0d00]277 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]278
[8e540f4]279 if (p == NULL)
280 {
281 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]282 }
[8e540f4]283 else
284 {
[3b1efcc]285 broadcastResponse = true;
286
[b128109]287 ostringstream oss;
288 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]289
[b128109]290 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]291 }
292
293 serverMsg.type = MSG_TYPE_CHAT;
294
295 break;
[e084950]296 }
[b128109]297 case MSG_TYPE_PLAYER_MOVE:
298 {
299 istringstream iss;
300 iss.str(clientMsg.buffer);
301
302 cout << "PLAYER_MOVE" << endl;
303
304 int id, x, y;
305
306 memcpy(&id, clientMsg.buffer, 4);
307 memcpy(&x, clientMsg.buffer+4, 4);
308 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]309
[b128109]310 cout << "x: " << x << endl;
311 cout << "y: " << y << endl;
312 cout << "id: " << id << endl;
[7b43385]313
[b128109]314 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
315 mapPlayers[id].addr.sin_port == from.sin_port )
316 {
[60017fc]317 // we need to make sure the player can move here
318 if (0 <= x && x < 300 && 0 <= y && y < 300 &&
319 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
320 {
[7b43385]321 cout << "valid terrain" << endl;
322
323 cout << "orig x: " << mapPlayers[id].pos.x << endl;
324 cout << "orig y: " << mapPlayers[id].pos.y << endl;
325 // first we get the correct vector
[60017fc]326 mapPlayers[id].target.x = x;
327 mapPlayers[id].target.y = y;
328 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
329 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
[7b43385]330 cout << "xDiff: " << xDiff << endl;
331 cout << "yDiff: " << yDiff << endl;
[60017fc]332
333 // then we get the correct angle
334 double angle = atan2(yDiff, xDiff);
[7b43385]335 cout << "angle: " << angle << endl;
[60017fc]336
337 // finally we use the angle to determine
338 // how much the player moves
339 // the player will move 50 pixels in the correct direction
[7b43385]340 //mapPlayers[id].pos.x += cos(angle)*50;
341 //mapPlayers[id].pos.y += sin(angle)*50;
[60017fc]342
343 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
344
345 memcpy(serverMsg.buffer, &id, 4);
346 memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
347 memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
348 //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
349
350 broadcastResponse = true;
351 }
352 else
353 cout << "Bad terrain detected" << endl;
[b128109]354 }
355 else // nned to send back a message indicating failure
356 cout << "Player id (" << id << ") doesn't match sender" << endl;
357
358 break;
359 }
[8e540f4]360 default:
361 {
362 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]363
[8e540f4]364 serverMsg.type = MSG_TYPE_CHAT;
[e084950]365
[8e540f4]366 break;
367 }
[e3535b3]368 }
[da692b9]369
[60017fc]370 cout << "Got to the end of the switch" << endl;
371
[da692b9]372 return broadcastResponse;
[e3535b3]373}
[da692b9]374
[1106210]375void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]376{
[1106210]377 while (mapPlayers.find(id) != mapPlayers.end())
378 id++;
[01d0d00]379}
Note: See TracBrowser for help on using the repository browser.