00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "BdD.h"
00022
00023 BdD::BdD(){
00024 mVerrou.Lock();
00025 Charger();
00026 mVerrou.Unlock();
00027 }
00028
00029 BdD::~BdD(){
00030 Sauvegarder();
00031 }
00032
00033 int BdD::AjouterUtilisateur(string login, string mdp){
00034 mVerrou.Lock();
00035 if(Rechercher(login)==-1){
00036 UserInfo u;
00037 u.login=login;
00038 u.mdp=mdp;
00039 u.score=0;
00040 mBdD.ConsQueue(u);
00041 mVerrou.Unlock();
00042 return 1;
00043 }
00044 mVerrou.Unlock();
00045 return -1;
00046 }
00047
00048 int BdD::SupprimerUtilisateur(string login){
00049 mVerrou.Lock();
00050 int pos=Rechercher(login);
00051 if(pos!=-1) {
00052 mBdD.Detruit(pos);
00053 mVerrou.Unlock();
00054 return 1;
00055 }
00056 mVerrou.Unlock();
00057 return -1;
00058 }
00059
00060 bool BdD::EstUtilisateur(string login, string mdp){
00061 mVerrou.Lock();
00062 int pos=Rechercher(login);
00063 if(pos!=-1) if(mBdD[pos].mdp==mdp){
00064 mVerrou.Unlock();
00065 return true;
00066 }
00067 mVerrou.Unlock();
00068 return false;
00069 }
00070
00071 int BdD::GetScore(string login){
00072 mVerrou.Lock();
00073 int pos=Rechercher(login);
00074 if(pos!=-1) {
00075 mVerrou.Unlock();
00076 return mBdD[pos].score;
00077 }
00078 mVerrou.Unlock();
00079 return -1;
00080 }
00081
00082 int BdD::SetScore(string login, unsigned int score){
00083 mVerrou.Lock();
00084 int pos=Rechercher(login);
00085 if(pos!=-1) {
00086 UserInfo u=mBdD[pos];
00087 u.score=score;
00088 mBdD.Modifier(pos,u);
00089 mVerrou.Unlock();
00090 return 1;
00091 }
00092 mVerrou.Unlock();
00093 return -1;
00094 }
00095
00096 void BdD::Charger(){
00097
00098 ifstream f("users.bdd");
00099 string line;
00100 if(f.is_open()){
00101 while(getline(f,line)){
00102 string login,mdp, score;
00103 istringstream iss(line);
00104 getline(iss,login,' '); getline(iss,mdp,' '); getline(iss,score,' ');
00105 istringstream conv(score);
00106 UserInfo u;
00107 u.login=login; u.mdp=mdp; conv>>u.score;
00108 mBdD.ConsQueue(u);
00109
00110 }
00111 } else {
00112
00113 ofstream fCreationBdD("users.bdd");
00114 fCreationBdD.close();
00115 }
00116 f.close();
00117 }
00118
00119 void BdD::Sauvegarder(){
00120 mVerrou.Lock();
00121 ofstream f("users.bdd");
00122 if(f.is_open()){
00123 for(unsigned int i=0; i<mBdD.Card(); i++){
00124 f<<mBdD[i].login<<" "<<mBdD[i].mdp<<" "<<mBdD[i].score<<endl;
00125 }
00126 } else cerr<<"Impossible d'ouvrir le fichier en écriture !"<<endl;
00127 f.close();
00128 mVerrou.Unlock();
00129 }
00130
00131
00132 int BdD::Rechercher(string login){
00133 for(unsigned int i=0; i<mBdD.Card(); i++){
00134 if(mBdD[i].login==login) {
00135 return i;
00136 }
00137 }
00138 return -1;
00139 }
00140
00141 void BdD::Afficher(){
00142 mVerrou.Lock();
00143 for(unsigned int i=0; i<mBdD.Card(); i++){
00144 cout<<mBdD[i].login<<" "<<mBdD[i].mdp<<" "<<mBdD[i].score<<endl;
00145 }
00146 mVerrou.Unlock();
00147 }
00148
00149 string BdD::ListeScore(){
00150 string res="";
00151 for(unsigned int i=0; i<mBdD.Card(); i++){
00152 res+=mBdD[i].login; res+=" ";
00153 ostringstream oss;
00154 oss<<mBdD[i].score;
00155 res+=oss.str(); res+=" ";
00156 if(i<mBdD.Card()-1) res+=": ";
00157 }
00158 return res;
00159 }