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: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44: class EventsController {
45:
46: 47: 48: 49:
50: public $moduleName;
51:
52:
53: public $db;
54:
55:
56: public $chatBot;
57:
58:
59: public $settingManager;
60:
61:
62: public $text;
63:
64:
65: public $util;
66:
67:
68: public $altsController;
69:
70: private $apisocket = null;
71:
72:
73: public function setup() {
74: $this->db->loadSQLFile($this->moduleName, "events");
75:
76: $this->settingManager->add($this->moduleName, "num_events_shown", "Maximum number of events shown", "edit", "number", "5", "5;10;15;20");
77: }
78:
79: 80: 81: 82:
83: public function eventsCommand($message, $channel, $sender, $sendto, $args) {
84: $msg = $this->getEvents();
85: if ($msg == '') {
86: $msg = "No events entered yet.";
87: }
88: $sendto->reply($msg);
89: }
90:
91: 92: 93: 94:
95: public function eventsJoinCommand($message, $channel, $sender, $sendto, $args) {
96: $id = $args[1];
97: $row = $this->db->queryRow("SELECT * FROM events WHERE `id` = ?", $id);
98: if (time() < ($row->event_date + (3600 * 3))) {
99:
100: if (strpos($row->event_attendees, $sender) !== false) {
101: $msg = "You are already on the event list.";
102: } else {
103: if ($row->event_attendees == "") {
104: $row->event_attendees = "$sender";
105: } else {
106: $row->event_attendees .= ",$sender";
107: }
108: $this->db->exec("UPDATE events SET `event_attendees` = ? WHERE `id` = ?", $row->event_attendees, $id);
109: $msg = "You have been added to the event.";
110: }
111: } else {
112: $msg = "You cannot join an event once it has already passed!";
113: }
114: $sendto->reply($msg);
115: }
116:
117: 118: 119: 120:
121: public function eventsLeaveCommand($message, $channel, $sender, $sendto, $args) {
122: $id = $args[1];
123: $row = $this->db->queryRow("SELECT * FROM events WHERE `id` = ?", $id);
124: if (time() < ($row->event_date + (3600 * 3))) {
125:
126: if (strpos($row->event_attendees,$sender) !== false) {
127: $event = explode(",", $row->event_attendees);
128: forEach ($event as $i => $value) {
129: if ($value == $sender) {
130: unset($event[$i]);
131: $event = array_values($event);
132: }
133: }
134: $event = implode(",", $event);
135: $this->db->exec("UPDATE events SET `event_attendees` = ? WHERE `id` = ?", $event, $id);
136: $msg = "You have been removed from the event.";
137: } else {
138: $msg = "You are not on the event list.";
139: }
140: } else {
141: $msg = "You cannot leave an event once it has already passed!";
142: }
143: $sendto->reply($msg);
144: }
145:
146: 147: 148: 149:
150: public function eventsListCommand($message, $channel, $sender, $sendto, $args) {
151: $id = $args[1];
152: $row = $this->db->queryRow("SELECT event_attendees FROM events WHERE `id` = ?", $id);
153: if ($row !== null) {
154: $link = $this->text->makeChatcmd("Join this event", "/tell <myname> events join $id")."\n";
155: $link .= $this->text->makeChatcmd("Leave this event", "/tell <myname> events leave $id")."\n\n";
156:
157: if (empty($row->event_attendees)) {
158: $link .= "No one has signed up to attend this event!";
159: } else {
160: $eventlist = explode(",", $row->event_attendees);
161: sort($eventlist);
162: forEach ($eventlist as $key => $name) {
163: $row = $this->db->queryRow("SELECT * FROM players WHERE name = ? AND dimension = '<dim>'", $name);
164: $info = '';
165: if ($row !== null) {
166: $info = " <white>Lvl $row->level $row->profession<end>";
167: }
168:
169: $altInfo = $this->altsController->getAltInfo($name);
170: $alt = '';
171: if (count($altInfo->alts) > 0) {
172: if ($altInfo->main == $name) {
173: $alt = " <highlight>::<end> " . $this->text->makeChatcmd("Alts", "/tell <myname> alts $name");
174: } else {
175: $alt = " <highlight>::<end> " . $this->text->makeChatcmd("Alts of {$altInfo->main}", "/tell <myname> alts $name");
176: }
177: }
178:
179: $link .= $name . "$info $alt\n";
180: }
181: }
182: $msg = $this->text->makeBlob("Players Attending Event $id", $link);
183: } else {
184: $msg = "Could not find event with id $id.";
185: }
186:
187: $sendto->reply($msg);
188: }
189:
190: 191: 192: 193:
194: public function eventsAddCommand($message, $channel, $sender, $sendto, $args) {
195: $eventName = $args[1];
196: $this->db->exec("INSERT INTO events (`time_submitted`, `submitter_name`, `event_name`, `event_date`) VALUES (?, ?, ?, null)", time(), $sender, $eventName);
197: $event_id = $this->db->lastInsertId();
198: $msg = "Event: '$eventName' was added [Event ID $event_id].";
199: $sendto->reply($msg);
200: }
201:
202: 203: 204: 205:
206: public function eventsRemoveCommand($message, $channel, $sender, $sendto, $args) {
207: $id = $args[1];
208: $row = $this->db->queryRow("SELECT * FROM events WHERE id = ?", $id);
209: if ($row === null) {
210: $msg = "Could not find an event with id $id.";
211: } else {
212: $this->db->exec("DELETE FROM events WHERE `id` = ?", $id);
213: $msg = "Event with id $id has been deleted.";
214: }
215: $sendto->reply($msg);
216: }
217:
218: 219: 220: 221:
222: public function eventsSetDescCommand($message, $channel, $sender, $sendto, $args) {
223: $id = $args[1];
224: $desc = $args[2];
225: $row = $this->db->queryRow("SELECT * FROM events WHERE id = ?", $id);
226: if ($row === null) {
227: $msg = "Could not find an event with id $id.";
228: } else {
229: $this->db->exec("UPDATE events SET `event_desc` = ? WHERE `id` = ?", $desc, $id);
230: $msg = "Description for event with id $id has been updated.";
231: }
232: $sendto->reply($msg);
233: }
234:
235: 236: 237: 238:
239: public function eventsSetDateCommand($message, $channel, $sender, $sendto, $args) {
240: $id = $args[1];
241: $row = $this->db->queryRow("SELECT * FROM events WHERE id = ?", $id);
242: if ($row === null) {
243: $msg = "Could not find an event with id $id.";
244: } else {
245:
246: $eventDate = mktime($args[5], $args[6], 0, $args[3], $args[4], $args[2]);
247: $this->db->exec("UPDATE events SET `event_date` = ? WHERE `id` = ?", $eventDate, $id);
248: $msg = "Date/Time for event with id $id has been updated.";
249: }
250: $sendto->reply($msg);
251: }
252:
253: public function getEvents() {
254: $sql = "SELECT * FROM events ORDER BY `event_date` DESC LIMIT ?";
255: $data = $this->db->query($sql, intval($this->settingManager->get('num_events_shown')));
256: if (count($data) > 0) {
257: $upcoming_title = "<header2>Upcoming Events<end>\n\n";
258: $past_title = "<header2>Past Events<end>\n\n";
259: $updated = 0;
260: forEach ($data as $row) {
261: if ($row->event_attendees == '') {
262: $attendance = 0;
263: } else {
264: $attendance = count(explode(",", $row->event_attendees));
265: }
266: if ($updated < $row->time_submitted) {
267: $updated = $row->time_submitted;
268: }
269:
270: if ($row->event_date > time()) {
271: $upcoming = "Event Date: <highlight>" . $this->util->date($row->event_date) . "<end>\n";
272: $upcoming .= "Event Name: <highlight>$row->event_name<end> [Event ID $row->id]\n";
273: $upcoming .= "Author: <highlight>$row->submitter_name<end>\n";
274: $upcoming .= "Attendance: <highlight>" . $this->text->makeChatcmd("$attendance signed up", "/tell <myname> events list $row->id") . "<end>" .
275: " [" . $this->text->makeChatcmd("Join", "/tell <myname> events join $row->id") . "/" .
276: $this->text->makeChatcmd("Leave", "/tell <myname> events leave $row->id") . "]\n";
277: $upcoming .= "Description: <highlight>" . $row->event_desc . "<end>\n";
278: $upcoming .= "Date Submitted: <highlight>" . $this->util->date($row->time_submitted) . "<end>\n\n";
279: $upcoming_events = $upcoming.$upcoming_events;
280: } else {
281: $past = "Event Date: <highlight>" . $this->util->date($row->event_date) . "<end>\n";
282: $past .= "Event Name: <highlight>$row->event_name<end> [Event ID $row->id]\n";
283: $past .= "Author: <highlight>$row->submitter_name<end>\n";
284: $past .= "Attendance: <highlight>" . $this->text->makeChatcmd("$attendance signed up", "/tell <myname> events list $row->id") . "<end>\n";
285: $past .= "Description: <highlight>" . $row->event_desc . "<end>\n";
286: $past .= "Date Submitted: <highlight>" . $this->util->date($row->time_submitted) . "<end>\n\n";
287: $past_events .= $past;
288: }
289: }
290: if (!$upcoming_events) {
291: $upcoming_events = "<i>More to come. Check back soon!</i>\n\n";
292: }
293: if (!$past_events) {
294: $link = $upcoming_title.$upcoming_events;
295: } else {
296: $link = $upcoming_title.$upcoming_events.$past_title.$past_events;
297: }
298:
299: return $this->text->makeBlob("Events" . " [Last updated " . $this->util->date($updated)."]", $link);
300: } else {
301: return "";
302: }
303: }
304:
305: 306: 307: 308:
309: public function logonEvent($eventObj) {
310: $sender = $eventObj->sender;
311:
312: if ($this->chatBot->isReady() && isset($this->chatBot->guildmembers[$sender])) {
313: if ($this->hasRecentEvents()) {
314: $this->chatBot->sendTell($this->getEvents(), $sender);
315: }
316: }
317: }
318:
319: 320: 321: 322:
323: public function joinPrivEvent($eventObj) {
324: $sender = $eventObj->sender;
325:
326: if ($this->hasRecentEvents()) {
327: $this->chatBot->sendTell($this->getEvents(), $sender);
328: }
329: }
330:
331: public function hasRecentEvents() {
332: $sevenDays = time() - (86400 * 7);
333: $row = $this->db->queryRow("SELECT * FROM events WHERE `event_date` > ? LIMIT 1", $sevenDays);
334: return $row !== null;
335: }
336: }
337:
338: