1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class RandomController {
32:
33: 34: 35: 36:
37: public $moduleName;
38:
39:
40: public $db;
41:
42:
43: public $text;
44:
45:
46: public $util;
47:
48:
49: public $settingManager;
50:
51:
52: public $commandAlias;
53:
54: 55: 56: 57: 58: 59: 60:
61: public $defaultTimeBetweenRolls = "30s";
62:
63: 64: 65: 66:
67: public function setup() {
68: $this->db->loadSQLFile($this->moduleName, 'roll');
69:
70: $this->commandAlias->register($this->moduleName, "roll heads tails", "flip");
71: }
72:
73: 74: 75: 76:
77: public function randomCommand($message, $channel, $sender, $sendto, $args) {
78: $text = explode(" ", trim($args[1]));
79: $low = 0;
80: $high = count($text) - 1;
81: while (true) {
82: $random = rand($low, $high);
83: if (!isset($marked[$random])) {
84: $count++;
85: $list []= $text[$random];
86: $marked[$random] = 1;
87: if (count($marked) == count($text)) {
88: break;
89: }
90: }
91: $i = $low;
92: while (true) {
93: if ($marked[$i] != 1) {
94: $low = $i;
95: break;
96: } else {
97: $i++;
98: }
99: }
100: $i = $high;
101: while (true) {
102: if ($marked[$i] != 1) {
103: $high = $i;
104: break;
105: } else {
106: $i--;
107: }
108: }
109: }
110:
111: $sendto->reply(implode(" ", $list));
112: }
113:
114: 115: 116: 117: 118:
119: public function rollNumericCommand($message, $channel, $sender, $sendto, $args) {
120: if (count($args) == 3) {
121: $min = $args[1];
122: $max = $args[2];
123: } else {
124: $min = 1;
125: $max = $args[1];
126: }
127:
128: if ($min >= $max) {
129: $msg = "The first number cannot be higher than or equal to the second number.";
130: } else {
131: $timeBetweenRolls = $this->settingManager->get('time_between_rolls');
132: $row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - $timeBetweenRolls);
133: if ($row === null) {
134: $options = array();
135: for ($i = $min; $i <= $max; $i++) {
136: $options []= $i;
137: }
138: list($ver_num, $result) = $this->roll($sender, $options);
139: $msg = "The roll is <highlight>$result<end> between $min and $max. To verify do /tell <myname> verify $ver_num";
140: } else {
141: $msg = "You can only roll once every $timeBetweenRolls seconds.";
142: }
143: }
144:
145: $sendto->reply($msg);
146: }
147:
148: 149: 150: 151:
152: public function rollNamesCommand($message, $channel, $sender, $sendto, $args) {
153: $names = $args[1];
154: $timeBetweenRolls = $this->settingManager->get('time_between_rolls');
155: $row = $this->db->queryRow("SELECT * FROM roll WHERE `name` = ? AND `time` >= ? LIMIT 1", $sender, time() - $timeBetweenRolls);
156: if ($row === null) {
157: $options = explode(' ', $names);
158: list($ver_num, $result) = $this->roll($sender, $options);
159: $msg = "The roll is <highlight>$result<end> out of possible options: $names. To verify do /tell <myname> verify $ver_num";
160: } else {
161: $msg = "You can only roll once every $timeBetweenRolls seconds.";
162: }
163:
164: $sendto->reply($msg);
165: }
166:
167: 168: 169: 170:
171: public function verifyCommand($message, $channel, $sender, $sendto, $args) {
172: $id = $args[1];
173: $row = $this->db->queryRow("SELECT * FROM roll WHERE `id` = ?", $id);
174: if ($row === null) {
175: $msg = "Verify number <highlight>$id<end> does not exist.";
176: } else {
177: $time = $this->util->unixtimeToReadable(time() - $row->time);
178: $msg = "<highlight>$row->result<end> rolled by <highlight>$row->name<end> $time ago. Possible options: ";
179: $msg .= str_replace("|", " ", $row->options) . ".";
180: }
181:
182: $sendto->reply($msg);
183: }
184:
185: public function roll($sender, $options) {
186: $result = $this->util->randomArrayValue($options);
187: $this->db->exec("INSERT INTO roll (`time`, `name`, `options`, `result`) VALUES (?, ?, ?, ?)", time(), $sender, implode($options, "|"), $result);
188: return array($this->db->lastInsertId(), $result);
189: }
190: }
191: