/* $Header: player.c,v 1.1 90/04/14 14:56:48 lachesis Exp $
 * $Log:	player.c,v $
 * Revision 1.1  90/04/14  14:56:48  lachesis
 * Initial revision
 * 
 */
#include "copyright.h"

#include "db.h"
#include "config.h"
#include "hashtab.h"
#include "interface.h"
#include "externs.h"

static hash_tab player_list[PLAYER_HASH_SIZE];

dbref lookup_player(const char *name)
{
  hash_data *hd;

  if ((hd = find_hash(name, player_list, PLAYER_HASH_SIZE)) == NULL)
    return NOTHING;
  else
    return (hd -> dbval);
}

dbref connect_player(const char *name, const char *password)
{
    dbref player;

    if((player = lookup_player(name)) == NOTHING) return NOTHING;
    if(db[player].sp.player.password
       && *db[player].sp.player.password
       &&strcmp(db[player].sp.player.password, password)) return NOTHING;

    return player;
}

dbref create_player(const char *name, const char *password)
{
    dbref player;

    if(!ok_player_name(name) || !ok_password(password)) return NOTHING;

    /* else he doesn't already exist, create him */
    player = new_object();

    /* initialize everything */
    db[player].name = alloc_string(name);
    db[player].location = PLAYER_START;	/* home */
    PUSH(player, db[PLAYER_START].contents);
    db[player].flags = TYPE_PLAYER;
    db[player].sp.player.home = PLAYER_START;
    db[player].sp.player.password = alloc_string(password);
    db[player].sp.player.actions = NOTHING;
    db[player].sp.player.pennies = 0;
    add_player(player);

    return player;
}

void do_password(dbref player, const char *old, const char *newobj)
{
    if(!db[player].sp.player.password || strcmp(old, db[player].sp.player.password)) {
	notify(player, "Sorry");
    } else if(!ok_password(newobj)) {
	notify(player, "Bad new password.");
    } else {
	free((void *) db[player].sp.player.password);
	db[player].sp.player.password = alloc_string(newobj);
	notify(player, "Password changed.");
    }
}

void clear_players(void)
{
  kill_hash(player_list, PLAYER_HASH_SIZE);
  return;
}

void delete_player(dbref who)
{
  (void) free_hash(db[who].name, player_list, PLAYER_HASH_SIZE);
  return;
}

void add_player(dbref who)
{
  hash_data hd;

  hd.dbval = who;
  if (add_hash(db[who].name, hd, player_list, PLAYER_HASH_SIZE) == NULL)
    panic("Out of memory");
  else
    return;
}