Raised This Month: $32 Target: $400
 8% 

Communication PHP <-> AMXX via MOTD


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
bboygrun
CHOMP
Join Date: May 2010
Location: France
Old 10-20-2012 , 12:00   Communication PHP <-> AMXX via MOTD
Reply With Quote #1

Communication PHP <-> AMXX via MOTD


Credits : Arkshine & xPaw

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 :
  1. SourceQuery (all files)
  2. A website

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

Here are the steps of the tutorial :
  1. How does it work ?
  2. PHP code
  3. PAWN code
  4. 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 )

PHP Code:
<html><head><title>Class Chooser</title></head><body>
 
<?php
    $s_index 
$_GET'index' ];
    
$s_ip $_GET'ip' ];
    
$s_class $_GET'class' ];
    
$ip $_SERVER'REMOTE_ADDR' ];
 
    
/*
        If the IP received by the server isn't the same as 
        the person who goes on the website, we show nothing
    */
 
    
if( $ip != $s_ip )
    {
        die( );
    }
?>
</body></html>
After this small security, we can show the menu to the player :

PHP Code:
<html><head><title>Class Chooser</title></head><body>
 
<?php
    $s_index 
$_GET'index' ];
    
$s_ip $_GET'ip' ];
    
$s_class $_GET'class' ];
    
$ip $_SERVER'REMOTE_ADDR' ];
 
    if( 
$ip != $s_ip )
    {
        die( );
    }
    else
    {
        
// We create an array where we put the classes' name
        
$classesName = array( "M4A1 + USP""Ak47 + GLOCK""FAMAS + DEAGLE""GALIL + DEAGLE" );
     
        
/* We do a FOR which permit to reduce greatly number
        of lines if there are more classes */
 
        
for( $x 0$x count$classesName ); $x ++ )
        {
            Echo 
"<a href=\""$_SERVER'PHP_SELF' ] ."?item=". ( $x ) ."&index="$s_index ."&class="$s_class ."&ip="$s_ip ."\">"$classesName$x ] ."</a><br/><br/>";
        }
    }
?>
</body></html>
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 )

PHP Code:
<html><head><title>Class Chooser</title></head><body>
 
<?php
    define
'TIME_OUT');
    
define'SERV_PORT'port_of_your_server );             // Change me
    
define'SERV_IP''ip_of_your_server' );                 // Change me
    
define'SERV_RCON''rcon_of_your_server' );             // Change me
 
    // Don't forget to install the SourceQuery class in your FTP
    
require 'SourceQuery.class.php';
 
    
$s_index $_GET'index' ];
    
$s_ip $_GET'ip' ];
    
$s_class $_GET'class' ];
    
$item $_GET'item' ];
    
$ip $_SERVER'REMOTE_ADDR' ];
 
    if( 
$ip != $s_ip )
    {
        die( );
    }
    else if( 
$item // An item has been chosen by a player, we send the command ...
    
{
        if( 
$class != $item // ... only if current player's class is different of the item.
        
{
            
$query = new SourceQuery( );
 
            try
            {
                
$query -> ConnectSERV_IPSERV_PORTTIME_OUTSourceQuery :: GOLDSOURCE ); // Connection
                
$query -> SetRconPasswordSERV_RCON ); // We set the RCON
                
$query -> Rcon'php_results  '$s_index .' '$item.' '$ip ); // We send the command
            
}
            catch( 
SQueryException $e )
            {
                echo 
$e -> getMessage( );
            }
 
            
$query -> Disconnect( ); // Disconnection
        
}
        else 
// ... item = current player's class, we redirect the player to the main page
        
{
            
header'Location: '$_SERVER'PHP_SELF' ] .'?index='$s_index .'&class='$s_class .'&ip='$s_ip .'' );
        }
    }
 
    else
    {
        
$classesName = array( "M4A1 + USP""Ak47 + GLOCK""FAMAS + DEAGLE""GALIL + DEAGLE" );
     
        for( 
$x 0$x count$classesName ); $x ++ )
        {
            Echo 
"<a href=\""$_SERVER'PHP_SELF' ] ."?item=". ( $x ) ."&index="$s_index ."&class="$s_class ."&ip="$s_ip ."\">"$classesName$x ] ."</a><br/><br/>";
        }
    }
 
?>
</body></html>
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.

PHP Code:
#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 
arguments25 ];
     
        if( 
read_argsargumentscharsmaxarguments ) ) ) // Read arguments and we stop here if there isn't strings
        
{
            new 
ipPHP16 ], idStr], itemStr];
            
            
// We need 3 informations.
            
if( parseargumentsidStrcharsmaxidStr ), itemStrcharsmaxitemStr ), ipPHPcharsmaxipPHP ) ) == )
            {
                new const 
player str_to_numidStr );
     
                
// Security, must be a valid player
                
if( <= player <= MaxServerSlots )
                {
                    new 
ip16 ];
                    
get_user_ipplayeripcharsmaxip ), );
                    
                    
// Such as PHP code, we need to check the IPs.
                    
                    
if( equalipipPHP ) )
                    {
                        
// 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 :

PHP Code:
#include < amxmodx >
#include < fun >
#include < cstrike >
#include < hamsandwich >
 
const MaxClients 32;
 
new 
PlayerClassMaxClients ];
new 
PlayerNextClassMaxClients ];
 
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" );
 
    
RegisterHamHam_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_aliveplayer ) )
    {
        if( !
PlayerClassplayer ] )
        {
            
OnChooseClassplayer );
        }
        else
        {
            new const 
nextClass PlayerNextClassplayer ];
 
            if( 
nextClass )
            {
                
PlayerClassplayer ] = nextClass;
                
PlayerNextClassplayer ] = 0;
            }
 
            
give_weaponsplayernextClass );
        }
    }
}
 
public 
client_disconnectclient )
{
    
PlayerNextClassclient ] = 0;
    
PlayerClassclient ] = 0;
}
 
public 
OnChooseClass( const player )
{
    new 
infos128 ], ip16 ];
    
get_user_ipplayeripcharsmaxip ), );
 
    
formatexinfoscharsmaxinfos ), "%s?index=%d&ip=%s&class=%d"URLplayeripPlayerClassplayer ] );
    
show_motdplayerinfos );
}
 
public 
OnResultShop( const idServer )
{
    if( !
idServer )
    {
        new 
arguments25 ];
     
        if( 
read_argsargumentscharsmaxarguments ) ) )
        {
            new 
ipPHP16 ], idStr], itemStr];
            
            if( 
parseargumentsidStrcharsmaxidStr ), itemStrcharsmaxitemStr ), ipPHPcharsmaxipPHP ) ) == )
            {
                new const 
player str_to_numidStr );
     
                if( 
<= player <= MaxServerSlots )
                {
                    new 
ip16 ];
                    
get_user_ipplayeripcharsmaxip ), );
                    
                    if( 
equalipipPHP ) )
                    {
                        new const 
item str_to_numitemStr );
                        new const 
currentClass PlayerClassplayer ];
 
                        if( 
currentClass )
                        {
                            if( 
currentClass != item )
                            {
                                
PlayerNextClassplayer ] = item;
                            }
                        }
                        else
                        {
                            
PlayerClassplayer ] = item;
                 
                            if( 
is_user_aliveplayer ) )
                            {
                                
give_weaponsplayeritem );
                            }
                        }
                    }
                }
            }
        }
    }
}

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_weaponsplayer );
    
give_itemplayer"weapon_knife" );
 
    switch( 
item )
    {
        case 
1:
        {
            
give_itemsplayer"weapon_m4a1"CSW_M4A190 );
            
give_itemsplayer"weapon_usp"CSW_USP100 );
        }
        case 
2:
        {
            
give_itemsplayer"weapon_ak47"CSW_AK4790 );
            
give_itemsplayer"weapon_glock18"CSW_GLOCK18120 );
        }
        case 
3:
        {
            
give_itemsplayer"weapon_famas"CSW_FAMAS90 );
            
give_itemsplayer"weapon_deagle"CSW_DEAGLE35 );
        }
        case 
4:
        {
            
give_itemsplayer"weapon_galil"CSW_GALIL90 );
            
give_itemsplayer"weapon_deagle"CSW_DEAGLE35 );
        }
    }

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 .

I hope you enjoyed this tutorial, here is a screenshot of what i did :

You can download SMA and PHP file wrote in the main tutorial post here.
__________________

Last edited by YamiKaitou; 05-12-2013 at 22:31. Reason: Updating links
bboygrun is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode