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.
222 lines (178 sloc)
7.6 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" | |
string hashPassword(string password) | |
{ | |
string ecryptedPassword; | |
ecryptedPassword = md5(password) ; | |
return ecryptedPassword; | |
} | |
int createTeam(int Player_ID, string teamName) | |
{ | |
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 >'5' || choice<'0' ); | |
switch (choice) | |
{ | |
case '1': | |
{ | |
string username, password, ecryptedPassword; | |
bool Blog = false; | |
// login part | |
logs: | |
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 Sucessful"<<endl; | |
MenuGame(Player_ID); | |
} | |
else{ | |
cout<<"Username of 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 with same user name | |
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!"<<endl; | |
goto regist; | |
}//end of if case | |
else | |
{ | |
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 Sucessful"<<endl; | |
cout<<endl<<endl; | |
// // to get the playerID (unique identification on DB) | |
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, password ); | |
int result= cur->step(); | |
int Player_ID = cur->get_int(0); | |
//sqlite::sqlite3_close(db); | |
cur = NULL; | |
createTeam(Player_ID,teamName); // 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; | |
break; | |
case '4': | |
cout << "End of Program."<< endl; | |
break; | |
default: | |
cout << "Not a Valid Choice."<< endl; | |
cout << "Choose again."<< endl; | |
goto MainMenu; | |
break; | |
} | |
return 0; | |
}// end of menu | |