Changeset 41ad8ed in network-game for server


Ignore:
Timestamp:
Dec 3, 2012, 12:27:01 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
cbc595d
Parents:
87b3ee2
Message:

The server properly handles registration messages

Location:
server
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • server/DataAccess.cpp

    r87b3ee2 r41ad8ed  
    4343   result = select("users", oss.str().c_str());
    4444
     45   cout << "Got result" << endl;
     46
    4547   if (result == NULL) {
     48      cout << "Error occured" << endl;
    4649      cout << mysql_error(connection) << endl;
    4750      return NULL;
     
    5053   if ( ( row = mysql_fetch_row(result)) != NULL )
    5154      p = new Player(string(row[1]), string(row[2]));
    52    else
     55   else {
     56      cout << "Returned no results for some reason" << endl;
    5357      p = NULL;
     58   }
    5459
    5560   mysql_free_result(result);
  • server/Player.cpp

    r87b3ee2 r41ad8ed  
    2727}
    2828
    29 // was meant for the find find function. Currently unused
    30 bool Player::operator == (const Player &p)
    31 {
    32    bool eq = addr.sin_addr.s_addr == p.addr.sin_addr.s_addr;
    33 
    34    return eq;
    35 }
    36 
    3729void Player::setAddr(sockaddr_in addr)
    3830{
  • server/Player.h

    r87b3ee2 r41ad8ed  
    1313   ~Player();
    1414
    15    bool operator == (const Player &p);
    16 
    1715   void setAddr(sockaddr_in addr);
    1816
  • server/server.cpp

    r87b3ee2 r41ad8ed  
    2525#include "../common/message.h"
    2626
    27 /*
    28  Protocol Design
    29 
    30  Client sends a login message
    31  Server replies with client's position in the world and positions of
    32  oall other logged in players
    33  Merver sends player ids along with locations
    34  This means a newly logged in client will need to know which id is
    35  assigned to it
    36  So server needs to send an id message, wait for an ack, and then send
    37  the location messages
    38  When a client shuts down, it sends a message to indicate this so the
    39  server can remove it from the list of connected clients
    40  Eventually, there'll need to be a way to detect timeouts from clients
    41  (if they crashed or otherwise failed to send the logout message)
    42 */
    43 
    4427using namespace std;
    4528
     
    5740   vector<Player>::iterator it;
    5841
     42   cout << "Entered findPlayerByName" << endl;
     43
    5944   for (it = vec.begin(); it != vec.end(); it++)
    6045   {
     46      cout << "Comparing name" << endl;
    6147      if ( it->name.compare(name) == 0 )
    6248         return &(*it);
    6349   }
    6450
     51   cout << "About to return" << endl;
    6552   return NULL;
    6653}
     
    11097      exit(1);
    11198   }
    112 
    113    /*
    114    DataAccess da;
    115 
    116    da.printPlayers();
    117 
    118    da.insertPlayer("playerName3", "playerPass");
    119    cout << endl << "Inserted player" << endl << endl;
    120 
    121    Player* p = da.getPlayer("playerName");
    122    cout << "player name: " << p->name << endl;
    123    delete(p);
    124 
    125    p = da.getPlayer("playerName3");
    126    cout << "player name: " << p->name << endl;
    127    delete(p);
    128 
    129    da.printPlayers();
    130    cout << endl;
    131    */
    132    
     99 
    133100   sock = socket(AF_INET, SOCK_DGRAM, 0);
    134101   if (sock < 0) error("Opening socket");
     
    168135void processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, vector<Player> &vctPlayers, int &num, NETWORK_MSG &serverMsg)
    169136{
     137   DataAccess da;
     138
    170139   cout << "ip address: " << inet_ntoa(from.sin_addr) << endl;
    171140   cout << "port: " << from.sin_port << endl;
     
    185154         cout << "password: " << password << endl;
    186155
    187          strcpy(serverMsg.buffer, "Registration test");
     156         da.insertPlayer(username, password);
     157
     158         strcpy(serverMsg.buffer, "Registration successful");
    188159
    189160         serverMsg.type = MSG_TYPE_REGISTER;
     
    194165      {
    195166         string username(clientMsg.buffer);
     167         string password(strchr(clientMsg.buffer, '\0')+1);
    196168         cout << "Player logging in: " << username << endl;
    197169
    198          Player *p = findPlayerByName(vctPlayers, username);
    199 
    200          if (p == NULL)
     170         Player* p = da.getPlayer(username);
     171
     172         if (p == NULL || p->password != password)
     173         {
     174            if (p != NULL)
     175            {
     176               cout << "p->password: " << p->password << endl;
     177               cout << "password: " << password << endl;
     178            }
     179            strcpy(serverMsg.buffer, "Incorrect username or password");
     180         }
     181         else if(findPlayerByName(vctPlayers, username) != NULL)
     182         {
     183            strcpy(serverMsg.buffer, "Player has already logged in.");
     184         }
     185         else
    201186         {
    202187            Player newP(username, "");
     
    206191            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
    207192         }
    208          else
    209          {
    210             strcpy(serverMsg.buffer, "Player has already logged in.");
    211          }
    212193
    213194         serverMsg.type = MSG_TYPE_LOGIN;
     195   
     196         delete(p);
    214197
    215198         break;
     
    234217         {
    235218            vctPlayers.erase((vector<Player>::iterator)p);
    236             strcpy(serverMsg.buffer, "You have successfully logged out. You may quit the game.");
     219            strcpy(serverMsg.buffer, "You have successfully logged out.");
    237220         }
    238221
Note: See TracChangeset for help on using the changeset viewer.