#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* main header file */
#include "mud.h"
#include "mysql.h"
void save_pfile ( D_MOBILE *dMob );
void save_player(D_MOBILE *dMob)
{
if (!dMob)
return;
save_pfile(dMob); /* saves the actual player data */
MySQL_save_profile(dMob); /* saves the players profile */
}
void save_pfile(D_MOBILE *dMob)
{
char pName[20];
char pfile[256];
FILE *fp;
int size, i;
pName[0] = toupper(dMob->name[0]);
size = strlen(dMob->name);
for (i = 1; i < size; i++)
pName[i] = tolower(dMob->name[i]);
pName[i] = '\0';
/* open the pfile so we can write to it */
sprintf(pfile, "../players/%s.pfile", pName);
if ((fp = fopen(pfile, "w")) == NULL)
{
bug("Unable to write to %s's pfile", dMob->name);
return;
}
/* dump the players data into the file */
fprintf(fp, "Name %s~\n", dMob->name);
fprintf(fp, "ALevel %d\n", dMob->admin_level);
fprintf(fp, "Password %s~\n", dMob->password);
fprintf(fp, "LastNote %d\n", dMob->last_note);
/* terminate the file */
fprintf(fp, "%s\n", FILE_TERMINATOR);
fclose(fp);
}
D_MOBILE *load_player(char *player)
{
FILE *fp;
D_MOBILE *dMob = NULL;
char pfile[256];
char pName[20];
char *word;
bool done = FALSE, found;
int i, size;
pName[0] = toupper(player[0]);
size = strlen(player);
for (i = 1; i < size; i++)
pName[i] = tolower(player[i]);
pName[i] = '\0';
/* open the pfile so we can write to it */
sprintf(pfile, "../players/%s.pfile", pName);
if ((fp = fopen(pfile, "r")) == NULL)
return NULL;
/* create new mobile data */
if (StackSize(dmobile_free) <= 0)
{
if ((dMob = malloc(sizeof(*dMob))) == NULL)
{
bug("Load_player: Cannot allocate memory.");
abort();
}
}
else
{
dMob = (D_MOBILE *) PopStack(dmobile_free);
}
clear_mobile(dMob);
/* load data */
word = fread_word(fp);
while (!done)
{
found = FALSE;
switch (word[0])
{
case 'A':
IREAD( "ALevel", dMob->admin_level);
break;
case 'E':
if (!strcasecmp(word, "EOF")) {done = TRUE; found = TRUE; break;}
break;
case 'L':
IREAD( "LastNote", dMob->last_note);
break;
case 'N':
SREAD( "Name", dMob->name );
break;
case 'P':
SREAD( "Password", dMob->password );
break;
}
if (!found)
{
bug("Load_player: unexpected '%s' in %s's pfile.", word, player);
free_mobile(dMob);
return NULL;
}
/* read one more */
if (!done) word = fread_word(fp);
}
fclose(fp);
return dMob;
}
/*
* MySQL_create_profile()
*
* Inserts a new profile into database
*/
void MySQL_create_profile(D_MOBILE * dMob)
{
char query[4096];
snprintf(query, sizeof(query) + 1, "INSERT INTO `vand_players` ( `name` , `admin_level` , `password`, `last_note`) VALUES ( '%s', '%d', '%s', `0` );",
dMob->name, dMob->admin_level, dMob->password);
MySQLQuery(query);
return;
}
/*
* MySQL_save_profile()
*
* Updates dMob's Profile
*/
void MySQL_save_profile(D_MOBILE * dMob)
{
char query[4096];
snprintf(query, sizeof(query) + 1, "UPDATE `vand_players` SET `admin_level` = '%d', `last_note` = '%d', `password` = '%s' WHERE CONVERT( `name` USING utf8 ) = '%s' LIMIT 1 ;",
dMob->admin_level, dMob->last_note, dMob->password, dMob->name);
MySQLQuery(query);
return;
}
D_MOBILE * MySQL_load_profile(char *player)
{
D_MOBILE *dMob = NULL;
char pName[20];
char query[4096 * 2];
MYSQL_RES *result;
int size, i, found;
pName[0] = toupper(player[0]);
size = strlen(player);
for (i = 1; i < size; i++)
pName[i] = tolower(player[i]);
pName[i] = '\0';
found = FALSE;
/* create new mobile data */
if (StackSize(dmobile_free) <= 0)
{
if ((dMob = malloc(sizeof(*dMob))) == NULL)
{
bug("Load_profile: Cannot allocate memory.");
abort();
}
}
else
{
dMob = (D_MOBILE *) PopStack(dmobile_free);
}
clear_mobile(dMob);
/* load data */
snprintf(query, sizeof(query) +1, "SELECT * FROM `vand_players` WHERE `name` = CONVERT( _utf8 '%s' USING latin1 ) COLLATE latin1_swedish_ci LIMIT 1", pName);
MySQLQuery(query);
result = mysql_store_result(&mysqlconn);
while ((row = mysql_fetch_row(result)))
{
found = TRUE;
dMob->name = strdup(row[0]);
dMob->password = strdup(row[2]);
dMob->last_note = atoi(row[3]);
}
mysql_free_result(result);
if (!found)
return NULL;
return dMob;
}