#include <stdio.h>
#include <ctype.h>
#include "player.h"
#include "stringops.h"

static char tempstr[1024];

player_list *player_load (char *filename)
{
	char name [128];
	player_list *pl;
	FILE *f;
	int status, verbose, ignore, command, owner;

	#ifdef FUNCTIONS
	puts ("**player_load");
	#endif

	f = fopen (filename, "r");

	pl = allocate (player_list);
	pl->head = pl->tail = NULL;
	pl->size = 0;

	if (f == NULL)
		return pl;

	fgets (tempstr, 127, f);
	while (!feof(f))
	{
		fscanf (f, "%s%d%d%d%d%d", name, &status, &verbose,
			&ignore, &command, &owner);
		if (!feof(f))
		{
			player_add (pl, name);

			pl->tail->status = status;
			pl->tail->verbose = verbose;
			pl->tail->ignore = ignore;
			pl->tail->command = command;
			pl->tail->owner = owner;
		}
	}
	return pl;
}

void player_add (player_list *pl, char *name)
{
	player *new;

	#ifdef FUNCTIONS
	puts ("**player_add");
	#endif

	new = allocate (player);

	copystring(new->name, name);
	new->status = 0;
	new->verbose = 0;
	new->ignore = 0;
	new->command = 0;
	new->owner = 0;
	new->next = NULL;

	if (pl->tail != NULL) pl->tail->next = new;
	pl->tail = new;
	if (pl->head == NULL) pl->head = new;

	(pl->size)++;
}

void player_delete (player_list *pl, player *dead)
{
	player *scan;

	#ifdef FUNCTIONS
	puts ("**player_delete");
	#endif

	if (pl->head == NULL) return;

	if (pl->head == dead)
	{
		if (pl->tail == dead) pl->tail = NULL;
		pl->head = dead->next;

		free (dead->name);
		free (dead);

		(pl->size)--;
		return;
	}

	for (scan = pl->head; (scan->next != dead) && (scan->next != NULL);
		scan = scan->next);

	if (scan != NULL)
	{
		if (pl->tail == dead) pl->tail = scan;
		scan->next = dead->next;

		free (dead->name);
		free (dead);

		(pl->size)--;
	}
}

player *player_find_num (player_list *pl, int n)
{
	player *scan;

	#ifdef FUNCTIONS
	puts ("**player_find_num");
	#endif

	for (scan = pl->head; (scan != NULL) && (n > 1);
		scan = scan->next, n--);

	return scan;
}

player *player_find (player_list *pl, char *name)
{
	player *scan;

	#ifdef FUNCTIONS
	puts ("**player_find");
	#endif

	if (isdigit (*name)) return (player_find_num (pl, atoi (name)));

	for (scan = pl->head; (scan != NULL) && (strcasecmp (scan->name, name));
		scan = scan->next);
	return scan;
}

void player_save (player_list *pl, char *name)
{
	FILE *f;
	player *scan;

	#ifdef FUNCTIONS
	puts ("**player_save");
	#endif

	f = fopen (name, "w");

	fputs ("Name                       S  V I C O\n", f);
	for (scan = pl->head; scan != NULL; scan = scan->next)
		fprintf (f, "%-25s %+03d %d %d %d %d\n", scan->name,
			scan->status, scan->verbose, scan->ignore,
			scan->command, scan->owner);
	fclose (f);
}

static void player_elements_burn (player *p)
{
	#ifdef FUNCTIONS
	puts ("**player_elements_burn");
	#endif

	if (p != NULL)
	{
		player_elements_burn (p->next);
		free (p->name);
		free (p);
	}
}

void player_burn (player_list *pl)
{
	#ifdef FUNCTIONS
	puts ("**player_burn");
	#endif

	if (pl)
	{
		player_elements_burn (pl->head);
		free (pl);
	}
}

int player_num (player_list *pl, player *p)
{
	int ret = 1;
	player *scan;

	#ifdef FUNCTIONS
	puts ("**player_num");
	#endif

	for (scan = pl->head; (scan != NULL) && (scan != p);
		scan = scan->next, ret++);

	if (scan != NULL)
		return ret;
	else
		return 0;
}