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: class RaffleController {
21:
22: 23: 24: 25:
26: public $moduleName;
27:
28:
29: public $settingManager;
30:
31:
32: public $accessManager;
33:
34:
35: public $text;
36:
37:
38: public $util;
39:
40: 41: 42:
43: public function setup() {
44: if (!isset($this->raffles)) {
45: $this->raffles = array(
46: "running" => false,
47: "owner" => null,
48: "item" => null,
49: "count" => null,
50: "time" => null,
51: "rafflees" => null,
52: "lastresult" => null,
53: "nextmsgtime" => null,
54: "sendto" => null
55: );
56: }
57:
58: $this->settingManager->add($this->moduleName, "defaultraffletime", "How long the raffle should go for", "edit", "time", '3m', '1m;2m;3m;4m;5m', '', 'mod', "raffle.txt");
59: }
60:
61: 62: 63: 64: 65:
66: public function raffleStartCommand($message, $channel, $sender, $sendto, $args) {
67: if ("msg" == $channel) {
68: $msg = "You cannot start a raffle in tells. Use the org channel or private channel.";
69: $sendto->reply($msg);
70: return;
71: }
72:
73: if ($this->raffles["running"]) {
74: $msg = "There is already a raffle in progress.";
75: $sendto->reply($msg);
76: return;
77: }
78:
79: if (count($args) == 3) {
80: $item = $args[2];
81: $count = $args[1];
82: } else {
83: $item = $args[1];
84: $count = 1;
85: }
86: $seconds = $this->settingManager->get("defaultraffletime");
87: $timeString = $this->util->unixtimeToReadable($seconds);
88:
89: $this->raffles = array(
90: "running" => true,
91: "owner" => $sender,
92: "item" => $item,
93: "count" => $count,
94: "time" => time() + $seconds,
95: "rafflees" => array(),
96: "lastresult" => null,
97: "sendto" => $sendto
98: );
99:
100: $joinLink = $this->text->makeChatcmd("here", "/tell <myname> raffle join");
101: $leaveLink = $this->text->makeChatcmd("here", "/tell <myname> raffle leave");
102:
103: $jnRflMsg = "<white>A raffle for $item (count: $count) has been started by $sender!<end>
104:
105: Click $joinLink to join the raffle!
106: Click $leaveLink if you wish to leave the raffle.";
107: $link = $this->text->makeBlob("here", $jnRflMsg, 'Raffle');
108: $msg = "
109: -----------------------------------------------------------------------
110: A raffle for $item (count: $count) has been started by $sender!
111: Click $link to join the raffle. Raffle will end in $timeString.
112: -----------------------------------------------------------------------";
113:
114: $this->raffles["nextmsgtime"] = $this->getNextTime($this->raffles["time"]);
115: $sendto->reply($msg);
116: }
117:
118: 119: 120: 121:
122: public function raffleCancelCommand($message, $channel, $sender, $sendto, $args) {
123: if (!$this->raffles["running"]) {
124: $msg = "There is no active raffle.";
125: $sendto->reply($msg);
126: return;
127: }
128:
129: if (($this->raffles["owner"] != $sender) && !$this->accessManager->checkAccess($sender, "mod")) {
130: $msg = "Only the owner or a moderator may cancel the raffle.";
131: $sendto->reply($msg);
132: return;
133: }
134: $sendtobuffer = $this->raffles["sendto"];
135: $this->raffles = array(
136: "running" => false,
137: "owner" => null,
138: "item" => null,
139: "count" => null,
140: "time" => null,
141: "rafflees" => null,
142: "lastresult" => "The last raffle was cancelled.",
143: "sendto" => $sendtobuffer
144: );
145:
146: $msg = "The raffle was cancelled.";
147: $this->raffles["sendto"]->reply($msg);
148: }
149:
150: 151: 152: 153:
154: public function raffleEndCommand($message, $channel, $sender, $sendto, $args) {
155: if (!$this->raffles["running"]) {
156: $msg = "There is no active raffle.";
157: $sendto->reply($msg);
158: return;
159: }
160:
161: if (($this->raffles["owner"] != $sender) && !$this->accessManager->checkAccess($sender, "mod")) {
162: $msg = "Only the owner or a moderator may end the raffle.";
163: $sendto->reply($msg);
164: return;
165: }
166:
167: $this->endraffle();
168: }
169:
170: 171: 172: 173:
174: public function raffleResultCommand($message, $channel, $sender, $sendto, $args) {
175: if (!isset ($this->raffles["lastresult"])) {
176: $msg = "Last raffles result could not be retrieved.";
177: $sendto->reply($msg);
178: return;
179: }
180:
181: $sendto->reply("Last raffle result: ".$this->raffles["lastresult"]);
182: }
183:
184: 185: 186: 187:
188: public function raffleJoinCommand($message, $channel, $sender, $sendto, $args) {
189: if (!$this->raffles["running"]) {
190: $msg = "There is no active raffle.";
191: $sendto->reply($msg);
192: return;
193: }
194:
195: if (isset($this->raffles["rafflees"][$sender])) {
196: $msg = "You are already in the raffle.";
197: $sendto->reply($msg);
198: return;
199: }
200:
201: $this->raffles["rafflees"][$sender] = 0;
202: $msg = "$sender has entered the raffle.";
203: $this->raffles["sendto"]->reply($msg);
204: }
205:
206: 207: 208: 209:
210: public function raffleLeaveCommand($message, $channel, $sender, $sendto, $args) {
211: if (!$this->raffles["running"]) {
212: $msg = "There is no active raffle.";
213: $sendto->reply($msg);
214: return;
215: }
216:
217: if (!isset( $this->raffles["rafflees"][$sender])) {
218: $msg = "You are not currently signed up for the raffle.";
219: $sendto->reply($msg);
220: return;
221: }
222:
223: unset($this->raffles["rafflees"][$sender]);
224: $msg = "$sender has left the raffle.";
225: $this->raffles["sendto"]->reply($msg);
226: }
227:
228: 229: 230: 231:
232: public function checkRaffleEvent($eventObj) {
233: if (!$this->raffles["running"]) {
234:
235: } else if (time() < $this->raffles["nextmsgtime"]) {
236:
237: } else if ($this->raffles["time"] == $this->raffles["nextmsgtime"]) {
238:
239: $this->endraffle();
240: } else {
241: $this->showRaffleReminder();
242: }
243: }
244:
245: function endraffle() {
246:
247: if (!$this->raffles["running"]) {
248: return;
249: }
250:
251: $this->raffles["running"] = false;
252:
253: $item = $this->raffles["item"];
254: $count = $this->raffles["count"];
255: $rafflees = array_keys($this->raffles["rafflees"]);
256: $rafflees_num = count($rafflees);
257:
258: if (0 == $rafflees_num) {
259: $msg = "No one joined the raffle, $item is free for all.";
260: $this->raffles["lastresult"] = $msg;
261:
262: $this->raffles["sendto"]->reply($msg);
263: return;
264: }
265:
266:
267: for ($i = 0; $i < 5; $i++) {
268: shuffle($rafflees);
269: }
270:
271: $rollcount = 1000 * $rafflees_num;
272:
273: for ($i = 0; $i < $rollcount; $i++) {
274:
275: $random_name = $rafflees[mt_rand(0, $rafflees_num - 1)];
276: $this->raffles["rafflees"][$random_name] ++;
277: }
278:
279:
280: arsort($this->raffles["rafflees"]);
281:
282: $blob = '';
283: if (1 == $count) {
284: $blob .= "Rolled $rollcount times for $item.\n \n Winner:";
285: } else {
286: $blob .= "Rolled $rollcount times for $item (count: $count).\n \n Winners:";
287: }
288:
289: $i = 0;
290: forEach ($this->raffles["rafflees"] as $char => $rolls) {
291: $i++;
292: $blob .= "\n$i. $char got $rolls rolls.";
293: if ($i == $count) {
294: $blob .= "\n-------------------------\n Unlucky:";
295: }
296: }
297: $results = $this->text->makeBlob("Detailed results", $blob);
298:
299: if (1 == $count) {
300: $msg = "The raffle for $item is over. Winner: ";
301: } else {
302: $msg = "The raffle for $item (count: $count) is over. Winners: ";
303: }
304:
305: $i = 0;
306: forEach ($this->raffles["rafflees"] as $char => $rolls) {
307: $i++;
308: $msg .= "{$char}!";
309: if ($i != $count) {
310: $msg .= ", ";
311: } else {
312: break;
313: }
314: }
315: $msg .= " Congratulations. $results";
316: $this->raffles["lastresult"] = $msg;
317: $this->raffles["sendto"]->reply($msg);
318: }
319:
320: function getNextTime($endtime) {
321: $tleft = $endtime - time();
322: if ($tleft <= 0) {
323: $ret = false;
324: } else if ($tleft <= 30) {
325: $ret = $endtime;
326: } else if ($tleft <= 60) {
327: $ret = $endtime - 30;
328: } else if ($tleft <= 120) {
329: $ret = $endtime - 60;
330: } else {
331: $ret = $endtime - floor(($tleft - 30) / 60) * 60;
332: }
333: return $ret;
334: }
335:
336: function showRaffleReminder() {
337:
338: $time_string = $this->util->unixtimeToReadable($this->raffles["time"] - $this->raffles["nextmsgtime"]);
339: $item = $this->raffles["item"];
340: $count = $this->raffles["count"];
341:
342:
343: $blob = "<header2>Current Members:<end>";
344: forEach (array_keys($this->raffles["rafflees"]) as $tempName) {
345: $blob .= "\n$tempName";
346: }
347: if (count($this->raffles["rafflees"]) == 0) {
348: $blob .= "No entrants yet.";
349: }
350:
351: $joinLink = $this->text->makeChatcmd("here", "/tell <myname> raffle join");
352: $leaveLink = $this->text->makeChatcmd("here", "/tell <myname> raffle leave");
353:
354: $blob .= "\n\nClick $joinLink to join the raffle!";
355: $blob .= "\nClick $leaveLink if you wish to leave the raffle.";
356: $blob .= "\n\n Time left: $time_string.";
357:
358: $link = $this->text->makeBlob("Raffle Info", $blob);
359: if (1 < $count) {
360: $msg = "Reminder: Raffle for $item (count: $count) has $time_string left. $link";
361: } else {
362: $msg = "Reminder: Raffle for $item has $time_string left. $link";
363: }
364:
365: $this->raffles["sendto"]->reply($msg);
366: $this->raffles["nextmsgtime"] = $this->getNextTime($this->raffles["time"]);
367: }
368: }
369: