1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: class ReputationController {
20:
21: 22: 23: 24:
25: public $moduleName;
26:
27:
28: public $db;
29:
30:
31: public $chatBot;
32:
33:
34: public $text;
35:
36:
37: public $util;
38:
39:
40: public $settingManager;
41:
42: 43: 44:
45: public function setup() {
46: $this->settingManager->add($this->moduleName, "reputation_min_time", "How much time is required for leaving reputation for the same character", "edit", "time", "6h", "1h;6h;24h", '', "mod");
47:
48: $this->db->loadSQLFile($this->moduleName, 'reputation');
49: }
50:
51: 52: 53: 54:
55: public function reputationListCommand($message, $channel, $sender, $sendto, $args) {
56: $sql = "
57: SELECT
58: name,
59: SUM(CASE WHEN reputation = '+1' THEN 1 ELSE 0 END) pos_rep,
60: SUM(CASE WHEN reputation = '-1' THEN 1 ELSE 0 END) neg_rep
61: FROM
62: reputation
63: GROUP BY
64: name";
65:
66: $data = $this->db->query($sql);
67: $count = count($data);
68:
69: if ($count == 0) {
70: $msg = "There are no characters on the reputation list.";
71: $sendto->reply($msg);
72: return;
73: }
74:
75: $blob = '';
76: forEach ($data as $row) {
77: $details_link = $this->text->makeChatcmd('Details', "/tell <myname> reputation $row->name");
78: $blob .= "$row->name <green>+{$row->pos_rep}<end> <orange>-{$row->neg_rep}<end> {$details_link}\n";
79: }
80: $msg = $this->text->makeBlob("Reputation List ($count)", $blob);
81: $sendto->reply($msg);
82: }
83:
84: 85: 86: 87:
88: public function reputationAddCommand($message, $channel, $sender, $sendto, $args) {
89: $name = ucfirst(strtolower($args[1]));
90: $charid = $this->chatBot->get_uid($name);
91: $rep = $args[2];
92: $comment = $args[3];
93:
94: if ($charid == false) {
95: $sendto->reply("Character <highlight>$name<end> does not exist.");
96: return;
97: }
98:
99: if ($sender == $name) {
100: $sendto->reply("You cannot give yourself reputation.");
101: return;
102: }
103:
104: $minTime = $this->settingManager->get('reputation_min_time');
105: $time = time() - $minTime;
106:
107: $sql = "SELECT dt FROM reputation WHERE `by` = ? AND `name` = ? AND `dt` > ? ORDER BY dt DESC LIMIT 1";
108: $row = $this->db->queryRow($sql, $sender, $name, $time);
109: if ($row !== null) {
110: $timeString = $this->util->unixtimeToReadable($row->dt - $time);
111: $sendto->reply("You must wait $timeString before submitting more reputation for $name.");
112: return;
113: }
114:
115: $sql = "
116: INSERT INTO reputation (
117: `name`,
118: `reputation`,
119: `comment`,
120: `by`,
121: `dt`
122: ) VALUES (
123: ?,
124: ?,
125: ?,
126: ?,
127: ?
128: )";
129:
130: $this->db->exec($sql, $name, $rep, $comment, $sender, time());
131: $sendto->reply("Reputation for $name added successfully.");
132: }
133:
134: 135: 136: 137: 138:
139: public function reputationViewCommand($message, $channel, $sender, $sendto, $args) {
140: $name = ucfirst(strtolower($args[1]));
141:
142: $limit = 10;
143: if (count($args) == 3) {
144: $limit = 1000;
145: }
146:
147: $sql = "
148: SELECT
149: sum(CASE WHEN reputation = '+1' THEN 1 ELSE 0 END) AS positive_rep,
150: sum(CASE WHEN reputation = '-1' THEN 1 ELSE 0 END) AS negative_rep
151: FROM
152: reputation
153: WHERE
154: name = ?";
155:
156: $row = $this->db->queryRow($sql, $name);
157: if ($row === null) {
158: $msg = "<highlight>$name<end> has no reputation.";
159: } else {
160: $num_positive = $row->positive_rep;
161: $num_negative = $row->negative_rep;
162:
163: $blob = "Positive reputation: <green>{$num_positive}<end>\n";
164: $blob .= "Negative reputation: <orange>{$num_negative}<end>\n\n";
165: if ($limit != 1000) {
166: $blob .= "Last $limit comments about this user:\n\n";
167: } else {
168: $blob .= "All comments about this user:\n\n";
169: }
170:
171: $sql = "SELECT * FROM reputation WHERE name = ? ORDER BY `dt` DESC LIMIT ?";
172: $data = $this->db->query($sql, $name, $limit);
173: forEach ($data as $row) {
174: if ($row->reputation == '-1') {
175: $blob .= "<orange>";
176: } else {
177: $blob .= "<green>";
178: }
179:
180: $time = $this->util->unixtimeToReadable(time() - $row->dt);
181: $blob .= "({$row->reputation}) $row->comment <end> $row->by <white>{$time} ago<end>\n\n";
182: }
183:
184: if ($limit != 1000) {
185: $blob .= $this->text->makeChatcmd("Show all comments", "/tell <myname> reputation $name all");
186: }
187:
188: $msg = $this->text->makeBlob("Reputation for {$name} (+$num_positive -$num_negative)", $blob);
189: }
190:
191: $sendto->reply($msg);
192: }
193: }
194: