⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 MUD游戏编程光盘代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MUD Programming
// Ron Penton
// (C)2003
// Game.cpp - This class is the game handler for SimpleMUD.
// 
// 

#include "Logon.h"
#include "Game.h"
#include "Train.h"
#include "ItemDatabase.h"
#include "PlayerDatabase.h"
#include "BasicLib/BasicLib.h"

using namespace SocketLib;
using namespace BasicLib;
using std::string;

namespace SimpleMUD
{


// declare the static instance of the timer
BasicLib::Timer Game::s_timer;
bool Game::s_running = false;

// ------------------------------------------------------------------------
//  This handles incomming commands. Anything passed into this function
//  is assumed to be a complete command from a client.
// ------------------------------------------------------------------------
void Game::Handle( string p_data )
{
    Player& p = *m_player;

    // check if the player wants to repeat a command
    if( p_data == "/" )
    {
        p_data = m_lastcommand;
    }
    else
    {
        // if not, record the command.
        m_lastcommand = p_data;
    }

    // get the first word and lowercase it.
    string firstword = BasicLib::LowerCase( ParseWord( p_data, 0 ) );


    // ------------------------------------------------------------------------
    //  REGULAR access commands
    // ------------------------------------------------------------------------
    if( firstword == "chat" || firstword == ":" )
    {
        string text = RemoveWord( p_data, 0 );
        SendGame( white + bold + p.Name() + " chats: " + text );
        return;
    }

    if( firstword == "experience" || firstword == "exp" )
    {
        p.SendString( PrintExperience() );
        return;
    }

    if( firstword == "help" || firstword == "commands" )
    {
        p.SendString( PrintHelp( p.Rank() ) );
        return;
    }

    if( firstword == "inventory" || firstword == "inv" )
    {
        p.SendString( PrintInventory() );
        return;
    }

    if( firstword == "quit" )
    {
        m_connection->Close();
        LogoutMessage( p.Name() + " has left the realm." );
        return;
    }

    if( firstword == "remove" )
    {
        RemoveItem( ParseWord( p_data, 1 ) );
        return;
    }

    if( firstword == "stats" || firstword == "st" )
    {
        p.SendString( PrintStats() );
        return;
    }

    if( firstword == "time" )
    {
        p.SendString( bold + cyan + 
                      "The current system time is: " + BasicLib::TimeStamp() + 
                      " on " + BasicLib::DateStamp() + 
                      "\r\nThe system has been up for: " 
                      + s_timer.GetString() + "." );
        return;
    }

    if( firstword == "use" )
    {
        UseItem( RemoveWord( p_data, 0 ) );
        return;
    }

    if( firstword == "whisper" )
    {
        // get the players name
        string name = ParseWord( p_data, 1 );
        string message = RemoveWord( RemoveWord( p_data, 0 ), 0 );

        Whisper( message, name );
        return;
    }

    if( firstword == "who" )
    {
        p.SendString( WhoList( BasicLib::LowerCase( ParseWord( p_data, 1 ) ) ) );
        return;
    }


    // ------------------------------------------------------------------------
    //  GOD access commands
    // ------------------------------------------------------------------------
    if( firstword == "kick" && p.Rank() >= GOD )
    {
        // find a player to kick
        PlayerDatabase::iterator itr = 
            PlayerDatabase::findloggedin( ParseWord( p_data, 1 ) );

        if( itr == PlayerDatabase::end() )
        {
            p.SendString( red + bold + "Player could not be found." );
            return;
        }

        if( itr->Rank() > p.Rank() )
        {
            p.SendString( red + bold + "You can't kick that player!" );
            return;
        }
            
        itr->Conn()->Close();
        LogoutMessage( itr->Name() + " has been kicked by " + 
                        p.Name() + "!!!" );

        return;
    }


    // ------------------------------------------------------------------------
    //  ADMIN access commands
    // ------------------------------------------------------------------------
    if( firstword == "announce" && p.Rank() >= ADMIN )
    {
        Announce( RemoveWord( p_data, 0 ) );
        return;
    }

    if( firstword == "changerank" && p.Rank() >= ADMIN )
    {
        string name = ParseWord( p_data, 1 );

        PlayerDatabase::iterator itr = PlayerDatabase::find( name );

        if( itr == PlayerDatabase::end() )
        {
            p.SendString( red + bold + "Error: Could not find user " +
                          name );
            return;
        }

        PlayerRank rank = GetRank( ParseWord( p_data, 2 ) );

        itr->Rank() = rank;
        SendGame( green + bold + itr->Name() + 
                  "'s rank has been changed to: " +
                  GetRankString( rank ) );
        return;
    }


    if( firstword == "reload" && p.Rank() >= ADMIN )
    {
        string db = BasicLib::LowerCase( ParseWord( p_data, 1 ) );

        if( db == "items" )
        {
            ItemDatabase::Load();
            p.SendString( bold + cyan + "Item Database Reloaded!" );
        }
        else
        {
            p.SendString( bold + red + "Invalid Database Name" );
        }
        return;
    }

    if( firstword == "shutdown" && p.Rank() >= ADMIN )
    {
        Announce( "SYSTEM IS SHUTTING DOWN" );
        Game::Running() = false;
        return;
    }


    // ------------------------------------------------------------------------
    //  Command not recognized, send to room
    // ------------------------------------------------------------------------
    SendGame( bold + p.Name() + " chats: " + p_data );
}


// ------------------------------------------------------------------------
//  This notifies the handler that there is a new connection
// ------------------------------------------------------------------------
void Game::Enter()
{
    USERLOG.Log(  GetIPString( m_connection->GetRemoteAddress() ) + 
                  " - User " + m_player->Name() + 
                  " entering Game state." );

    m_lastcommand = "";

    Player& p = *m_player;
    p.Active() = true;
    p.LoggedIn() = true;

    SendGame( bold + green + p.Name() + " has entered the realm." );

    if( p.Newbie() )
        GotoTrain();
}

void Game::Leave()
{
    // deactivate player
    m_player->Active() = false;

    // log out the player from the database if the connection has been closed
    if( m_connection->Closed() )
    {
        PlayerDatabase::Logout( m_player );
    }
}

// ------------------------------------------------------------------------
//  This notifies the handler that a connection has unexpectedly hung up.
// ------------------------------------------------------------------------
void Game::Hungup()
{
    Player& p = *m_player;
    LogoutMessage( p.Name() + " has suddenly disappeared from the realm." );
}

// ------------------------------------------------------------------------
//  This notifies the handler that a connection is being kicked due to 
//  flooding the server.
// ------------------------------------------------------------------------
void Game::Flooded()
{
    Player& p = *m_player;
    LogoutMessage( p.Name() + " has been kicked for flooding!" );
}

// ------------------------------------------------------------------------
//  Sends a string to everyone connected.
// ------------------------------------------------------------------------
void Game::SendGlobal( const string& p_str )
{
    operate_on_if( PlayerDatabase::begin(),
                   PlayerDatabase::end(),
                   playersend( p_str ),
                   playerloggedin() );
}

// ------------------------------------------------------------------------
//  Sends a string to everyone "within the game"
// ------------------------------------------------------------------------
void Game::SendGame( const std::string& p_str )
{
    operate_on_if( PlayerDatabase::begin(),
                   PlayerDatabase::end(),
                   playersend( p_str ),
                   playeractive() );
}

// ------------------------------------------------------------------------
//  Sends a logout message
// ------------------------------------------------------------------------
void Game::LogoutMessage( const string& p_reason )
{
    SendGame( SocketLib::red + SocketLib::bold + p_reason );
}

// ------------------------------------------------------------------------
//  Sends a system announcement
// ------------------------------------------------------------------------
void Game::Announce( const string& p_announcement )
{
    SendGlobal( SocketLib::cyan + SocketLib::bold + 
                "System Announcement: " + p_announcement );
}

// ------------------------------------------------------------------------
//  Sends a whisper string to the requested player
// ------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -