Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include "User.h"
  5. #include "Room.h"
  6. #include "Question.h"
  7. #include "Validator.h"
  8. #include "sqlite3.h"
  9. #include <map>
  10. #include <vector>
  11. #include <queue>
  12. #include "DataBase.h"
  13.  
  14. static string outsideString = "";
  15. static int outsideNumber = 0;
  16.  
  17. int callbackExistingUsernameOrAddedUsername(void* notUsed, int argc, char** argv, char** azCol) //returns if the command was successful (of sql)
  18. {
  19. if (argv[0] == NULL)
  20. {
  21. return -1;
  22. }
  23.  
  24. return 1;
  25. }
  26.  
  27. int callbackNumber(void* notUsed, int argc, char** argv, char** azCol) //The value that was called back will be stored outside
  28. {
  29. outsideNumber = 0;
  30.  
  31. if (argv[0] == NULL)
  32. {
  33. return -1;
  34. }
  35.  
  36.  
  37. string temp = argv[0];
  38.  
  39.  
  40. outsideNumber = atoi(temp.c_str());
  41.  
  42. return 1;
  43. }
  44.  
  45. int callbackString(void* notUsed, int argc, char** argv, char** azCol) //The value that was called back will be stored outside
  46. {
  47. outsideString = "";
  48. if (argv[0] == NULL)
  49. {
  50. return -1;
  51. }
  52.  
  53. for (int i = 0; i < argc; i++)
  54. {
  55. outsideString += argv[i];
  56. if (i != argc - 1)
  57. {
  58. outsideString += " ";
  59. }
  60. }
  61.  
  62.  
  63. return 1;
  64. }
  65.  
  66.  
  67. DataBase::DataBase() //checked
  68. {
  69. int rc;
  70. try
  71. {
  72. rc = sqlite3_open("trivia.db", &_db);
  73. if (rc)
  74. {
  75. throw sqlite3_errmsg(_db);
  76. }
  77. }
  78. catch (std::exception &ex)
  79. {
  80. std::cout << ex.what() << std::endl;
  81. }
  82. }
  83. DataBase::DataBase(DataBase *db) //checked
  84. {
  85. this->_db = db->_db;
  86. this->_games = db->_games;
  87. this->_questions = db->_questions;
  88. this->_users = db->_users;
  89.  
  90.  
  91. }
  92. DataBase::~DataBase()//checked
  93. {
  94. try
  95. {
  96. int rc = sqlite3_close(_db);
  97. if (rc)
  98. {
  99. throw sqlite3_errmsg(_db);
  100. }
  101. }
  102. catch (std::exception &ex)
  103. {
  104. std::cout << ex.what() << std::endl;
  105. }
  106. }
  107. std::map<int, Game*> DataBase::getGames() //checked
  108. {
  109.  
  110. return _games;
  111. }
  112. bool DataBase::isUserExists(string username) //checked
  113. {
  114.  
  115. char** err = NULL;
  116. string a = "SELECT username FROM t_users WHERE username = '" + username + "';";
  117. const char* line = a.c_str();
  118. int rc2 = sqlite3_exec(_db, line, callbackExistingUsernameOrAddedUsername, NULL, err);
  119.  
  120. if ((rc2 == 0 )|| (rc2 == -1))
  121. {
  122. return false;
  123. }
  124.  
  125. return true;
  126.  
  127. }
  128. bool DataBase::addNewUserInDataBase(string username, string password, string email) // checked
  129. {
  130. if (!isUserExists(username))
  131. {
  132. //adds to the db
  133. char** err = NULL;
  134.  
  135. string a = "INSERT INTO t_users values('" + username + "', '" + password + "', '" + email + "' );";
  136. const char* line = a.c_str();
  137. int rc2 = sqlite3_exec(_db, line, callbackExistingUsernameOrAddedUsername, NULL, err);
  138.  
  139. if (rc2 == 0)
  140. {
  141. return false;
  142. }
  143. std::vector<string> temp;
  144. temp.push_back(password);
  145. temp.push_back(email);
  146.  
  147. std::map <string, std::vector<string>> output;
  148. output.insert(std::make_pair(username, temp));
  149.  
  150. sqlite3_free(err);
  151. return true;
  152.  
  153. }
  154. return false;
  155. }
  156. bool DataBase::isUserAndPassMatch(string username, string password) // checked
  157. {
  158. if (isUserExists(username))
  159. {
  160.  
  161. char** err = NULL;
  162. string a = "SELECT username FROM t_users WHERE username = '" + username + "' and password = '" + password + "' ;";
  163. const char* line = a.c_str();
  164. int rc2 = sqlite3_exec(_db, line, callbackExistingUsernameOrAddedUsername, NULL, err);
  165.  
  166. if ((rc2 == 0) || (rc2 == -1))
  167. {
  168. return false;
  169. }
  170. sqlite3_free(err);
  171. return true;
  172.  
  173. }
  174.  
  175.  
  176. return false;
  177. }
  178. std::vector<Question*> DataBase::initQuestions(int questionsNo) //checked + confirmed randomizations of both questions and answers
  179. {
  180.  
  181. std::queue<int> randomNumbers;
  182. std::queue<int> tempQueue;
  183. randomNumbers.push(-1);
  184.  
  185. std::vector<Question*> empty;
  186.  
  187. for (int i = 0; i < questionsNo; i++)
  188. {
  189. int spot = 0;
  190.  
  191. // randomizing the questions
  192. int size1 = randomNumbers.size();
  193. if (size1 == 1)
  194. {
  195. spot = rand() % 9 + 1;
  196. randomNumbers.push(spot);
  197. }
  198. else
  199. {
  200. bool found = false;
  201. do
  202. {
  203. found = false;
  204. spot = rand() % 9 + 1;
  205. while (randomNumbers.size() != 0)
  206. {
  207. int x = randomNumbers.front();
  208. randomNumbers.pop();
  209. if (spot == x)
  210. {
  211. found = true;
  212. }
  213.  
  214. tempQueue.push(x);
  215. }
  216.  
  217. while (tempQueue.size() != 0)
  218. {
  219. randomNumbers.push(tempQueue.front());
  220. tempQueue.pop();
  221. }
  222. } while (found);
  223. randomNumbers.push(spot);
  224. }
  225. // end of randomizing questions
  226.  
  227. Question* q = NULL;
  228. char** err = 0;
  229.  
  230. std::string temp1 = "SELECT question FROM t_questions WHERE question_id = '" + std::to_string(spot) + "';";
  231. const char* qs = { temp1.c_str() };
  232. std::string temp2 = "SELECT correct_ans FROM t_questions WHERE question_id = " + std::to_string(spot) + ';';
  233. const char* cor_ans = { temp2.c_str() };
  234. std::string temp3 = "SELECT ans2 FROM t_questions WHERE question_id = " + std::to_string(spot) + ';';
  235. const char* ans2 = { temp3.c_str() };
  236. std::string temp4 = "SELECT ans3 FROM t_questions WHERE question_id = " + std::to_string(spot) + ';';
  237. const char* ans3 = { temp4.c_str() };
  238. std::string temp5 = "SELECT ans4 FROM t_questions WHERE question_id = " + std::to_string(spot) + ';';
  239. const char* ans4 = { temp5.c_str() };
  240.  
  241.  
  242. int rc2 = sqlite3_exec(_db, qs, callbackString, NULL, err);
  243. std::string question = outsideString;
  244. err = 0;
  245.  
  246. rc2 = sqlite3_exec(_db, cor_ans, callbackString, NULL, err);
  247. std::string correct_ans = outsideString;
  248. err = 0;
  249. rc2 = sqlite3_exec(_db, ans2, callbackString, NULL, err);
  250. std::string answer2 = outsideString;
  251. err = 0;
  252. rc2 = sqlite3_exec(_db, ans3, callbackString, NULL, err);
  253. std::string answer3 = outsideString;
  254. err = 0;
  255. rc2 = sqlite3_exec(_db, ans4, callbackString, NULL, err);
  256. std::string answer4 = outsideString;
  257. err = 0;
  258.  
  259.  
  260.  
  261. std::string questions[] = { correct_ans, answer2, answer3, answer4 };
  262.  
  263. q = new Question(spot, question, 0, questions);
  264. empty.push_back(q);
  265. sqlite3_free(err);
  266. }
  267.  
  268. return empty;
  269.  
  270. }
  271. std::vector<string> DataBase::getBestScores() //checked
  272. {
  273.  
  274.  
  275. std::map<string, int> names;
  276. int max = 0, max2 = 0, max3 = 0;
  277. string allScores = "", temp1 = "", temp2 = "";;
  278. std::vector<string> output;
  279. char** err = 0;
  280.  
  281.  
  282. sqlite3_stmt *stmt;
  283.  
  284. const char* allScoresSums = "select username, SUM(is_correct) as TotalSums "
  285. "from t_players_answers group by username "
  286. "having sum(is_correct) order by (TotalSums) desc LIMIT 3;";
  287.  
  288.  
  289. int rc = sqlite3_prepare_v2(_db, allScoresSums, -1, &stmt, NULL);
  290. if (rc != SQLITE_OK) {
  291. printf("error: %s!\n", sqlite3_errmsg(_db));
  292. return output;
  293. }
  294.  
  295. for (;;)
  296. {
  297. rc = sqlite3_step(stmt);
  298. if (rc == SQLITE_DONE)
  299. break;
  300. if (rc != SQLITE_ROW)
  301. {
  302. printf("error: %s!\n", sqlite3_errmsg(_db));
  303. break;
  304. }
  305.  
  306.  
  307. const char *text = (const char *)sqlite3_column_text(stmt, 0);
  308. string line = text ? text : "";
  309. line += " " + std::to_string(sqlite3_column_int(stmt, 1));
  310. output.push_back(line);
  311. }
  312.  
  313. sqlite3_finalize(stmt);
  314. sqlite3_free(err);
  315.  
  316.  
  317.  
  318.  
  319. return output;
  320. }
  321. std::vector<string> DataBase::getPersonalStatus(string username) // checked
  322. {
  323.  
  324. std::vector<string> empty;
  325. if (isUserExists(username))
  326. {
  327. char** err = 0;
  328.  
  329. string output = "";
  330. string line1 = "select count (distinct game_id) from t_players_answers where username = '" + username + "';";
  331. string line2 = "select count ( is_correct) from t_players_answers where username = '" + username + "' and is_correct = 1;";
  332. string line3 = "select count ( is_correct) from t_players_answers where username = '" + username + "' and is_correct = 0;";
  333. string line4 = "select avg( answer_time) from t_players_answers where username = '" + username + "';";
  334. const char* c_line1 = line1.c_str();
  335. const char* c_line2 = line2.c_str();
  336. const char* c_line3 = line3.c_str();
  337. const char* c_line4 = line4.c_str();
  338. int rc2 = sqlite3_exec(_db, c_line1, callbackString, NULL, err);
  339. if (rc2 != -1)
  340. {
  341. output = outsideString;
  342. empty.push_back(output);
  343. output = "";
  344. }
  345. else
  346. {
  347. empty.push_back("");
  348. }
  349. err = 0;
  350. rc2 = sqlite3_exec(_db, c_line2, callbackString, NULL, err);
  351. err = 0;
  352. if (rc2 != -1)
  353. {
  354. output = outsideString;
  355. empty.push_back(output);
  356. output = "";
  357. }
  358. else
  359. {
  360. empty.push_back("");
  361. }
  362. rc2 = sqlite3_exec(_db, c_line3, callbackString, NULL, err);
  363. if (rc2 != -1)
  364. {
  365. output = outsideString;
  366. empty.push_back(output);
  367. output = "";
  368. }
  369. else
  370. {
  371. empty.push_back("");
  372. }
  373. err = 0;
  374. rc2 = sqlite3_exec(_db, c_line4, callbackString, NULL, err);
  375. if (rc2 != -1)
  376. {
  377. output = outsideString;
  378. empty.push_back(output);
  379. output = "";
  380. }
  381. else
  382. {
  383. empty.push_back("");
  384. }
  385. err = 0;
  386.  
  387.  
  388. sqlite3_free(err);
  389. }
  390.  
  391. return empty;
  392. }
  393. int DataBase::insertNewGame() // checked
  394. {
  395.  
  396. char**zErrMsg = 0;
  397. char**zErrMsg2 = 0;
  398.  
  399. int id = -1;
  400. string a = "INSERT INTO t_games values(null, 0, (DateTime('now')) , null);";
  401. //this part (first 0) was supposed to be a function to print out the success
  402. int rc1 = sqlite3_exec(_db, a.c_str(), 0, 0, zErrMsg);
  403. if (rc1 != SQLITE_OK)
  404. {
  405. std::cout << "problem in insertNewGame first if statement" << std::endl;
  406. sqlite3_free(zErrMsg);
  407. return id;
  408. }
  409. a = "select game_id from t_games where rowid = (select max(rowid) from t_games); ";
  410. const char* b = a.c_str();
  411. int rc2 = sqlite3_exec(_db, a.c_str(), callbackNumber, 0, zErrMsg2);
  412. id = outsideNumber;
  413. sqlite3_free(zErrMsg2);
  414.  
  415.  
  416.  
  417. return id;
  418. }
  419. bool DataBase::updateGameStatus(int status)//checked
  420. {
  421. const char* max = "select Max(game_id) from t_games;";
  422. char** zErrMsg = 0;
  423. int size = sqlite3_exec(_db, max, callbackNumber, NULL, zErrMsg);
  424.  
  425. if (size != -1)
  426. {
  427.  
  428. if ((status > 0) && (status <= outsideNumber))
  429. {
  430.  
  431.  
  432.  
  433. string a = "UPDATE t_games SET status = 1, end_time = ( select DateTime('now') ) where game_id = " + std::to_string(status) + ";";
  434. const char* line = a.c_str();
  435. zErrMsg = 0;
  436. int rc2 = sqlite3_exec(_db, line, 0, 0, zErrMsg);
  437.  
  438.  
  439.  
  440.  
  441. if (rc2 != SQLITE_OK)
  442. {
  443. std::cout << "problem in updateGameStatus first if statement" << std::endl;
  444. sqlite3_free(zErrMsg);
  445. return false;
  446. }
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. bool DataBase::addAnswerToPlayer(int gameId, string username, int questionId, string answer, bool isCorrect, int answerTime)//checked
  453. {
  454. char* zErrMsg = 0;
  455. int correct = 0;
  456. if (isCorrect)
  457. {
  458. correct++;
  459. }
  460. string a = "INSERT INTO t_players_answers values( '" + std::to_string(gameId) + "' , '" + username + "' , '" + std::to_string(questionId) + "' , '" + answer + "' , '" + std::to_string(correct) + "' , '" + std::to_string(answerTime) + "' );";
  461. int rc = sqlite3_exec(_db, a.c_str(), 0, 0, &zErrMsg);
  462. if (rc != SQLITE_OK)
  463. {
  464. std::cout << "problem in addAnswerToPlayer first if statement" << std::endl;
  465. sqlite3_free(zErrMsg);
  466. return false;
  467. }
  468. return true;
  469. }
  470. int DataBase::callbackCount(void*, int, char**, char**)
  471. {
  472. return -1;
  473. }
  474. int DataBase::callbackQuestions(void*, int, char**, char**)
  475. {
  476. return -1;
  477. }
  478. int DataBase::callbackBestScores(void*, int, char**, char**)
  479. {
  480. return -1;
  481. }
  482. int DataBase::callbackPersonalStatus(void*, int, char**, char**)
  483. {
  484. return -1;
  485. }
  486.  
  487.  
  488.  
  489.  
  490. bool DataBase::updateGameStatus(Game* gm) //checked
  491. {
  492. return this->updateGameStatus(gm->getID());
  493.  
  494. }
  495.  
  496.  
  497. /**
  498. sqlite3_stmt *stmt;
  499. std::map<int, Game*> games;
  500. const char* allGames = "select * from t_games;";
  501. Game* g = 0;
  502.  
  503. int rc = sqlite3_prepare_v2(_db, allGames, -1, &stmt, NULL);
  504. if (rc != SQLITE_OK) {
  505. printf("error: %s!\n", sqlite3_errmsg(_db));
  506. return games;
  507. }
  508.  
  509. for (;;)
  510. {
  511. rc = sqlite3_step(stmt);
  512. if (rc == SQLITE_DONE)
  513. break;
  514. if (rc != SQLITE_ROW)
  515. {
  516. printf("error: %s!\n", sqlite3_errmsg(_db));
  517. break;
  518. }
  519.  
  520.  
  521. int id = sqlite3_column_int(stmt, 0);
  522. int status = sqlite3_column_int(stmt, 1);
  523. const char *text1 = (const char *)sqlite3_column_text(stmt, 2);
  524. const char *text2 = (const char *)sqlite3_column_text(stmt, 3);
  525.  
  526. string line1 = text1 ? text1 : "";
  527. string line2 = text2 ? text2 : "";
  528.  
  529. g = new Game();
  530.  
  531. output.push_back(line);
  532. }
  533.  
  534. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement