Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <mysql_connection.h>
- #include <cppconn/driver.h>
- #include <cppconn/exception.h>
- #include <cppconn/resultset.h>
- #include <cppconn/statement.h>
- #include <cppconn/prepared_statement.h>
- #define HOST "tcp://127.0.0.1:3306"
- #define USER "mudgame"
- #define PASSWORD "linux123"
- #define DATABASE "MUDGAMEDB"
- enum GameState { PLAY = 1, EXIT = 2, MENU = 3, NEWPLAYER = 4 };
- void selectQuery()
- {
- std::string sql;
- try
- {
- sql::Driver* driver = get_driver_instance();
- sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
- con->setSchema(DATABASE);
- sql::Statement* stmt = con->createStatement();
- //Get results using SELECT
- sql = "SELECT PlayerName, PlayerPassword FROM Players";
- sql::ResultSet* res = stmt->executeQuery(sql);
- std::cout << "PLAYERNAME | PLAYERPASSWORD" << std::endl;
- while(res->next())
- {
- std::cout << res->getString("PlayerName") << " | " << res->getString("PlayerPassword") << std::endl;
- }
- delete(res);
- //Check if exists using COUNT
- sql = "SELECT COUNT(*) FROM Players WHERE PlayerID = 1";
- res = stmt->executeQuery(sql);
- if(res->next())
- {
- int existe = res->getInt(1);
- if(existe == 1)
- std::cout << "El usuario existe en la BBDD. La autenticacion es correcta." << std::endl;
- else
- std::cout << "El usuario NO existe en la BBDD." << std::endl;
- }
- delete(res);
- delete(stmt);
- delete(con);
- }
- catch(sql::SQLException &e)
- {
- std::cout << "Se produce el error " << e.getErrorCode() << std::endl;
- }
- }
- void insertAndUpdate()
- {
- std::string sql;
- try
- {
- sql::Driver* driver = get_driver_instance();
- sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
- con->setSchema(DATABASE);
- sql::Statement* stmt = con->createStatement();
- //Insert
- sql = "INSERT INTO Players(PlayerName, PlayerPassword) VALUES ('player63', 'player123')";
- int numRows = stmt->executeUpdate(sql);
- if(numRows == 1)
- {
- std::cout << "Se ha insertado un nuevo usuario" << std::endl;
- }
- //Update
- sql = "UPDATE Players SET PlayerPassword = 'changedpwd' WHERE PlayerID = 3";
- numRows = stmt->executeUpdate(sql);
- if(numRows == 1)
- {
- std::cout << "Se ha actualizado la contraseña" << std::endl;
- }
- delete(stmt);
- delete(con);
- }
- catch(sql::SQLException &e)
- {
- std::cout << "Se produce el error " << e.getErrorCode() << std::endl;
- }
- }
- int main()
- {
- //selectQuery();
- //insertAndUpdate();
- GameState status;
- status = PLAY;
- sql::Driver* driver = get_driver_instance();
- sql::Connection* con = driver->connect(HOST, USER, PASSWORD);
- con->setSchema(DATABASE);
- sql::Statement* stmt = con->createStatement();
- /*
- 2. Si el nombre de usuario es “nuevo”, nos vamos al Ítem 3
- 4. Buscamos en la base de datos si existe un usuario con esa contraseña.
- a. SELECT count(*) FROM USERS WHERE username=’###’ and
- userpassword=’€€€’
- 5. Si el resultado de la consulta nos da cero, no existe una cuenta de usuario con ese
- username y password. Ha habido un error y volvermos a pedirlo.
- 6. Si el resultado de la consulta da uno, es que el usuario y password ha sido introducido
- correctamente y que ese usuario existe.
- 7. Damos el mensaje de “Empieza el juego”
- */
- std::string sql, username, pwd1, pwd2;
- int exist, playerID, characterID, raceID;
- sql::ResultSet* res;
- while(status != EXIT)
- {
- username = "";
- std::cout << "Type in your username: " << std::endl;
- std::cin >> username;
- std::cout << username <<std::endl;
- //std::transform(username.begin(), username.end(), username.begin(), ::tolower);
- //New Player
- if(username == "nuevo")
- {
- std::cout << "=== |Creating New Player| === " << std::endl;
- do
- {
- std::cout << "Type in your new username: " << std::endl;
- std::cin >> username;
- //Comprueba si existe en la BBDD
- sql = "SELECT COUNT(*) FROM Players WHERE PlayerName = '" + username + "';";
- res = stmt->executeQuery(sql);
- if(res->next())
- {
- exist = res->getInt(1);
- if(exist == 1)
- {
- std::cout << "This username is taken. Please choose another username." << std::endl;
- }
- else
- {
- do
- {
- if(pwd1 != pwd2){ std::cout << "Passwords don't match. Try again." << std::endl;}
- //Usuario no existe. Pide el password 2 veces
- std::cout << "Type your password: " << std::endl;
- std::cin >> pwd1;
- std::cout << "Type again your password: " << std::endl;
- std::cin >> pwd2;
- }
- while(pwd1 != pwd2);
- //TODO: Crea usuario
- std::cout << "Crea usuario" << std::endl;
- }
- }
- delete(res);
- }
- while(exist == 1);
- }
- //Login
- else
- {
- do
- {
- std::cout << "Type in your password: " <<std::endl;
- std::cin >> pwd1;
- sql = "SELECT PlayerID FROM Players WHERE PlayerName = '" + username + "' AND PlayerPassword = '" + pwd1 + "' LIMIT 0, 1;";
- res = stmt->executeQuery(sql);
- while(res->next())
- {
- playerID = res->getInt("PlayerID");
- }
- if(playerID == 0)
- {
- std::cout << "Error: There is no player with that username/password combination. Please try again. " << std::endl;
- std::cout << "Type in your username: " << std::endl;
- std::cin >> username;
- }
- delete(res);
- //Lista personajes
- }
- while(exist == 0);
- //Login
- std::cout << "Logged in successfully. Select your character:" <<std::endl;
- do
- {
- sql = "SELECT CharacterID, CharacterName FROM Characters WHERE PlayerID = " + std::to_string(playerID) + ";";
- res = stmt->executeQuery(sql);
- while(res->next())
- {
- std::cout << "<" << res->getInt("CharacterID") << "> " << res->getString("CharacterName") << std::endl;;
- exist = res->getInt(1);
- }
- if(exist == 0)
- {
- std::cout << "Error: There is no characters for this player. Do you want to create one now? [Y/N]. " << std::endl;
- }
- delete(res);
- }
- while(exist == 0);
- std::cout << "The Game Starts. " <<std::endl;
- }
- /*
- switch (status) {
- case LOGIN:
- break;
- case NEWPLAYER:
- std::cout << "Nuevo usuario" << std::endl;
- break;
- case SELECTCHAR:
- break;
- case NEWCHAR:
- break;
- default:
- break;
- }
- */
- }
- /*
- sql = "SELECT COUNT(*) FROM Players WHERE PlayerName = '" + cmd + "'";
- res = stmt->executeQuery(sql);
- if(res->next())
- {
- int existe = res->getInt(1);
- //3. Si el nombre de usuario es <>”nuevo”, preguntamos la contraseña.
- if(existe == 1)
- std::cout << "El usuario existe en la BBDD. La autenticacion es correcta." << std::endl;
- else
- std::cout << "El usuario NO existe en la BBDD." << std::endl;
- }
- */
- delete(res);
- delete(stmt);
- delete(con);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement