Changes in server/server.cpp [8f85180:01d0d00] in network-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r8f85180 r01d0d00  
    66#include <sstream>
    77#include <cstring>
    8 #include <cmath>
    98
    109#include <vector>
     
    1514#include <netinet/in.h>
    1615#include <arpa/inet.h>
    17 
    18 #include <crypt.h>
    1916
    2017/*
     
    2724#include "../common/Common.h"
    2825#include "../common/Message.h"
    29 #include "../common/WorldMap.h"
    3026#include "../common/Player.h"
    3127
     
    3430using namespace std;
    3531
    36 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
    37 
    38 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     32bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     33
     34void updateUnusedId(unsigned int& id);
    3935
    4036// this should probably go somewhere in the common folder
     
    9894   NETWORK_MSG clientMsg, serverMsg;
    9995   map<unsigned int, Player> mapPlayers;
    100    unsigned int unusedId = 1;
     96   unsigned int unusedId = 0;
    10197
    10298   //SSL_load_error_strings();
     
    108104      exit(1);
    109105   }
    110 
    111    WorldMap* gameMap = NULL; //WorldMap::createDefaultMap();
    112106 
    113107   sock = socket(AF_INET, SOCK_DGRAM, 0);
     
    133127         cout << "Got a message" << endl;
    134128
    135          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
    136 
    137          // probably replace this with a function that prints based on the
    138          // message type
     129         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
     130
    139131         cout << "msg: " << serverMsg.buffer << endl;
    140          cout << "broadcastResponse: " << broadcastResponse << endl;
     132
    141133         if (broadcastResponse)
    142134         {
     
    144136
    145137            map<unsigned int, Player>::iterator it;
     138
    146139            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    147140            {
     
    158151         }
    159152
    160          // update player positions
    161          map<unsigned int, Player>::iterator it;
    162          for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    163          {
    164             it->second.move();
    165          }
    166 
    167153         broadcastPlayerPositions(mapPlayers, sock);
    168154      }
     
    172158}
    173159
    174 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
     160bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
    175161{
    176162   DataAccess da;
     
    209195      case MSG_TYPE_LOGIN:
    210196      {
    211          cout << "Got login message" << endl;
    212 
    213197         string username(clientMsg.buffer);
    214198         string password(strchr(clientMsg.buffer, '\0')+1);
     199         cout << "Player logging in: " << username << endl;
    215200
    216201         Player* p = da.getPlayer(username);
    217202
    218          if (p == NULL || !da.verifyPassword(password, p->password))
     203         if (p == NULL || p->password != password)
    219204         {
    220205            strcpy(serverMsg.buffer, "Incorrect username or password");
     
    227212         {
    228213            p->setAddr(from);
    229             updateUnusedId(unusedId, mapPlayers);
    230214            p->id = unusedId;
    231215            mapPlayers[unusedId] = *p;
    232 
    233             // sendd back the new player info to the user
    234             p->serialize(serverMsg.buffer);
     216            updateUnusedId(unusedId);
     217
     218            strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");
    235219         }
    236220
     
    251235         {
    252236            strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
    253             cout << "Player not logged in" << endl;
    254237         }
    255238         else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
     
    257240         {
    258241            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.");
    259             cout << "Player logged in using a different connection" << endl;
    260242         }
    261243         else
     
    265247            mapPlayers.erase(p->id);
    266248            strcpy(serverMsg.buffer, "You have successfully logged out.");
    267             cout << "Player logged out successfuly" << endl;
    268          }
    269 
    270          // should really be serverMsg.type = MSG_TYPE_LOGOUT;
    271          serverMsg.type = MSG_TYPE_LOGIN;
     249         }
    272250
    273251         break;
     
    287265            broadcastResponse = true;
    288266
    289             ostringstream oss;
    290             oss << p->name << ": " << clientMsg.buffer;
    291 
    292             strcpy(serverMsg.buffer, oss.str().c_str());
     267            stringstream ss;
     268            ss << p->name << ": " << clientMsg.buffer;
     269
     270            strcpy(serverMsg.buffer, ss.str().c_str());
    293271         }     
    294272
     
    297275         break;
    298276      }
    299       case MSG_TYPE_PLAYER_MOVE:
    300       {
    301          cout << "Got a move message" << endl;
    302 
    303          istringstream iss;
    304          iss.str(clientMsg.buffer);
    305 
    306          cout << "PLAYER_MOVE" << endl;
    307 
    308          int id, x, y;
    309 
    310          memcpy(&id, clientMsg.buffer, 4);
    311          memcpy(&x, clientMsg.buffer+4, 4);
    312          memcpy(&y, clientMsg.buffer+8, 4);
    313          
    314          cout << "x: " << x << endl;
    315          cout << "y: " << y << endl;
    316          cout << "id: " << id << endl;
    317 
    318          if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
    319               mapPlayers[id].addr.sin_port == from.sin_port )
    320          {
    321             // we need to make sure the player can move here
    322             if (0 <= x && x < 300 && 0 <= y && y < 300 &&
    323                gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
    324             {
    325                // first we get the correct vector
    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;
    330                cout << "xDiff: " << xDiff << endl;               
    331                cout << "yDiff: " << yDiff << endl;               
    332 
    333                // then we get the correct angle
    334                double angle = atan2(yDiff, xDiff);
    335                cout << "angle: " << angle << endl;               
    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
    340                mapPlayers[id].pos.x += cos(angle)*50;
    341                mapPlayers[id].pos.y += sin(angle)*50;
    342                cout << "new x: " << mapPlayers[id].pos.x << endl;               
    343                cout << "new y: " << mapPlayers[id].pos.y << endl;               
    344 
    345                serverMsg.type = MSG_TYPE_PLAYER_MOVE;
    346                
    347                memcpy(serverMsg.buffer, &id, 4);
    348                memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
    349                memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
    350                //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
    351 
    352                broadcastResponse = true;
    353             }
    354             else
    355                cout << "Bad terrain detected" << endl;
    356          }
    357          else  // nned to send back a message indicating failure
    358             cout << "Player id (" << id << ") doesn't match sender" << endl;
    359 
    360          break;
    361       }
    362277      default:
    363278      {
     
    370285   }
    371286
    372    cout << "Got to the end of the switch" << endl;
    373 
    374287   return broadcastResponse;
    375288}
    376289
    377 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
    378 {
    379    while (mapPlayers.find(id) != mapPlayers.end())
    380       id++;
    381 }
     290void updateUnusedId(unsigned int& id)
     291{
     292   id = 5;
     293}
Note: See TracChangeset for help on using the changeset viewer.