/*

 BabbleMud - A MUD server

 Author:  Nick Gammon 
          http://www.gammon.com.au/ 

(C) Copyright Nick Gammon 2004. Permission to copy, use, modify, sell and
distribute this software is granted provided this copyright notice appears
in all copies. This software is provided "as is" without express or implied
warranty, and with no claim as to its suitability for any purpose.
 
*/

#include "commands.h"

const int RESTORE_VALUE = 200;

//~ attributes used:

//~ PLAYER (number):
  
const string ROOM = "room";     // current room


void DoForce (CommandInfo & info)
{
Character & target = info.target ();

  // some people can't be forced
  if (target.getb ("no_force"))
    throw msg ("cannot_force_target");

  try
    {
    info.target ().DoCommand (info.wildcards_ ["what"], true);
    }
  // indicate error was from target, not from us
  catch (exception & e)
    {
    info.sub_ ["exception"].sets ("short", e.what ());
    throw msg ("forced_exception");
    }
  
} // end of DoForce

// functor for sending messages to all players
struct forcePlayer
{
  const string who_;
  const string what_;
  // ctor
  forcePlayer (const string & who, const string & what) 
    : who_ (who), what_ (what) {}
  // force this player
  void operator() (Character * p) 
    {
    if (p->IsPlaying () && !p->getb ("no_force"))
      {
      p->send (who_ + p->message ("someone_forces_you") + what_);
      p->DoCommand (what_, true);
      } // end of this one actually playing
    } // end of operator()  
};  // end of forcePlayer

void DoForceAll (CommandInfo & info)
{
  for_each (ISEQ (playerlist ()), 
            forcePlayer (info.player ().GetName (), info.wildcards_ ["what"]));
} // end of DoForceAll

void DoGoToRoom (CommandInfo & info)
  {
Character & p (info.player ());
  
  PlayerToRoom (p, 
                convertString<int> (info.wildcards_ ["room"], p.error ("bad_numeric_value")), 
                info);
  } // end of DoGoToRoom

void DoGoToPlayer (CommandInfo & info)
  {
  PlayerToRoom (info.player (), info.target ().geti (ROOM), info); 
  } // end of DoGoToPlayer

void DoTransfer (CommandInfo & info)
{
Character & p (info.player ());

  int room = p.geti (ROOM);

  if (!info.wildcards_ ["where"].empty ())
    room = convertString<int> (info.wildcards_ ["where"], p.error ("bad_numeric_value"));

  // for message to me
  info.sub_ ["room"].sets ("short", MAKE_STRING (room));
  
  p.send (p.message ("you_transfer") + info.target ().GetName () 
          + p.message ("to_room") + info.sub_ ["room"].gets ("short"));
  
  PlayerToRoom (info.target (), room, info); 
  
} // end of DoTransfer

void DoShutdown (CommandInfo & info)
{
  bStopNow = true;
} // end of DoShutdown

void DoBoot (CommandInfo & info)
  {
Character & target = info.target ();
  
  if (tolower (info.wildcards_ ["block"]) == "block")
    target.setb ("blocked", true);   // block them from reconnecting
  
  target.ClosePlayer ();
  } // end of DoBoot

void DoRestore (CommandInfo & info)
  {
Character & target = info.target ();
  
  target.seti ("hp", RESTORE_VALUE);
  target.seti ("mana", RESTORE_VALUE);
  target.seti ("move", RESTORE_VALUE);
  target.seti ("coins", RESTORE_VALUE);
  
  } // end of DoRestore

// give me an ID for playing with
void DoMakeID (CommandInfo & info)
  {
  info.player ().send ("ID = " + UniqueID ());
  } // end of DoMakeID

// give me a hashed password
void DoMakePassword (CommandInfo & info)
  {
  info.player ().send ("Hash = " + SecureHash (info.wildcards_ ["password"]).str ());
  } // end of DoMakePassword

// release a blocked IP address
void DoReleaseIp (CommandInfo & info)
  {
  badPlayerPasswords () [info.wildcards_ ["ip"]] = 0;    // this IP is OK now
  badWebPasswords () [info.wildcards_ ["ip"]] = 0;    
  } // end of DoReleaseIp

static int LoadCommandsMap ()
  {
static CommandHandlerMap & commandmap = getCommandHandlerMap ();

  cout << "Loading command handlers in " __FILE__ << endl;
    
  COMMAND (DoGoToRoom);  // go to room
  COMMAND (DoForce);     // force player to perform command
  COMMAND (DoBoot);      // boot off an annoying player
  COMMAND (DoForceAll);  // force all players to perform command
  COMMAND (DoGoToPlayer); // go to where a player is
  COMMAND (DoTransfer);  // transfer someone else
  COMMAND (DoShutdown);  // shutdown the MUD
  COMMAND (DoMakeID);     // give me a unique ID
  COMMAND (DoMakePassword);  // hash a password
  COMMAND (DoReleaseIp);    // unblock an IP address
  COMMAND (DoRestore);    // restore a player's points

  return 0;
  } // end of LoadCommands

static int loadThemUp = LoadCommandsMap ();

