Changeset b128109 in network-game for server/server.cpp


Ignore:
Timestamp:
Jan 29, 2013, 7:34:29 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
60b77d2
Parents:
5806dc2
Message:

Added MSG_TYPE_PLAYER_MOVE, which allows players to tell the server where they want to move

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r5806dc2 rb128109  
    1414#include <netinet/in.h>
    1515#include <arpa/inet.h>
     16
     17#include <crypt.h>
    1618
    1719/*
     
    129131         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
    130132
     133         // probably replace this with a function that prints based on the
     134         // message type
    131135         cout << "msg: " << serverMsg.buffer << endl;
    132 
     136         cout << "broadcastResponse: " << broadcastResponse << endl;
    133137         if (broadcastResponse)
    134138         {
     
    197201         string username(clientMsg.buffer);
    198202         string password(strchr(clientMsg.buffer, '\0')+1);
    199          cout << "Player logging in: " << username << endl;
    200203
    201204         Player* p = da.getPlayer(username);
    202205
    203          if (p == NULL || p->password != password)
     206         if (p == NULL || !da.verifyPassword(password, p->password))
    204207         {
    205208            strcpy(serverMsg.buffer, "Incorrect username or password");
     
    272275            broadcastResponse = true;
    273276
    274             stringstream ss;
    275             ss << p->name << ": " << clientMsg.buffer;
    276 
    277             strcpy(serverMsg.buffer, ss.str().c_str());
     277            ostringstream oss;
     278            oss << p->name << ": " << clientMsg.buffer;
     279
     280            strcpy(serverMsg.buffer, oss.str().c_str());
    278281         }     
    279282
    280283         serverMsg.type = MSG_TYPE_CHAT;
     284
     285         break;
     286      }
     287      case MSG_TYPE_PLAYER_MOVE:
     288      {
     289         istringstream iss;
     290         iss.str(clientMsg.buffer);
     291
     292         cout << "PLAYER_MOVE" << endl;
     293
     294         int id, x, y;
     295
     296         memcpy(&id, clientMsg.buffer, 4);
     297         memcpy(&x, clientMsg.buffer+4, 4);
     298         memcpy(&y, clientMsg.buffer+8, 4);
     299         
     300         cout << "x: " << x << endl;
     301         cout << "y: " << y << endl;
     302         cout << "id: " << id << endl;
     303
     304         if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
     305              mapPlayers[id].addr.sin_port == from.sin_port )
     306         {
     307            memcpy(&mapPlayers[id].pos.x, clientMsg.buffer+4, 4);
     308            memcpy(&mapPlayers[id].pos.y, clientMsg.buffer+8, 4);
     309
     310            serverMsg.type = MSG_TYPE_PLAYER_MOVE;
     311            memcpy(serverMsg.buffer, clientMsg.buffer, 12);
     312
     313            broadcastResponse = true;
     314         }
     315         else  // nned to send back a message indicating failure
     316            cout << "Player id (" << id << ") doesn't match sender" << endl;
    281317
    282318         break;
Note: See TracChangeset for help on using the changeset viewer.