AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Communication PHP <-> AMXX via MOTD (https://forums.alliedmods.net/showthread.php?t=198774)

bboygrun 10-20-2012 12:00

Communication PHP <-> AMXX via MOTD
 
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.

http://img15.hostingpics.net/pics/778785zdzd.png 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)
http://img15.hostingpics.net/pics/778785zdzd.png 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.
http://img15.hostingpics.net/pics/778785zdzd.png 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
http://img15.hostingpics.net/pics/778785zdzd.png 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 :wink:)

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.
http://img15.hostingpics.net/pics/778785zdzd.png 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 );
        }
    }


http://img15.hostingpics.net/pics/778785zdzd.png 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 :wink:.

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

http://cloud.steampowered.com/ugc/56...8.resizedimage
You can download SMA and PHP file wrote in the main tutorial post here.

bboygrun 10-20-2012 12:01

Re: Communication PHP <-> AMXX via MOTD
 
1 Attachment(s)
http://img15.hostingpics.net/pics/778785zdzd.png 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.

quilhos 10-20-2012 12:24

Re: Communication PHP <-> AMXX via MOTD
 
Looks nice, ty for sharing x)

bibu 10-20-2012 12:55

Re: Communication PHP <-> AMXX via MOTD
 
Good job and thanks for sharing this.

Quote:

Originally Posted by bboygrun (Post 1822298)
I will maybe add Shop & Class Choosers codes here, maybe designs too.

Would be really appreciated. :)

xPaw 10-20-2012 14:16

Re: Communication PHP <-> AMXX via MOTD
 
Nice to see my class used in the wild. Good job.

Groven 10-20-2012 19:21

Re: Communication PHP <-> AMXX via MOTD
 
Nice man!

bboygrun 10-20-2012 20:36

Re: Communication PHP <-> AMXX via MOTD
 
Thanks.

Quote:

Originally Posted by bibu (Post 1822338)
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.

avril-lavigne 10-21-2012 06:01

Re: Communication PHP <-> AMXX via MOTD
 
Im saw videos on youtube first my thought was thats impossible to do .... now I see it is possible

Brian-__- 10-21-2012 06:47

Re: Communication PHP <-> AMXX via MOTD
 
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

bboygrun 10-21-2012 07:20

Re: Communication PHP <-> AMXX via MOTD
 
Rcon password is only set in PHP script, it isn't dangerous.


All times are GMT -4. The time now is 09:19.

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