@@ -16,27 +16,42 @@ public function __construct()
1616 //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
1717 }
1818
19+ private function getToken ()
20+ {
21+ $ res = parent ::execute ($ this ->url ('api/v2/auth/login ' ), $ this ->authAttrs ());
22+ $ data = json_decode ($ res ->getBody ());
23+ if ($ data ->status == 'ok ' ) {
24+ $ this ->token = $ data ->data ->token ;
25+ }
26+ }
27+
1928 public function test ()
2029 {
21- $ test = parent ::appTest ($ this ->url ('api/v2/auth/login ' ), $ this ->authAttrs ());
22- echo $ test ->status ;
30+ try {
31+ $ this ->getToken ();
32+ echo "Successfully communicated with the API " ;
33+ } catch (Exception $ err ) {
34+ echo $ err ->getMessage ();
35+ }
2336 }
2437
2538 public function livestats ()
2639 {
2740 $ status = "inactive " ;
28- $ res = parent ::execute ($ this ->url ('api/v2/auth/login ' ), $ this ->authAttrs ());
29- $ data = json_decode ($ res ->getBody ());
30- if ($ data ->status == 'ok ' ) {
31- $ this ->token = $ data ->data ->token ;
32- }
41+ $ this ->getToken ();
3342
34- $ res = parent ::execute ($ this ->url ('api/v2/servers ' ), attrs: $ this ->attrs (), overridemethod: 'GET ' );
43+ $ res = parent ::execute (
44+ $ this ->url ('api/v2/servers ' ),
45+ attrs: $ this ->attrs (),
46+ overridemethod: 'GET '
47+ );
3548 $ details = json_decode ($ res ->getBody ());
3649
3750 $ vars ['servers_total ' ] = count ($ details ->data );
3851
39- $ online = 0 ;
52+ $ servers_online = 0 ;
53+ $ players_online = 0 ;
54+ $ mem = 0 ;
4055
4156 foreach ($ details ->data as $ server ) {
4257 $ server_res = parent ::execute (
@@ -45,42 +60,76 @@ public function livestats()
4560 overridemethod: 'GET '
4661 );
4762 $ server_details = json_decode ($ server_res ->getBody ());
63+ $ players_online += $ server_details ->data ->online ;
64+ $ mem += $ this ->decodeSizeToGB ($ server_details ->data ->mem );
4865 if ($ server_details ->data ->running == true ) {
49- $ online ++;
66+ $ servers_online ++;
5067 }
5168 }
5269
53- $ vars ['servers_online ' ] = $ online ;
70+ $ vars ['servers_online ' ] = $ servers_online ;
71+ $ vars ['players_online ' ] = $ players_online ;
72+ $ vars ['mem ' ] = $ mem ;
5473
5574 return parent ::getLiveStats ($ status , $ vars );
5675 }
5776
5877 private function authAttrs ()
5978 {
79+ $ ignoreTls = $ this ->getConfigValue ("ignore_tls " , false );
6080 return [
6181 "body " => json_encode ([
62- "username " => $ this ->config -> username ,
63- "password " => $ this ->config -> password
82+ "username " => $ this ->getConfigValue ( " username " , null ) ,
83+ "password " => $ this ->getConfigValue ( " password " , null )
6484 ]),
65- "verify " => false
85+ "verify " => ( $ ignoreTls ? false : true )
6686 ];
6787 }
6888
6989
7090 public function attrs ()
7191 {
72- $ attrs ["headers " ] = [
73- "content-type " => "application/json " ,
74- "Authorization " => "Bearer " . $ this ->token ,
92+ $ ignoreTls = $ this ->getConfigValue ("ignore_tls " , false );
93+ return [
94+ "headers " => [
95+ "content-type " => "application/json " ,
96+ "Authorization " => "Bearer " . $ this ->token ,
97+ ],
98+ "verify " => ($ ignoreTls ? false : true )
7599 ];
76- $ attrs ["verify " ] = false ;
77-
78- return $ attrs ;
79100 }
80101
81102 public function url ($ endpoint )
82103 {
83104 $ api_url = parent ::normaliseurl ($ this ->config ->url ) . $ endpoint ;
84105 return $ api_url ;
85106 }
107+
108+ private function getConfigValue ($ key , $ default = null )
109+ {
110+ return isset ($ this ->config ) && isset ($ this ->config ->$ key )
111+ ? $ this ->config ->$ key
112+ : $ default ;
113+ }
114+
115+ private function decodeSizeToGB ($ size )
116+ {
117+ $ units = [
118+ 'B ' => 1 / (1024 * 1024 * 1024 ), // Convert bytes to GB
119+ 'KB ' => 1 / (1024 * 1024 ), // Convert KB to GB
120+ 'MB ' => 1 / 1024 , // Convert MB to GB
121+ 'GB ' => 1 , // GB is the base unit
122+ 'TB ' => 1024 , // Convert TB to GB
123+ 'PB ' => 1024 * 1024 , // Convert PB to GB
124+ ];
125+
126+ if (preg_match ('/^([\d\.]+)\s*([KMGTP]?B)$/i ' , strtoupper ($ size ), $ matches )) {
127+ $ value = (float )$ matches [1 ];
128+ $ unit = $ matches [2 ];
129+
130+ return $ value * ($ units [$ unit ] ?? 1 );
131+ }
132+
133+ return false ; // Invalid format
134+ }
86135}
0 commit comments