Changeset 59061f6 in network-game
- Timestamp:
- Nov 27, 2012, 7:03:33 PM (12 years ago)
- Branches:
- master
- Children:
- 371ce29
- Parents:
- 36082e8
- Location:
- server
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
server/DataAccess.cpp
r36082e8 r59061f6 2 2 3 3 #include <iostream> 4 5 #include <mysql/mysql.h> 4 #include <sstream> 6 5 7 6 using namespace std; … … 9 8 DataAccess::DataAccess() 10 9 { 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; 11 17 } 12 18 13 19 DataAccess::~DataAccess() 14 20 { 21 mysql_close(connection); 22 mysql_close(&mysql); 15 23 } 16 24 17 int DataAccess:: dbtest()25 int DataAccess::insertPlayer(string username, string password) 18 26 { 19 MYSQL *connection, mysql; 27 ostringstream oss; 28 29 oss << "'" << username << "', '" << password << "'"; 30 31 return insert("users", "name, password", oss.str()); 32 } 33 34 Player *DataAccess::getPlayer(string username) 35 { 20 36 MYSQL_RES *result; 21 37 MYSQL_ROW row; 22 int query_state; 38 Player *p; 39 ostringstream oss; 23 40 24 mysql_init(&mysql);41 oss << "name='" << username << "'"; 25 42 26 connection = mysql_real_connect(&mysql,"localhost","pythonAdmin","pyMaster09*","pythondb",0,0,0);43 result = select("users", oss.str().c_str()); 27 44 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 } 33 49 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; 35 54 36 if (query_state !=0) { 55 mysql_free_result(result); 56 57 return p; 58 } 59 60 int DataAccess::printPlayers() 61 { 62 MYSQL_RES *result; 63 MYSQL_ROW row; 64 ostringstream oss; 65 66 result = select("users", ""); 67 68 if (result == NULL) { 37 69 cout << mysql_error(connection) << endl; 38 70 return 1; 39 71 } 40 41 result = mysql_store_result(connection);42 72 43 73 while ( ( row = mysql_fetch_row(result)) != NULL ) { … … 46 76 47 77 mysql_free_result(result); 48 mysql_close(connection);49 50 cout << "Test finished" << endl;51 78 52 79 return 0; 53 80 } 81 82 int 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 100 MYSQL_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 1 1 #ifndef _DATA_ACCES_H 2 2 #define _DATA_ACCESS_H 3 4 #include <string> 5 6 #include <mysql/mysql.h> 7 8 #include "Player.h" 9 10 using namespace std; 3 11 4 12 class DataAccess { … … 7 15 ~DataAccess(); 8 16 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 25 private: 26 MYSQL *connection, mysql; 10 27 }; 11 28 -
server/Player.cpp
r36082e8 r59061f6 6 6 using namespace std; 7 7 8 Player::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 8 16 Player::Player(string name, sockaddr_in addr) 9 17 { 10 18 this->name = name; 19 this->password = ""; 11 20 this->addr = addr; 12 21 … … 25 34 return eq; 26 35 } 36 37 void Player::setAddr(sockaddr_in addr) 38 { 39 this->addr = addr; 40 } -
server/Player.h
r36082e8 r59061f6 9 9 class Player { 10 10 public: 11 Player(string name, sockaddr_in addr); 11 Player(string name, string password); 12 Player(string name, sockaddr_in addr); // this will be deleted 12 13 ~Player(); 13 14 14 15 bool operator == (const Player &p); 15 16 17 void setAddr(sockaddr_in addr); 18 16 19 string name; 20 string password; 17 21 sockaddr_in addr; 18 22 }; -
server/server.cpp
r36082e8 r59061f6 63 63 } 64 64 65 // not sure if we actually need this function66 // when I made it, I thought we did67 65 Player *findPlayerByAddr(vector<Player> &vec, const sockaddr_in &addr) 68 66 { … … 100 98 exit(1); 101 99 } 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; 102 118 103 119 sock=socket(AF_INET, SOCK_DGRAM, 0); … … 162 178 if (p == NULL) 163 179 { 164 vctPlayers.push_back(Player(username, from)); 180 Player newP(username, ""); 181 newP.setAddr(from); 182 183 vctPlayers.push_back(newP); 165 184 strcpy(serverMsg.buffer, "I'm thinking of a number between 1 and 1000. Guess what it is."); 166 185 }
Note:
See TracChangeset
for help on using the changeset viewer.