Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <mysql_connection.h>
  5. #include <cppconn/driver.h>
  6. #include <cppconn/exception.h>
  7. #include <cppconn/resultset.h>
  8. #include <cppconn/statement.h>
  9. #include <cppconn/prepared_statement.h>
  10.  
  11. #define HOST "tcp://127.0.0.1:3306"
  12. #define USER "mudgame"
  13. #define PASSWORD "linux123"
  14. #define DATABASE "MUDGAMEDB"
  15.  
  16.  
  17. enum GameState { PLAY = 1, EXIT = 2, MENU = 3, NEWPLAYER = 4 };
  18.  
  19. void selectQuery()
  20. {
  21. std::string sql;
  22.  
  23. try
  24. {
  25. sql::Driver* driver = get_driver_instance();
  26. sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
  27. con->setSchema(DATABASE);
  28.  
  29. sql::Statement* stmt = con->createStatement();
  30.  
  31. //Get results using SELECT
  32. sql = "SELECT PlayerName, PlayerPassword FROM Players";
  33. sql::ResultSet* res = stmt->executeQuery(sql);
  34. std::cout << "PLAYERNAME | PLAYERPASSWORD" << std::endl;
  35. while(res->next())
  36. {
  37. std::cout << res->getString("PlayerName") << " | " << res->getString("PlayerPassword") << std::endl;
  38. }
  39. delete(res);
  40.  
  41. //Check if exists using COUNT
  42. sql = "SELECT COUNT(*) FROM Players WHERE PlayerID = 1";
  43. res = stmt->executeQuery(sql);
  44. if(res->next())
  45. {
  46. int existe = res->getInt(1);
  47. if(existe == 1)
  48. std::cout << "El usuario existe en la BBDD. La autenticacion es correcta." << std::endl;
  49. else
  50. std::cout << "El usuario NO existe en la BBDD." << std::endl;
  51. }
  52. delete(res);
  53. delete(stmt);
  54. delete(con);
  55. }
  56. catch(sql::SQLException &e)
  57. {
  58. std::cout << "Se produce el error " << e.getErrorCode() << std::endl;
  59. }
  60.  
  61. }
  62.  
  63. void insertAndUpdate()
  64. {
  65. std::string sql;
  66.  
  67. try
  68. {
  69. sql::Driver* driver = get_driver_instance();
  70. sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
  71. con->setSchema(DATABASE);
  72.  
  73. sql::Statement* stmt = con->createStatement();
  74.  
  75. //Insert
  76. sql = "INSERT INTO Players(PlayerName, PlayerPassword) VALUES ('player63', 'player123')";
  77. int numRows = stmt->executeUpdate(sql);
  78. if(numRows == 1)
  79. {
  80. std::cout << "Se ha insertado un nuevo usuario" << std::endl;
  81. }
  82.  
  83. //Update
  84. sql = "UPDATE Players SET PlayerPassword = 'changedpwd' WHERE PlayerID = 3";
  85. numRows = stmt->executeUpdate(sql);
  86. if(numRows == 1)
  87. {
  88. std::cout << "Se ha actualizado la contraseña" << std::endl;
  89. }
  90.  
  91. delete(stmt);
  92. delete(con);
  93. }
  94. catch(sql::SQLException &e)
  95. {
  96. std::cout << "Se produce el error " << e.getErrorCode() << std::endl;
  97. }
  98. }
  99.  
  100.  
  101.  
  102. int main()
  103. {
  104. //selectQuery();
  105. //insertAndUpdate();
  106.  
  107.  
  108. GameState status;
  109. status = PLAY;
  110. sql::Driver* driver = get_driver_instance();
  111. sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
  112. con->setSchema(DATABASE);
  113.  
  114. sql::Statement* stmt = con->createStatement();
  115.  
  116. /*
  117.  
  118. 2. Si el nombre de usuario es “nuevo”, nos vamos al Ítem 3
  119.  
  120. 4. Buscamos en la base de datos si existe un usuario con esa contraseña.
  121. a. SELECT count(*) FROM USERS WHERE username=’###’ and
  122. userpassword=’€€€’
  123. 5. Si el resultado de la consulta nos da cero, no existe una cuenta de usuario con ese
  124. username y password. Ha habido un error y volvermos a pedirlo.
  125. 6. Si el resultado de la consulta da uno, es que el usuario y password ha sido introducido
  126. correctamente y que ese usuario existe.
  127. 7. Damos el mensaje de “Empieza el juego”
  128. */
  129.  
  130. std::string sql, username, pwd1, pwd2;
  131. int exist, playerID, characterID, raceID;
  132. sql::ResultSet* res;
  133.  
  134.  
  135.  
  136.  
  137.  
  138. while(status != EXIT)
  139. {
  140.  
  141. username = "";
  142. std::cout << "Type in your username: " << std::endl;
  143. std::cin >> username;
  144. std::cout << username <<std::endl;
  145.  
  146. //std::transform(username.begin(), username.end(), username.begin(), ::tolower);
  147.  
  148. //New Player
  149. if(username == "nuevo")
  150. {
  151. std::cout << "=== |Creating New Player| === " << std::endl;
  152. do
  153. {
  154. std::cout << "Type in your new username: " << std::endl;
  155. std::cin >> username;
  156.  
  157. //Comprueba si existe en la BBDD
  158. sql = "SELECT COUNT(*) FROM Players WHERE PlayerName = '" + username + "';";
  159. res = stmt->executeQuery(sql);
  160.  
  161. if(res->next())
  162. {
  163. exist = res->getInt(1);
  164. if(exist == 1)
  165. {
  166. std::cout << "This username is taken. Please choose another username." << std::endl;
  167. }
  168. else
  169. {
  170. do
  171. {
  172. if(pwd1 != pwd2){ std::cout << "Passwords don't match. Try again." << std::endl;}
  173. //Usuario no existe. Pide el password 2 veces
  174. std::cout << "Type your password: " << std::endl;
  175. std::cin >> pwd1;
  176.  
  177. std::cout << "Type again your password: " << std::endl;
  178. std::cin >> pwd2;
  179. }
  180. while(pwd1 != pwd2);
  181. //TODO: Crea usuario
  182. std::cout << "Crea usuario" << std::endl;
  183. }
  184. }
  185. delete(res);
  186. }
  187. while(exist == 1);
  188. }
  189. //Login
  190. else
  191. {
  192. do
  193. {
  194. std::cout << "Type in your password: " <<std::endl;
  195. std::cin >> pwd1;
  196.  
  197. sql = "SELECT PlayerID FROM Players WHERE PlayerName = '" + username + "' AND PlayerPassword = '" + pwd1 + "' LIMIT 0, 1;";
  198. res = stmt->executeQuery(sql);
  199. while(res->next())
  200. {
  201. playerID = res->getInt("PlayerID");
  202. }
  203. if(playerID == 0)
  204. {
  205. std::cout << "Error: There is no player with that username/password combination. Please try again. " << std::endl;
  206. std::cout << "Type in your username: " << std::endl;
  207. std::cin >> username;
  208. }
  209. delete(res);
  210.  
  211. //Lista personajes
  212. }
  213. while(exist == 0);
  214. //Login
  215. std::cout << "Logged in successfully. Select your character:" <<std::endl;
  216.  
  217. do
  218. {
  219. sql = "SELECT CharacterID, CharacterName FROM Characters WHERE PlayerID = " + std::to_string(playerID) + ";";
  220. res = stmt->executeQuery(sql);
  221. while(res->next())
  222. {
  223. std::cout << "<" << res->getInt("CharacterID") << "> " << res->getString("CharacterName") << std::endl;;
  224. exist = res->getInt(1);
  225. }
  226. if(exist == 0)
  227. {
  228. std::cout << "Error: There is no characters for this player. Do you want to create one now? [Y/N]. " << std::endl;
  229. }
  230. delete(res);
  231. }
  232. while(exist == 0);
  233.  
  234.  
  235. std::cout << "The Game Starts. " <<std::endl;
  236.  
  237. }
  238.  
  239.  
  240. /*
  241. switch (status) {
  242.  
  243. case LOGIN:
  244. break;
  245. case NEWPLAYER:
  246. std::cout << "Nuevo usuario" << std::endl;
  247. break;
  248. case SELECTCHAR:
  249. break;
  250. case NEWCHAR:
  251. break;
  252. default:
  253. break;
  254. }
  255. */
  256.  
  257. }
  258.  
  259.  
  260.  
  261.  
  262. /*
  263. sql = "SELECT COUNT(*) FROM Players WHERE PlayerName = '" + cmd + "'";
  264. res = stmt->executeQuery(sql);
  265. if(res->next())
  266. {
  267. int existe = res->getInt(1);
  268. //3. Si el nombre de usuario es <>”nuevo”, preguntamos la contraseña.
  269. if(existe == 1)
  270. std::cout << "El usuario existe en la BBDD. La autenticacion es correcta." << std::endl;
  271. else
  272. std::cout << "El usuario NO existe en la BBDD." << std::endl;
  273. }
  274. */
  275.  
  276.  
  277.  
  278.  
  279. delete(res);
  280. delete(stmt);
  281. delete(con);
  282.  
  283.  
  284.  
  285.  
  286. return 0;
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement