Permalink
Cannot retrieve contributors at this time
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?
f5OSG_Project_Witch_Hunt/database.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
595 lines (572 sloc)
13.9 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "database.h" | |
/** | |
* @author Balazs Bordas | |
*/ | |
int DataBase::playerID = 0; | |
const std::string DataBase::sqliteFile = "osg"; | |
/** | |
* @return the current value stored in DataBase::playerID. | |
*/ | |
int DataBase::getPlayerID(){ | |
return DataBase::playerID; | |
} | |
/** | |
* Inserts the string passed into it as a playerName in the database, | |
* and sets the DataBase::playerID variable to the inserted player's ID | |
* | |
* @param the username you want to register | |
*/ | |
void DataBase::registerPlayer(std::string playerName) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
std::cout << "connected to database" << std::endl; | |
//sql by me (Balazs) | |
cur->set_sql("INSERT INTO players (playerName) values (?)"); | |
cur->prepare(); | |
cur->bind(1, playerName); | |
cur->step(); | |
cur = db.get_statement(); | |
//sql by me (Balazs) | |
cur->set_sql("SELECT COUNT(*) FROM PLAYERS;"); | |
cur->prepare(); | |
cur->step(); | |
DataBase::playerID = cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Sets the health of the player with the given ID to 0. | |
* | |
* @param playerID | |
*/ | |
void DataBase::killPlayer(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("UPDATE players SET playerHealth = 0 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
std::cout << "You have died. :(" << std::endl; | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
Returns the current level of the player with the given ID. | |
@param playerID | |
@return current level of player | |
*/ | |
int DataBase::getPlayerLevel(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("SELECT playerLevel FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Adds one to the current level of the player with the given ID. | |
* | |
* @param playerID | |
*/ | |
void DataBase::levelUpPlayer(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("UPDATE players SET playerLevel = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, DataBase::getPlayerLevel(playerID) + 1); | |
cur->bind(2, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the amount of money the player with the given ID has. | |
* | |
* @param playerID | |
* @return amount of money | |
*/ | |
int DataBase::getPlayerMoney(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("SELECT playerMoney FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Modifies the amount of money the player has. | |
* | |
* @param playerID | |
* @param modification | |
*/ | |
void DataBase::modifyPlayerMoney(int playerID, int modification) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
int current = DataBase::getPlayerMoney(playerID); | |
if (current > -modification) { | |
//sql by Miles | |
cur->set_sql("UPDATE players SET playerMoney = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, current + modification); | |
cur->bind(2, playerID); | |
cur->step(); | |
} else { | |
//sql by Miles | |
cur->set_sql("UPDATE players SET playerMoney = 0 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Return how many items the player has. | |
* | |
* @param playerID | |
* @return player item count | |
*/ | |
int DataBase::getPlayerItemsTotal(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("SELECT playerItemsTotal FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Adds one to the number of items the player has. | |
* | |
* @param playerID | |
*/ | |
void DataBase::addOneToPlayerItemsTotal(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Miles | |
cur->set_sql("UPDATE players SET playerItemsTotal = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, DataBase::getPlayerItemsTotal(playerID) + 1); | |
cur->bind(2, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Return the current health the player has. | |
* | |
* @param playerID | |
* @return player health | |
*/ | |
int DataBase::getPlayerHealth(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Alicja | |
cur->set_sql("SELECT playerHealth FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Modifies the amount of health the player has. | |
* | |
* @param playerID | |
* @param modification | |
*/ | |
void DataBase::modifyPlayerHealth(int playerID, int modification) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
int current = DataBase::getPlayerHealth(playerID); | |
if (current > -modification) { | |
//sql by Alicja | |
cur->set_sql("UPDATE players SET playerHealth = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, current + modification); | |
cur->bind(2, playerID); | |
cur->step(); | |
} else { | |
DataBase::killPlayer(playerID); | |
} | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Return the current attack modifier the player has. | |
* | |
* @param playerID | |
* @return player attack modifier | |
*/ | |
int DataBase::getPlayerAttack(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Shuvas | |
cur->set_sql("SELECT playerAttack FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Modifies the attack modifier the player has. | |
* | |
* @param playerID | |
* @param modification | |
*/ | |
void DataBase::modifyPlayerAttack(int playerID, int modification) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
int current = DataBase::getPlayerAttack(playerID); | |
if (current > -modification) { | |
//sql by Shuvas | |
cur->set_sql("UPDATE players SET playerAttack = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, current + modification); | |
cur->bind(2, playerID); | |
cur->step(); | |
} else { | |
//sql by Shuvas | |
cur->set_sql("UPDATE players SET playerAttack = 0 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Return the current defense modifier the player has. | |
* | |
* @param playerID | |
* @return player defense modifier | |
*/ | |
int DataBase::getPlayerDefense(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Shuvas | |
cur->set_sql("SELECT playerDefense FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Modifies the defense modifier the player has. | |
* | |
* @param playerID | |
* @param modification | |
*/ | |
void DataBase::modifyPlayerDefense(int playerID, int modification) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
int current = DataBase::getPlayerDefense(playerID); | |
if (current > -modification) { | |
//sql by Shuvas | |
cur->set_sql("UPDATE players SET playerDefense = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, current + modification); | |
cur->bind(2, playerID); | |
cur->step(); | |
} else { | |
//sql by Shuvas | |
cur->set_sql("UPDATE players SET playerDefense = 0 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the current state of the 'hasMachete' bool | |
* | |
* @param playerID | |
* @return the current state of the 'hasMachete' bool | |
*/ | |
bool DataBase::checkForMachete(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("SELECT hasMachete FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return (cur->get_int(0) == 1); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Sets the 'hasMachete' bool true for the player | |
* | |
* @param playerID | |
*/ | |
void DataBase::assignMachete(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("UPDATE players SET hasMachete=1 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the current state of the 'hasKey' bool | |
* | |
* @param playerID | |
* @return the current state of the 'hasKey' bool | |
*/ | |
bool DataBase::checkForKey(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("SELECT hasKey FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return (cur->get_int(0) == 1); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Sets the 'hasKey' bool true for the player | |
* | |
* @param playerID | |
*/ | |
void DataBase::assignKey(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("UPDATE players SET hasKey=1 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the current state of the 'hasChip' bool | |
* | |
* @param playerID | |
* @return the current state of the 'hasChip' bool | |
*/ | |
bool DataBase::checkForChip(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("SELECT hasChip FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return (cur->get_int(0) == 1); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Sets the 'hasChip' bool true for the player | |
* | |
* @param playerID | |
*/ | |
void DataBase::assignChip(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Hamza | |
cur->set_sql("UPDATE players SET hasChip=1 WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the number of potions the player has. | |
* | |
* @param playerID | |
* @return potion count | |
*/ | |
int DataBase::getPotionCount(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by me (Balazs) | |
cur->set_sql("SELECT playerPotionCount FROM players WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns true if the player has any potions. | |
* | |
* @param playerID | |
* @return whether the user has any potions | |
*/ | |
bool DataBase::checkForPotion(int playerID){ | |
return (DataBase::getPotionCount(playerID) > 0); | |
} | |
/** | |
* Adds one to the number of potions the player has. | |
* | |
* @param playerID | |
*/ | |
void DataBase::addOnePotion(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by me (Balazs) | |
cur->set_sql("UPDATE players SET playerPotionCount = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, DataBase::getPotionCount(playerID) + 1); | |
cur->bind(2, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Substracts one from the number of potions the player has. | |
* | |
* @param playerID | |
*/ | |
void DataBase::reducePotionByOne(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by me (Balazs) | |
cur->set_sql("UPDATE players SET playerPotionCount = ? WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, DataBase::getPotionCount(playerID) - 1); | |
cur->bind(2, playerID); | |
cur->step(); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the biggest attack modifier the player's weapons have. | |
* | |
* @param playerID | |
* @return biggest defense modifier | |
*/ | |
int DataBase::getBiggestAttack(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Alicja | |
cur->set_sql("SELECT MAX(weaponAtkMod) FROM weapons JOIN armory ON (weapons.weaponID=armory.weaponID) WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} | |
/** | |
* Returns the biggest defense modifier the player's weapons have. | |
* | |
* @param playerID | |
* @return biggest defense modifier | |
*/ | |
int DataBase::getBiggestDefense(int playerID) { | |
try { | |
sqlite::sqlite db(DataBase::sqliteFile); | |
auto cur = db.get_statement(); | |
//sql by Alicja | |
cur->set_sql("SELECT MAX(weaponDefMod) FROM weapons JOIN armory ON (weapons.weaponID=armory.weaponID) WHERE playerID = ?"); | |
cur->prepare(); | |
cur->bind(1, playerID); | |
cur->step(); | |
return cur->get_int(0); | |
} | |
catch (sqlite::exception e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} |