Communication PHP <-> AMXX via MOTD

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

Communication PHP <-> AMXX via MOTD

Post by Archive-Poster »

Communication PHP <-> AMXX via MOTD

You always wanted to create a shop or a class chooser on your MOTD page ?
It's possible with xPaw's SourceQuery Class.

REQUIREMENTS
To make the communication possible, you need :
SourceQuery (all files attached to this post)
A website

And you will need to have knowledges in :
PAWN
PHP
HTML/CSS (Optional)
CONTENT
For this tutorial i will make a "Class Chooser", not a shop.

Here are the steps of the tutorial :
How does it work ?
PHP code
PAWN code
Conclusion & Screenshots
First, we will see PHP code, and then Pawn code.
HOW DOES IT WORK ?
We will show a menu in the MOTD to a player, where he can choose something.
When the player will choose an item, the website will send all the informations we need to the server, and then our plugin will do the rest.

The website will send the informations to the server via the RCON, with the command named php_results for example.
The server will receive all the informations with the native register_concmd.

Conclusion : Server -> Website -> Server
PHP CODE
We have to recover these informations :
Player's Index
Player's IP (Security)
Player's current class

Here is the begin of the code : (With some HTML, but we won't do design here )

https://projectgodmode.com/custom_conte ... tion_1.txt

After this small security, we can show the menu to the player :

https://projectgodmode.com/custom_conte ... tion_2.txt

And now we just need to send what item player choosed, it's easy, we just have to use $_GET[ 'item' ].

However, we will send the command only if player's class is different than the item.
And it's in this part that we will introduce xPaw's SourceQuery Class.

I added at the begin of the code, some defines that you may change.
( time_out is at 3, by default )

https://projectgodmode.com/custom_conte ... tion_3.txt

Ok, we have now finished PHP code.
Of course you will have to do YOUR design, YOUR menu, YOUR ideas.
PAWN CODE
First, we will begin with the recover of informations sent by the Website (with the RCON)

Remember, we used php_results command.

Code: Select all

#include < amxmodx >
 
new MaxServerSlots;
 
public plugin_init( )
{
    register_plugin( "AMXX/PHP MOTD", "1.0.0", "Bboy Grun" );
 
    register_concmd( "php_results", "OnResultShop" );
 
    MaxServerSlots = get_maxplayers( );
}
 
public OnResultShop( const idServer )
{
    if( !idServer ) // idServer must be equal to 0 ( = server )
    {
        new arguments[ 25 ];
     
        if( read_args( arguments, charsmax( arguments ) ) ) // Read arguments and we stop here if there isn't strings
        {
            new ipPHP[ 16 ], idStr[ 3 ], itemStr[ 3 ];
            
            // We need 3 informations.
            if( parse( arguments, idStr, charsmax( idStr ), itemStr, charsmax( itemStr ), ipPHP, charsmax( ipPHP ) ) == 3 )
            {
                new const player = str_to_num( idStr );
     
                // Security, must be a valid player
                if( 1 <= player <= MaxServerSlots )
                {
                    new ip[ 16 ];
                    get_user_ip( player, ip, charsmax( ip ), 1 );
                    
                    // Such as PHP code, we need to check the IPs.
                    
                    if( equal( ip, ipPHP ) )
                    {
                        // We can continue
                    }
                }
            }
        }
    }
} 
Good ! We have now all the informations we needed.

But to finish this tutorial, i have to do the class system, give the weapon when the player spawns, show the MOTD if he doesn't have class, give directly the weapons if the players didn't have class before, ect ...

Here is an example :

Code: Select all

#include < amxmodx >
#include < fun >
#include < cstrike >
#include < hamsandwich >
 
const MaxClients = 32;
 
new PlayerClass[ MaxClients + 1 ];
new PlayerNextClass[ MaxClients + 1 ];
 
new MaxServerSlots;
 
// Where you placed your MOTD
new const URL[ ] = "http://my-website.com/index.php";
 
public plugin_init( )
{
    register_plugin( "AMXX/PHP MOTD", "1.0.0", "Bboy Grun" );
 
    RegisterHam( Ham_Spawn, "player", "OnPlayerSpawn", .Post = true );
 
    register_clcmd( "say /class", "OnChooseClass" );
    register_concmd( "php_results", "OnResultShop" );
   
    MaxServerSlots = get_maxplayers( );
}
 
public OnPlayerSpawn( const player )
{
    if( is_user_alive( player ) )
    {
        if( !PlayerClass[ player ] )
        {
            OnChooseClass( player );
        }
        else
        {
            new const nextClass = PlayerNextClass[ player ];
 
            if( nextClass )
            {
                PlayerClass[ player ] = nextClass;
                PlayerNextClass[ player ] = 0;
            }
 
            give_weapons( player, nextClass );
        }
    }
}
 
public client_disconnect( client )
{
    PlayerNextClass[ client ] = 0;
    PlayerClass[ client ] = 0;
}
 
public OnChooseClass( const player )
{
    new infos[ 128 ], ip[ 16 ];
    get_user_ip( player, ip, charsmax( ip ), 1 );
 
    formatex( infos, charsmax( infos ), "%s?index=%d&ip=%s&class=%d", URL, player, ip, PlayerClass[ player ] );
    show_motd( player, infos );
}
 
public OnResultShop( const idServer )
{
    if( !idServer )
    {
        new arguments[ 25 ];
     
        if( read_args( arguments, charsmax( arguments ) ) )
        {
            new ipPHP[ 16 ], idStr[ 3 ], itemStr[ 3 ];
            
            if( parse( arguments, idStr, charsmax( idStr ), itemStr, charsmax( itemStr ), ipPHP, charsmax( ipPHP ) ) == 3 )
            {
                new const player = str_to_num( idStr );
     
                if( 1 <= player <= MaxServerSlots )
                {
                    new ip[ 16 ];
                    get_user_ip( player, ip, charsmax( ip ), 1 );
                    
                    if( equal( ip, ipPHP ) )
                    {
                        new const item = str_to_num( itemStr );
                        new const currentClass = PlayerClass[ player ];
 
                        if( currentClass )
                        {
                            if( currentClass != item )
                            {
                                PlayerNextClass[ player ] = item;
                            }
                        }
                        else
                        {
                            PlayerClass[ player ] = item;
                 
                            if( is_user_alive( player ) )
                            {
                                give_weapons( player, item );
                            }
                        }
                    }
                }
            }
        }
    }
}

give_weapons( const player, const item )
{
    #define give_items(%1,%2,%3,%4) ( give_item( %1, %2 ), cs_set_user_bpammo( %1, %3, %4 ) )
 
    strip_user_weapons( player );
    give_item( player, "weapon_knife" );
 
    switch( item )
    {
        case 1:
        {
            give_items( player, "weapon_m4a1", CSW_M4A1, 90 );
            give_items( player, "weapon_usp", CSW_USP, 100 );
        }
        case 2:
        {
            give_items( player, "weapon_ak47", CSW_AK47, 90 );
            give_items( player, "weapon_glock18", CSW_GLOCK18, 120 );
        }
        case 3:
        {
            give_items( player, "weapon_famas", CSW_FAMAS, 90 );
            give_items( player, "weapon_deagle", CSW_DEAGLE, 35 );
        }
        case 4:
        {
            give_items( player, "weapon_galil", CSW_GALIL, 90 );
            give_items( player, "weapon_deagle", CSW_DEAGLE, 35 );
        }
    }
} 
CONCLUSION
The only thing i have to tell you, don't make pictures biger than 600x350 in your MOTD, then they will be in the good size for each resolutions

It's now your turn to make your Class/Shop System with your own ideas, design.
With this type of communication, you can make nice shops such as Killing Floor shop .
Attachments
PHP-Source-Query-master.zip
(96.01 KiB) Downloaded 6 times
tutorial_source.zip
(2.13 KiB) Downloaded 6 times

Who is online

Users browsing this forum: No registered users and 1 guest