Example #1
0
int CMD_check( int argc, char **argv, int kind ) {
  int result;
  char *logName;
  envelope *anEnvelope;
  char* forecastName;
  char* returnAddress;
  char* raceName;
  char* password;
  char* final_orders;
  game* aGame;
  FILE* forecast;
  player* aPlayer;
  
  int resNumber, theTurnNumber;
  
  result = FALSE;
  
  logName = createString( "%s/log/orders_processed.txt", galaxynghome );
  openLog( logName, "a" );
  free( logName );
  
  plogtime( LBRIEF );
  if ( argc >= 2 ) {
    anEnvelope = createEnvelope(  );
    returnAddress = getReturnAddress( stdin );
    raceName = NULL;
    password = NULL;
    final_orders = NULL;
    aGame = NULL;
    resNumber = areValidOrders( stdin, &aGame, &raceName,
				&password, &final_orders, &theTurnNumber );
    plog( LBRIEF, "game %s\n", aGame->name );
    
    setHeader(anEnvelope, MAILHEADER_FROM, "%s", aGame->serverOptions.SERVERemail);
    
    setHeader( anEnvelope, MAILHEADER_TO, "%s", returnAddress );
    setHeader(anEnvelope, MAILHEADER_REPLYTO, aGame->serverOptions.ReplyTo);
    anEnvelope->from_name = strdup(aGame->serverOptions.SERVERname);
    anEnvelope->from_address = strdup(aGame->serverOptions.SERVERemail);

    if ( resNumber == RES_OK) {
      aPlayer = findElement( player, aGame->players, raceName );
      aPlayer->orders = NULL;
      
      plog( LBRIEF, "Orders from %s\n", returnAddress );
      
      /* produce an XML forecast */
      if ( aPlayer->flags & F_XMLREPORT ) {
	if ( ( theTurnNumber == LG_CURRENT_TURN ) ||
	     ( theTurnNumber == ( aGame->turn ) + 1 ) ) {
	  forecastName = createString( "%s/NG_XML_%d_forecast",
				       tempdir, getpid(  ) );
	  copyOrders( aGame, stdin, raceName, password, final_orders,
		      aGame->turn + 1 );
	  if ( ( forecast =
		 GOS_fopen( forecastName, "w" ) ) == NULL ) {
	    plog( LBRIEF, "Could not open %s for forecasting\n",
		  forecastName );
	    fprintf( stderr,
		     "Could not open %s for forecasting\n",
		     forecastName );
	    return EXIT_FAILURE;
	  }
	  
	  if (final_orders)
	    setHeader( anEnvelope, MAILHEADER_SUBJECT,
		       "[GNG] %s turn %d XML finalorders forecast for %s",
		       aGame->name, ( aGame->turn ) + 1, raceName );
	  else
	    setHeader( anEnvelope, MAILHEADER_SUBJECT,
		       "[GNG] %s turn %d XML forecast for %s",
		       aGame->name, ( aGame->turn ) + 1, raceName );
	  
	  checkOrders( aGame, raceName, forecast, F_XMLREPORT );
	  
	  fclose( forecast );
	  if ( kind == CMD_CHECK_REAL ) {
	    plog( LBRIEF, "mailing XML report %s to %s\n",
		  forecastName, anEnvelope->to );
	    fprintf( stderr, "mailing XML report %s to %s\n",
		     forecastName, anEnvelope->to );
	    result |= eMail( aGame, anEnvelope, forecastName );
	  } else {
	    char *forecastFile;
	    forecastFile =
	      createString( "%s/forecasts/%s/%s_XML",
			    galaxynghome, argv[2],
			    returnAddress );
	    GOS_copy( forecastName, forecastFile );
	  }
	  result |= GOS_delete( forecastName );
	  free( forecastName );
	}
      }
      
      /* produce a text forecast */
      if ( aPlayer->flags & F_TXTREPORT ) {
	if ( ( theTurnNumber == LG_CURRENT_TURN ) ||
	     ( theTurnNumber == ( aGame->turn ) + 1 ) ) {
	  forecastName = createString( "%s/NG_TXT_%d_forecast",
				       tempdir, getpid(  ) );
	  if ( ( forecast =
		 GOS_fopen( forecastName, "w" ) ) == NULL ) {
	    plog( LBRIEF, "Could not open %s for forecasting\n",
		  forecastName );
	    return EXIT_FAILURE;
	  }
	  
	  if ( aPlayer->orders == NULL )
	    copyOrders( aGame, stdin, raceName, password, final_orders,
			aGame->turn + 1 );
	  
	  if (final_orders)
	    setHeader( anEnvelope, MAILHEADER_SUBJECT,
		       "[GNG] %s turn %d text finalorders forecast for %s",
		       aGame->name, ( aGame->turn ) + 1, raceName );
	  else 
	    setHeader( anEnvelope, MAILHEADER_SUBJECT,
		       "[GNG] %s turn %d text forecast for %s",
		       aGame->name, ( aGame->turn ) + 1, raceName );
	  
	  checkOrders( aGame, raceName, forecast, F_TXTREPORT );
	  
	  fclose( forecast );
	  
	  if ( kind == CMD_CHECK_REAL ) {
	    plog( LBRIEF, "mailing TXT report %s to %s\n",
		  forecastName, anEnvelope->to );
	    fprintf( stderr, "mailing TXT report %s to %s\n",
		     forecastName, anEnvelope->to );
	    
	    result |= eMail( aGame, anEnvelope, forecastName );
	  } else {
	    char *forecastFile;
	    forecastFile =
	      createString( "%s/forecasts/%s/%s_TXT",
			    galaxynghome, argv[2],
			    returnAddress );
	    GOS_copy( forecastName, forecastFile );
	  }
	  result |= GOS_delete( forecastName );
	  free( forecastName );
	}
      }
      
    } else {
      forecastName = createString( "%s/NG_TXT_%d_errors",
				   tempdir, getpid(  ) );
      forecast = GOS_fopen( forecastName, "w" );
      setHeader( anEnvelope, MAILHEADER_SUBJECT,
		 "[GNG] Major Trouble" );
      plog( LBRIEF, "Major Trouble %d\n", resNumber );
      
      generateErrorMessage( resNumber, aGame, raceName,
			    theTurnNumber, forecast );
      fclose( forecast );
      
      if ( kind == CMD_CHECK_REAL ) {
	plog( LBRIEF, "mailing error report %s to %s\n", forecastName,
	      anEnvelope->to );
	
	result |= eMail( aGame, anEnvelope, forecastName );
      } else {
	char *forecastFile;
	forecastFile =
	  createString( "%s/forecasts/%s/%s_ERR",
			galaxynghome, argv[2], returnAddress );
	GOS_copy( forecastName, forecastFile );
			}
      result |= GOS_delete( forecastName );
      free( forecastName );
    }
    
    /* code here for advanced orders, we need to see how to determine this */
    if ( theTurnNumber > (aGame->turn+1)) {
      
      if ( aPlayer->orders == NULL )
	copyOrders( aGame, stdin, raceName, password, final_orders,
		    theTurnNumber );
      
      if (final_orders) {
	setHeader( anEnvelope, MAILHEADER_SUBJECT,
		   "[GNG] %s turn %d advance finalorders received for %s.",
		   aGame->name, theTurnNumber, raceName );
	plog( LBRIEF, "%s turn %d advance finalorders received for %s.\n",
	      aGame->name, theTurnNumber, raceName );
      }
      else {
	setHeader( anEnvelope, MAILHEADER_SUBJECT,
		   "[GNG] %s turn %d advance orders received for %s.",
		   aGame->name, theTurnNumber, raceName );
	plog( LBRIEF, "%s turn %d advance orders received for %s.\n",
	      aGame->name, theTurnNumber, raceName );
      }
      
      if ( aPlayer->flags & F_XMLREPORT ) {
	forecastName = createString( "%s/NG_XML_forecast", tempdir );
	forecast = GOS_fopen( forecastName, "w" );
	
	fprintf( forecast,
		 "<galaxy>\n  <variant>GalaxyNG</variant>\n" );
	fprintf( forecast, "  <version>%d.%d.%d</version>\n",
		 GNG_MAJOR, GNG_MINOR, GNG_RELEASE );
	fprintf( forecast, "  <game name=\"%s\">\n", aGame->name );
	fprintf( forecast, "    <turn num=\"%d\">\n", theTurnNumber );
	fprintf( forecast, "      <race name=\"%s\">\n", raceName );
	fprintf( forecast, "        <message>\n" );
	fprintf( forecast, "          <line num=\"1\">"
		 "O wise leader, your orders for turn %d</line>",
		 theTurnNumber );
	fprintf( forecast, "          <line num=\"2\">"
		 "have been received and stored.</line>" );
	fprintf( forecast, "        </message>\n" );
	fprintf( forecast, "      </race>\n" );
	fprintf( forecast, "    </turn>\n" );
	fprintf( forecast, "  </game>\n" );
	fprintf( forecast, "</galaxy>\n" );
	fclose( forecast );
	if ( kind == CMD_CHECK_REAL ) {
	  result |= eMail( aGame, anEnvelope, forecastName );
	} else {
	  char *forecastFile;
	  
	  forecastFile =
	    createString( "%s/forecasts/%s/%s_XML",
			  galaxynghome, argv[2], returnAddress );
	  GOS_copy( forecastName, forecastFile );
	}
	result |= GOS_delete( forecastName );
	free( forecastName );
      }
      
      if ( aPlayer->flags & F_TXTREPORT ) {
	if ( aPlayer->orders == NULL )
	  copyOrders( aGame, stdin, raceName, password, final_orders,
		      theTurnNumber );
	forecastName = createString( "%s/NG_TXT_forecast", tempdir );
	forecast = GOS_fopen( forecastName, "w" );
	fprintf( forecast, "O wise leader your orders for turn %d "
		 "have been received and stored.\n", theTurnNumber );
	fclose( forecast );
	if ( kind == CMD_CHECK_REAL ) {
	  result |= eMail( aGame, anEnvelope, forecastName );
	} else {
	  char *forecastFile;
	  
	  forecastFile =
	    createString( "%s/forecasts/%s/%s_TXT",
			  galaxynghome, argv[2], returnAddress );
	  GOS_copy( forecastName, forecastFile );
	}
	
	result |= GOS_delete( forecastName );
	free( forecastName );
      }
    }
  }
  
  if ( raceName )
    free( raceName );
  if ( password )
    free( password );
  destroyEnvelope( anEnvelope );
  result = ( result ) ? EXIT_FAILURE : EXIT_SUCCESS;
  
  return result;
}
 void WaveletAnalysis::createEnvelopes()
 {
     for(auto i = 0; i < numFilters-2; i++)
         createEnvelope(i);
 }
Example #3
0
int CMD_check( int argc, char **argv, int kind ) {
  char *logName;
  int result = FALSE;
  
  logName = createString( "%s/log/orders_processed.txt", galaxynghome );
  openLog( logName, "a" );
  free( logName );
  
  plogtime( LBRIEF );
  if ( argc < 2 ) {
    result = TRUE;
  }
  else {
    envelope *anEnvelope = createEnvelope(  );
    char* returnAddress = getReturnAddress( stdin );
    int   theTurnNumber = getTurnNumber( stdin );
    char* raceName = NULL;
    char* password = NULL;
    char* final_orders = NULL;
    game *aGame = NULL;
    int resNumber = areValidOrders( stdin, &aGame, &raceName,
				    &password, &final_orders, &theTurnNumber );
    plog( LBRIEF, "game %s\n", aGame->name );

    setHeader(anEnvelope, MAILHEADER_FROM, "%s", aGame->serverOptions.SERVERemail);
    
    setHeader( anEnvelope, MAILHEADER_TO, "%s", returnAddress );
    setHeader(anEnvelope, MAILHEADER_REPLYTO, aGame->serverOptions.ReplyTo);
    anEnvelope->from_address = strdup(aGame->serverOptions.SERVERemail);
    anEnvelope->from_name = strdup(aGame->serverOptions.SERVERname);

    if ( resNumber == RES_OK ) {
      player *aPlayer = findElement( player, aGame->players, raceName );
      assert( aPlayer);
      aPlayer->orders = NULL;
      plog( LBRIEF, "Orders from %s\n", returnAddress );
      
      if ( ( theTurnNumber == LG_CURRENT_TURN ) ||
	   ( theTurnNumber == ( aGame->turn ) + 1 ) ) {
	/* They are orders for the coming turn, copy them. */
	copyOrders( aGame, stdin, raceName, password, aGame->turn + 1 );
	/* Check them */
	checkOrders( aGame, raceName );
	/* Now mail the result */
	if ( aPlayer->flags & F_XMLREPORT ) {
	  result = mail_Forecast( aGame, aPlayer, anEnvelope, raceName,
				  kind, REP_XML );
	}
	if ( aPlayer->flags & F_TXTREPORT ) {
	  result = mail_Forecast( aGame, aPlayer, anEnvelope, raceName,
				  kind, REP_TXT );
	}
      } else if ( theTurnNumber > ( aGame->turn ) + 1 ) {
	/* They are advance orders */
	copyOrders( aGame, stdin, raceName, password, theTurnNumber );
	setHeader( anEnvelope, MAILHEADER_SUBJECT,
		   "[GNG] %s turn %d advance orders received for %s.",
		   aGame->name, theTurnNumber, raceName );
	plog( LBRIEF, "%s turn %d advance orders received for %s.\n",
	      aGame->name, theTurnNumber, raceName );
	if ( aPlayer->flags & F_XMLREPORT ) {
	  result = mail_AdvanceReport( aGame, aPlayer, anEnvelope, raceName,
				       kind, REP_XML );
	}
	if ( aPlayer->flags & F_TXTREPORT ) {
	  result = mail_AdvanceReport( aGame, aPlayer, anEnvelope, raceName,
				       kind, REP_TXT );
	}
      }
      else {
	/* Orders for a turn that already ran. 
	 * Should be handled by areValidOrders() 
	 */
	assert( 0 );
      }
    }
    else {
      /* Some major error */
      result |= mail_TXT_Error( aGame, anEnvelope, 
				raceName, kind, resNumber, theTurnNumber );
    }
  }
  
  result = ( result ) ? EXIT_FAILURE : EXIT_SUCCESS;
  return result;
}
Example #4
0
int CMD_ordersdue(int argc, char** argv) {
  FILE* gmnote;
  FILE* mof_fp;
  
  char* gmbody;
  
  game* aGame;
  player* aplayer;
  envelope* env;
  char* missing_orders_file = NULL;
  char* orders_dir;
  char* orders_file;
  int result;
  int msg_count = 0;
  
  result = EXIT_FAILURE;
  
  if (argc < 3) {
    usage();
  }
  else if ((aGame = loadgame(argv[2], LG_CURRENT_TURN)) != NULL) {
    loadNGConfig( aGame );
    gmbody = createString("%s/orders_due_%s", tempdir, aGame->name);
    gmnote = fopen(gmbody, "w");
    
    orders_dir = createString("%s/orders/%s/", galaxynghome, aGame->name);
    for (aplayer = aGame->players; aplayer; aplayer = aplayer->next) {
      if (aplayer->flags & F_DEAD)
	continue;
      
      orders_file = createString("%s/%s.%d", orders_dir,
				 aplayer->name, aGame->turn+1);
      if (access(orders_file, R_OK) == -1) {
	free(orders_file);
	orders_file = createString("%s/%s_final.%d", orders_dir,
				   aplayer->name, aGame->turn+1);
	if (access(orders_file, R_OK) == -1) {
	  env = createEnvelope();
	  env->to = strdup(aplayer->addr);
	  env->from = strdup(aGame->serverOptions.SERVERemail);
	  env->subject = createString("Turn %d of %s is about to run",
				      aGame->turn+1, argv[2]);
	  setHeader(env, MAILHEADER_REPLYTO,
		    aGame->serverOptions.ReplyTo);
	  env->from_name = strdup(aGame->serverOptions.SERVERname);
	  env->from_address = strdup(aGame->serverOptions.SERVERemail);

	  if (msg_count == 0) {
	    fprintf(gmnote, "The following players have not yet "
		    "submitted orders for turn %d of %s\n",
		    aGame->turn+1, aGame->name);
	    
	    missing_orders_file =
	      createString("%s/data/%s/missing_orders.%d",
			   galaxynghome, aGame->name,
			   aGame->turn+1);
	    mof_fp = fopen(missing_orders_file, "w");
	    fprintf(mof_fp,
		    "Your orders for turn %d for %s have not been "
		    "received.\nOrders are due in %s hours. "
		    "Please send them now.\n"
		    "If you don't plan on sending in orders, please submit a\n"
		    "Quit order (\"Q <racename>\") as it will stop these\n"
		    "annoying emails to you and could help speed up the game\n"
		    "for the other players. Thank you.\n",
		    aGame->turn+1, aGame->name,
		    aGame->serverOptions.due);
	    fclose(mof_fp);
	  }
	  fprintf(gmnote, "%s has not turned in orders.\n",
		  aplayer->name);
	  result |= eMail(aGame, env, missing_orders_file);
	  destroyEnvelope(env);
	  msg_count++;
	}
	free(orders_file);
      }
    }
    free(orders_dir);
  }
  else {
    fprintf(stderr, "Cannot open game %s\n", argv[2]);
  }
  
  if (missing_orders_file) {
    unlink(missing_orders_file);
    free(missing_orders_file);
  }
  
  if (msg_count) {
    fclose(gmnote);
    env = createEnvelope();
    env->to = strdup(aGame->serverOptions.GMemail);
    env->from = strdup(aGame->serverOptions.SERVERemail);
    env->subject = createString("Turn %d of %s is about to run",
				aGame->turn+1, aGame->name);
    setHeader(env, MAILHEADER_REPLYTO, aGame->serverOptions.ReplyTo);
    env->from_name = strdup(aGame->serverOptions.SERVERname);
    env->from_address = strdup(aGame->serverOptions.SERVERemail);

    result |= eMail(aGame, env, gmbody);
    
    destroyEnvelope(env);
  }
  
  return result;
}