PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/STFU/plugin.php

https://github.com/jesstelford/silverbot-plugins
PHP | 171 lines | 148 code | 20 blank | 3 comment | 18 complexity | f57fb0c17d0c8a45fec72cb5018a5c00 MD5 | raw file
  1. <?php
  2. class STFU extends SilverBotPlugin {
  3. private $data = array();
  4. private $expletives = array();
  5. private $savemtime = 0;
  6. private $dataFile = '';
  7. private $swearFile = '';
  8. public function __construct() {
  9. $this->dataFile = $this->getDataDirectory() . 'savedata.db';
  10. $this->swearFile = $this->getDataDirectory() . 'expletives.db';
  11. $this->load();
  12. $stat = stat($this->dataFile);
  13. $this->savemtime = $stat['mtime'];
  14. }
  15. public function pub($data) {
  16. if (empty($this->data[$data['source']]['user'][$data['username']]))
  17. $this->data[$data['source']]['user'][$data['username']] = array('c' => 0, 'l' => 0, 'w' => 0, 'e' => 0, 'swears' => array());
  18. if (empty($this->data[$data['source']]['glob']))
  19. $this->data[$data['source']]['glob'] = array('c' => 0, 'l' => 0, 'w' => 0, 'e' => 0, 'swears' => array());
  20. $len = strlen($data['text']);
  21. $words = count(explode(' ', $data['text']));
  22. $this->data[$data['source']]['user'][$data['username']]['c'] += $len; // add string length to char count
  23. $this->data[$data['source']]['glob']['c'] += $len;
  24. $this->data[$data['source']]['user'][$data['username']]['w'] += $words; // increase word count
  25. $this->data[$data['source']]['glob']['w'] += $words;
  26. $this->data[$data['source']]['user'][$data['username']]['l']++; // increase line count
  27. $this->data[$data['source']]['glob']['l']++;
  28. // this is gonna suck when we're tracking lots of swears
  29. foreach ($this->expletives as $word) {
  30. if (preg_match_all("/\b$word\b/i", $data['text'], $null) == true) {
  31. $count = count($null[0]);
  32. $this->data[$data['source']]['user'][$data['username']]['e'] += $count; // increase expletive count
  33. $this->data[$data['source']]['user'][$data['username']]['swears'][$word] += $count; // track words per user
  34. $this->data[$data['source']]['glob']['e'] += $count; // and words per channel
  35. $this->data[$data['source']]['glob']['swears'][$word] += $count;
  36. }
  37. }
  38. // auto-save after five minutes worth of chat (minimum, might be more depending on time between messages)
  39. if ((time() - $this->savemtime) > 300)
  40. $this->save();
  41. }
  42. public function pub_stfu($data) {
  43. if (empty($this->data[$data['source']])) {
  44. $this->bot->reply("I haven't tracked any data for this channel yet");
  45. return;
  46. }
  47. $dataset = $this->data[$data['source']];
  48. foreach ($dataset['user'] as $username=>$userdata) {
  49. $lines[$username] = $userdata['l'];
  50. $chars[$username] = $userdata['c'];
  51. $words[$username] = $userdata['w'];
  52. }
  53. foreach ($lines as $username=>$count) {
  54. $wplavg[$username] = number_format($words[$username] / $count, 2); // words per line avg
  55. $cpwavg[$username] = number_format($chars[$username] / $words[$username], 2); // chars per word average
  56. }
  57. arsort($lines); reset($lines);
  58. arsort($chars); reset($chars);
  59. arsort($words); reset($words);
  60. arsort($wplavg); reset($wplavg);
  61. arsort($cpwavg); reset($cpwavg);
  62. $who = key($lines);
  63. $this->bot->reply("Most lines: " . $who . " with " . $lines[$who] . " lines");
  64. $who = key($cpwavg);
  65. $this->bot->reply("Biggest words: " . $who . " with " . $cpwavg[$who] . " characters per word average");
  66. }
  67. public function pub_stfuswear($data) {
  68. $params = explode(' ', $data['data']);
  69. if (empty($this->data[$data['source']])) {
  70. $this->bot->reply("I haven't tracked any data for this channel yet");
  71. return;
  72. }
  73. // get channel statistics
  74. if (empty($params[0])) {
  75. $dataset = $this->data[$data['source']];
  76. foreach ($dataset['user'] as $username=>$userdata) {
  77. foreach ($userdata['swears'] as $word=>$count) $swearers[$username] += $count;
  78. }
  79. arsort($swearers);
  80. $totswears = $dataset['glob']['e'];
  81. $this->bot->reply("Total channel expletives uttered: " . $totswears);
  82. $this->bot->reply("Top 3 swearers:");
  83. reset($swearers);
  84. for ($i = 1; $i < 4; $i++) {
  85. $username = key($swearers);
  86. $this->bot->reply("$i: '$username' with " . $swearers[$username] . " expletives counted");
  87. if (next($swearers) === false)
  88. break;
  89. }
  90. } else { // get for a specific user
  91. $username = $params[0];
  92. if (empty($this->data[$data['source']]['user'][$username])) {
  93. $this->bot->reply("I haven't tracked any data for that user yet");
  94. return;
  95. }
  96. $dataset = $this->data[$data['source']]['user'][$username];
  97. $swears = $dataset['swears'];
  98. arsort($swears); reset($swears);
  99. $this->bot->reply("{$username}'s most commonly used expletives:");
  100. $out = array();
  101. for ($i = 1; $i < 6; $i++) {
  102. $word = key($swears);
  103. $out[] = "'$word' with " . $swears[$word] . " uses";
  104. if (next($swears) === false)
  105. break;
  106. }
  107. $this->bot->reply(join(", ", $out));
  108. }
  109. }
  110. private function reset() {
  111. unset($this->data);
  112. $this->data['since'] = time();
  113. $this->save();
  114. }
  115. private function addSwear($word) {
  116. if (in_array($word, $this->expletives))
  117. return false;
  118. $this->expletives[] = $word;
  119. $this->save();
  120. return true;
  121. }
  122. private function delSwear($word) {
  123. if ($key = array_search($word, $this->expletives)) {
  124. unset($this->expletives[$key]);
  125. $this->save();
  126. return true;
  127. }
  128. return false;
  129. }
  130. private function save() {
  131. file_put_contents($this->swearFile, join("\n", $this->expletives)); // expletives are \n delimited
  132. file_put_contents($this->dataFile, serialize($this->data)); // but data is serialized, since it's an array
  133. $this->savemtime = time();
  134. }
  135. private function load() {
  136. if (file_exists($this->swearFile))
  137. $this->expletives = explode("\n", file_get_contents($this->swearFile));
  138. if (file_exists($this->dataFile))
  139. $this->data = unserialize(file_get_contents($this->dataFile));
  140. else
  141. $this->reset(); // we wanna init the data file if we can't find it
  142. }
  143. }