00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ClientSP.h"
00022
00023 ClientSP::ClientSP(string adresse, unsigned int port, string login, string mdp){
00024 mSocket=new SocketTCP(adresse,port);
00025 mPortRenseigne=false;
00026 mPort=0;
00027 Traitement(login, mdp);
00028 }
00029
00030 ClientSP::~ClientSP(){
00031 delete mSocket;
00032 }
00033
00034 void ClientSP::Traitement(string login, string mdp){
00035 SendHello();
00036 string rep;
00037 do{
00038 cout<<"Etes-vous déjà utilisateur? o/n "; cin>>rep;
00039 if(rep=="o"){
00040 SendLogin(login,mdp); break;
00041 } else if(rep=="n"){
00042 RegLogin(login,mdp); break;
00043 }
00044 } while(1);
00045 rep="";
00046 do{
00047 cout<<endl;
00048 cout<<"p Lister les parties"<<endl;
00049 cout<<"s Avoir les scores"<<endl;
00050 cout<<"c Choisir ou créer une partie"<<endl<<">"; cin>>rep;
00051 if(rep=="p"){
00052 ListerPartie();
00053 } else if(rep=="s"){
00054 ListerScore();
00055 } else if(rep=="c"){
00056 ChoisirPartie(); break;
00057 }
00058 } while(1);
00059
00060
00061 }
00062
00063 void ClientSP::SendHello(){
00064 while(1){
00065 cout<<"Initialisation du protocole avec le serveur";
00066 mSocket->Envoyer("HELLO");
00067 if(mSocket->Ecouter()!="HELLO") printf( "\e[31m [Echec]\n\e[m");
00068 else {
00069 printf( "\e[32m [OK]\n\e[m");
00070 break;
00071 }
00072 }
00073 }
00074
00075 void ClientSP::SendLogin(string login, string mdp){
00076 string retour, reponse;
00077 while(1){
00078 cout<<"Envoi du login au serveur"<<endl;
00079 while(login==""){
00080 cout<<"Login: "; cin>>login;
00081 }
00082 while(mdp==""){
00083 cout<<"Mot de passe: "; cin>>mdp;
00084 }
00085 reponse="LOGIN "; reponse+=login; reponse+=" "; reponse +=mdp;
00086 mSocket->Envoyer(reponse);
00087 if(mSocket->Ecouter()=="LOGIN OK") break;
00088 else {
00089 login=""; mdp="";
00090 }
00091
00092 }
00093 mLogin=login;
00094 }
00095
00096 void ClientSP::RegLogin(string login, string mdp){
00097 string retour;
00098 while(1){
00099 cout<<"Création d'un compte"<<endl;
00100 while(login==""){
00101 cout<<"Login: "; cin>>login;
00102 }
00103 while(mdp==""){
00104 cout<<"Mot de passe: "; cin>>mdp;
00105 }
00106 string reponse="REG "; reponse+=login; reponse+=" "; reponse +=mdp;
00107 mSocket->Envoyer(reponse);
00108 if(mSocket->Ecouter()=="REG OK") break;
00109 else {
00110 login=""; mdp="";
00111 }
00112 }
00113 }
00114
00115 unsigned int ClientSP::ListerPartie(){
00116 mSocket->Envoyer("LIST");
00117 string line=mSocket->Ecouter();
00118 istringstream iss(line);
00119 string cmd;
00120 getline(iss,cmd,' ');
00121 unsigned int nb=0;
00122 if(cmd=="LIST"){
00123 cout<<endl<<"Liste des parties en cours :"<<endl;
00124 do{
00125 string port, nbFantome;
00126 getline(iss,port,' '); getline(iss,nbFantome,' ');
00127 if(port!=""){
00128 nb++;
00129 cout<<nb<<") sur le port "<<port<<" avec "<<nbFantome<<" automatiques"<<endl;
00130 string loginP, scoreP;
00131 getline(iss,loginP,' '); getline(iss,scoreP,' ');
00132 if(loginP=="libre") cout<<"\tPacman [libre] "<<endl;
00133 else cout<<"\tPacman [déja connecté] : "<<loginP<<" avec un score de "<<scoreP<<endl;
00134 for(int i=0; i<3; i++){
00135 string loginF, scoreF;
00136 getline(iss,loginF,' '); getline(iss,scoreF,' ');
00137 if(loginF=="libre") cout<<"\tFantome "<<i<<" [libre] "<<endl;
00138 else cout<<"\tFantome "<<i<<" [déja connecté] : "<<loginF<<" avec un score de "<<scoreF<<endl;
00139 }
00140 } else {
00141 cout<<"\tAucune partie en cours"<<endl;
00142 break;
00143 }
00144 string continuer;
00145 getline(iss,continuer,' ');
00146 if(continuer!=":") break;
00147 } while(1);
00148 } else cout<<"pas de LIST reçu"<<endl;
00149 return nb;
00150 }
00151
00152 void ClientSP::ListerScore(){
00153 mSocket->Envoyer("SCORE");
00154 string line=mSocket->Ecouter();
00155 istringstream iss(line);
00156 string cmd;
00157 getline(iss,cmd,' ');
00158 if(cmd=="SCORE"){
00159 cout<<endl<<"Liste des scores :"<<endl;
00160 do{
00161 string login, score;
00162 getline(iss,login,' '); getline(iss,score,' ');
00163 if(login!=""){
00164 cout<<login<<" avec un score de "<<score<<endl;
00165 } else {
00166 cout<<"\tAucun score enregistré"<<endl;
00167 break;
00168 }
00169 string continuer;
00170 getline(iss,continuer,' ');
00171 if(continuer!=":") break;
00172 } while(1);
00173 } cout<<"pas de SCORE reçu"<<endl;
00174 }
00175
00176 void ClientSP::ChoisirPartie(){
00177 unsigned int nb=ListerPartie();
00178 mSocket->Envoyer("SUITE");
00179 if(mSocket->Ecouter()=="SUITE"){
00180 while(1){
00181 if(nb!=0){
00182 cout<<"Pour rejoindre une partie entrez le port sinon 'c' pour en créer une"<<endl;
00183 string c;
00184 cout<<">"; cin>>c;
00185 if(c=="c"){
00186 if(CreationPartie()!=-1) break;
00187 } else if(RejoindrePartie(c)!=-1) break;
00188 } else if(CreationPartie()!=-1) break;
00189 }
00190 } cout<<"pas de SUITE reçu"<<endl;
00191 }
00192
00193 int ClientSP::RejoindrePartie(string port){
00194 string cmd="JOIN "; cmd+=port;
00195 mSocket->Envoyer(cmd);
00196 string retour=mSocket->Ecouter();
00197 string resultat, confirmationPort;
00198 istringstream iss(retour);
00199 getline(iss,resultat,' ');
00200 if (resultat=="GAME"){
00201 getline(iss,confirmationPort,' ');
00202 if (confirmationPort!=""){
00203 mPortRenseigne=true;
00204 istringstream iss(confirmationPort);
00205 iss >> mPort;
00206 return 1;
00207 }
00208 }
00209 return -1;
00210 }
00211
00212
00213 int ClientSP::CreationPartie(){
00214 string nbFantome, retour;
00215 do{
00216 cout<<"Demande de création de la partie"<<endl;
00217 while(nbFantome==""){
00218 cout<<"Nombre de fantomes automatiques: "; cin>>nbFantome;
00219 }
00220 string reponse="GAME "; reponse+=nbFantome;
00221 mSocket->Envoyer(reponse);
00222 retour=mSocket->Ecouter();
00223 string cmd, port;
00224 istringstream iss(retour);
00225 getline(iss,cmd,' ');
00226 if (cmd=="GAME"){
00227 getline(iss,port,' ');
00228 if (port!=""){
00229 mPortRenseigne=true;
00230 istringstream iss(port);
00231 iss >> mPort;
00232 return 1;
00233 }
00234 }
00235 nbFantome="";
00236 } while(1);
00237 }