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

📄 game.cpp

📁 MUD游戏编程光盘代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// 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 "RoomDatabase.h"
#include "StoreDatabase.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( magenta + bold + p.Name() + " chats: " + white + 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;
    }


    if( firstword == "look" || firstword == "l" )
    {
        p.SendString( PrintRoom( p.CurrentRoom() ) );
        return;
    }

    if( firstword == "north" || firstword == "n" )
    {
        Move( NORTH );
        return;
    }
    if( firstword == "east" || firstword == "e" )
    {
        Move( EAST );
        return;
    }
    if( firstword == "south" || firstword == "s" )
    {
        Move( SOUTH );
        return;
    }
    if( firstword == "west" || firstword == "w" )
    {
        Move( WEST );
        return;
    }

    if( firstword == "get" || firstword == "take" )
    {
        GetItem( RemoveWord( p_data, 0 ) );
        return;
    }

    if( firstword == "drop" )
    {
        DropItem( RemoveWord( p_data, 0 ) );
        return;
    }


    if( firstword == "train" )
    {
        if( p.CurrentRoom()->Type() != TRAININGROOM )
        {
            p.SendString( red + bold + "You cannot train here!" );
            return;
        }

        if( p.Train() )
        {
            p.SendString( green + bold + "You are now level " + tostring( p.Level() ) );
        }
        else
        {
            p.SendString( red + bold + "You don't have enough experience to train!" );
        }

        return;
    }

    if( firstword == "editstats" )
    {
        if( p.CurrentRoom()->Type() != TRAININGROOM )
        {
            p.SendString( red + bold + "You cannot edit your stats here!" );
            return;
        }

        GotoTrain();
        return;
    }

    if( firstword == "list" )
    {
        if( p.CurrentRoom()->Type() != STORE )
        {
            p.SendString( red + bold + "You're not in a store!" );
            return;
        }

        p.SendString( StoreList( p.CurrentRoom()->Data() ) );
        return;
    }

    if( firstword == "buy" )
    {
        if( p.CurrentRoom()->Type() != STORE )
        {
            p.SendString( red + bold + "You're not in a store!" );
            return;
        }

        Buy( RemoveWord( p_data, 0 ) );
        return;
    }

    if( firstword == "sell" )
    {
        if( p.CurrentRoom()->Type() != STORE )
        {
            p.SendString( red + bold + "You're not in a store!" );
            return;
        }

        Sell( RemoveWord( p_data, 0 ) );
        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 if( db == "player" )
        {
            string user = ParseWord( p_data, 2 );
            PlayerDatabase::iterator itr = PlayerDatabase::findfull( user );

            if( itr == PlayerDatabase::end() )
            {
                p.SendString( bold + red + "Invalid Player Name" );
            }
            else
            {
                bool a = itr->Active();
                if( a )     itr->Conn()->Handler()->Leave();
                PlayerDatabase::LoadPlayer( itr->Name() );
                if( a )     itr->Conn()->Handler()->Enter();

                p.SendString( bold + cyan + "Player " + 
                              itr->Name() + " Reloaded!" );
            }
        }
        else if( db == "rooms" )
        {
            RoomDatabase::LoadTemplates();
            p.SendString( bold + cyan + "Room Template Database Reloaded!" );
        }
        else if( db == "stores" )
        {
            StoreDatabase::Load();
            p.SendString( bold + cyan + "Store 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;
    }

⌨️ 快捷键说明

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