AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [ socket_recv + json/data ] (https://forums.alliedmods.net/showthread.php?t=254671)

Xablau 01-04-2015 14:49

[ socket_recv + json/data ]
 
See my code:

PHP Code:

public client_connect(id)
{
    
get_player_info(id)
}
public 
get_player_info(id
{
    new 
ip[64], nick[32], steamid[32], errorsendbuffer[512]
    
get_user_ip(0,ip,63,0// IP
    
get_user_name(id,nick31// Nick
    
get_user_authid(id,steamid,31// SteamID
    
    
g_Socket socket_open("site.com"80SOCKET_TCPerror
    
    
format(sendbuffer511"GET %s HTTP/1.1^nHost:site.com^r^n^r^n""/player.php?steamid=%s&server=%s&nick=%s"steamidipnick)
    
socket_send(g_Socketsendbuffer511)
    
    
// It changed? 
    
if (socket_change(g_Socket)) 
    { 
        
// Get the data 
        
socket_recv(g_Socketg_Data999)
    }
    
socket_close(g_Socket


site.com/player.php will return a data dictionary to player.
How do I interpret this dictionary?

I need the dictionary is in json. Or have another best method?

MetalMessiah 01-04-2015 15:12

Re: [ socket_recv + json/data ]
 
There is NO json parser at AmxmodX as I know.
Use signatures, CSV or any other simple format. Or write your own JSON parser.

P.S. do it at client_putinserver(id) otherwise you will have DoS on your web server or MYSQL in case of many connections / fake player flood etc.

P.S.2 server= ?? MAybe &ip= ???

klippy 01-04-2015 15:16

Re: [ socket_recv + json/data ]
 
There is a JSON parser written in pure Pawn by Exolent (search), and another one written as a module by Exeorb: https://forums.alliedmods.net/showthread.php?t=244605

YamiKaitou 01-04-2015 15:27

Re: [ socket_recv + json/data ]
 
Can you show us an example of the returned data?

Xablau 01-04-2015 15:32

Re: [ socket_recv + json/data ]
 
Quote:

Originally Posted by YamiKaitou (Post 2244783)
Can you show us an example of the returned data?

PHP Code:

{
      
server:
      {
            
"success":true
      
},
      
player:
      {
            
"maxhp":110,
            
"hat":"squash"
      
}



YamiKaitou 01-04-2015 15:35

Re: [ socket_recv + json/data ]
 
If you don't want to write your own parser, then try using https://forums.alliedmods.net/showthread.php?t=189772

Xablau 01-04-2015 15:38

Re: [ socket_recv + json/data ]
 
Quote:

Originally Posted by YamiKaitou (Post 2244788)
If you don't want to write your own parser, then try using https://forums.alliedmods.net/showthread.php?t=189772

Tanks :}

Xablau 01-04-2015 15:39

Re: [ socket_recv + json/data ]
 
Quote:

Originally Posted by MetalMessiah (Post 2244772)
There is NO json parser at AmxmodX as I know.
Use signatures, CSV or any other simple format. Or write your own JSON parser.

P.S. do it at client_putinserver(id) otherwise you will have DoS on your web server or MYSQL in case of many connections / fake player flood etc.

P.S.2 server= ?? MAybe &ip= ???

Thank U. I changed to client_putinserver

Xablau 01-05-2015 06:15

Re: [ socket_recv + json/data ]
 
PHP Code:

public client_putinserver(id)
{
    
get_data(id)
}
public 
get_data(id
{
    new 
ip[16], server[16], nick[32],steamid[32], errorsendbuffer[512]
    
get_user_ip(0,ip,63,0)
    
get_user_name(id,nick31)
    
get_user_authid(id,steamid,31)
    
    
g_Socket socket_open("site.com"80SOCKET_TCPerror
    
    
format(sendbuffer511"GET %s HTTP/1.1^nHost:site.com^r^n^r^n""/player.php?steamid=%s&ip=%s&nick=%s"steamidipnick)
    
socket_send(g_Socketsendbuffer511)
    
    const 
SIZE 63
    
new line_variable[SIZE 1], line_value[SIZE 1]
    
    if (
socket_change(g_Socket))
    {
        new 
lines[30][100], count 0
        socket_recv
(g_Socketg_Data511)
        
count ExplodeString(lines50119g_Data13)
        for(new 
i=0;i<count;i++)
        {
            
parse(lines[i], line_variableSIZEline_valueSIZE)
            if (
equal(line_variable"max_hp"))
                
set_user_health(idline_value)
            if (
equal(line_variable"max_armor"))
                
set_user_armor(idline_value)
        }   
    }
    
socket_close(g_Socket
}
stock ExplodeStringp_szOutput[][], p_nMaxp_nSizep_szInput[], p_szDelimiter ) { // Function by xeroblood
    
new nIdx 0strlen(p_szInput)
    new 
nLen = (copycp_szOutput[nIdx], p_nSizep_szInputp_szDelimiter ))
    while( (
nLen l) && (++nIdx p_nMax) )
        
nLen += (copycp_szOutput[nIdx], p_nSizep_szInput[nLen], p_szDelimiter ))
    return 
nIdx


Work on player.php, example:

"max_hp" "110"
"max_armor" "120"
"value" "data"
...

I need error:

Error: Argument type mismatch (argument 2) on line 45
Line 45:
PHP Code:

set_user_health(idline_value

How to fix?

YamiKaitou 01-05-2015 08:53

Re: [ socket_recv + json/data ]
 
Line_value should be an integer, not an array/string


All times are GMT -4. The time now is 10:51.

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