How to query a CS Server

From Scriptwiki
Revision as of 13:27, 14 April 2006 by Doomie (talk | contribs) (doesnt work + weird text - needs rewriting perhaps)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

If you want to e.g. get the umber and names of all player who are currently playing on a CS-server, you have to send a so-called "query" to this server (using UDP) and handle everything the server sends back to you. This isn't as easy as it sounds. At first, you would have to 'learn', or, at least, understand the protocol a CS-server uses (e.g. what do I have to send to the server to get a 'good' reply and what's the structure of this reply). If you want to learn more about these protocols, take a look at

The following script echos the output to your active window. You will have to modify it to be able to use it as 'bot'. It's a more complex script and you don't need to understand it (completly) to be able to use it.

; this is a handy alias to call the script. It will call the "hlx" alias with an ip and a port as argument
; you can add more aliases like this one.
; if you want to all "hlq" directly, you can use something like: "/hlq <ip> <port>"
alias hlx { hlq <ip> <port> }

; the main alias to open a new socket connections
; syntax: /hlq <ip> <port> 
alias hlq {
 sockudp -k hlq $1-2 $+($str($chr(255),4),players)

; this event will read every data from the cs server
on *:udpread:hlq*:{
 ; we put it in a binary variable
 sockread -f &data
 ; the sixth byte is the number of players, so we set it to a variable 
 var %players = $bvar(&data, 6, 1)
 var %offset = 7
 ; that's a variable for the while loop some lines below
 var %d = 1
 ; return a line seperator (usually "-")
 ; echo the number of players
 echo -a %players Players
 ; now we are looping through all players to get some stats about them
 while (%d <= %players) {
   ; the following lines are getting these stats. If you want to learn more about this, take a look
   ; at
   ; we get the playernumber
   var %playernum = $bvar(&data, %offset, 1)
   inc %offset
   ; the name of the player
   var %name = $bvar(&data, %offset, 200).text
   inc %offset $calc($len(%name) + 1)
   ; the current score
   var %score = $bvar(&data, %offset, 4)
   inc %offset 4
   inc %offset 4
   ; and echo everything
   echo -a %playernum - name: %name - score: $be2le(%score)
   ; increase the variable for the while loop to get stats about the next player  
   inc %d
 ; at the end, we are closing the socket to be able to open a new one later
 sockclose $sockname
; this alias converts 'big endians' into 'little endians'
alias be2le {
 tokenize 32 $1 | return $calc($1 + $2 * 2^8 + $4 * 2^16 + $3 * 2^24)