format
[PHP-P10.git] / Bots / PulseBot.class.php
index 63d3e4a0269f5c042c2f68b4327b3bfd41c469ab..961d9747a6b7c0756bd7a42159aacf20f98a38b8 100644 (file)
@@ -1,12 +1,12 @@
 <?php
 /******************************* PHP-P10 v2 *****************************
- * Copyright (C) 2011  Philipp Kreil (pk910)                            *
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
  *                                                                      *
  * This program is free software: you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
  * the Free Software Foundation, either version 3 of the License, or    *
  * (at your option) any later version.                                  *
- *                                                                      * 
+ *                                                                      *
  * This program is distributed in the hope that it will be useful,      *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
@@ -16,7 +16,7 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  *                                                                      *
  ************************************************************************
- * 
+ *
  *  Bots/PulseBot.class.php
  *
  * WhatPulse bot...
@@ -28,9 +28,9 @@ class {$_NAME} extends Bot {
        private $pulsebot;
        private $cache = array();
        private $db = array();
-       
+
        const TEAM_ID = 19418;
-       
+
        public function load($uplink, $old = false) {
                $this->uplink = $uplink;
                if(!$old) {
@@ -48,12 +48,12 @@ class {$_NAME} extends Bot {
                } else {
                        $this->pulsebot = $old;
                }
-               BotLoader::registerDB($this, "PulseBot");
+               BotLoader::registerDB($this, "pulsebot");
                ModCMD::bind($this, BIND_CHANMSG, "recive_privmsg");
                ModCMD::bind($this, BIND_JOIN, "recive_join");
                ModCMD::bind($this, BIND_QUIT, "recive_quit");
        }
-       
+
        public function unload($rehash = false) {
                if($rehash) {
                        return $this->pulsebot;
@@ -61,15 +61,15 @@ class {$_NAME} extends Bot {
                        $this->uplink->delUser($this->pulsebot, "Bye.");
                }
        }
-       
+
        public function readDB($db) {
                $this->db = $db;
        }
-       
+
        public function writeDB() {
                return $this->db;
        }
-       
+
        private function getStats() {
                if(array_key_exists("stats", $this->cache) && time() - $this->cache['stats_time'] < 300) {
                        //use cached stats
@@ -82,7 +82,7 @@ class {$_NAME} extends Bot {
                }
                return $stats;
        }
-       
+
        private function getUserStats($name) {
                if(array_key_exists("ustats_".strtolower($name), $this->cache) && time() - $this->cache['ustats_'.strtolower($name).'_time'] < 300) {
                        //use cached stats
@@ -90,24 +90,24 @@ class {$_NAME} extends Bot {
                        return $ustats;
                }
                $stats = $this->getStats();
-               $users = $stats->TeamStats->Members;
+               $users = $stats->TeamStats->Members->Member;
                foreach ($users as $user) {
                        if(strtolower($user->MemberName) == strtolower($name)) {
                                $xmlstr = file_get_contents("http://whatpulse.org/api/user.php?UserID=".$user->MemberUserID);
                                $ustats = new SimpleXMLElement($xmlstr);
-                               $this->cache["ustats_".strtolower($name)] = $stats;
+                               $this->cache["ustats_".strtolower($name)] = $ustats;
                                $this->cache["ustats_".strtolower($name)."_time"] = time();
                                return $ustats;
                        }
                }
                return null;
        }
-       
+
        private function botOppedOnChannel($channel) {
                $privs = $channel->getUserPrivs($this->pulsebot);
                return ($privs & P10_Channel::USERPRIV_OPED);
        }
-       
+
        function recive_privmsg($user, $channel, $message) {
                if(!$this->botOppedOnChannel($channel)) return;
                $privs = $channel->getUserPrivs($user);
@@ -122,7 +122,7 @@ class {$_NAME} extends Bot {
                                        $pulseUserName = $exp[1];
                                        $stats = $this->getStats();
                                        $pulseUser = null;
-                                       $users = $stats->TeamStats->Members;
+                                       $users = $stats->TeamStats->Members->Member;
                                        foreach ($users as $puser) {
                                                if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
                                                        $pulseUser = $puser;
@@ -145,8 +145,8 @@ class {$_NAME} extends Bot {
                                        if(!array_key_exists("users", $this->db)) {
                                                $this->db['users'] = array();
                                        }
-                                       $this->db['users'][$auth] = $pulseUser->MemberName;
-                                       $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' added (Keys: ".$pulseUser->MemberKeys.", Clicks: ".$pulseUser->MemberClicks.").");
+                                       $this->db['users'][$auth] = strval($pulseUser->MemberName);
+                                       $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$pulseUserName."' added (Keys: ".number_format(floatval($pulseUser->MemberKeys),0,',','.').", Clicks: ".number_format(floatval($pulseUser->MemberClicks),0,',','.').").");
                                }
                                break;
                        case ".deluser":
@@ -164,13 +164,13 @@ class {$_NAME} extends Bot {
                                        }
                                }
                                if(!$f)
-                                       $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' is not added.");
+                               $this->uplink->notice($this->pulsebot, $user, "WhatPulse user '".$exp[1]."' is not added.");
                                break;
                        case ".users":
                                $table = new Table(5);
                                $table->add("User", "Keys", "Clicks", "Miles", "IRC");
                                $stats = $this->getStats();
-                               $users = $stats->TeamStats->Members;
+                               $users = $stats->TeamStats->Members->Member;
                                if(!array_key_exists("users", $this->db)) {
                                        $this->db['users'] = array();
                                }
@@ -182,14 +182,14 @@ class {$_NAME} extends Bot {
                                                        break;
                                                }
                                        }
-                                       $table->add($puser->MemberName, $puser->MemberKeys, $puser->MemberClicks, $puser->MemberMiles, $ircUserName);
+                                       $table->add($puser->MemberName, number_format(floatval($puser->MemberKeys),0,',','.'), number_format(floatval($puser->MemberClicks),0,',','.'), number_format(floatval($puser->MemberMiles),2,',','.'), $ircUserName);
                                }
                                $lines = $table->end();
                                foreach($lines as $line) {
                                        if(count($exp) > 1)
-                                               $this->uplink->privmsg($this->pulsebot, $channel, $line);
+                                       $this->uplink->privmsg($this->pulsebot, $channel, $line);
                                        else
-                                               $this->uplink->notice($this->pulsebot, $user, $line);
+                                       $this->uplink->notice($this->pulsebot, $user, $line);
                                }
                                break;
                        case ".mypulse":
@@ -207,7 +207,7 @@ class {$_NAME} extends Bot {
                                $pulseUser = null;
                                if($pulseUserName) {
                                        $stats = $this->getStats();
-                                       $users = $stats->TeamStats->Members;
+                                       $users = $stats->TeamStats->Members->Member;
                                        foreach ($users as $puser) {
                                                if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
                                                        $pulseUser = $puser;
@@ -220,8 +220,9 @@ class {$_NAME} extends Bot {
                                        break;
                                }
                                $ustats = $this->getUserStats($pulseUser->MemberName);
-                               $message = "User \002".$pulseUser->MemberName."\002 has \002".$pulseUser->MemberKeys."\002 keys (".$ustats->AvKPS." per second), \002".$pulseUser->MemberClicks."\002 clicks (".$ustats->AvCPS." per second) and ".$pulseUser->MemberMiles." miles.\n";
-                               $message .= "Last Pulse: ".$ustats->LastPulse;
+                               $message = array();
+                               $message[] = "User \002".$pulseUser->MemberName."\002 has \002".number_format(floatval($pulseUser->MemberKeys),0,',','.')."\002 keys (".number_format(floatval($ustats->AvKPS),2,',','.')." per second), \002".number_format(floatval($pulseUser->MemberClicks),0,',','.')."\002 clicks (".number_format(floatval($ustats->AvCPS),2,',','.')." per second) and ".number_format(floatval($pulseUser->MemberMiles),2,',','.')." miles.";
+                               $message[] = "Last Pulse: ".$ustats->LastPulse;
                                if(count($exp) > 1) {
                                        foreach($message as $line) {
                                                $this->uplink->privmsg($this->pulsebot, $channel, $line);
@@ -234,7 +235,7 @@ class {$_NAME} extends Bot {
                                break;
                }
        }
-       
+
        public function recive_join($user, $channel, $isBurst) {
                if(!$this->botOppedOnChannel($channel)) return false;
                if(!($auth = $user->getModes()->hasMode('r'))) return;
@@ -248,7 +249,7 @@ class {$_NAME} extends Bot {
                $pulseUser = null;
                if($pulseUserName) {
                        $stats = $this->getStats();
-                       $users = $stats->TeamStats->Members;
+                       $users = $stats->TeamStats->Members->Member;
                        foreach ($users as $puser) {
                                if(strtolower($puser->MemberName) == strtolower($pulseUserName)) {
                                        $pulseUser = $puser;
@@ -258,10 +259,10 @@ class {$_NAME} extends Bot {
                }
                if($pulseUser) {
                        $this->uplink->mode($this->pulsebot, $channel, "+v ".$user->getNumeric());
-                       $this->uplink->privmsg($this->pulsebot, $channel, "WhatPulse User \002".$pulseUser->MemberName."\002 (\002".$pulseUser->MemberKeys."\002 keys and \002".$pulseUser->MemberClicks."\002 clicks) has joined the channel.");
+                       $this->uplink->privmsg($this->pulsebot, $channel, "WhatPulse User \002".$pulseUser->MemberName."\002 (\002".number_format(floatval($pulseUser->MemberKeys),0,',','.')."\002 keys and \002".number_format(floatval($pulseUser->MemberClicks),0,',','.')."\002 clicks) has joined the channel.");
                }
        }
-       
+
        public function recive_quit($user, $reason) {
                if($user === $this->pulsebot) {
                        $this->load($this->uplink);