Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include "player.h"
using namespace std;
vector<string> nameStrings;
vector<string> passStrings;
string playername;
string pass1;
int callback(void* NotUsed, int argc, char** argv, char** azColName) {
// int argc: holds the number of results
// (array) azColName: holds each column returned
// (array) argv: holds each value
for (int i = 0; i < argc; i++) {
// Show column name, value, and newline
cout << azColName[i] << ": " << argv[i] << endl;
}
cout << endl;
return 0;
}
//
void Sql::statementFunction(sqlite3* a) //OVERLOADING FUNCTIONS
{
sqlite3_stmt* res;
rc = sqlite3_prepare_v2(a, sql.c_str(), -1, &res, 0);
if (rc == SQLITE_OK)
{
sqlite3_bind_text(res, 1, playername.c_str(), playername.length(), SQLITE_STATIC);
sqlite3_bind_text(res, 2, pass1.c_str(), pass1.length(), SQLITE_STATIC);
}
else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(a));
}
rc = sqlite3_step(res);
sqlite3_reset(res);
}
bool Sql::statementFunction(sqlite3* a, string q)
{
sqlite3_stmt* res;
rc = sqlite3_prepare_v2(a, q.c_str(), -1, &res, 0);
if (rc == SQLITE_OK)
{
sqlite3_bind_text(res, 1, playername.c_str(), playername.length(), SQLITE_STATIC);
sqlite3_bind_text(res, 2, pass1.c_str(), pass1.length(), SQLITE_STATIC);
}
else {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(a));
}
rc = sqlite3_step(res);
bool exists = sqlite3_column_int(res, 0);
sqlite3_reset(res);
return exists;
}
int Sql::updateStrengthTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
string playername1;
playername1 = playername;
sql = "UPDATE PLAYERS SET STRENGTH = STRENGTH + 20 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
statementFunction(db);
sqlite3_close(db);
return 1;
}
//
int Sql::updateIntelligenceTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
string playername1;
playername1 = playername;
sql = "UPDATE PLAYERS SET INTELLIGENCE = INTELLIGENCE + 20 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
//sql = "UPDATE PLAYERS SET STRENGTH = STRENGTH + 8 WHERE PLAYERNAME = '" + playername1 + "';";
// Run the SQL (convert the string to a C-String with c_str() )
statementFunction(db);
sqlite3_close(db);
return 1;
}
//
int Sql::updateDehydrationTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
sql = "UPDATE PLAYERS SET HYDRATION = HYDRATION - 20 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
statementFunction(db);
sqlite3_close(db);
return 1;
}
//
int Sql::updateStaminaTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
sql = "UPDATE PLAYERS SET STAMINA = STAMINA + 20 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
statementFunction(db);
sqlite3_close(db);
return 1;
}
//
int Sql::updateWaterTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
sql = "UPDATE PLAYERS SET HYDRATION = HYDRATION + 20 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
statementFunction(db);
sqlite3_close(db);
return 1;
}
int Sql::updateBalanceCleaningTable() {
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
sql = "UPDATE PLAYERS SET BALANCE = BALANCE + 5 WHERE PLAYERNAME = ? AND PASSWORD = ? ; ";
statementFunction(db);
sqlite3_close(db);
return 1;
}
int Sql::Query(bool b)
{
sqlite3* db;
rc = sqlite3_open("stats.db", &db);
if (rc) {
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return(1);
}
cout << "Enter Your Player Name" << endl;
cin >> playername;
cout << "Enter Your Password" << endl;
cin >> pass1;
auto callback = [](void*, int, char** argv, char**) -> int
{
return atoi(argv[0]);
};
//created a query which prints 1 if exists and 0 if doesnt.
std::string query = "SELECT count(*) FROM PLAYERS WHERE PLAYERNAME = ? AND PASSWORD = ? LIMIT 0,1;";
statementFunction(db);
b = statementFunction(db, query);
std::cout << (b ? "Logging in ...." : "Not exists") << std::endl;
value = b;
sqlite3_close(db);
if (value == false)
{
rc = sqlite3_open("stats.db", &db);
sql = "INSERT INTO PLAYERS ('PLAYERID', 'PLAYERNAME','PASSWORD', 'HEALTH','STRENGTH','STAMINA','HYDRATION','INTELLIGENCE','BALANCE' ) VALUES (NULL ,?,?,'100','0','0','100','0','0');";
statementFunction(db);
std::string query = "SELECT count(*) FROM PLAYERS WHERE PLAYERNAME = ? AND PASSWORD = ? LIMIT 0,1;";
b = statementFunction(db, query);
std::cout << (b ? "New User Created" : "Not exists") << std::endl;
//cout << exists << endl;
value = b;
//
sqlite3_close(db);
return value;
}
}
int Sql::selectTable() {
// Pointer to SQLite connection
sqlite3* db;
// Save the result of opening the file
rc = sqlite3_open("stats.db", &db);
if (rc) {
// Show an error message
cout << "DB Error: " << sqlite3_errmsg(db) << endl;
// Close the connection
sqlite3_close(db);
// Return an error
return(1);
}
sql = "SELECT STRENGTH, HEALTH, STAMINA, HYDRATION, INTELLIGENCE,BALANCE FROM 'PLAYERS' WHERE PLAYERNAME = '" + playername + "' AND PASSWORD = '" + pass1 + "';";
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &zErrMsg);
sqlite3_close(db);
return 1;
}