Changeset 59061f6 in network-game


Ignore:
Timestamp:
Nov 27, 2012, 7:03:33 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
371ce29
Parents:
36082e8
Message:

Added new data access methods to save and retrieve player data

Location:
server
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • server/DataAccess.cpp

    r36082e8 r59061f6  
    22
    33#include <iostream>
    4 
    5 #include <mysql/mysql.h>
     4#include <sstream>
    65
    76using namespace std;
     
    98DataAccess::DataAccess()
    109{
     10   mysql_init(&mysql);
     11   connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
     12
     13   if (connection == NULL) {
     14      cout << mysql_error(&mysql) << endl;
     15   }else
     16      cout << "Connection successful" << endl;
    1117}
    1218
    1319DataAccess::~DataAccess()
    1420{
     21   mysql_close(connection);
     22   mysql_close(&mysql);
    1523}
    1624
    17 int DataAccess::dbtest()
     25int DataAccess::insertPlayer(string username, string password)
    1826{
    19    MYSQL *connection, mysql;
     27   ostringstream oss;
     28
     29   oss << "'" << username << "', '" << password << "'";
     30
     31   return insert("users", "name, password", oss.str());
     32}
     33
     34Player *DataAccess::getPlayer(string username)
     35{
    2036   MYSQL_RES *result;
    2137   MYSQL_ROW row;
    22    int query_state;
     38   Player *p;
     39   ostringstream oss;
    2340
    24    mysql_init(&mysql);
     41   oss << "name='" << username << "'";
    2542
    26    connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);
     43   result = select("users", oss.str().c_str());
    2744
    28    if (connection == NULL) {
    29       cout << mysql_error(&mysql) << endl;
    30       return 1;
    31    }else
    32       cout << "Connection successful" << endl;
     45   if (result == NULL) {
     46      cout << mysql_error(connection) << endl;
     47      return NULL;
     48   }
    3349
    34    query_state = mysql_query(connection, "SELECT * FROM users");
     50   if ( ( row = mysql_fetch_row(result)) != NULL )
     51      p = new Player(string(row[1]), string(row[2]));
     52   else
     53      p = NULL;
    3554
    36    if (query_state !=0) {
     55   mysql_free_result(result);
     56
     57   return p;
     58}
     59
     60int DataAccess::printPlayers()
     61{
     62   MYSQL_RES *result;
     63   MYSQL_ROW row;
     64   ostringstream oss;
     65
     66   result = select("users", "");
     67
     68   if (result == NULL) {
    3769      cout << mysql_error(connection) << endl;
    3870      return 1;
    3971   }
    40 
    41    result = mysql_store_result(connection);
    4272
    4373   while ( ( row = mysql_fetch_row(result)) != NULL ) {
     
    4676
    4777   mysql_free_result(result);
    48    mysql_close(connection);
    49 
    50    cout << "Test finished" << endl;
    5178
    5279   return 0;
    5380}
     81
     82int DataAccess::insert(string table, string rows, string values)
     83{
     84   int query_state;
     85   ostringstream oss;
     86
     87   oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")";
     88   cout << "query: " << oss.str() << endl;
     89
     90   query_state = mysql_query(connection, oss.str().c_str());
     91
     92   if (query_state != 0) {
     93      cout << mysql_error(connection) << endl;
     94      return 1;
     95   }
     96
     97   return 0;
     98}
     99
     100MYSQL_RES *DataAccess::select(string table, string filter)
     101{
     102   MYSQL_RES *result;
     103   int query_state;
     104   ostringstream oss;
     105
     106   oss << "SELECT * FROM " << table;
     107   if (!filter.empty())
     108      oss << " WHERE " << filter;
     109
     110   query_state = mysql_query(connection, oss.str().c_str());
     111
     112   if (query_state != 0) {
     113      cout << mysql_error(connection) << endl;
     114      return NULL;
     115   }
     116
     117   return mysql_store_result(connection);
     118}
  • server/DataAccess.h

    r36082e8 r59061f6  
    11#ifndef _DATA_ACCES_H
    22#define _DATA_ACCESS_H
     3
     4#include <string>
     5
     6#include <mysql/mysql.h>
     7
     8#include "Player.h"
     9
     10using namespace std;
    311
    412class DataAccess {
     
    715   ~DataAccess();
    816
    9    int dbtest();
     17   int insertPlayer(string username, string password);
     18
     19   Player *getPlayer(string username);
     20   int printPlayers();
     21
     22   int insert(string table, string rows, string values);
     23   MYSQL_RES *select(string table, string filter);
     24
     25private:
     26   MYSQL *connection, mysql;
    1027};
    1128
  • server/Player.cpp

    r36082e8 r59061f6  
    66using namespace std;
    77
     8Player::Player(string name, string password)
     9{
     10   this->name = name;
     11   this->password = password;
     12
     13   cout << "Created new player: " << this->name << endl;
     14}
     15
    816Player::Player(string name, sockaddr_in addr)
    917{
    1018   this->name = name;
     19   this->password = "";
    1120   this->addr = addr;
    1221
     
    2534   return eq;
    2635}
     36
     37void Player::setAddr(sockaddr_in addr)
     38{
     39   this->addr = addr;
     40}
  • server/Player.h

    r36082e8 r59061f6  
    99class Player {
    1010public:
    11    Player(string name, sockaddr_in addr);
     11   Player(string name, string password);
     12   Player(string name, sockaddr_in addr); // this will be deleted
    1213   ~Player();
    1314
    1415   bool operator == (const Player &p);
    1516
     17   void setAddr(sockaddr_in addr);
     18
    1619   string name;
     20   string password;
    1721   sockaddr_in addr;
    1822};
  • server/server.cpp

    r36082e8 r59061f6  
    6363}
    6464
    65 // not sure if we actually need this function
    66 // when I made it, I thought we did
    6765Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr)
    6866{
     
    10098      exit(1);
    10199   }
     100
     101   DataAccess da;
     102
     103   da.printPlayers();
     104
     105   da.insertPlayer("playerName3", "playerPass");
     106   cout << endl << "Inserted player" << endl << endl;
     107
     108   Player* p = da.getPlayer("playerName");
     109   cout << "player name: " << p->name << endl;
     110   delete(p);
     111
     112   p = da.getPlayer("playerName3");
     113   cout << "player name: " << p->name << endl;
     114   delete(p);
     115
     116   da.printPlayers();
     117   cout << endl;
    102118   
    103119   sock=socket(AF_INET, SOCK_DGRAM, 0);
     
    162178         if (p == NULL)
    163179         {
    164             vctPlayers.push_back(Player(username, from));
     180            Player newP(username, "");
     181            newP.setAddr(from);
     182
     183            vctPlayers.push_back(newP);
    165184            strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is.");
    166185         }
Note: See TracChangeset for help on using the changeset viewer.