Raised This Month: $12 Target: $400
 3% 

Communication PHP <-> AMXX via MOTD


Post New Thread Reply   
 
Thread Tools Display Modes
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
bboygrun
CHOMP
Join Date: May 2010
Location: France
Old 10-20-2012 , 12:01   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #2

DOWNLOADS

Tutorial Codes : tutorial_source.zip

SourceQuery Class : https://github.com/xPaw/PHP-Source-Q...er/SourceQuery

I will maybe add Shop & Class Choosers codes here, maybe designs too.
Attached Files
File Type: zip tutorial_source.zip (2.1 KB, 907 views)
__________________

Last edited by YamiKaitou; 05-12-2013 at 22:31. Reason: updating links
bboygrun is offline
quilhos
Veteran Member
Join Date: Jun 2010
Old 10-20-2012 , 12:24   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #3

Looks nice, ty for sharing x)
__________________
ELO RATING SYSTEM - SQL [COMPLETE]
Quote:
Originally Posted by Liverwiz View Post
DDDRRRRAAAAMMMMAAAAA!!!???

Put this shit on pause while i go get some popcorn!!
quilhos is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 10-20-2012 , 12:55   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #4

Good job and thanks for sharing this.

Quote:
Originally Posted by bboygrun View Post
I will maybe add Shop & Class Choosers codes here, maybe designs too.
Would be really appreciated.
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 10-20-2012 , 14:16   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #5

Nice to see my class used in the wild. Good job.
__________________
xPaw is offline
Groven
AlliedModders Donor
Join Date: Apr 2011
Location: Sweden
Old 10-20-2012 , 19:21   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #6

Nice man!
__________________
Groven is offline
bboygrun
CHOMP
Join Date: May 2010
Location: France
Old 10-20-2012 , 20:36   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #7

Thanks.

Quote:
Originally Posted by bibu View Post
Would be really appreciated.
I will work on it, but since it takes time to make all these things and i don't have a lot of free times, i won't do them soon.
__________________
bboygrun is offline
avril-lavigne
Banned
Join Date: Apr 2009
Old 10-21-2012 , 06:01   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #8

Im saw videos on youtube first my thought was thats impossible to do .... now I see it is possible
__________________
VDS in Europe 1 gb/s unmetered.Any configurations.
I accept Paypal, Moneybookers,etc
avril-lavigne is offline
Brian-__-
Member
Join Date: Jan 2010
Old 10-21-2012 , 06:47   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #9

why don't use socket? design your own protocol
http://forums.alliedmods.net/showthread.php?t=60026

There are a lot of rcon brute, rcon is dangerous

Last edited by Brian-__-; 10-21-2012 at 06:49.
Brian-__- is offline
bboygrun
CHOMP
Join Date: May 2010
Location: France
Old 10-21-2012 , 07:20   Re: Communication PHP <-> AMXX via MOTD
Reply With Quote #10

Rcon password is only set in PHP script, it isn't dangerous.
__________________
bboygrun is offline
Reply


Thread Tools
Display Modes

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 13:09.


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