1: <?php
2:
3: namespace Budabot\Core\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class BuddylistController {
21:
22: 23: 24: 25:
26: public $moduleName;
27:
28:
29: public $chatBot;
30:
31:
32: public $buddylistManager;
33:
34:
35: public $text;
36:
37: 38: 39: 40: 41:
42: public function buddylistShowCommand($message, $channel, $sender, $sendto, $args) {
43: if (count($args) == 2) {
44: $cleanup = true;
45: }
46:
47: $orphanCount = 0;
48: if (count($this->buddylistManager->buddyList) == 0) {
49: $msg = "There are no players on the buddy list.";
50: } else {
51: $count = 0;
52: forEach ($this->getSortedBuddyList() as $value) {
53: if (!isset($value['name'])) {
54:
55: continue;
56: }
57:
58: $count++;
59: $removed = '';
60: if (count($value['types']) == 0) {
61: $orphanCount++;
62: if ($cleanup) {
63: $this->buddylistManager->remove($value['name']);
64: $removed = "<red>REMOVED<end>";
65:
66:
67: $count--;
68: }
69: }
70:
71: $blob .= $value['name'] . " $removed " . implode(' ', array_keys($value['types'])) . "\n";
72: }
73:
74: if ($cleanup) {
75: $blob .="\n\nRemoved: ($orphanCount)";
76: } else {
77: $blob .= "\n\nUnknown: ($orphanCount) ";
78: if ($orphanCount > 0) {
79: $blob .= $this->text->makeChatcmd('Remove Orphans', '/tell <myname> <symbol>buddylist clean');
80: }
81: }
82:
83: if ($cleanup) {
84: $sendto->reply("Removed $orphanCount characters from the buddy list.");
85: }
86: $msg = $this->text->makeBlob("Buddy list ($count)", $blob);
87: }
88: $sendto->reply($msg);
89: }
90:
91: 92: 93: 94:
95: public function buddylistAddCommand($message, $channel, $sender, $sendto, $args) {
96: $name = ucfirst(strtolower($args[1]));
97: $type = $args[2];
98:
99: if ($this->buddylistManager->add($name, $type)) {
100: $msg = "<highlight>$name<end> added to the buddy list successfully.";
101: } else {
102: $msg = "Could not add <highlight>$name<end> to the buddy list.";
103: }
104:
105: $sendto->reply($msg);
106: }
107:
108: 109: 110: 111:
112: public function buddylistRemAllCommand($message, $channel, $sender, $sendto, $args) {
113: forEach ($this->buddylistManager->buddyList as $uid => $buddy) {
114: $this->chatBot->buddy_remove($uid);
115: }
116:
117: $msg = "All characters have been removed from the buddy list.";
118: $sendto->reply($msg);
119: }
120:
121: 122: 123: 124:
125: public function buddylistRemCommand($message, $channel, $sender, $sendto, $args) {
126: $name = ucfirst(strtolower($args[1]));
127: $type = $args[2];
128:
129: if ($this->buddylistManager->remove($name, $type)) {
130: $msg = "<highlight>$name<end> removed from the buddy list successfully.";
131: } else {
132: $msg = "Could not remove <highlight>$name<end> from the buddy list.";
133: }
134:
135: $sendto->reply($msg);
136: }
137:
138: 139: 140: 141:
142: public function buddylistSearchCommand($message, $channel, $sender, $sendto, $args) {
143: $search = $args[1];
144:
145: if (count($this->buddylistManager->buddyList) == 0) {
146: $msg = "There are no characters on the buddy list.";
147: } else {
148: $count = 0;
149: $blob = "Buddy list Search: '{$search}'\n\n";
150: forEach ($this->getSortedBuddyList() as $value) {
151: $removed = '';
152: if (preg_match("/$search/i", $value['name'])) {
153: $count++;
154: $blob .= $value['name'] . " " . implode(' ', array_keys($value['types'])) . "\n";
155: }
156: }
157:
158: if ($count > 0) {
159: $msg = $this->text->makeBlob("Buddy List Search ($count)", $blob);
160: } else {
161: $msg = "No characters on the buddy list found containing '$search'";
162: }
163: }
164: $sendto->reply($msg);
165: }
166:
167: public function getSortedBuddyList() {
168: $buddylist = $this->buddylistManager->buddyList;
169: usort($buddylist, function ($entry1, $entry2) {
170: return $entry1['name'] > $entry2['name'];
171: });
172: return $buddylist;
173: }
174: }
175: