00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "Partie.h"
00022
00023 int Partie::AjouterPartie(unsigned int id){
00024 struct PartieInfo p;
00025 p.id=id;
00026 p.loginP="";
00027 p.scoreP=-1;
00028 p.nbFantomeAuto=-1;
00029 for(unsigned int i=0; i<3; i++){
00030 p.scoreF[i]=-1; p.loginF[i]="";
00031 }
00032 mVerrou.Lock();
00033 mPartie.ConsQueue(p);
00034 mVerrou.Unlock();
00035 }
00036
00037 int Partie::AjouterPartie(const PartieInfo &p){
00038 int pos=Rechercher(p.id);
00039 if(pos=-1) {
00040 mVerrou.Lock();
00041 mPartie.ConsQueue(p);
00042 mVerrou.Unlock();
00043 return 0;
00044 }
00045 return -1;
00046 }
00047
00048 int Partie::SupprimerPartie(unsigned int id){
00049 int pos=Rechercher(id);
00050 if(pos!=-1) {
00051 mVerrou.Lock();
00052 mPartie.Detruit(pos);
00053 mVerrou.Unlock();
00054 return 0;
00055 }
00056 return -1;
00057 }
00058
00059 string Partie::ToString(){
00060 string res="";
00061 for(unsigned int i=0; i<mPartie.Card(); i++){
00062 ostringstream ossPort,ossScore, ossNBF;
00063 ossPort<<mPartie[i].id;
00064 string port=ossPort.str();
00065 res+=port; res+=" ";
00066 ossNBF<<mPartie[i].nbFantomeAuto;
00067 string nbf=ossNBF.str();
00068 res+=nbf; res+=" ";
00069 if(mPartie[i].loginP!="") {
00070 res+=mPartie[i].loginP; res+=" ";
00071 } else res+="libre ";
00072 ossScore<<mPartie[i].scoreP;
00073 string score=ossScore.str();
00074 res+=score; res+=" ";
00075 for(unsigned int j=0; j<3; j++){
00076 if(mPartie[i].loginF[j]!="") {
00077 res+=mPartie[i].loginF[j]; res+=" ";
00078 } else res+="libre ";
00079 ostringstream ossScoreF;
00080 ossScoreF<<mPartie[i].scoreF[j];
00081 string scoreF=ossScoreF.str();
00082 res+=scoreF; res+=" ";
00083 }
00084 if(i<mPartie.Card()-1) res+=": ";
00085 }
00086 return res;
00087 }
00088
00089 void Partie::Afficher(){
00090 for(unsigned int i=0; i<mPartie.Card(); i++){
00091 Afficher(mPartie[i].id);
00092 }
00093 }
00094
00095 void Partie::Afficher(unsigned int id){
00096 int pos=Rechercher(id);
00097 if(pos!=-1) {
00098 cout<<"Partie sur le port "<<mPartie[pos].id<<" avec "<<mPartie[pos].nbFantomeAuto<<" fantomes automatiques"<<endl;
00099 cout<<"\tPacman: "<<mPartie[pos].loginP<<" avec un score de "<<mPartie[pos].scoreP<<endl;
00100 for(unsigned int i=0; i<3; i++){
00101 cout<<"\tFantome "<<i<<": "<<mPartie[pos].loginF[i]<<" avec un score de "<<mPartie[pos].scoreF[i]<<endl;
00102 }
00103 }
00104 }
00105
00106 int Partie::GetScorePacman(unsigned int id){
00107 int pos=Rechercher(id);
00108 if(pos!=-1) {
00109 return mPartie[pos].scoreP;
00110 }
00111 return -1;
00112 }
00113
00114 int Partie::SetScorePacman(unsigned int id, unsigned int score){
00115 int pos=Rechercher(id);
00116 if(pos!=-1) {
00117 struct PartieInfo p=mPartie[pos];
00118 p.scoreP=score;
00119 mVerrou.Lock();
00120 mPartie.Modifier(pos,p);
00121 mVerrou.Unlock();
00122 return 0;
00123 }
00124 return -1;
00125 }
00126
00127 int Partie:: GetScoreF(unsigned int id, unsigned int idFantome){
00128 int pos=Rechercher(id);
00129 if(pos!=-1) {
00130 return mPartie[pos].scoreF[idFantome];
00131 }
00132 return -1;
00133 }
00134
00135 int Partie::SetScoreF(unsigned int id, unsigned int idFantome, unsigned int score){
00136 int pos=Rechercher(id);
00137 if(pos!=-1) {
00138 struct PartieInfo p=mPartie[pos];
00139 p.scoreF[idFantome]=score;
00140 mVerrou.Lock();
00141 mPartie.Modifier(pos,p);
00142 mVerrou.Unlock();
00143 return 0;
00144 }
00145 return -1;
00146 }
00147
00148 int Partie::SetLoginPacman(unsigned int id, string login){
00149 int pos=Rechercher(id);
00150 if(pos!=-1) {
00151 struct PartieInfo p=mPartie[pos];
00152 p.loginP=login;
00153 mVerrou.Lock();
00154 mPartie.Modifier(pos,p);
00155 mVerrou.Unlock();
00156 return 0;
00157 }
00158 return -1;
00159 }
00160
00161 string Partie::GetLoginPacman(unsigned int id){
00162 int pos=Rechercher(id);
00163 if(pos!=-1) {
00164 return mPartie[pos].loginP;
00165 }
00166 return "";
00167 }
00168
00169 int Partie::SetLoginF(unsigned int id, unsigned int idFantome, string login){
00170 int pos=Rechercher(id);
00171 if(pos!=-1) {
00172 struct PartieInfo p=mPartie[pos];
00173 p.loginF[idFantome]=login;
00174 mVerrou.Lock();
00175 mPartie.Modifier(pos,p);
00176 mVerrou.Unlock();
00177 return 0;
00178 }
00179 return -1;
00180 }
00181
00182
00183 string Partie::GetLoginF(unsigned int id, unsigned int idFantome){
00184 int pos=Rechercher(id);
00185 if(pos!=-1) {
00186 return mPartie[pos].loginF[idFantome];
00187 }
00188 return "";
00189 }
00190
00191 int Partie::GetNbFantomeAuto(unsigned int id){
00192 int pos=Rechercher(id);
00193 if(pos!=-1) {
00194 return mPartie[pos].nbFantomeAuto;
00195 }
00196 return -1;
00197 }
00198
00199 int Partie::SetNbFantomeAuto(unsigned int id, unsigned int nbFantomeAuto){
00200 int pos=Rechercher(id);
00201 if(pos!=-1) {
00202 struct PartieInfo p=mPartie[pos];
00203 p.nbFantomeAuto=nbFantomeAuto;
00204 mVerrou.Lock();
00205 mPartie.Modifier(pos,p);
00206 mVerrou.Unlock();
00207 return 0;
00208 }
00209 return -1;
00210 }
00211
00212
00213
00214 int Partie::Rechercher(unsigned int id){
00215 mVerrou.Lock();
00216 for(unsigned int i=0; i<mPartie.Card(); i++){
00217 if(mPartie[i].id==id) {
00218 mVerrou.Unlock();
00219 return i;
00220 }
00221 }
00222 mVerrou.Unlock();
00223 return -1;
00224 }