source: network-game/server/server.cpp@ 093c141

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

Added code for player movement

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