CHOMP
Join Date: May 2010
Location: France
|
10-20-2012
, 13:00
Communication PHP <-> AMXX via MOTD
|
#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.
REQUIREMENTSTo make the communication possible, you need :- SourceQuery (all files)
- 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 CODEWe 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 + 1 ) ."&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', 3 ); 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 -> Connect( SERV_IP, SERV_PORT, TIME_OUT, SourceQuery :: GOLDSOURCE ); // Connection $query -> SetRconPassword( SERV_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 + 1 ) ."&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 CODEFirst, 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 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 :
PHP Code:
#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 ); } } }
CONCLUSIONThe 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 23:31.
Reason: Updating links
|
|