This repository has been archived by the owner. It is now read-only.
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?
ALLGameGheorghe/Menu.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
237 lines (181 sloc)
8.32 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 <vector> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <stdexcept> | |
#include <openssl/md5.h> | |
#include <stdio.h> | |
#include <string.h> | |
using namespace std; | |
#include "libsqlite.hpp" | |
#include "InGameMenu.cpp" | |
#include "md5.h" | |
// this funcition to encrypt the password, i did some research on internet, is not mine. | |
string hashPassword(string password) | |
{ | |
string ecryptedPassword; | |
ecryptedPassword = md5(password) ; | |
return ecryptedPassword; | |
} | |
// this funcition is simply to insert into the database ( creates the team for each player when registing) | |
int createTeam(int Player_ID, string teamName, sqlite::sqlite &db ) | |
{ | |
//sqlite::sqlite db( "ball.sqlite" ); | |
auto cur = db.get_statement(); | |
cur->set_sql( "INSERT INTO MyTeam (Player_ID, TeamName) VALUES (?,?);" ); | |
cur->prepare(); | |
cur->bind( 1, Player_ID ); | |
cur->bind( 2, teamName ); | |
cur->step(); | |
return 0; | |
} | |
// First menu, this will be showed when u start | |
char menuu() | |
{ | |
char choice; | |
MainMenu: | |
do | |
{ | |
cout << "*******************************\n"; | |
cout << " 1 - Login."<< endl; | |
cout << " 2 - Register "<< endl; | |
cout << " 3 - Credits."<< endl; | |
cout << " 4 - Exit."<< endl; | |
cout << " Enter your choice and press return: "; | |
cin >> choice; | |
} // end of do while | |
while (choice >'4' || choice<'0' ); | |
//if (choice == '1') | |
switch (choice) | |
{ | |
case '1': | |
{ | |
logs: | |
string username, password, ecryptedPassword; | |
bool Blog = false; | |
// login part | |
cout << "*******************************\n"; | |
cout << "ID: "; | |
cin >> username ; | |
cout << "Password: "; | |
cin >> password ; | |
cout << "Checking Data..." << endl ; | |
ecryptedPassword = hashPassword(password); | |
// cheking on the data base | |
try | |
{ | |
sqlite::sqlite db( "ball.sqlite" ); | |
auto cur = db.get_statement(); | |
cur->set_sql( "SELECT Player_ID FROM Player " | |
"WHERE UserName = ? AND Password = ?;" ); | |
cur->prepare(); | |
cur->bind( 1, username ); | |
cur->bind( 2, ecryptedPassword ); | |
int result= cur->step(); | |
int Player_ID = cur->get_int(0); // to get the playerID (unique identification on DB) | |
if (result == true){ | |
cout<<"login Successful"<<endl; | |
MenuGame(Player_ID, db); // if the login sucesful it goes to the InGameMenu | |
return Player_ID; // returning to the main the Player ID | |
} | |
else{ | |
cout<<"Username or password are wrong"<<endl << "Try Again" << endl; | |
goto logs; | |
} //end else case | |
} | |
catch( sqlite::exception e ) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
break; | |
} | |
case '2': | |
{ | |
string username, email, password, veriyPassword, teamName, ecryptedPassword; | |
regist: | |
// login part | |
cout << "ID: "; | |
cin >> username ; | |
do{ | |
cout << "Password: "; | |
cin >> password ; | |
cout << "Insert tha same password" << endl ; | |
cout << "Repeat Password: "; | |
cin >> veriyPassword ; | |
} | |
while(password != veriyPassword); | |
cout << "Email: "; | |
cin >> email ; | |
cout << "Team Name: "; | |
cin >> teamName ; | |
cout << "Processing Data" << endl ; | |
cout << ""<< endl; | |
//check if already took the same user name or email | |
try | |
{ | |
sqlite::sqlite db( "ball.sqlite" ); | |
auto cur = db.get_statement(); | |
cur->set_sql( "SELECT * FROM Player " | |
"WHERE UserName = ? OR Email = ?;" ); | |
cur->prepare(); | |
cur->bind( 1, username ); | |
cur->bind( 2, email ); | |
int result= cur->step(); | |
if (result == true) | |
{ | |
cout<<"The username or email are already in use! Try Again With diferrent details"<<endl; | |
goto regist; | |
}//end of if case | |
else // if no one already took that usarname or email, is going to regist him | |
{ | |
ecryptedPassword = hashPassword(password); // ecrypting the password | |
//registin part | |
cur = db.get_statement(); | |
cur->set_sql( "INSERT INTO Player (UserName, Email,Password) " | |
"VALUES (?, ?,?);" ); | |
cur->prepare(); | |
cur->bind( 1, username ); | |
cur->bind( 2, email ); | |
cur->bind( 3, ecryptedPassword ); | |
cur->step(); | |
cout<<"Registed Successful"<<endl; | |
cout<<endl<<endl; | |
//to get the Player_ID (unique identification on database) | |
cur = db.get_statement(); | |
cur->set_sql( "SELECT Player_ID FROM Player " | |
"WHERE UserName = ? AND Password = ?;" ); | |
cur->prepare(); | |
cur->bind( 1, username ); | |
cur->bind( 2, ecryptedPassword ); | |
cur->step(); | |
int Player_ID = cur->get_int(0); | |
//cur = NULL; | |
createTeam(Player_ID,teamName, db); // create the team for the player (inserting in dabase) | |
// end of getting the ID | |
goto MainMenu; | |
}//end of else | |
}// end of try block | |
catch( sqlite::exception e ) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} // if registatin fails | |
// if checking if the user already exists fails | |
break; | |
} | |
case '3': | |
cout << "Group E7"<< endl; | |
cout << "Gheorghe Craciun"<< endl; | |
goto MainMenu; | |
break; | |
case '4': | |
cout << "End of Program."<< endl; | |
break; | |
default: | |
cout << "Not a Valid Choice."<< endl; | |
cout << "Choose again."<< endl; | |
goto MainMenu; | |
break; | |
} | |
}// end of menu | |