Changeset b128109 in network-game


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

Location:
server
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • server/DataAccess.cpp

    r5806dc2 rb128109  
    33#include <iostream>
    44#include <sstream>
     5#include <cstdlib>
    56
    67using namespace std;
     
    2728   ostringstream oss;
    2829
    29    oss << "'" << username << "', '" << password << "'";
     30   string salt = "$1$";
     31   int random;
     32   char chr;
     33   for(int i=0; i<8; i++)
     34   {
     35      random = rand() % 62;
     36      if (random < 26)
     37         chr = (char)('a'+random);
     38      else if (random < 52)
     39         chr = (char)('A'+random-26);
     40      else
     41         chr = (char)('0'+random-52);
     42      salt += chr;
     43   }
     44   salt += '$';
     45
     46   string encrypted(crypt(password.c_str(), salt.c_str()));
     47
     48   oss << "'" << username << "', '" << encrypted << "'";
    3049
    3150   return insert("users", "name, password", oss.str());
     51}
     52
     53int DataAccess::updatePlayer(string username, string password)
     54{
     55   ostringstream values, where;
     56
     57   values << "password='" << password << "'";
     58   
     59   where << "name='" << username << "'";
     60
     61   return update("users", values.str(), where.str());
    3262}
    3363
     
    6393}
    6494
    65 int DataAccess::printPlayers()
     95// need to make sure this list is freed
     96// since we need to create a DataAccess class
     97// when calling these functions,
     98// we could free this list in the destructor
     99list<Player*>* DataAccess::getPlayers()
    66100{
    67101   MYSQL_RES *result;
     
    73107   if (result == NULL) {
    74108      cout << mysql_error(connection) << endl;
    75       return 1;
     109      return NULL;
    76110   }
    77111
     112   list<Player*>* lstPlayers = new list<Player*>();
    78113   while ( ( row = mysql_fetch_row(result)) != NULL ) {
    79114      cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
     115      lstPlayers->push_back(new Player(row[1], row[2]));
    80116   }
    81117
    82118   mysql_free_result(result);
    83119
    84    return 0;
     120   return lstPlayers;
    85121}
    86122
    87 int DataAccess::insert(string table, string rows, string values)
     123bool DataAccess::verifyPassword(string password, string encrypted)
     124{
     125   string test(crypt(password.c_str(), encrypted.c_str()));
     126
     127   return encrypted.compare(test) == 0;
     128}
     129
     130int DataAccess::insert(string table, string columns, string values)
    88131{
    89132   int query_state;
    90133   ostringstream oss;
    91134
    92    oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")";
     135   oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
     136   cout << "query: " << oss.str() << endl;
     137
     138   query_state = mysql_query(connection, oss.str().c_str());
     139
     140   if (query_state != 0) {
     141      cout << mysql_error(connection) << endl;
     142      return 1;
     143   }
     144
     145   return 0;
     146}
     147
     148int DataAccess::update(string table, string values, string where)
     149{
     150   int query_state;
     151   ostringstream oss;
     152
     153   oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
    93154   cout << "query: " << oss.str() << endl;
    94155
  • server/DataAccess.h

    r5806dc2 rb128109  
    33
    44#include <string>
     5#include <list>
    56
    67#include <mysql/mysql.h>
     
    1617
    1718   int insertPlayer(string username, string password);
     19   int updatePlayer(string username, string password);
    1820
    19    Player *getPlayer(string username);
    20    int printPlayers();
     21   Player* getPlayer(string username);
     22   list<Player*>* getPlayers();
     23   bool verifyPassword(string encrypted, string password);
    2124
    2225   int insert(string table, string rows, string values);
     26   int update(string table, string values, string where);
    2327   MYSQL_RES *select(string table, string filter);
    2428
  • server/makefile

    r5806dc2 rb128109  
    11CC = g++
    2 LIB_FLAGS = -lssl -lmysqlclient
     2LIB_FLAGS = -lssl -lmysqlclient -lcrypt
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
  • 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.