deleting pfiles

Posted by Orik on Wed 13 Feb 2008 11:06 PM — 8 posts, 29,382 views.

USA #0
Hello,

when a player deletes, the lua file is not deleted. What do I need to do in order to delete the associated lua file?
Australia Forum Administrator #1
Well, in the player directories is the additional .lua file, for example:


Admin Admin.lua


In the do_destroy function in act_wiz.c you would add a bit of code to delete (or, probably, rename) that as well.
USA #2

void do_destroy( CHAR_DATA * ch, char *argument )
{
   CHAR_DATA *victim;
   char buf[MAX_STRING_LENGTH];
   char buf2[MAX_STRING_LENGTH];
   char arg[MAX_INPUT_LENGTH];
   char *name;
   struct stat fst;

   set_char_color( AT_RED, ch );

   one_argument( argument, arg );
   if( arg[0] == '\0' )
   {
      send_to_char( "Destroy what player file?\r\n", ch );
      return;
   }

   /*
    * Set the file points.
    */
   name = capitalize( arg );
   snprintf( buf, MAX_STRING_LENGTH, "%s%c/%s", PLAYER_DIR, tolower( arg[0] ), name );
   snprintf( buf2, MAX_STRING_LENGTH, "%s%c/%s", BACKUP_DIR, tolower( arg[0] ), name );

   /*
    * This check makes sure the name is valid and that the file is there, else there
    * is no need to go on. -Orion
    */
   if( !check_parse_name( name, TRUE ) || lstat( buf, &fst ) == -1 )
   {
      ch_printf( ch, "No player exists by the name %s.\r\n", name );
      return;
   }

   for( victim = first_char; victim; victim = victim->next )
      if( !IS_NPC( victim ) && !str_cmp( victim->name, arg ) )
        break;

   if( !victim )
   {
      DESCRIPTOR_DATA *d;

      /*
       * Make sure they aren't halfway logged in.
       */
      for( d = first_descriptor; d; d = d->next )
         if( ( victim = d->character ) && !IS_NPC( victim ) && !str_cmp( victim->name, arg ) )
            break;
      if( d )
         close_socket( d, TRUE );
   }
   else
   {
      int x, y;

      quitting_char = victim;
      save_char_obj( victim );
      saving_char = NULL;
      extract_char( victim, TRUE );
      for( x = 0; x < MAX_WEAR; x++ )
         for( y = 0; y < MAX_LAYERS; y++ )
            save_equipment[x][y] = NULL;
   }

   
  if( !rename( buf, buf2 ) )
   {
      AREA_DATA *pArea;

      set_char_color( AT_RED, ch );
      ch_printf( ch, "Player %s destroyed.  Pfile saved in backup directory.\r\n", name );
      snprintf( buf, MAX_STRING_LENGTH, "%s%s", GOD_DIR, name );
      if( !remove( buf ) )
         send_to_char( "Player's immortal data destroyed.\r\n", ch );
      else if( errno != ENOENT )
      {
         ch_printf( ch, "Unknown error #%d - %s (immortal data). Report to www.smaugfuss.org\r\n", errno,
                    strerror( errno ) );
         snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s", ch->name, buf );
         perror( buf2 );
      }

      snprintf( buf2, MAX_STRING_LENGTH, "%s.are", name );
      for( pArea = first_build; pArea; pArea = pArea->next )
         if( !str_cmp( pArea->filename, buf2 ) )
         {
            snprintf( buf, MAX_STRING_LENGTH, "%s%s", BUILD_DIR, buf2 );
            if( IS_SET( pArea->status, AREA_LOADED ) )
               fold_area( pArea, buf, FALSE );
            close_area( pArea );
            snprintf( buf2, MAX_STRING_LENGTH, "%s.bak", buf );
            set_char_color( AT_RED, ch ); /* Log message changes colors */
            if( !rename( buf, buf2 ) )
               send_to_char( "Player's area data destroyed.  Area saved as backup.\r\n", ch );
            else if( errno != ENOENT )
            {
               ch_printf( ch, "Unknown error #%d - %s (area data). Report to www.smaugfuss.org\r\n", errno,
                          strerror( errno ) );
               snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s", ch->name, buf );
               perror( buf2 );
            }
            break;
         }
   }
   else if( errno == ENOENT )
   {
      set_char_color( AT_PLAIN, ch );
      send_to_char( "Player does not exist.\r\n", ch );
   }
   else
   {
      set_char_color( AT_WHITE, ch );
      ch_printf( ch, "Unknown error #%d - %s. Report to www.smaugfuss.org\r\n", errno, strerror( errno ) );
      snprintf( buf, MAX_STRING_LENGTH, "%s destroying %s", ch->name, arg );
      perror( buf );
   }
   return;
}


Not seeing where it exactly deletes the pfile.
Australia Forum Administrator #3

 if( !rename( buf, buf2 ) )


It is renaming it from PLAYER_DIR to BACKUP_DIR (see the snprintf further up), so there is still a copy in case you do it by mistake.

So you could probably repeat the operation with the player name with .lua at the end of it.
USA #4
so:


snprintf( buf3, MAX_STRING_LENGTH, "%s%c/%s.lua", PLAYER_DIR, tolower( arg[0] ), name );


then:

if( !rename( buf3, buf2 ) )
   {
      AREA_DATA *pArea;

      set_char_color( AT_RED, ch );
      ch_printf( ch, "Player %s.lua destroyed.  Lua Pfile saved in backup directory.\r\n", name );
      snprintf( buf, MAX_STRING_LENGTH, "%s%s", GOD_DIR, name );
      if( !remove( buf3 ) )
         send_to_char( "Player's immortal data destroyed.\r\n", ch );
      else if( errno != ENOENT )
      {
         ch_printf( ch, "Unknown error #%d - %s (immortal data). Report to www.smaugfuss.org\r\n", errno,
                    strerror( errno ) );
         snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s.lua", ch->name, buf3 );
         perror( buf2 );
      }

      snprintf( buf2, MAX_STRING_LENGTH, "%s.are", name );
      for( pArea = first_build; pArea; pArea = pArea->next )
         if( !str_cmp( pArea->filename, buf2 ) )
         {
            snprintf( buf, MAX_STRING_LENGTH, "%s%s", BUILD_DIR, buf2 );
            if( IS_SET( pArea->status, AREA_LOADED ) )
               fold_area( pArea, buf3, FALSE );
            close_area( pArea );
            snprintf( buf2, MAX_STRING_LENGTH, "%s.bak", buf );
            set_char_color( AT_RED, ch ); /* Log message changes colors */
            if( !rename( buf, buf2 ) )
               send_to_char( "Player's area data destroyed.  Area saved as backup.\r\n", ch );
            else if( errno != ENOENT )
            {
               ch_printf( ch, "Unknown error #%d - %s (area data). Report to www.smaugfuss.org\r\n", errno,
                          strerror( errno ) );
               snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s", ch->name, buf3 );
               perror( buf2 );
            }
            break;
         }
   }
   else if( errno == ENOENT )
   {
      set_char_color( AT_PLAIN, ch );
      send_to_char( "Player does not exist.\r\n", ch );
   }
   else
   {
      set_char_color( AT_WHITE, ch );
      ch_printf( ch, "Unknown error #%d - %s. Report to www.smaugfuss.org\r\n", errno, strerror( errno ) );
      snprintf( buf3, MAX_STRING_LENGTH, "%s destroying %s", ch->name, arg );
      perror( buf );
   }


Would that work?
Australia Forum Administrator #5
Well not exactly, that would rename the .lua file but leave the original player file there. You need to do 2 renames.
USA #6
Well, that's what I meant. I'd have the first rename with the first buf, then have a second rename with the third buf. I could probably take out the imm stuff and area stuff as well to make it smaller.
USA #7
This is what I came up with. Feel free to use it. It deletes the lua file in the player_dir and places it in the backup_dir.


void do_destroy( CHAR_DATA * ch, char *argument )
{
   CHAR_DATA *victim;
   char buf[MAX_STRING_LENGTH];
   char buf2[MAX_STRING_LENGTH];
   
   /* Deleting Lua Files and placing them in backup Dir - Rao 2/14/08*/
   char buf3[MAX_STRING_LENGTH];
   char buf4[MAX_STRING_LENGTH];
   /******************************************************************/

   char arg[MAX_INPUT_LENGTH];
   char *name;
   struct stat fst;

   set_char_color( AT_RED, ch );

   one_argument( argument, arg );
   if( arg[0] == '\0' )
   {
      send_to_char( "Destroy what player file?\r\n", ch );
      return;
   }

   /*
    * Set the file points.
    */
   name = capitalize( arg );
   snprintf( buf, MAX_STRING_LENGTH, "%s%c/%s", PLAYER_DIR, tolower( arg[0] ), name );
   snprintf( buf2, MAX_STRING_LENGTH, "%s%c/%s", BACKUP_DIR, tolower( arg[0] ), name );

   /* Deleting Lua Files and placing them in backup Dir - Rao 2/14/08*/
   snprintf( buf3, MAX_STRING_LENGTH, "%s%c/%s.lua", PLAYER_DIR, tolower( arg[0] ), name );
   snprintf( buf4, MAX_STRING_LENGTH, "%s%c/%s.lua", BACKUP_DIR, tolower( arg[0] ), name );
   /******************************************************************/

   /*
    * This check makes sure the name is valid and that the file is there, else there
    * is no need to go on. -Orion
    */
   if( !check_parse_name( name, TRUE ) || lstat( buf, &fst ) == -1 )
   {
      ch_printf( ch, "No player exists by the name %s.\r\n", name );
      return;
   }

   for( victim = first_char; victim; victim = victim->next )
      if( !IS_NPC( victim ) && !str_cmp( victim->name, arg ) )
         break;

   if( !victim )
   {
      DESCRIPTOR_DATA *d;

      /*
       * Make sure they aren't halfway logged in. 
       */
      for( d = first_descriptor; d; d = d->next )
         if( ( victim = d->character ) && !IS_NPC( victim ) && !str_cmp( victim->name, arg ) )
            break;
      if( d )
         close_socket( d, TRUE );
   }
   else
   {
      int x, y;

      quitting_char = victim;
      save_char_obj( victim );
      saving_char = NULL;
      extract_char( victim, TRUE );
      for( x = 0; x < MAX_WEAR; x++ )
         for( y = 0; y < MAX_LAYERS; y++ )
            save_equipment[x][y] = NULL;
   }

   if( !rename( buf, buf2 ) )
   {
      AREA_DATA *pArea;

      set_char_color( AT_RED, ch );
      ch_printf( ch, "Player %s destroyed.  Pfile saved in backup directory.\r\n", name );
      snprintf( buf, MAX_STRING_LENGTH, "%s%s", GOD_DIR, name );
      if( !remove( buf ) )
         send_to_char( "Player's immortal data destroyed.\r\n", ch );
      else if( errno != ENOENT )
      {
         ch_printf( ch, "Unknown error #%d - %s (immortal data). Report to www.legendsofold.com\r\n", errno,
                    strerror( errno ) );
         snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s", ch->name, buf );
         perror( buf2 );
      }

      snprintf( buf2, MAX_STRING_LENGTH, "%s.are", name );
      for( pArea = first_build; pArea; pArea = pArea->next )
         if( !str_cmp( pArea->filename, buf2 ) )
         {
            snprintf( buf, MAX_STRING_LENGTH, "%s%s", BUILD_DIR, buf2 );
            if( IS_SET( pArea->status, AREA_LOADED ) )
               fold_area( pArea, buf, FALSE );
            close_area( pArea );
            snprintf( buf2, MAX_STRING_LENGTH, "%s.bak", buf );
            set_char_color( AT_RED, ch ); /* Log message changes colors */
            if( !rename( buf, buf2 ) )
               send_to_char( "Player's area data destroyed.  Area saved as backup.\r\n", ch );
            else if( errno != ENOENT )
            {
               ch_printf( ch, "Unknown error #%d - %s (area data). Report to www.legendsofold.com\r\n", errno,
                          strerror( errno ) );
               snprintf( buf2, MAX_STRING_LENGTH, "%s destroying %s", ch->name, buf );
               perror( buf2 );
            }
            break;
         }
   }
   else if( errno == ENOENT )
   {
      set_char_color( AT_PLAIN, ch );
      send_to_char( "Player does not exist.\r\n", ch );
   }
   else
   {
      set_char_color( AT_WHITE, ch );
      ch_printf( ch, "Unknown error #%d - %s. Report to www.legendsofold.com\r\n", errno, strerror( errno ) );
      snprintf( buf, MAX_STRING_LENGTH, "%s destroying %s", ch->name, arg );
      perror( buf );
   }

   /* Deleting Lua Files and placing them in backup Dir - Rao 2/14/08*/
   if( !rename( buf3, buf4 ) )
   {

      set_char_color( AT_RED, ch );
      ch_printf( ch, "LUA: Player %s Lua file destroyed.  Lua Pfile saved in backup directory.\r\n", name );
   }
   else if( errno == ENOENT )
   {
      set_char_color( AT_PLAIN, ch );
      send_to_char( "LUA: Player does not exist.\r\n", ch );
   }
   else
   {
      set_char_color( AT_WHITE, ch );
      ch_printf( ch, "LUA: Unknown error #%d - %s. Report to www.legendsofold.com\r\n", errno, strerror( errno ) );
      snprintf( buf3, MAX_STRING_LENGTH, "LUA: %s destroying %s", ch->name, arg );
      perror( buf3 );
   }
   /******************************************************************/

   return;
}