bot = &$bot; } /* This gets called on cron */ function cron() { $this -> bot -> log("ROSTER", "UPDATE", "Update of memberslists started!"); $timenow = time(); // Get list of orgbots to update: $orgbots = $this -> bot -> db -> select("SELECT * FROM whoisnet_orgbots"); foreach ($orgbots as $currentbot) { $this -> bot -> log("ROSTER", "UPDATE", "Current bot: " . $currentbot[0] . "!"); $orglist = $this -> bot -> db -> select("SELECT * FROM " . $currentbot[0] . "_trusted_orgs"); $users = $currentbot[0] . "_users"; $org_changes = $currentbot[0] . "_org_changes"; $this -> added = 0; $this -> removed = 0; $this -> rerolled = 0; foreach ($orglist as $orgid) { $this -> bot -> log("ROSTER", "UPDATE", "Starting roster update for " . $orgid[0]); $i = 0; do { $i++; $this -> bot -> log("ROSTER", "UPDATE", "Try " . $i); // Get the guild roster for the current guild: $members = $this -> parse_org($this -> bot -> dimension, $orgid[0]); // if error while grabbing the data, wait 5 seconds so bot doesn't spam FC site if ($members === FALSE) sleep(15); } while ($members === FALSE); $this -> bot -> log("ROSTER", "UPDATE", "Adding members..."); /* Go trough all members and make sure we are up to date. */ foreach ($members as $member) { $db_member = $this -> bot -> db -> select("SELECT char_id, nickname, user_level, updated_at FROM " . $users . " WHERE nickname = '" . $member["nickname"] . "'"); /* If we dont have this user in the user table, or if its a guest, or if its a deleted character we have no updates for over 2 days on, its a new member we havent picked up for some reason. */ if (empty($db_member)) { $this -> add ($currentbot[0], "Roster-XML", $member["id"], $member["nickname"], "from XML"); } else if ($db_member[0][2] == 1 || ($db_member[0][2] == 0 && (($db_member[0][3] + 172800) <= time()))) { $this -> add ($currentbot[0], "Roster-XML", $member["id"], $member["nickname"], "from XML"); } /* We have an entry for the nickname, but the character id's have changed, rerolled character. */ else if ($db_member[0][0] != $member["id"]) { echo "Wrong ID!\n"; $this -> erase($currentbot[0], "Roster-XML", $db_member[0][0], $member["nickname"], "char_id mismatch"); $this -> add ($currentbot[0], "Roster-XML-Reroll", $member["id"], $member["nickname"], "after reroll"); $this -> rerolled++; } /* Update the timestamp */ $this -> bot -> db -> query("UPDATE " . $users . " SET updated_at = '" . $timenow ."' WHERE nickname = '" . $member["nickname"] . "'"); } $this -> bot -> log("ROSTER", "UPDATE", "Members added!"); } $this -> bot -> log("ROSTER", "UPDATE", "Parsing org changes..."); // use org change table for additional joins/leaves: // first remove all no longer in org $remmems = $this -> bot -> db -> select("SELECT nickname FROM " . $currentbot[0] . "_org_changes " . "WHERE lastaction = 'leave' OR lastaction = 'kick'"); $memdelcount = 0; if (!empty($remmems)) { foreach ($remmems as $memgone) { $this -> del($currentbot[0], "Org.Msg history", $this -> bot -> aoc -> get_uid($memgone[0]), $memgone[0], "no longer in org"); $memdelcount++; } } $this -> bot -> log("ROSTER", "UPDATE", $memdelcount . " members potentially deleted due to org. msg. infos!"); // now make sure all new members are in there $addmems = $this -> bot -> db -> select("SELECT nickname FROM " . $currentbot[0] . "_org_changes WHERE lastaction = 'join'"); $memaddcount = 0; if (!empty($addmems)) { foreach ($addmems as $newmem) { $this -> add($currentbot[0], "Org.Msg history", $this -> bot -> aoc -> get_uid($newmem[0]), $newmem[0], "joined org"); $this -> bot -> db -> query("UPDATE " . $users . " SET updated_at = '" . $timenow ."' WHERE nickname = '" . $newmem[0] . "'"); $memaddcount++; } } $this -> bot -> log("ROSTER", "UPDATE", $memaddcount . " members potentially added due to org. msg. infos!"); // finally delete all old entries: $this -> bot -> db -> query("DELETE FROM " . $currentbot[0] . "_org_changes WHERE timestamp <= " . time() . " - 7*24*60*60"); $this -> bot -> log("ROSTER", "UPDATE", "Outdated information out of org. msg. deleted!"); // Now delete all characters that still are in the members table, but didn't get updated. Assume they left: $countdels = $this -> bot -> db -> select("SELECT count(*) FROM " . $users . " WHERE updated_at < " . $timenow . " AND user_level >= 2"); $this -> bot -> db -> query("UPDATE " . $users . " SET user_level = 0, deleted_by = 'Whoisnet', deleted_at = " . $timenow . ", updated_at = " . $timenow . " WHERE updated_at < " . $timenow . " AND user_level >= 2"); $this -> removed += $countdels[0][0]; $this -> bot -> db -> query("UPDATE roster_updates SET status = 'true' WHERE botname = '" . $currentbot[0] . "'"); $this -> bot -> log("ROSTER", "UPDATE", "Memberslist of " . $currentbot[0] . " updated!"); if ($currentbot[1] != '') { $this -> bot -> db -> query("UPDATE roster_updates SET status = 'true' WHERE botname = '" . $currentbot[1] . "'"); $this -> bot -> log("ROSTER", "UPDATE", $currentbot[1] . " informed about update too!"); } $this -> bot -> log("ROSTER", "UPDATE", "Roster update complete. Added " . $this -> added . " members and removed " . $this -> removed . " of which " . $this -> rerolled . " was rerolled."); // Sleep 15secs to reduce spammage of FC server sleep(15); } $this -> bot -> log("ROSTER", "UPDATE", "Work done!"); // This bot has done it's job when all memberlists are updated, so terminating: die("The bot has been shutdown.\n"); } function add($bot, $source, $id, $name, $reason) { $result = $this -> bot -> user -> add($bot, $source, $name, $id, 2, 0, 1); if ($result["error"]) { $this -> bot -> log("ROSTER", "FAILED", $result["errordesc"]); } else { $this -> added++; } } function del($bot, $source, $id, $name, $reason) { $result = $this -> bot -> user -> del($bot, $source, $name, $id, 1); if ($result["error"]) { $this -> bot -> log("ROSTER", "FAILED", $result["errordesc"]); } else { $this -> removed++; } } function erase($bot, $source, $id, $nickname, $reason) { $result = $this -> bot -> user -> erase($bot, $source, $nickname, 1, $id); if ($result["error"]) { $this -> bot -> log("ROSTER", "FAILED", $result["errordesc"]); } } function parse_org($dim, $id) { // Get the guild roster $ret = $this -> bot -> get_site("http://people.anarchy-online.com/org/stats/d/$dim/name/$id/basicstats.xml"); if ($ret['error']) { echo "ERROR: " . $ret['errordesc'] . "\n"; return FALSE; } $org = $ret['content']; if (stripos($org, "") === false) { echo "Incomplete XML tree!\n"; return FALSE; } $faction = $this -> bot -> xmlparse($org, "side"); $orgname = $this -> bot -> xmlparse($org, "name"); $org = explode("", $org); // Build array of members for ($i = 1; $i < count($org); $i++) { $members[($i - 1)]["nickname"] = $this -> bot -> xmlparse($org[$i], "nickname"); $members[($i - 1)]["id"] = $this -> bot -> aoc -> get_uid($members[($i - 1)]["nickname"]); } $this -> bot -> log("ROSTER", "UPDATE", "Parsing done!"); return $members; } } ?>