Advanced Strategic Command
pbem-server-interaction.cpp
Go to the documentation of this file.
1 
2 
4 
5 
6 
20 size_t ASC_PBEM_writeInternal( void* inboundData, size_t size, size_t nmemb, void *vector )
21 {
22  if( size != sizeof( char ) ) return 0;
23  char* buffer = new char[ nmemb+1 ];
24  memset( buffer, '\0', nmemb+1 );
25  for( int i=0; i<nmemb; i++ )
26  {
27  buffer[ i ] = ((char*) inboundData )[ i ];
28  }
29  ( (std::vector<ASCString> *) vector )->push_back( buffer );
30  delete buffer;
31  return nmemb;
32 }
33 
34 
35 int ASC_PBEM_writeBuffer( Uint8* buffer, const Uint8* source, int startPos, int bufferSize, int sourceSize )
36 {
37  int i=0;
38  for( ; i<bufferSize && i<(sourceSize-startPos); i++ )
39  {
40  buffer[ i ] = source[ startPos + i ];
41  //std::cout << buffer[ i ];
42  }
43 
44  return i;
45 }
46 
47 size_t ASC_PBEM_readInternal( Uint8* outboundBuffer, size_t size, size_t nitems, void *infoStruct )
48 {
50 
51  switch( control->step )
52  {
53  case 0: // header of first part
54  {
55  ASCString header = ASCString("--") + control->boundary + "\n" +
56  "Content-Disposition: form-data; name=\"file\"; filename=\"" +
57  control->fileName + "\"\n" +
58  "Content-Type: application/octet-stream\n \n";
59  // end of header has to be a \n<space>\n for the server to reliably identify it as empty line
60  int sent = ASC_PBEM_writeBuffer( outboundBuffer, (const Uint8*) header.c_str(), control->sent_step, size*nitems, header.size() );
61  if( sent+control->sent_step == header.size() )
62  {
63  control->sent_step = 0;
64  control->step++;
65  }else
66  {
67  control->sent_step += sent;
68  }
69 
70  return sent;
71  }
72 
73  case 1: // first part
74  {
75  int sent = ASC_PBEM_writeBuffer( outboundBuffer, control->data, control->sent_step, size*nitems, control->size );
76  if( sent+control->sent_step == control->size )
77  {
78  control->sent_step = 0;
79  control->step++;
80  }else
81  {
82  control->sent_step += sent;
83  }
84 
85  return sent;
86  }
87 
88  case 2: // footer: "--" + boundary + "--"
89  {
90  // keine ahnung warum da 2x\n nötig sind... wird aber sonst nicht korrekt geparsed
91  ASCString header = ASCString("\n\n--") + control->boundary;
92  if( control->parameters.size() == 0 )
93  header += "--\n";
94  else
95  header += "\n";
96 
97  int sent = ASC_PBEM_writeBuffer( outboundBuffer, (const Uint8*) header.c_str(), control->sent_step, size*nitems, header.size() );
98  if( sent+control->sent_step == header.size() )
99  {
100  control->sent_step = 0;
101  control->step++;
102  }else
103  {
104  control->sent_step += sent;
105  }
106 
107  return sent;
108  }
109  default: // footer: "--" + boundary + "--"
110  {
111  // parameters
112  int parameterIndex = (control->step - 3) * 2;
113  if( control->parameters.size() > parameterIndex )
114  {
115  ASCString name = control->parameters[ parameterIndex ];
116  ASCString value = control->parameters[ parameterIndex + 1 ];
117 
118  ASCString data = ASCString("Content-Disposition: form-data; name=\"") + name + "\"\n\n" + value +
119  "\n\n--" + control->boundary;
120 
121  if( control->parameters.size() > parameterIndex + 2 )
122  {
123  data += "\n";
124  }else
125  {
126  data += "--\n";
127  }
128 
129  int sent = ASC_PBEM_writeBuffer( outboundBuffer, (const Uint8*) data.c_str(), control->sent_step, size*nitems, data.size() );
130  if( sent+control->sent_step == data.size() )
131  {
132  control->sent_step = 0;
133  control->step++;
134  }else
135  {
136  control->sent_step += sent;
137  }
138 
139  return sent;
140  }else
141  {
142  return 0;
143  }
144  }
145  }
146  return 0;
147 }
148 
149 
151 {
152  this->serverBase = serverBase;
153  if( serverBase[ serverBase.size() - 1 ] != '/' )
154  {
155  this->serverBase = serverBase + "/";
156  }
157  curl_handle = curl_easy_init();
158 
159  usable = true;
160  statusCode = -1;
161  loggedIn = false;
162  serverVersion = 0;
163 
164  request( "" );
165 
166  usable = false;
167 
168  if(
169  statusCode == 200 &&
170  serverVersion >= MIN_SERVER_VERSION &&
171  serverVersion < MAX_SERVER_VERSION
172  )
173  {
174  usable = true;
175  }
176 
177 }
178 
179 
181 {
182  curl_easy_cleanup( curl_handle );
183 }
184 
186 {
187  return usable;
188 }
189 
191 {
192  return loggedIn;
193 }
194 
195 bool ASC_PBEM::request( ASCString url )
196 {
197  std::vector<ASCString> parameters;
198  return request( url, parameters );
199 }
200 
201 bool ASC_PBEM::request( ASCString url, std::vector<ASCString> parameters )
202 {
203  if( ! usable ) return false;
204 
205  statusCode = -1;
206  header.clear();
207  body.clear();
208  curl_easy_reset( curl_handle );
209  ASCString urlInternal = serverBase + url;
210  curl_easy_setopt( curl_handle, CURLOPT_URL, urlInternal.c_str() );
211  curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, &ASC_PBEM_writeInternal );
212  curl_easy_setopt( curl_handle, CURLOPT_WRITEHEADER, &header );
213  curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, &body );
214 
215  // post parameters
216  // struct curl_httppost* post = NULL;
217  // struct curl_httppost* last = NULL;
218 
219  ASCString encodedRequest; // lifetime considerations require this parameter to be _outside_ the loop
220  if( parameters.size() > 0 )
221  {
222  for( int i=0; i<parameters.size(); i++ )
223  {
224  ASCString name = parameters[ i ];
225  i++;
226  ASCString value = parameters[ i ];
227  if( encodedRequest.length() > 0 ) encodedRequest += "&";
228  encodedRequest += name + "=" + value;
229  }
230  curl_easy_setopt( curl_handle, CURLOPT_POSTFIELDS, encodedRequest.c_str() );
231  }
232 
233  // additional headers; currently only sessionid
234  struct curl_slist *headers=NULL;
235  ASCString cookieString = "cookie: JSESSIONID=" + sessionID + ";"; // lifetime considerations require this parameter to be _outside_ the loop
236  ASCString acceptString = "Accept: text/plain"; // lifetime considerations require this parameter to be _outside_ the loop
237  if( sessionID.length() > 0 )
238  {
239  headers = curl_slist_append( headers, cookieString.c_str() );
240  }
241  headers = curl_slist_append( headers, acceptString.c_str() );
242  curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, headers );
243 
244  // actual perform the request
245  usable = curl_easy_perform( curl_handle ) == 0;
246 
247  // parse header for status code and session id
248  if( usable )
249  {
250  ASCString headerString;
251  for( int i=0; i<header.size(); i++ ) headerString += header[ i ];
252  parseHeader( headerString );
253  }
254 
255  // clean up
256  curl_slist_free_all( headers );
257 
258  return usable;
259 }
260 
261 
262 bool ASC_PBEM::request( ASCString url, ASC_PBEM_FileUploadControl *fileUploadControl )
263 {
264  if( ! usable ) return false;
265 
266  statusCode = -1;
267  header.clear();
268  body.clear();
269  curl_easy_reset( curl_handle );
270  ASCString urlInternal = serverBase + url;
271  curl_easy_setopt( curl_handle, CURLOPT_URL, urlInternal.c_str() );
272  curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, &ASC_PBEM_writeInternal );
273  curl_easy_setopt( curl_handle, CURLOPT_WRITEHEADER, &header );
274  curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, &body );
275 
276  curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, &ASC_PBEM_readInternal);
277  curl_easy_setopt(curl_handle, CURLOPT_INFILE, fileUploadControl);
278  curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, true );
279 
280  // additional headers; sessionid + content type
281  struct curl_slist *headers=NULL;
282  ASCString cookieString = "cookie: JSESSIONID=" + sessionID + ";"; // lifetime considerations require this parameter to be _outside_ the loop
283  ASCString contentString = "content-type: multipart/form-data; boundary=" + fileUploadControl->boundary;
284  ASCString acceptString = "Accept: text/plain"; // lifetime considerations require this parameter to be _outside_ the loop
285  if( sessionID.length() > 0 )
286  {
287  headers = curl_slist_append( headers, cookieString.c_str() );
288  }
289  headers = curl_slist_append( headers, contentString.c_str() );
290  headers = curl_slist_append( headers, acceptString.c_str() );
291  curl_easy_setopt( curl_handle, CURLOPT_HTTPHEADER, headers );
292 
293  // actual perform the request
294  usable = curl_easy_perform( curl_handle ) == 0;
295 
296  // parse header for status code and session id
297  if( usable )
298  {
299  ASCString headerString;
300  for( int i=0; i<header.size(); i++ ) headerString += header[ i ];
301  parseHeader( headerString );
302  }
303 
304  // clean up
305  curl_slist_free_all( headers );
306 
307  return usable;
308 }
309 
310 void ASC_PBEM::parseHeader( ASCString headerString )
311 {
312  int currentIndex = 0;
313  while( currentIndex < headerString.size() )
314  {
315  int lineEnd = headerString.find( '\n', currentIndex );
316  ASCString line;
317  if( lineEnd != ASCString::npos )
318  {
319  line = headerString.substr( currentIndex, lineEnd-currentIndex );
320  parseHeaderLine( line );
321  }else
322  {
323  line = headerString.substr( currentIndex );
324  parseHeaderLine( line );
325  break;
326  }
327  currentIndex = lineEnd + 1;
328  }
329 }
330 
331 void ASC_PBEM::parseHeaderLine( ASCString line )
332 {
333  if( line.find( "HTTP" ) == 0 )
334  {
335  // http status line... grab code
336  int space = line.find( " " );
337  if( space == ASCString::npos ) return;
338  space++;
339  int end = line.find( " ", space );
340  if( end == ASCString::npos ) return;
341 
342  ASCString codeStr = line.substr( space, end-space );
343  statusCode = atoi( codeStr.c_str() );
344  }else if( line.find( "Set-Cookie:" ) == 0 )
345  {
346  if( line.find( "JSESSIONID" ) != ASCString::npos )
347  {
348  ASCString sessionID = line.substr( line.find( "JSESSIONID" ) );
349  if( sessionID.find( "=" ) == ASCString::npos ) return;
350  sessionID = sessionID.substr( sessionID.find( "=" ) + 1 );
351  if( sessionID.find( ";" ) == ASCString::npos ) return;
352  sessionID = sessionID.substr( 0, sessionID.find( ";" ) );
353  this->sessionID = sessionID;
354  }
355  }else if( line.find( "X-Servlet:" ) == 0 )
356  {
357  if( line.find( "ASC_PBEM_Server/" ) != ASCString::npos )
358  {
359  serverVersion = atoi( line.substr( line.find( "ASC_PBEM_Server/" ) + 16 ).c_str() );
360  }else
361  {
362  serverVersion = -1;
363  }
364  }
365 }
366 
368 {
369  // don't check for isLoggedIn() on logout... if for any bug
370  // this flag isn't set, we should still be able to invalidate
371  // this session
372 
373  // whatever happens, clean up the flag
374  loggedIn = false;
375 
376  if( ! request( "tools/logout" ) ) return false;
377 
378  return statusCode == 200;
379 }
380 
381 bool ASC_PBEM::login( ASCString user, ASCString passwd )
382 {
383  if( loggedIn ) return false;
384 
385  std::vector< ASCString > parameters;
386  parameters.push_back( "name" );
387  parameters.push_back( user );
388  parameters.push_back( "passwd" );
389  parameters.push_back( passwd );
390 
391  if( ! request( "tools/login", parameters ) ) return false;
392 
393  loggedIn = statusCode == 202;
394 
395  return loggedIn;
396 }
397 
399 {
400  if( loggedIn ) return false;
401 
402  std::vector< ASCString > parameters;
403  parameters.push_back( "name" );
404  parameters.push_back( user );
405  parameters.push_back( "passwd" );
406  parameters.push_back( passwd );
407  parameters.push_back( "passwd2" );
408  parameters.push_back( passwd );
409  parameters.push_back( "email" );
410  parameters.push_back( email );
411 
412  if( ! request( "tools/create", parameters ) ) return false;
413 
414  return statusCode == 201;
415 }
416 
418 {
419  if( loggedIn ) return false;
420 
421  std::vector< ASCString > parameters;
422  parameters.push_back( "name" );
423  parameters.push_back( user );
424  parameters.push_back( "code" );
425  parameters.push_back( code );
426 
427  if( ! request( "tools/activate", parameters ) ) return false;
428 
429  return statusCode == 202;
430 }
431 
432 bool ASC_PBEM::uploadFile( ASCString fileName, const Uint8* data, const int size, const int gameID )
433 {
434  if( ! loggedIn ) return false;
435 
436 
437  ASC_PBEM_FileUploadControl control( data, size );
438  control.step = 0;
439  control.sent_step = 0;
440  control.fileName = fileName;
441  control.boundary = "--------------------ASCPBEM65834626956";
442  // ??? todo: a better boundary id would be nice... perhapse a random id
443 
444  char gameIDString[ 30 ];
445  sprintf( gameIDString, "%i", gameID );
446 
447  control.parameters.push_back( "gameID" );
448  control.parameters.push_back( gameIDString );
449 
450  if( ! request( "tools/upload", &control ) ) return false;
451 
452  return statusCode == 201;
453 }
454 
455 bool ASC_PBEM::createGame( ASCString fileName, const Uint8* data, const int size, ASCString gameName, ASCString fileNamePattern, char* roles, int* players, int projectID, int turn, int currentSlot )
456 {
457  if( ! loggedIn ) return false;
458 
459 
460  ASC_PBEM_FileUploadControl control( data, size );
461  control.step = 0;
462  control.sent_step = 0;
463  control.fileName = fileName;
464  control.boundary = "--------------------ASCPBEM65834626956";
465  // ??? todo: a better boundary id would be nice... perhapse a random id
466 
467  char projectIDString[ 30 ];
468  sprintf( projectIDString, "%i", projectID );
469 
470  char turnString[ 30 ];
471  sprintf( turnString, "%i", turn );
472 
473  char currentSlotString[ 30 ];
474  sprintf( currentSlotString, "%i", currentSlot );
475 
476  control.parameters.push_back( "projectID" );
477  control.parameters.push_back( projectIDString );
478  control.parameters.push_back( "turn" );
479  control.parameters.push_back( turnString );
480  control.parameters.push_back( "currentSlot" );
481  control.parameters.push_back( currentSlotString );
482  control.parameters.push_back( "gameName" );
483  control.parameters.push_back( gameName );
484  control.parameters.push_back( "pattern" );
485  control.parameters.push_back( fileNamePattern );
486 
487  for( int i=0; i<8; i++ )
488  {
489  char playerString[ 10 ];
490  char playerIDString[ 10 ];
491  sprintf( playerString, "player_%i", i );
492  sprintf( playerIDString, "%i", players[ i ] );
493 
494  char roleString[ 10 ];
495  char roleIDString[ 10 ];
496  sprintf( roleString, "role_%i", i );
497  sprintf( roleIDString, "%c", roles[ i ] );
498 
499  control.parameters.push_back( playerString );
500  control.parameters.push_back( playerIDString );
501  control.parameters.push_back( roleString );
502  control.parameters.push_back( roleIDString );
503  }
504 
505  if( ! request( "tools/newgame", &control ) ) return false;
506 
507  return statusCode == 201;
508 }
509 
510 
511 std::vector<TGameInfo> ASC_PBEM::getCurrentGamesInfo( bool myTurnOnly )
512 {
513  std::vector<TGameInfo> games;
514 
515  if( !loggedIn ) return games;
516 
517  std::vector< ASCString > parameters;
518  parameters.push_back( "myTurnOnly" );
519  if( myTurnOnly )
520  parameters.push_back( "1" );
521  else
522  parameters.push_back( "0" );
523 
524  if( request( "tools/currentGameList", parameters ) )
525  {
526  if( statusCode == 200 )
527  {
528  int currentIndex = 0;
529  ASCString bodyString = getBody();
530  while( currentIndex < bodyString.size() )
531  {
532  int lineEnd = bodyString.find( '\n', currentIndex );
533  ASCString line;
534  if( lineEnd != ASCString::npos )
535  {
536  line = bodyString.substr( currentIndex, lineEnd-currentIndex );
537  games.push_back( parseGameInfoLine( line ) );
538  }else
539  {
540  line = bodyString.substr( currentIndex );
541  games.push_back( parseGameInfoLine( line ) );
542  break;
543  }
544  currentIndex = lineEnd + 1;
545  }
546 
547  }
548  }
549 
550  return games;
551 
552 }
553 
554 
555 std::vector<TUserData> ASC_PBEM::getUserList( bool activeOnly )
556 {
557  std::vector<TUserData> users;
558 
559  if( !loggedIn ) return users;
560 
561  std::vector< ASCString > parameters;
562  parameters.push_back( "activeOnly" );
563  if( activeOnly )
564  parameters.push_back( "1" );
565  else
566  parameters.push_back( "0" );
567 
568  if( request( "tools/userList", parameters ) )
569  {
570  if( statusCode == 200 )
571  {
572  int currentIndex = 0;
573  ASCString bodyString = getBody();
574  while( currentIndex < bodyString.size() )
575  {
576  int lineEnd = bodyString.find( '\n', currentIndex );
577  ASCString line;
578  if( lineEnd != ASCString::npos )
579  {
580  line = bodyString.substr( currentIndex, lineEnd-currentIndex );
581  users.push_back( parseUserInfoLine( line ) );
582  }else
583  {
584  line = bodyString.substr( currentIndex );
585  users.push_back( parseUserInfoLine( line ) );
586  break;
587  }
588  currentIndex = lineEnd + 1;
589  }
590 
591  }
592  }
593 
594  return users;
595 }
596 
597 std::vector<ASCString> ASC_PBEM::listPlayers()
598 {
599  std::vector<ASCString> newList;
600  std::vector<TUserData> list = getUserList();
601 
602  for ( std::vector<TUserData>::iterator i = list.begin(); i != list.end(); ++i )
603  newList.push_back( i->userName );
604 
605  return newList;
606 }
607 
608 
609 
611 {
612  if( ! loggedIn ) return NULL;
613  /*
614  std::vector< ASCString > parameters;
615 
616  parameters.push_back( "name" );
617  parameters.push_back( user );
618  parameters.push_back( "code" );
619  parameters.push_back( code );
620  */
621  char gameIDString[ 30 ];
622  sprintf( gameIDString, "%i", game.gameID );
623  char* fileNameEscaped = curl_escape( game.currentSaveGameName.c_str(), 0 );
624 
625  ASCString requestString = "tools/download/";
626  requestString += fileNameEscaped;
627  requestString += "?gameID=";
628  requestString += gameIDString;
629 
630  if( ! request( requestString ) )
631  {
632  curl_free( fileNameEscaped );
633  return NULL;
634  }
635  curl_free( fileNameEscaped );
636 
637  if( statusCode == 200 )
638  {
639  ASCString bodyString = getBody();
640 
641  TFileData *data = new TFileData;
642  data->fileName = game.currentSaveGameName;
643  data->fileSize = bodyString.size();
644  data->fileData = new Uint8[ bodyString.size() ];
645  ASC_PBEM_writeBuffer( data->fileData, (const Uint8*) bodyString.c_str(), 0, bodyString.size(), bodyString.size() );
646 
647  return data;
648  }
649 
650  return NULL;
651 }
652 
653 
654 
655 TUserData ASC_PBEM::parseUserInfoLine( ASCString line )
656 {
657  TUserData info;
658 
659  info.userID = atoi( line.substr( 0, line.find( '\t' ) ).c_str() );
660  int currentIndex = line.find( '\t' ) + 1;
661  info.userName = line.substr( currentIndex );
662 
663  return info;
664 }
665 
666 TGameInfo ASC_PBEM::parseGameInfoLine( ASCString line )
667 {
668  TGameInfo info;
669 
670  info.gameID = atoi( line.substr( 0, line.find( '\t' ) ).c_str() );
671  int currentIndex = line.find( '\t' ) + 1;
672  info.name = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
673  currentIndex = line.find( '\t', currentIndex ) + 1;
674  info.projectID = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
675  currentIndex = line.find( '\t', currentIndex ) + 1;
676  info.currentSlot = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
677  currentIndex = line.find( '\t', currentIndex ) + 1;
678  info.turn = atoi( line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex ).c_str() );
679  currentIndex = line.find( '\t', currentIndex ) + 1;
680  info.currentSaveGameName = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
681  currentIndex = line.find( '\t', currentIndex ) + 1;
682  info.lastActive = line.substr( currentIndex, line.find( '\t', currentIndex ) - currentIndex );
683  currentIndex = line.find( '\t', currentIndex ) + 1;
684  info.currentPlayerName = line.substr( currentIndex );
685 
686  return info;
687 }
688 
690 {
691  ASCString headerString;
692  for( int i=0; i<header.size(); i++ )
693  headerString += header[ i ];
694  return headerString;
695 }
696 
698 {
699  ASCString bodyString;
700  for( int i=0; i<body.size(); i++ )
701  bodyString += body[ i ];
702  return bodyString;
703 }
704 
706 {
707  return statusCode;
708 }
709 
710 int nmain(int argc, char *argv[])
711 {
712  std::cout << "start" << std::endl;
713  curl_global_init(CURL_GLOBAL_ALL);
714 
715  if( false )
716  {
717  // schnelle übersicht, was man wie machen kann
718  // wichtig: curl_global_init(CURL_GLOBAL_ALL); nicht vergessen ;)
719  // weiter unten sind dann die sachen, die ich zum testen verwendet habe,
720  // ist halt unklarer geschrieben ;)
721 
782  }
783 
784  ASC_PBEM asc_pbem( "http://localhost:8080/ascServer/" );
785 
786  if( asc_pbem.isUsable() )
787  {
788  std::cout << "primary isUsable" << std::endl;
789  /*
790  if( asc_pbem.createAccount( "codeUser", "codePW", "jade@Rhiow.Errant" ) )
791  {
792  std::cout << "create codeUser worked" << std::endl;
793 
794  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
795  std::cout << "body:" << asc_pbem.getBody() << std::endl;
796  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
797 
798  }else
799  {
800  std::cout << "create codeUser failed" << std::endl;
801  }
802  */
803  /*
804  if( asc_pbem.activateAccount( "codeUser", "8670616" ))
805  {
806  std::cout << "activate codeUser worked" << std::endl;
807  }else
808  {
809  std::cout << "activate codeUser failed" << std::endl;
810  }
811  */
812 
813  if( asc_pbem.login( "miau", "aaaa" ) )
814  {
815  std::cout << "login worked" << std::endl;
816  }else
817  {
818  std::cout << "login failed" << std::endl;
819  }
820  /*
821  ASCString uploadData = "a\ns\nd\nmiau\n";
822  int gameID = 3;
823  //std::cout << uploadData.size() << std::endl;
824  if( asc_pbem.uploadFile( "some filename.ascpbm", uploadData.c_str(), uploadData.size(), gameID ) )
825  {
826  std::cout << "uploadFile worked" << std::endl;*/
827  /*
828  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
829  std::cout << "body:" << asc_pbem.getBody() << std::endl;
830  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
831  */
832 
833  /*
834  }else
835  {
836  std::cout << "uploadFile failed" << std::endl;
837 
838  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
839  std::cout << "body:" << asc_pbem.getBody() << std::endl;
840  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
841 
842  }*/
843 
844  std::vector<TGameInfo> allGames = asc_pbem.getCurrentGamesInfo( false );
845  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
846  std::cout << "body:" << asc_pbem.getBody() << std::endl;
847  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
848  std::cout << "allGames.size() = " << allGames.size() << std::endl;
849 
850  for( int i=0; i<allGames.size(); i++ )
851  {
852  TGameInfo game = allGames[ i ];
853  std::cout << "Game " << game.gameID << std::endl;
854  std::cout << "name " << game.name << std::endl;
855  std::cout << "projectID " << game.projectID << std::endl;
856  std::cout << "currentSlot " << game.currentSlot << std::endl;
857  std::cout << "turn " << game.turn << std::endl;
858  std::cout << "currentSaveGameName " << game.currentSaveGameName << std::endl;
859  std::cout << "lastActive " << game.lastActive << std::endl;
860  std::cout << "currentPlayerName " << game.currentPlayerName << std::endl;
861  }
862 
863  /*
864  char roles[ 8 ];
865  int players[ 8 ];
866  for( int i=0; i<8; i++ )
867  {
868  roles[ i ] = asc_pbem.ROLE_PLAYER;
869  players[ i ] = -1;
870  }
871 
872  players[ 0 ] = 11;
873  players[ 1 ] = 11;
874 
875  int projectID = -1;
876  int turn = 1;
877  int currentSlot = 1;
878  ASCString gameName = "evil isle";
879  ASCString fileName = "evilisle-A-1.ascpbm";
880  ASCString fileNamePattern = "evilisle-$p-$t.ascpbm";
881 
882  if( asc_pbem.createGame( fileName, fileName.c_str(), fileName.size(), gameName, fileNamePattern, roles, players, projectID, turn, currentSlot ) )
883  {
884  std::cout << "game created" << std::endl;
885  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
886  std::cout << "body:" << asc_pbem.getBody() << std::endl;
887  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
888  }else
889  {
890  std::cout << "game creation failed" << std::endl;
891  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
892  std::cout << "body:" << asc_pbem.getBody() << std::endl;
893  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
894  }
895  */
896 
897 
898  /*
899  for( int i=0; i<allGames.size(); i++ )
900  {
901  TGameInfo game = allGames[ i ];
902 
903  if( game.gameID == 3 )
904  {
905  TFileData *download = asc_pbem.downloadGame( game );
906 
907  if( download == NULL )
908  {
909  std::cout << "download failed" << std::endl;
910  }else
911  {
912  std::cout << "download worked:" << std::endl;
913  std::cout << download->fileName << std::endl;
914  std::cout << download->fileSize << std::endl;
915  std::cout << download->fileData << std::endl;
916 
917  delete download;
918  }
919  }
920 
921 
922  }
923  */
924 /*
925  std::vector<TUserData> users = asc_pbem.getUserList();
926  std::cout << "users.size() = " << users.size() << std::endl;
927 
928  for( int i=0; i<users.size(); i++ )
929  {
930  TUserData user = users[ i ];
931  std::cout << "userName " << user.userName << std::endl;
932  std::cout << "userID " << user.userID << std::endl;
933  }
934 */
935  if( asc_pbem.logout() )
936  {
937  std::cout << "logout worked" << std::endl;
938  }else
939  {
940  std::cout << "logout failed" << std::endl;
941  }
942  }else
943  {
944  std::cout << "primary NOT isUsable" << std::endl;
945 
946  std::cout << "header:" << asc_pbem.getHeader() << std::endl;
947  std::cout << "body:" << asc_pbem.getBody() << std::endl;
948  std::cout << "status:" << asc_pbem.getStatusCode() << std::endl;
949  }
950  return 0;
951 }
952 
bool createAccount(ASCString user, ASCString passwd, ASCString email)
bool login(ASCString user, ASCString passwd)
bool createGame(ASCString fileName, const Uint8 *data, const int size, ASCString gameName, ASCString fileNamePattern, char *roles, int *players, int projectID=-1, int turn=1, int currentSlot=1)
ASCString currentPlayerName
ASCString currentSaveGameName
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
bool uploadFile(ASCString fileName, const Uint8 *data, const int size, const int gameID)
size_t ASC_PBEM_readInternal(Uint8 *outboundBuffer, size_t size, size_t nitems, void *infoStruct)
std::vector< ASCString > listPlayers()
int nmain(int argc, char *argv[])
bool activateAccount(ASCString user, ASCString code)
std::vector< TGameInfo > getCurrentGamesInfo(bool myTurnOnly)
size_t ASC_PBEM_writeInternal(void *inboundData, size_t size, size_t nmemb, void *vector)
file: ASC_PBEM.cpp author: Jade Rogalski license: GPL warranty: none
file: ASC_PBEM.h author: Jade Rogalski license: GPL warranty: none
std::vector< TUserData > getUserList(bool activeOnly=true)
TFileData * downloadGame(TGameInfo game)
int atoi(const std::string &s)
Definition: misc.cpp:152
std::vector< ASCString > parameters
ASC_PBEM(ASCString serverBase)
default constructor:
int ASC_PBEM_writeBuffer(Uint8 *buffer, const Uint8 *source, int startPos, int bufferSize, int sourceSize)
void line(int x1, int y1, int x2, int y2, Uint8 actcol)
draws a simple line on the screen. Not very fast...
Definition: basegfx.cpp:181
bool isUsable()
important: check this first after instantiation this class will be false after any ...