/*
 *	BANK.C
 *
 *	A generic banking system written for Mordor.
 *
 *      Copyright 1997 Maelstrom Computer Consulting, All rights reserved.
 *      Designed for Maelstrom Mordor Mud on Aalynor's Nexus
 *      http://nexus.mudservices.com/
 *
 *	Mordor Mud (c) 1997 Brooke Paul
 *
*/
#include "mstruct.h"
#include "mextern.h"
#ifdef DMALLOC
  #include "/usr/local/include/dmalloc.h"
#endif

/*
   INTRODUCTION

   The banking system basically installs four new commands:
      "balance", "deposit", "transfer", and "withdraw".

   You have to get a bit creative with finding a long in the
   player structure, we accomplished this by changing the structure,
   however, I have #defined BANK_BALANCE as pointing to the
   actual slot on the player.
 
   (You can add 'long  bank;' and '#define BANK_BALANCE bank' to
   the mstruct.h file in the 'creature' structure if you are
   starting from scratch with no player files).

   You then need to add a bank flag to 'mtype.h' which will be
   at the end, and should be RBANK. You will also have to change
   the "dm_stat_rom" function to display this flag.

   This setup will allow you to have multiple branches of the bank.
   By simply setting the RBANK flag in a room, players will have
   access to their accounts.

   RELEASE NOTES:
 
   I'm releasing this to anyone who would like to use it. 
   Provided you email me and let me know: gallant@nexus.mudservices.com.

*/

/*************************************************************************/
/*                            cmd_balance                               */
/*************************************************************************/

int cmd_balance( ply_ptr, cmnd )
creature	*ply_ptr;
cmd		*cmnd;
{
	int fd = ply_ptr->fd;

	if( !F_ISSET( ply_ptr->parent_rom, RBANK ) )
	{
		print(fd, "How would you do that here?\n");
		return( PROMPT );
	}
	if( ply_ptr->BANK_BALANCE < 1  || ply_ptr->BANK_BALANCE > 10000000 )
	{
		ply_ptr->BANK_BALANCE = 0;
		print(fd, "You have no gold marks in the bank.\n");
	}
	else
                print(fd, "\nYour account balance is %ld gold marks.\n",
                        ply_ptr->BANK_BALANCE);

	return( PROMPT );
}




/*************************************************************************/
/*                            cmd_withdraw                               */
/*************************************************************************/

int cmd_withdraw( ply_ptr, cmnd )
creature	*ply_ptr;
cmd		*cmnd;
{
	int fd = ply_ptr->fd;

	if( !F_ISSET( ply_ptr->parent_rom, RBANK ) )
	{
		print(fd, "How would you do that here?\n");
		return( PROMPT );
	}
	if( cmnd->num < 1 || cmnd->str[0] == 0 )
	{
		print(fd, "You must specify how much you wish to withdraw.\n");
		return( PROMPT );
	}
	if( cmnd->val[0] < 1 || cmnd->val[0] > 10000000 )
	{
		print(fd, "You can't do that!\n");
		return( PROMPT );
	}
	if( ply_ptr->BANK_BALANCE < cmnd->val[0] )
	{
		print(fd, "\nYou don't have that much gold in your account!\n");
		return( PROMPT );
	}
        print(fd, "\nYou withdraw %ld gold marks.\n", cmnd->val[0]);
	ply_ptr->gold += cmnd->val[0];
	ply_ptr->BANK_BALANCE -= cmnd->val[0];
	if( ply_ptr->BANK_BALANCE < 1  || ply_ptr->BANK_BALANCE > 10000000 )
	{
		ply_ptr->BANK_BALANCE = 0;
		print(fd, "You now have no gold marks in the bank.\n");
	}
	else
                print(fd, "Your balance is now %ld gold marks.\n",
                        ply_ptr->BANK_BALANCE);
	save_ply(ply_ptr->name,ply_ptr);
	return( PROMPT );
}

/*************************************************************************/
/*                            cmd_deposit                                */
/*************************************************************************/

int cmd_deposit( ply_ptr, cmnd )
creature	*ply_ptr;
cmd		*cmnd;
{
	long tax;
	int fd = ply_ptr->fd;

	if( !F_ISSET( ply_ptr->parent_rom, RBANK ) )
	{
		print(fd, "How would you do that here?\n");
		return( PROMPT );
	}
	if( cmnd->num < 1 || cmnd->str[0] == 0 )
	{
		print(fd, "You must specify how much you wish to deposit.\n");
		return( PROMPT );
	}
	if( cmnd->val[0] < 100 || cmnd->val[0] > 10000000 )
	{
		print(fd, 
                "\n\"I'm sorry, but we only accept deposits between 100 and 10,000,000");
                print(fd, " gold marks.\"\n");

		return( PROMPT );
	}
	if( ply_ptr->gold < cmnd->val[0] )
	{
		print(fd, "You don't have that much gold!\n");
		return( PROMPT );
	}
	if( (cmnd->val[0] + ply_ptr->BANK_BALANCE) > 10000000 )
	{
		print(fd, "The bank manager refuses the deposit.\n");
		return( PROMPT );
	}

	tax = ((cmnd->val[0]/100) * 3);

        print(fd, "\nYou deposit %ld gold marks.\n", cmnd->val[0]);
        print(fd, "You pay %ld gold marks in service charges.\n", tax);

	ply_ptr->gold -= cmnd->val[0];

	ply_ptr->BANK_BALANCE += (cmnd->val[0] - tax);
	if( ply_ptr->BANK_BALANCE < 1  || ply_ptr->BANK_BALANCE > 10000000 )
	{
		ply_ptr->BANK_BALANCE = 0;
		print(fd, "You now have no gold marks in the bank.\n");
	}
	else
                print(fd, "Your balance is now %ld gold marks.\n",
                        ply_ptr->BANK_BALANCE);

	save_ply(ply_ptr->name,ply_ptr);

	return( PROMPT );
}

/*************************************************************************/
/*                            cmd_transfer                                */
/*************************************************************************/

int bank_transfer(ply_ptr, cmnd)
creature	*ply_ptr;
cmd		*cmnd;
{
	int online=0;
	int fd = ply_ptr->fd;
	long amount;
	creature *tra_ptr;		/* player to transfer to */
	
	if(!F_ISSET(ply_ptr->parent_rom, RBANK)) {
		print(fd, "How would you do that here?\n");
		return(PROMPT);
	}

	if(ply_ptr->BANK_BALANCE < 1 || ply_ptr->BANK_BALANCE > 10000000) {
		print(fd, "You don't have any money to transfer.\n");
		return(PROMPT);
	}

	if(cmnd->num < 3 || cmnd->str[0] == 0) 
        {
		print(fd, "Transfer how much to whom?\n");
		return(PROMPT);
	}

	if( cmnd->str[1][0] != '$') 
        {
		print(fd, "Transfer how much to whom?\n");
		return(PROMPT);
	}
	
	cmnd->str[2][0] = up(cmnd->str[2][0]);

        tra_ptr = 0;
        tra_ptr = find_who( cmnd->str[2] );

        if(!tra_ptr)
        {
                if(load_ply(cmnd->str[2], &tra_ptr) < 0) {
                        print(fd, "Player does not exist!\n");
                        return(PROMPT);
                }
         }
        else
        {
                online=1;
        }

	amount = atol(&cmnd->str[1][1]);

	if(amount < 1 || amount > 10000000) {
		print(fd, "Get real.\n");
		return(PROMPT);
	}
	if(amount > ply_ptr->BANK_BALANCE) {
		print(fd, "You don't have that much gold.\n");
		return(PROMPT);
	}
	if((amount + tra_ptr->BANK_BALANCE) > 10000000) {
		print(fd, "The bank will not accept your transfer.\n");
		return(PROMPT);
	}

	tra_ptr->BANK_BALANCE += amount;
	ply_ptr->BANK_BALANCE -= amount;
	save_ply(tra_ptr->name, tra_ptr);
	save_ply(ply_ptr->name, ply_ptr);

        print(fd, "You have transferred %ld gold marks to %s.\n", amount,
                cmnd->str[2]);

	if(online == 0)
		free(tra_ptr);

	return(PROMPT);
}
	



