Check in modified mamer
[capablanca.git] / lasker-2.2.3 / bots / mamer / User.cc
1 //--------------------------------------------------------------------------
2 // User.cc - Source file for User class
3 //
4 // Matthew E. Moses
5 //
6 // $Revision: 1.10 $
7 // $Date: 1998/09/10 19:57:17 $
8 //
9 // $Author: mlong $
10 // $Locker:  $
11 //
12 // $Log: User.cc,v $
13 // Revision 1.10  1998/09/10 19:57:17  mlong
14 // lots of little bug fixes and a few new features
15 //
16 // Revision 1.9  1998/04/29 15:23:19  mlong
17 // prepairing for the move to daimi
18 // new sorting routine.
19 //
20 //
21 // Revision 1.7  1997/05/15 18:27:53  chess
22 // added pending and TourneyPlayers support
23 // added HandleGetPlayerInfo & HandleGetGameInfo
24 //
25 // Revision 1.6  1997/04/13 03:14:35  chess
26 // setstatistic and addabuse added for data manipulation
27 //
28 // Revision 1.4  1997/03/27 13:45:58  chess
29 // added functions to return user's statistics
30 //
31 // Revision 1.3  1997/03/21 15:29:53  moses
32 // changed the initial clearing of the name variable
33 //
34 // Revision 1.2  1996/10/01  20:14:43  moses
35 // added a new Method IsUser
36 // added a new Method GetManagerLevel
37 //
38 // Revision 1.1  1996/09/30  20:52:48  moses
39 // Initial revision
40 //
41 //--------------------------------------------------------------------------
42
43 // static char RCSid[] = "$Id: User.cc,v 1.10 1998/09/10 19:57:17 mlong Exp $";
44
45 #include "User.hh"
46
47 //- Constructors ------------------------------------------------------------
48 User::User() {
49     memset(name, '\0', NAMELEN);
50     abuse = 0;
51     playedTourneys = wins = losses = draws = 0;
52     firsts = seconds = thirds = 0;
53     rating = placePoints = 0.0;
54
55     managerLevel = 0;
56     managedTourneys = 0;
57     last = 0;
58     tourneyLocation = 0;
59
60     inTourney = 0;
61
62     managerLevel = 0;
63     managedTourneys = 0;
64     last = 0;
65
66 } //- End of User
67
68 User::User(char *path, char *inName) {
69     abuse = 0;
70     playedTourneys = wins = losses = draws = 0;
71     firsts = seconds = thirds = 0;
72     rating = placePoints = 0.0;
73     char *lowername;
74     int i=0, length=strlen(inName);
75
76     lowername = new char[NAMELEN];
77     memset(name, '\0', NAMELEN);
78     memset(lowername, '\0', NAMELEN);
79     for(i = 0; i < length; i++) lowername[i] = tolower(inName[i]);
80
81     strncpy(name, lowername, MIN(length, NAMELEN));
82
83     //    printf("User:%s::%s::%s\n", inName, name, path);
84
85     delete(lowername);
86
87     managerLevel = 0;
88     managedTourneys = 0;
89     last = 0;
90     tourneyLocation = 0;
91
92     inTourney = 0;
93
94     //    printf("User:%s::%s::%s\n", inName, name, path);
95
96     if(0 == LoadPlayer(path, name))
97         SavePlayer(path);
98 } //- End of User
99
100 //- Deconstructor ----------------------------------------------------------
101 User::~User() {
102     
103 } //- End of ~User
104
105 //- IsUser -----------------------------------------------------------------
106 int User::IsUser(char *user) {
107   //  printf("IsUser: %18s :: %s\n", user, name);
108   
109   if(0 == strcasecmp(user, name))
110     return(1);
111   else
112     return(0);
113 } //- End of IsUser
114
115 //- LoadPlayer -------------------------------------------------------------
116 void User::LoadPlayer(char *filePath) {
117     char filename[MAXPATHLEN];
118     fstream theFile;
119     
120     CreateDirectory(filePath, name);
121     sprintf(filename, "%s/%c/%s", filePath, name[0], name);
122     theFile.open(filename, ios::in);
123
124     theFile >> name >> abuse >> playedTourneys >> wins >> losses >> draws
125             >> firsts >> seconds >> thirds >> rating >> placePoints 
126             >> managerLevel >> managedTourneys >> last >> tourneyLocation;
127
128     CalculateRating();
129     theFile.close();    
130 } //- End of LoadPlayer
131
132 //- LoadPlayer -------------------------------------------------------------
133 short User::LoadPlayer(char *filePath, char *user) {
134     struct stat statBuffer;
135     char filename[MAXPATHLEN];
136     fstream theFile;
137     char *lowername;
138     int i=0, length=strlen(user);
139
140     //    printf("LoadPlayer: %18s :: %s :: %i\n", user, filePath, length);
141
142     lowername = new char[NAMELEN];
143     memset(lowername, '\0', NAMELEN);
144     for(i = 0; i < length; i++) lowername[i] = tolower(user[i]);
145
146     user = lowername;
147     sprintf(filename, "%s/%c/%s", filePath, user[0], user);
148
149     i = stat(filename, &statBuffer);
150
151     //    printf("%i filename: %s :: User: %18s :: %s\n", i, filename, name, user);
152
153     if(0 == i) {
154         theFile.open(filename, ios::in);
155     
156         theFile >> name >> abuse >> playedTourneys >> wins >> losses >> draws
157                 >> firsts >> seconds >> thirds >> rating >> placePoints 
158                 >> managerLevel >> managedTourneys >> last >> tourneyLocation;
159
160         theFile.close();
161
162         //      printf("%i filename: %s :: User: %18s :: %s\n", i, filename, name, user);
163
164         return(1);
165     }
166
167     delete(lowername);
168
169     return(0);
170 } //- End of LoadPlayer
171
172 //- SavePlayer -----------------------------------------------------------
173 void User::SavePlayer(char *filePath) {
174     char filename[MAXPATHLEN];
175     fstream theFile;
176
177     CreateDirectory(filePath, name);
178     sprintf(filename, "%s/%c/%s", filePath, name[0], name);
179     
180     CalculateRating();
181
182     theFile.open(filename, ios::out);
183
184     printf("Saving=%s  Abuse=%i\n", name, abuse);
185
186     theFile << name << " " << abuse << " " << playedTourneys << " " 
187             << wins << " " << losses << " " << draws << " " << firsts << " " 
188             << seconds << " " << thirds << " " << rating << " " 
189             << placePoints << " " << managerLevel << " " 
190             << managedTourneys << " " << last << " " << tourneyLocation 
191             << endl;
192     theFile.close();
193 } //- End of SavePlayer
194
195 //- GetRating --------------------------------------------------------------
196 float User::GetRating(void) {
197     return(rating);
198 } //- End of GetRating
199
200 int User::GetAbuse(void) {
201   return(abuse);
202 }
203
204 long User::GetWins(void) {
205   return(wins);
206 }
207
208 long User::GetLosses(void) {
209   return(losses);
210 }
211     
212 long User::GetDraws(void) {
213   return(draws);
214 }
215  
216 long User::GetFirsts(void) {
217   return(firsts);
218 }
219
220 long User::GetSeconds(void) {
221   return(seconds);
222 }
223
224 long User::GetThirds(void) {
225   return(thirds);
226 }
227
228 float User::GetPlacePoints(void) {
229   return(placePoints);
230 }
231
232 long User::GetPlayedTourneys(void) {
233   return(playedTourneys);
234 }
235
236 long User::GetManagedTourneys(void) {
237   return(managedTourneys);
238 }
239
240 long User::GetLast(void) {
241   return(last);
242 }
243
244 void User::SetLast(long n) {
245   last = n;  
246 }
247
248 //- AddAbuse ---------------------------------------------------------------
249 void User::AddAbuse(int num) {
250     abuse += num;
251     if(abuse < 0)
252       abuse = 0;
253 } //- End of AddAbuse
254
255 //- ResetAbuse -------------------------------------------------------------
256 void User::ResetAbuse(void) {
257     abuse = ABUSE_RESET_VALUE;
258 } //- End of ResetAbuse
259
260 //- Start AddStat -----------------------------------------
261 void User::AddStat(double i) {
262   if(i == 0.0) {
263     AddLoss();
264   } else if(i == 0.5) {
265     AddDraw();
266   } else if(i == 1.0) {
267     AddWin();
268   }
269 }//- End AddStat -----------------------------------------                  
270
271 //- Start AddStat -----------------------------------------
272 void User::AddStat(int i) {
273   switch(i) {
274   case 1:
275     AddFirst();
276     break;
277   case 2:
278     AddSecond();
279     break;
280   case 3:
281     AddThird();
282     break;
283   default:
284     break;
285   }
286 }//- End AddStat -----------------------------------------                  
287
288 //- AddWin -----------------------------------------------------------------
289 void User::AddWin(void) {
290     wins++;
291 } //- End of AddWin
292
293 //- AddLoss -----------------------------------------------------------------
294 void User::AddLoss(void) {
295     losses++;
296 } //- End of AddLoss
297
298 //- AddDraw -----------------------------------------------------------------
299 void User::AddDraw(void) {
300     draws++;
301 } //- End of AddDraw
302
303 //- AddFirst -----------------------------------------------------------------
304 void User::AddFirst(void) {
305     firsts++;
306 } //- End of AddFirst
307
308 //- AddSecond -----------------------------------------------------------------
309 void User::AddSecond(void) {
310     seconds++;
311 } //- End of AddSecond
312
313 //- AddThird -----------------------------------------------------------------
314 void User::AddThird(void) {
315     thirds++;
316 } //- End of AddThird
317
318 //- AddPlayedTourney -------------------------------------------------------
319 void User::AddPlayedTourney(void) {
320     playedTourneys++;
321 } //- End of playedTourney
322
323 //- SetStatistic ---------------------------------------------------------------
324 int User::SetStatistic(int why, int newValue) {
325   
326   switch (why) {
327   case 1:
328     playedTourneys = newValue;
329     break;
330   case 2:
331     wins = newValue;
332     break;
333   case 3:
334     losses = newValue;
335     break;
336   case 4:
337     draws = newValue;
338     break;
339   case 5:
340     firsts = newValue;
341     break;
342   case 6:
343     seconds = newValue;
344     break;
345   case 7:
346     thirds = newValue;
347     break;
348   case 8:
349     abuse = newValue;
350     break;
351   case 9:
352     rating = (float)newValue/100.0;
353     break;
354   case 10:
355     managedTourneys = newValue;
356     break;
357   default:
358     return 0;
359     break;
360   }
361
362   CalculateRating();
363   return 1;
364 } //- End of SetStatistic
365
366 //- CalculateRating ---------------------------------------------------------
367 void User::CalculateRating(void) {
368   int games = (wins + losses + draws);
369
370   if((rating == 0.0) && ((wins + draws) > 0)) {
371     if(games != 0)
372       rating = (wins +(0.5 * draws)) / (games * 0.5); 
373   }
374 } //- End of CalculateRating
375
376 //- CalculateRating ---------------------------------------------------------
377 void User::CalculateRating(float tourn_expect, float pre_expect) {
378   if((tourn_expect + pre_expect) > 0)
379     rating = (wins + (draws * 0.5)) / (pre_expect + tourn_expect);
380 } //-End of CalculateRating
381
382 //- AddManagedTourney -------------------------------------------------------
383 void User::AddManagedTourney(void) {
384     managedTourneys++;
385 } //- End of AddManagedTourney
386
387 //- ChangeManagerLevel ------------------------------------------------------
388 void User::ChangeManagerLevel(int newLevel) {
389     managerLevel = newLevel;
390 } //- End of ChangeManagerLevel
391
392 //- CreateDirectory ---------------------------------------------------------
393 void User::CreateDirectory(char *path, char *user) {
394     struct stat statBuffer;
395     char filename[MAXPATHLEN];
396
397     sprintf(filename, "%s/%c", path, user[0]);
398     if(-1 == stat(filename, &statBuffer)) {
399         switch(errno) {
400          case ENOENT:
401             if(-1 == mkdir(filename, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
402                 switch(errno) {
403                  case EACCES:
404                     break;
405                  case ENOENT:
406                     break;
407                  case ENOTDIR:
408                     break;
409                 }
410             }
411             break;
412         }
413     }
414 } //- End of CreateDirectory
415
416 //- GetMangerLevel ----------------------------------------------------------
417 int User::GetManagerLevel(void) {
418     return(managerLevel);
419 } //- End of GetManagerLevel