1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use stdClass;
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: 45: 46: 47: 48: 49: 50: 51: 52:
53: class RaidController {
54:
55: 56: 57: 58:
59: public $moduleName;
60:
61:
62: public $db;
63:
64:
65: public $chatBot;
66:
67:
68: public $settingManager;
69:
70:
71: public $commandAlias;
72:
73:
74: public $text;
75:
76:
77: public $chatLeaderController;
78:
79: private $loot = array();
80: private $residual = array();
81:
82: 83: 84:
85: public function setup() {
86: $this->settingManager->add($this->moduleName, "add_on_loot", "Adding to loot show on", "edit", "options", "2", "tells;privatechat;privatechat and tells", '1;2;3', "mod");
87:
88: $this->commandAlias->register($this->moduleName, "flatroll", "rollloot");
89: $this->commandAlias->register($this->moduleName, "flatroll", "result");
90: $this->commandAlias->register($this->moduleName, "flatroll", "win");
91: $this->commandAlias->register($this->moduleName, "loot addmulti", "multiloot");
92:
93: $this->commandAlias->register($this->moduleName, "loot", "list");
94: }
95:
96: 97: 98: 99:
100: public function lootCommand($message, $channel, $sender, $sendto, $args) {
101: $msg = $this->getCurrentLootList();
102: $sendto->reply($msg);
103: }
104:
105: 106: 107: 108:
109: public function lootClearCommand($message, $channel, $sender, $sendto, $args) {
110: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
111: $sendto->reply("You must be Raid Leader to use this command.");
112: return;
113: }
114:
115: $this->loot = array();
116: $this->residual = array();
117: $msg = "Loot has been cleared by <highlight>$sender<end>.";
118: $this->chatBot->sendPrivate($msg);
119:
120: if ($channel != 'priv') {
121: $sendto->reply($msg);
122: }
123: }
124:
125: 126: 127: 128:
129: public function lootAddByIdCommand($message, $channel, $sender, $sendto, $args) {
130: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
131: $sendto->reply("You must be Raid Leader to use this command.");
132: return;
133: }
134:
135: $id = $args[1];
136:
137: $sql =
138: "SELECT *, COALESCE(a.name, r.name) AS name
139: FROM raid_loot r LEFT JOIN aodb a ON (r.name = a.name AND r.ql >= a.lowql AND r.ql <= a.highql)
140: WHERE id = ?";
141: $row = $this->db->queryRow($sql, $id);
142:
143: if ($row === null) {
144: $msg = "Could not find item with id <highlight>$id<end> to add.";
145: $sendto->reply($msg);
146: return;
147: }
148:
149: $key = $this->getLootItem($row->name);
150: if ($key !== null) {
151: $item = $this->loot[$key];
152: $item->multiloot += $row->multiloot;
153: } else {
154: if (!empty($this->loot)) {
155: $key = count($this->loot) + 1;
156: } else {
157: $key = 1;
158: }
159:
160: $item = new stdClass;
161:
162: $item->name = $row->name;
163: $item->icon = $row->icon;
164: $item->added_by = $sender;
165: $item->display = $this->text->makeItem($row->lowid, $row->highid, $row->ql, $row->name);
166: $item->multiloot = $row->multiloot;
167: $item->users = array();
168:
169: $this->loot[$key] = $item;
170: }
171:
172: $msg = "$sender added <highlight>{$item->name}<end> (x$item->multiloot). To add use <symbol>add $key.";
173: $this->chatBot->sendPrivate($msg);
174: }
175:
176: 177: 178: 179:
180: public function lootAddCommand($message, $channel, $sender, $sendto, $args) {
181: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
182: $sendto->reply("You must be Raid Leader to use this command.");
183: return;
184: }
185:
186: $input = $args[1];
187: $this->addLootItem($input, 1, $sender);
188: }
189:
190: 191: 192: 193: 194:
195: public function multilootCommand($message, $channel, $sender, $sendto, $args) {
196: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
197: $sendto->reply("You must be Raid Leader to use this command.");
198: return;
199: }
200:
201: $multiloot = $args[1];
202: $input = $args[2];
203: $this->addLootItem($input, $multiloot, $sender);
204: }
205:
206: public function addLootItem($input, $multiloot, $sender) {
207:
208: if (preg_match("|^<a href=\"itemref://(\\d+)/(\\d+)/(\\d+)\">(.+)</a>(.*)$|i", $input, $arr)) {
209: $item_ql = $arr[3];
210: $item_highid = $arr[1];
211: $item_lowid = $arr[2];
212: $item_name = $arr[4];
213: } else if (preg_match("|^(.+)<a href=\"itemref://(\\d+)/(\\d+)/(\\d+)\">(.+)</a>(.*)$|i", $input, $arr)){
214: $item_ql = $arr[4];
215: $item_highid = $arr[2];
216: $item_lowid = $arr[3];
217: $item_name = $arr[5];
218: } else {
219: $item_name = $input;
220: }
221:
222:
223: $row = $this->db->queryRow("SELECT * FROM aodb WHERE `name` LIKE ?", $item_name);
224: if ($row !== null) {
225: $item_name = $row->name;
226:
227:
228: $looticon = $row->icon;
229:
230:
231: if (!isset($item_highid)) {
232: $item_lowid = $row->lowid;
233: $item_highid = $row->highid;
234: $item_ql = $row->highql;
235: }
236: }
237:
238:
239: $key = $this->getLootItem($item_name);
240: if ($key !== null) {
241: $item = $this->loot[$key];
242: $item->multiloot += $multiloot;
243: } else {
244:
245: if (!empty($this->loot)) {
246: $key = count($this->loot) + 1;
247: } else {
248: $key = 1;
249: }
250:
251: $item = new stdClass;
252:
253: $item->name = $item_name;
254: $item->icon = $looticon;
255: $item->added_by = $sender;
256: $item->multiloot = $multiloot;
257: $item->users = array();
258:
259: if (isset($item_highid)) {
260: $item->display = $this->text->makeItem($item_lowid, $item_highid, $item_ql, $item_name);
261: } else {
262: $item->display = $item_name;
263: }
264: $this->loot[$key] = $item;
265: }
266:
267: $msg = "$sender added <highlight>{$item->name}<end> (x$item->multiloot) to Slot <highlight>#$key<end>.";
268: $msg .= " To add use <symbol>add $key, or <symbol>rem to remove yourself.";
269: $this->chatBot->sendPrivate($msg);
270: }
271:
272: 273: 274: 275:
276: public function lootRemCommand($message, $channel, $sender, $sendto, $args) {
277: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
278: $sendto->reply("You must be Raid Leader to use this command.");
279: return;
280: }
281:
282: $key = $args[1];
283:
284: if ($key > 0 && $key <= count($this->loot)) {
285:
286: if (count($this->loot) <= 1) {
287: $this->loot = array();
288: $this->chatBot->sendPrivate("Item in slot <highlight>#".$key."<end> was the last item in the list. The list has been cleared.");
289: } else {
290:
291: $loop = $key;
292: while($loop < count($this->loot)){
293: $this->loot[$loop] = $this->loot[$loop+1];
294: $loop++;
295: }
296: unset($this->loot[count($this->loot)]);
297: $this->chatBot->sendPrivate("Removing item in slot <highlight>#".$key."<end>");
298: }
299: } else {
300: $this->chatBot->sendPrivate("There is no item at slot <highlight>#".$key."<end>");
301: }
302: }
303:
304: 305: 306: 307:
308: public function rerollCommand($message, $channel, $sender, $sendto, $args) {
309: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
310: $sendto->reply("You must be Raid Leader to use this command.");
311: return;
312: }
313:
314:
315: if (empty($this->residual)) {
316: $msg = "There are no remaining items to re-add.";
317: $sendto->reply($msg);
318: return;
319: }
320:
321:
322: forEach ($this->residual as $key => $item) {
323: $this->loot[$key] = $item;
324: $this->loot[$key]->added_by = $sender;
325: }
326:
327:
328: $this->residual = array();
329:
330: $msg = "All remaining items have been re-added by <highlight>$sender<end>. Check <symbol>loot.";
331: $this->chatBot->sendPrivate($msg);
332: if ($channel != 'priv') {
333: $sendto->reply($msg);
334: }
335:
336: $msg = $this->getCurrentLootList();
337: $sendto->reply($msg);
338: }
339:
340: 341: 342: 343:
344: public function flatrollCommand($message, $channel, $sender, $sendto, $args) {
345: if (!$this->chatLeaderController->checkLeaderAccess($sender)) {
346: $sendto->reply("You must be Raid Leader to use this command.");
347: return;
348: }
349:
350: global $loot_winners;
351:
352: if (empty($this->loot)) {
353: $msg = "There is nothing to roll atm.";
354: $sendto->reply($msg);
355: return;
356: }
357:
358: srand(((int)((double)microtime() * 1000003)));
359:
360: $list = '';
361:
362: $resnum = 1;
363: forEach ($this->loot as $key => $item) {
364: $list .= "Item: <orange>{$item->name}<end>\n";
365: $list .= "Winner(s): ";
366: $users = count($item->users);
367: if ($users == 0) {
368: $list .= "<highlight>None added.<end>\n\n";
369: $this->residual[$resnum] = $item;
370: $resnum++;
371: } else {
372: if ($item->multiloot > 1) {
373: if ($item->multiloot > count($item->users)) {
374: $arrolnum = count($item->users);
375: } else {
376: $arrolnum = $item->multiloot;
377: }
378:
379: for ($i = 0; $i < $arrolnum; $i++) {
380: $winner = array_rand($item->users, 1);
381: unset($item->users[$winner]);
382: $list .= "<red>$winner<end> ";
383: }
384:
385: if ($arrolnum < $item->multiloot) {
386: $newmultiloot = $item->multiloot - $arrolnum;
387: $this->residual[$resnum] = $item;
388: $this->residual[$resnum]->multiloot = $newmultiloot;
389: $resnum++;
390: }
391: } else {
392: $winner = array_rand($item->users, 1);
393: $list .= "<red>$winner<end>";
394: }
395: $list .= "\n\n";
396: }
397: }
398:
399:
400: $this->loot = array();
401:
402:
403: $msg = $this->text->makeBlob("Winner List", $list);
404: if (!empty($this->residual)) {
405: $msg .= " (There are item(s) left to be rolled. To re-add, type <symbol>reroll)";
406: }
407:
408: $this->chatBot->sendPrivate($msg);
409: if ($channel != 'priv') {
410: $sendto->reply($msg);
411: }
412: }
413:
414: 415: 416: 417:
418: public function addCommand($message, $channel, $sender, $sendto, $args) {
419: $slot = $args[1];
420: $found = false;
421: if (count($this->loot) > 0) {
422:
423: if (!isset($this->loot[$slot])) {
424: $msg = "The slot you are trying to add in does not exist.";
425: $this->chatBot->sendTell($msg, $sender);
426: return;
427: }
428:
429:
430: $found = false;
431: forEach ($this->loot as $key => $item) {
432: if ($this->loot[$key]->users[$sender] == true) {
433: unset($this->loot[$key]->users[$sender]);
434: $found = true;
435: }
436: }
437:
438:
439: $this->loot[$slot]->users[$sender] = true;
440:
441: if ($found == false) {
442: $msg = "$sender has added to <highlight>\"{$this->loot[$slot]->name}\"<end>.";
443: } else {
444: $msg = "$sender has moved to <highlight>\"{$this->loot[$slot]->name}\"<end>.";
445: }
446:
447: $this->chatBot->sendPrivate($msg);
448: } else {
449: $this->chatBot->sendTell("No loot list available.", $sender);
450: }
451: }
452:
453: 454: 455: 456:
457: public function remCommand($message, $channel, $sender, $sendto, $args) {
458: if (count($this->loot) > 0) {
459: forEach ($this->loot as $key => $item) {
460: if ($this->loot[$key]->users[$sender] == true) {
461: unset($this->loot[$key]->users[$sender]);
462: }
463: }
464:
465: $msg = "$sender has been removed from all rolls.";
466: $this->chatBot->sendPrivate($msg);
467: } else {
468: $this->chatBot->sendTell("There is nothing to remove you from.", $sender);
469: }
470: }
471:
472: public function getCurrentLootList() {
473: if (!empty($this->loot)) {
474: $list = "Use <symbol>flatroll to roll.\n\n";
475: $players = 0;
476: $items = count($this->loot);
477: forEach ($this->loot as $key => $item) {
478: $add = $this->text->makeChatcmd("Add", "/tell <myname> add $key");
479: $rem = $this->text->makeChatcmd("Remove", "/tell <myname> rem");
480: $added_players = count($item->users);
481: $players += $added_players;
482:
483: if ($item->icon != "") {
484: $list .= $this->text->makeImage($item->icon) . "\n";
485: }
486:
487: if ($item->multiloot > 1) {
488: $ml = " <highlight>(x".$item->multiloot.")<end>";
489: } else {
490: $ml = "";
491: }
492:
493: $list .= "\n{$item->display}".$ml."";
494: $list .= "\n<header2>Slot #$key<end> $add / $rem";
495: $list .= "\nPlayers added (<highlight>$added_players<end>):";
496: if (count($item->users) > 0) {
497: forEach ($item->users as $key => $value) {
498: $list .= " [<yellow>$key<end>]";
499: }
500: }
501:
502: $list .= "\n\n\n";
503: }
504: $msg = $this->text->makeBlob("Loot List (Items: $items, Players: $players)", $list);
505: } else {
506: $msg = "No loot list exists yet.";
507: }
508:
509: return $msg;
510: }
511:
512: public function addRaidToLootList($raid, $category) {
513:
514: $this->loot = array();
515: $count = 1;
516:
517: $sql = "SELECT * FROM raid_loot r LEFT JOIN aodb a ON (r.name = a.name AND r.ql >= a.lowql AND r.ql <= a.highql) WHERE raid = ? AND category = ?";
518: $data = $this->db->query($sql, $raid, $category);
519:
520: if (count($data) == 0) {
521: return false;
522: }
523:
524: forEach ($data as $row) {
525: $item = $this->text->makeItem($row->lowid, $row->highid, $row->ql, $row->name);
526: if (empty($row->comment)) {
527: $row->display = $item;
528: } else {
529: $row->display = $item . " ($row->comment)";
530: }
531: $row->users = array();
532: $this->loot[$count] = $row;
533: $count++;
534: }
535:
536: return true;
537: }
538:
539: public function getLootItem($name) {
540: forEach ($this->loot as $key => $item) {
541: if ($item->name == $name){
542: return $key;
543: }
544: }
545: return null;
546: }
547: }
548: