Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <regex>
#include <sockets_hz>
#define PLUGIN "Online Map"
#define VERSION "1.1"
#define AUTHOR "Sebastian Groß"
new Regex:RegexHandle;
new Socket;
new MapName[ 32 ];
new BombPos[4];
public plugin_init()
{
register_plugin( PLUGIN, VERSION, AUTHOR );
new result, errorstr[ 2 ], errorno;
RegexHandle = regex_compile( "jsonp=(.+)&_=", result, errorstr, charsmax( errorstr ), "i" );
socket_unblock( Socket = socket_listen( "127.0.0.1", 1107, SOCKET_TCP, errorno ) );
server_print( "[Online Map] Waiting for requests on port %d", 1107 );
register_event( "BombDrop", "bombDrop", "a" );
register_logevent( "roundEnd", 2, "1=Round_End" );
get_mapname( MapName, charsmax( MapName ) );
set_task( 0.1, "OnSocketReply", .flags = "b" );
}
public plugin_end()
{
socket_close( Socket );
}
public bombDrop( const player )
{
BombPos[ 0 ] = read_data( 1 );
BombPos[ 1 ] = read_data( 2 );
BombPos[ 2 ] = read_data( 3 );
BombPos[ 3 ] = read_data( 4 );
}
public roundEnd(id)
{
BombPos[ 3 ] = 0;
}
public OnSocketReply()
{
static const requestHeader [] = "HTTP/1.1 200 OK^r^nServer: Online Map^r^nConnection: close^r^nContent-Type: text/plain^r^n^r^n";
static const requestMapHeader [] = "({^r^n^t^"bombpos^": [ ^"%d^", ^"%d^", ^"%d^", ^"%d^" ], ^r^n^t^"map^": ^"%s^",^r^n^t^"users^": [";
static const requestPlayerHeader[] = "^r^n^t{^r^n^t^t^"name^": ^"%s^",^r^n^t^t^"status^": [ ^"%d^", ^"%d^", ^"%d^", ^"%d^" ],^r^n^t^t^"position^": [ ^"%d^", ^"%d^", ^"%d^" ],^r^n^t^t^"angle^": [ ^"%d^", ^"%d^", ^"%d^" ],^r^n^t^t^"stats^": [ ^"%d^", ^"%d^" ]^r^n^t},^r^n";
static const requestEndHeader [] = "]^r^n})";
static request;
if( ( request = socket_accept( Socket ) ) > 0 )
{
static requestData[ 2047 ];
static requestRecv[ 256 ];
static length;
static result;
socket_recv( request, requestRecv, charsmax( requestRecv ) );
length = copy( requestData, charsmax( requestData ), requestHeader );
if( regex_match_c( requestRecv, RegexHandle, result ) )
{
static match[ 64 ];
regex_substr( RegexHandle, 1, match, charsmax( match ) );
length += copy( requestData[ length ] , charsmax( requestData ) - length, match );
}
length += formatex( requestData[ length ], charsmax( requestData ) - length, requestMapHeader, BombPos[0], BombPos[1], BombPos[2], BombPos[3], MapName) ;
static playersList[ 32 ], playersCount, player;
static playerName[ 32 ];
static angles[ 3 ];
static origin[ 3 ];
static i;
get_players( playersList, playersCount );
for( i = 0; i < playersCount; i++ )
{
player = playersList[i];
get_user_name( player, playerName, charsmax( playerName ) );
get_user_origin( player, origin, 0 );
get_user_origin( player, angles, 3 );
length += formatex( requestData[ length ], charsmax( requestData ) - length, requestPlayerHeader,
playerName,
is_user_alive( player ),
cs_get_user_team( player ),
get_user_weapon( player ),
get_user_health( player ),
origin[0], origin[1], origin[2],
angles[0], angles[1], angles[2],
get_user_frags( player ),
get_user_deaths( player ) );
}
length += copy( requestData[ length ] , charsmax( requestData ) - length, requestEndHeader );
socket_send( request, requestData, length );
socket_close( request )
}
}