1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: use stdClass;
6: use Budabot\Core\CommandAlias;
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class UsageController {
24: 25: 26: 27:
28: public $moduleName;
29:
30:
31: public $db;
32:
33:
34: public $http;
35:
36:
37: public $settingManager;
38:
39:
40: public $eventManager;
41:
42:
43: public $util;
44:
45:
46: public $text;
47:
48:
49: public $chatBot;
50:
51: 52: 53:
54: public function setup() {
55: $this->db->loadSQLFile($this->moduleName, 'usage');
56:
57: $this->settingManager->add($this->moduleName, "record_usage_stats", "Record usage stats", "edit", "options", "1", "true;false", "1;0");
58: $this->settingManager->add($this->moduleName, 'botid', 'Botid', 'noedit', 'text', '');
59: $this->settingManager->add($this->moduleName, 'last_submitted_stats', 'last_submitted_stats', 'noedit', 'text', 0);
60: }
61:
62: 63: 64: 65: 66:
67: public function usagePlayerCommand($message, $channel, $sender, $sendto, $args) {
68: if (count($args) == 3) {
69: $time = $this->util->parseTime($args[2]);
70: if ($time == 0) {
71: $msg = "Please enter a valid time.";
72: $sendto->reply($msg);
73: return;
74: }
75: $time = $time;
76: } else {
77: $time = 604800;
78: }
79:
80: $timeString = $this->util->unixtimeToReadable($time);
81: $time = time() - $time;
82:
83: $player = ucfirst(strtolower($args[1]));
84:
85: $sql = "SELECT command, COUNT(command) AS count FROM usage_<myname> WHERE sender = ? AND dt > ? GROUP BY command ORDER BY count DESC";
86: $data = $this->db->query($sql, $player, $time);
87: $count = count($data);
88:
89: if ($count > 0) {
90: $blob .= '';
91: forEach ($data as $row) {
92: $blob .= "<highlight>{$row->command}<end> ({$row->count})\n";
93: }
94:
95: $msg = $this->text->makeBlob("Usage for $player - $timeString ($count)", $blob);
96: } else {
97: $msg = "No usage statistics found for <highlight>$player<end>.";
98: }
99: $sendto->reply($msg);
100: }
101:
102: 103: 104: 105: 106:
107: public function usageCmdCommand($message, $channel, $sender, $sendto, $args) {
108: if (count($args) == 3) {
109: $time = $this->util->parseTime($args[2]);
110: if ($time == 0) {
111: $msg = "Please enter a valid time.";
112: $sendto->reply($msg);
113: return;
114: }
115: $time = $time;
116: } else {
117: $time = 604800;
118: }
119:
120: $timeString = $this->util->unixtimeToReadable($time);
121: $time = time() - $time;
122:
123: $cmd = strtolower($args[1]);
124:
125: $sql = "SELECT sender, COUNT(sender) AS count FROM usage_<myname> WHERE command = ? AND dt > ? GROUP BY sender ORDER BY count DESC";
126: $data = $this->db->query($sql, $cmd, $time);
127: $count = count($data);
128:
129: if ($count > 0) {
130: $blob .= '';
131: forEach ($data as $row) {
132: $blob .= "<highlight>{$row->sender}<end> ({$row->count})\n";
133: }
134:
135: $msg = $this->text->makeBlob("Usage for $cmd - $timeString ($count)", $blob);
136: } else {
137: $msg = "No usage statistics found for <highlight>$cmd<end>.";
138: }
139: $sendto->reply($msg);
140: }
141:
142: 143: 144: 145: 146:
147: public function usageCommand($message, $channel, $sender, $sendto, $args) {
148: if (count($args) == 2) {
149: $time = $this->util->parseTime($args[1]);
150: if ($time == 0) {
151: $msg = "Please enter a valid time.";
152: $sendto->reply($msg);
153: return;
154: }
155: $time = $time;
156: } else {
157: $time = 604800;
158: }
159:
160: $timeString = $this->util->unixtimeToReadable($time);
161: $time = time() - $time;
162: $limit = 25;
163:
164:
165: $sql = "SELECT type, COUNT(type) cnt FROM usage_<myname> WHERE dt > ? GROUP BY type ORDER BY type";
166: $data = $this->db->query($sql, $time);
167:
168: $blob = "<header2>Channel Usage<end>\n";
169: forEach ($data as $row) {
170: if ($row->type == "msg") {
171: $blob .= "Number of commands executed in tells: <highlight>$row->cnt<end>\n";
172: } else if ($row->type == "priv") {
173: $blob .= "Number of commands executed in private channel: <highlight>$row->cnt<end>\n";
174: } else if ($row->type == "guild") {
175: $blob .= "Number of commands executed in guild channel: <highlight>$row->cnt<end>\n";
176: }
177: }
178: $blob .= "\n";
179:
180:
181: $sql = "SELECT command, COUNT(command) AS count FROM usage_<myname> WHERE dt > ? GROUP BY command ORDER BY count DESC LIMIT ?";
182: $data = $this->db->query($sql, $time, $limit);
183:
184: $blob .= "<header2>$limit Most Used Commands<end>\n";
185: forEach ($data as $row) {
186: $commandLink = $this->text->makeChatcmd($row->command, "/tell <myname> usage cmd $row->command");
187: $blob .= "{$commandLink} ({$row->count})\n";
188: }
189:
190:
191: $sql = "SELECT sender, COUNT(sender) AS count FROM usage_<myname> WHERE dt > ? GROUP BY sender ORDER BY count DESC LIMIT ?";
192: $data = $this->db->query($sql, $time, $limit);
193:
194: $blob .= "\n<header2>$limit Most Active Users<end>\n";
195: forEach ($data as $row) {
196: $senderLink = $this->text->makeChatcmd($row->sender, "/tell <myname> usage player $row->sender");
197: $blob .= "{$senderLink} ({$row->count})\n";
198: }
199:
200: $msg = $this->text->makeBlob("Usage Statistics - $timeString", $blob);
201: $sendto->reply($msg);
202: }
203:
204: public function record($type, $cmd, $sender, $handler) {
205:
206: if ($cmd == 'grc' || "CommandAlias.process" == $handler) {
207: return;
208: }
209:
210: $sql = "INSERT INTO usage_<myname> (type, command, sender, dt) VALUES (?, ?, ?, ?)";
211: $this->db->exec($sql, $type, $cmd, $sender, time());
212: }
213:
214: 215: 216: 217: 218:
219: public function submitAnonymousUsage($eventObj) {
220: $debug = false;
221: $time = time();
222: $settingName = 'last_submitted_stats';
223: $lastSubmittedStats = $this->settingManager->get($settingName);
224:
225: $postArray['stats'] = json_encode($this->getUsageInfo($lastSubmittedStats, $time, $debug));
226:
227: $url = 'http://stats.budabot.jkbff.com/stats/submitUsage.php';
228: $this->http->post($url)->withQueryParams($postArray);
229:
230: $this->settingManager->save($settingName, $time);
231: }
232:
233: public function getUsageInfo($lastSubmittedStats, $now, $debug = false) {
234: global $version;
235:
236: $botid = $this->settingManager->get('botid');
237: if ($botid == '') {
238: $botid = $this->util->genRandomString(20);
239: $this->settingManager->save('botid', $botid);
240: }
241:
242: $sql = "SELECT type, command FROM usage_<myname> WHERE dt >= ? AND dt < ?";
243: $data = $this->db->query($sql, $lastSubmittedStats, $now);
244:
245: $settings = array();
246: $settings['dimension'] = $this->chatBot->vars['dimension'];
247: $settings['is_guild_bot'] = ($this->chatBot->vars['my_guild'] == '' ? '0' : '1');
248: $settings['guildsize'] = $this->getGuildSizeClass(count($this->chatBot->guildmembers));
249: $settings['using_chat_proxy'] = $this->chatBot->vars['use_proxy'];
250: $settings['db_type'] = $this->db->getType();
251: $settings['bot_version'] = $version;
252: $settings['using_git'] = (file_exists("./modules/GIT_MODULE/GitController.class.php") === true ? '1' : '0');
253: $settings['os'] = (\budabot\core\isWindows() === true ? 'Windows' : 'Other');
254:
255: $settings['symbol'] = $this->settingManager->get('symbol');
256: $settings['relay_enabled'] = ($this->settingManager->get('relaybot') == 'Off' ? '0' : '1');
257: $settings['relay_type'] = $this->settingManager->get('relaytype');
258: $settings['first_and_last_alt_only'] = $this->settingManager->get('first_and_last_alt_only');
259: $settings['aodb_db_version'] = $this->settingManager->get('aodb_db_version');
260: $settings['max_blob_size'] = $this->settingManager->get('max_blob_size');
261: $settings['online_show_org_guild'] = $this->settingManager->get('online_show_org_guild');
262: $settings['online_show_org_priv'] = $this->settingManager->get('online_show_org_priv');
263: $settings['online_admin'] = $this->settingManager->get('online_admin');
264: $settings['relay_symbol_method'] = $this->settingManager->get('relay_symbol_method');
265: $settings['http_server_enable'] = ($this->eventManager->getKeyForCronEvent("60", "httpservercontroller.startHTTPServer") != null ? "1" : "0");
266: $settings['tower_attack_spam'] = $this->settingManager->get('tower_attack_spam');
267:
268: $obj = new stdClass;
269: $obj->id = sha1($botid . $this->chatBot->vars['name'] . $this->chatBot->vars['dimension']);
270: $obj->version = "2";
271: $obj->debug = ($debug == true ? '1' : '0');
272: $obj->commands = $data;
273: $obj->settings = $settings;
274:
275: return $obj;
276: }
277:
278: public function getGuildSizeClass($size) {
279: $guildClass = "";
280: if ($size == 0) {
281: $guildClass = "class0";
282: } else if ($size < 10) {
283: $guildClass = "class1";
284: } else if ($size < 30) {
285: $guildClass = "class2";
286: } else if ($size < 150) {
287: $guildClass = "class3";
288: } else if ($size < 300) {
289: $guildClass = "class4";
290: } else if ($size < 650) {
291: $guildClass = "class5";
292: } else if ($size < 1000) {
293: $guildClass = "class6";
294: } else {
295: $guildClass = "class7";
296: }
297: return $guildClass;
298: }
299: }
300: