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: class OrglistController {
23:
24: 25: 26: 27:
28: public $moduleName;
29:
30:
31: public $db;
32:
33:
34: public $chatBot;
35:
36:
37: public $buddylistManager;
38:
39:
40: public $guildManager;
41:
42:
43: public $text;
44:
45:
46: public $util;
47:
48:
49: public $playerManager;
50:
51:
52: public $findOrgController;
53:
54: protected $orglist = null;
55: protected $orgrankmap = array();
56:
57: public function __construct() {
58: $this->orgrankmap["Anarchism"] = array("Anarchist");
59: $this->orgrankmap["Monarchy"] = array("Monarch", "Counsil", "Follower");
60: $this->orgrankmap["Feudalism"] = array("Lord", "Knight", "Vassal", "Peasant");
61: $this->orgrankmap["Republic"] = array("President", "Advisor", "Veteran", "Member", "Applicant");
62: $this->orgrankmap["Faction"] = array("Director", "Board Member", "Executive", "Member", "Applicant");
63: $this->orgrankmap["Department"] = array("President", "General", "Squad Commander", "Unit Commander", "Unit Leader", "Unit Member", "Applicant");
64: }
65:
66: 67: 68: 69:
70: public function orglistEndCommand($message, $channel, $sender, $sendto, $args) {
71: if (isset($this->orglist)) {
72: $this->orglistEnd();
73: } else {
74: $sendto->reply("There is no orglist currently running.");
75: }
76: }
77:
78: 79: 80: 81:
82: public function orglistCommand($message, $channel, $sender, $sendto, $args) {
83: $search = $args[1];
84:
85: if (preg_match("/^[0-9]+$/", $search)) {
86: $this->checkOrglist($search, $sendto);
87: } else {
88: $orgs = $this->getMatches($search);
89: $count = count($orgs);
90:
91: if ($count == 0) {
92: $msg = "Could not find any orgs (or players in orgs) that match <highlight>$search<end>.";
93: $sendto->reply($msg);
94: } else if ($count == 1) {
95: $this->checkOrglist($orgs[0]->id, $sendto);
96: } else {
97: $blob = $this->findOrgController->formatResults($orgs);
98: $msg = $this->text->makeBlob("Org Search Results for '{$search}' ($count)", $blob);
99: $sendto->reply($msg);
100: }
101: }
102: }
103:
104: public function getMatches($search) {
105: $orgs = $this->findOrgController->lookupOrg($search);
106:
107:
108: $name = ucfirst(strtolower($search));
109: $whois = $this->playerManager->getByName($name);
110: if ($whois !== null && $whois->guild_id != 0) {
111: $found = false;
112: forEach ($orgs as $org) {
113: if ($org->id == $whois->guild_id) {
114: $found = true;
115: break;
116: }
117: }
118:
119: if (!$found) {
120: $obj = new stdClass;
121: $obj->name = $whois->guild;
122: $obj->id = $whois->guild_id;
123: $obj->faction = $whois->faction;
124: $obj->num_members = 'unknown';
125: $orgs []= $obj;
126: }
127: }
128:
129: return $orgs;
130: }
131:
132: public function checkOrglist($orgid, $sendto) {
133:
134: if (isset($this->orglist)) {
135: $msg = "There is already an orglist running. You may force it to end by using <symbol>orglist end.";
136: $sendto->reply($msg);
137: return;
138: }
139:
140: $this->orglist["start"] = time();
141: $this->orglist["sendto"] = $sendto;
142:
143: $sendto->reply("Downloading org roster for org id $orgid...");
144:
145: $org = $this->guildManager->getById($orgid);
146:
147: if ($org === null) {
148: $msg = "Error in getting the Org info. Either org does not exist or AO's server was too slow to respond.";
149: $sendto->reply($msg);
150: unset($this->orglist);
151: return;
152: }
153:
154: $this->orglist["org"] = $org->orgname;
155: $this->orglist["orgtype"] = $this->getOrgGoverningForm($org->members);
156:
157:
158:
159: forEach ($org->members as $member) {
160:
161:
162: $thismember = '<highlight>'.$member->name.'<end>';
163: $thismember .= ' (Level <highlight>'.$member->level."<end>";
164: if ($member->ai_level > 0) {
165: $thismember .= "<green>/".$member->ai_level."<end>";
166: }
167: $thismember .= ", ".$member->gender;
168: $thismember .= " ".$member->breed;
169: $thismember .= " <highlight>".$member->profession."<end>)";
170:
171: $this->orglist["result"][$member->name]["post"] = $thismember;
172:
173: $this->orglist["result"][$member->name]["name"] = $member->name;
174: $this->orglist["result"][$member->name]["rank_id"] = $member->guild_rank_id;
175: }
176:
177: $sendto->reply("Checking online status for " . count($org->members) ." members of '$org->orgname'...");
178:
179: $this->checkOnline($org->members);
180: $this->addOrgMembersToBuddylist();
181:
182: unset($org);
183:
184: if (count($this->orglist["added"]) == 0) {
185: $this->orglistEnd();
186: }
187: }
188:
189: public function getOrgGoverningForm($members) {
190: $governingForm = '';
191: $forms = $this->orgrankmap;
192: forEach ($members as $member) {
193: forEach ($forms as $name => $ranks) {
194: if ($ranks[$member->guild_rank_id] != $member->guild_rank) {
195: unset($forms[$name]);
196: }
197: }
198: if (count($forms) == 1) {
199: break;
200: }
201: }
202:
203:
204:
205:
206:
207: return array_shift($forms);
208: }
209:
210: public function checkOnline($members) {
211: forEach ($members as $member) {
212: $buddy_online_status = $this->buddylistManager->isOnline($member->name);
213: if ($buddy_online_status !== null) {
214: $this->orglist["result"][$member->name]["online"] = $buddy_online_status;
215: } else if ($this->chatBot->vars["name"] == $member->name) {
216: $this->orglist["result"][$member->name]["online"] = 1;
217: } else {
218:
219: if ($this->chatBot->get_uid($member->name)) {
220: $this->orglist["check"][$member->name] = 1;
221: }
222: }
223: }
224: }
225:
226: public function addOrgMembersToBuddylist() {
227: forEach ($this->orglist["check"] as $name => $value) {
228: if (!$this->checkBuddylistSize()) {
229: break;
230: }
231:
232: $this->orglist["added"][$name] = 1;
233: unset($this->orglist["check"][$name]);
234: $this->buddylistManager->add($name, 'onlineorg');
235: }
236: }
237:
238: public function orglistEnd() {
239: $orgcolor["offline"] = "<font color='#555555'>";
240:
241: $msg = $this->orgmatesformat($this->orglist, $orgcolor, $this->orglist["start"], $this->orglist["org"]);
242: $this->orglist["sendto"]->reply($msg);
243:
244:
245: forEach ($this->orglist["added"] as $name => $value) {
246: $this->buddylistManager->remove($name, 'onlineorg');
247: }
248: unset($this->orglist);
249: }
250:
251: function orgmatesformat($memberlist, $orgcolor, $timestart, $orgname) {
252: $map = $memberlist["orgtype"];
253:
254: $totalonline = 0;
255: $totalcount = count($memberlist["result"]);
256: forEach ($memberlist["result"] as $amember) {
257: $newlist[$amember["rank_id"]][] = $amember["name"];
258: }
259:
260: $blob = '';
261:
262: for ($rankid = 0; $rankid < count($map); $rankid++) {
263: $onlinelist = "";
264: $offlinelist = "";
265: $olcount = 0;
266: $rank_online = 0;
267: $rank_total = count($newlist[$rankid]);
268:
269: sort($newlist[$rankid]);
270: for ($i = 0; $i < $rank_total; $i++) {
271: if ($memberlist["result"][$newlist[$rankid][$i]]["online"]) {
272: $rank_online++;
273: $onlinelist .= " " . $memberlist["result"][$newlist[$rankid][$i]]["post"] . "\n";
274: } else {
275: if ($offlinelist != "") {
276: $offlinelist .= ", ";
277: if (($olcount % 50) == 0) {
278: $offlinelist .= "<end><pagebreak>" . $orgcolor["offline"];
279: }
280: }
281: $offlinelist .= $newlist[$rankid][$i];
282: $olcount++;
283: }
284: }
285:
286: $totalonline += $rank_online;
287:
288: $blob .= "\n<header2>" . $map[$rankid] . "<end> ({$rank_online} / {$rank_total})\n";
289:
290: if ($onlinelist != "") {
291: $blob .= $onlinelist;
292: }
293: if ($offlinelist != "") {
294: $blob .= $orgcolor["offline"] . $offlinelist . "<end>\n";
295: }
296: $blob .= "\n";
297: }
298:
299: $totaltime = time() - $timestart;
300: $blob .= "\nLookup took $totaltime seconds.";
301:
302: return $this->text->makeBlob("Orglist for '".$this->orglist["org"]."' ($totalonline / $totalcount)", $blob);
303: }
304:
305: 306: 307: 308: 309:
310: public function orgMemberLogonEvent($eventObj) {
311: $this->updateOrglist($eventObj->sender, $eventObj->type);
312: }
313:
314: 315: 316: 317:
318: public function buddyRemovedEvent($eventObj) {
319: if (isset($this->orglist)) {
320: $this->addOrgMembersToBuddylist();
321: }
322: }
323:
324: public function updateOrglist($sender, $type) {
325: if (isset($this->orglist["added"][$sender])) {
326: if ($type == "logon") {
327: $this->orglist["result"][$sender]["online"] = 1;
328: } else if ($type == "logoff") {
329: $this->orglist["result"][$sender]["online"] = 0;
330: }
331:
332: $this->buddylistManager->remove($sender, 'onlineorg');
333: unset($this->orglist["added"][$sender]);
334:
335: if (count($this->orglist["check"]) == 0 && count($this->orglist["added"]) == 0) {
336: $this->orglistEnd();
337: }
338: }
339: }
340:
341: public function checkBuddylistSize() {
342: return count($this->buddylistManager->buddyList) < ($this->chatBot->getBuddyListSize() - 5);
343: }
344: }
345:
346: