root/tools/getlogincount @ 1

Revision 1, 3.5 kB (checked in by jinshiro, 17 years ago)
RevLine 
[1]1#!/usr/bin/perl -w
2
3##########################################################################
4# INFORMATION TOOL ABOUT THE # OF ONLINE PLAYERS ON ATHENA SERVERS
5#
6# By connection on the athena login-server, this software displays the
7# number of online players.
8#
9#-------------------------------------------------------------------------
10# Software usage:
11#   Configure the IP, the port and a valid account of the server.
12#   After, use at your choice:
13#   ./getlogincount - display the number of online players on all servers.
14#   ./getlogincount --premier or
15#   ./getlogincount --first -- display the number of online players of the
16#                              first server in the received list.
17#   ./getlogincount [servername] -- display the number of online players
18#                                   of the specified server.
19#
20# If successfull, the software return the value 0.
21#
22##########################################################################
23
24#------------------------------ CONFIGURATION ----------------------------
25
26$loginserverip = "127.0.0.1";  # IP of the login-server
27$loginserverport = 6900;       # port of the login-server
28$loginaccount = "s1";          # a valid account name
29$loginpasswd = "p1";           # the password of the valid account name
30
31$connecttimeout = 10;          # Connection timeout (in seconds)
32
33#-------------------------------------------------------------------------
34
35use IO::Socket;
36
37my($sname) = $ARGV[0];
38if (!defined($sname)) {
39        $sname = "";
40}
41
42# Connection to the login-server
43my($so,$er) = ();
44eval{
45        $so = IO::Socket::INET->new(
46                PeerAddr=> $loginserverip,
47                PeerPort=> $loginserverport,
48                Proto   => "tcp",
49                Timeout => $connecttimeout) or $er=1;
50};
51if($er || $@){
52        print "Can't not connect to the login-server [${loginserverip}:$loginserverport] !\n";
53        exit(2);
54}
55
56# Request to connect on login-server
57print $so pack("v V a24 a24 C",0x0064,9,$loginaccount,$loginpasswd,3);
58$so->flush();
59
60# Fail to connect
61if(unpack("v", &soread(\$so,2)) != 0x0069) {
62        print "Login error.\n";
63        exit(3);
64}
65
66# Get length of the received packet
67my($plen) = unpack("v",&soread(\$so,2))-4;
68
69# Suppress information of the account (we need only information about the servers)
70&soread(\$so,43);
71$plen -= 43;
72
73# Check about the number of online servers
74if ($plen < 32) {
75        printf "No server is connected to login-server.\n";
76        exit(1);
77}
78
79# Read information of the servers
80my(@slist) = ();
81for(;$plen > 0;$plen -= 32) {
82        my($name,$count) = unpack("x6 a20 V",&soread(\$so,32));
83        $name = substr($name,0,index($name,"\0"));
84        push @slist, [ $name, $count ];
85}
86
87# Display the result
88if($sname eq "--first" || $sname eq "--premier") { # If we ask only for the first server
89        printf "%-20s : %5d\n",$slist[0][0],$slist[0][1];
90} elsif ($sname eq "") { # If we ask for all servers
91        foreach $i(@slist) {
92                printf "%-20s : %5d\n",$i->[0],$i->[1];
93        }
94} else { # If we ask for a specified server (by its name)
95        my($flag) = 1;
96        foreach $i(@slist) {
97                if($i->[0] eq $sname) {
98                        printf "%-20s : %5d\n",$i->[0],$i->[1];
99                        $flag = 0;
100                }
101        }
102        if($flag) { # If the server doesn't exist
103                printf "The server [$sname] doesn't exist.\n";
104                exit(1);
105        }
106}
107
108# End of the software
109$so->shutdown(2);
110$so->close();
111exit(0);
112
113# Sub-function: get data from the socket
114sub soread {
115        my($so,$len) = @_;
116        my($sobuf);
117        if(read($$so,$sobuf,$len) < $len) {
118                print "Socket read error.\n";
119                exit(5);
120        }
121        return $sobuf;
122};
Note: See TracBrowser for help on using the browser.