I figured this would be handy for those who want to have a local guild system that allows people to name their own guilds without conflicting with global ones.
The only issue with it at the moment is that it doesn't auto-update, but that can be fixed if you have a loop check the count every hour or so. I'll leave that part up to you.
After the script pulls all the guild information from the Graal website it's ready to be manipulated, and compared.
Enjoy.
PHP Code:
// Store it in a DB-NPC called GlobalGuilds or something..
function onCreated() {
onPopulateDB();
}
function onCheckCount() {
temp.oldcount = this.guildcount;
getGuildCount();
waitfor(this, "CompletedPhase");
if (this.guildcount != oldcount) onPopulateDB(true);
}
function onPopulateDB(silent) {
// Clear DB
this.clearvars();
// Specify temp file
this.parsefile = "cartridge/testfile.txt";
// Get Global Guild Count
getGuildCount();
waitfor(this, "CompletedPhase");
// Determine Pages
temp.pages = ciel(this.guildcount / 50);
// Begin crawling through pages
for (temp.i = 1; temp.i <= pages; temp.i++) {
getGuildList(i);
waitfor(this, "CompletedPhase");
}
// Get Special Guilds
getGuildList("1&type=special");
waitfor(this, "CompletedPhase");
// Delete temp file
deletefile(this.parsefile);
this.parsefile = "";
// Finished
if (!silent) echo("Finished Populating Global Guilds DB");
}
function ciel(val) {
if (val.pos(".") >= 0) {
val = int(val)+1;
}
return val;
}
/*
Determine Active Global Guild Count
*/
function getGuildCount() {
temp.link = "http://www.graalonline.com/zone/guilds/guildslist?type=OK&sort=created_gmt&order=descending&p=1";
temp.req = requesturl(link);
this.catchevent(temp.req, "onReceiveData", "onGuildCountData");
}
function onGuildCountData(obj) {
obj.fulldata.savestring(this.parsefile, 0);
onParseCountData();
}
function onParseCountData() {
temp.lines.loadlines(this.parsefile);
temp.gcstr = "<span class=\"values\">";
for (temp.l: lines) {
if (l.pos(gcstr) >= 0) {
temp.gcount = l.substring(l.pos(gcstr)+gcstr.length());
temp.gcount = gcount.substring(0, gcount.pos("<"));
this.guildcount = gcount;
break;
}
}
this.trigger("CompletedPhase", "");
}
/*
Parse Guilds List
*/
function getGuildList(page) {
temp.link = "http://www.graalonline.com/zone/guilds/guildslist?type=OK&sort=created_gmt&order=descending&p=" @ page;
temp.req = requesturl(link);
this.catchevent(temp.req, "onReceiveData", "onGuildListData");
}
function onGuildListData(obj) {
// Saves it into a parseable format.
obj.fulldata.savestring(this.parsefile, 0);
// Parse list
onParseListData();
}
function onParseListData() {
temp.lines.loadlines(this.parsefile);
for (temp.l: lines) {
// Check for Guild Tag Line
if (l.pos("<td class=\"guildtag\">") >= 0) {
// Record Guild Data
temp.guildnext = true;
continue;
}
else if (guildnext) {
// Unset Boolean
temp.guildnext = false;
// Get Guild ID
temp.gid = l.substring(l.pos("gid=") + "gid=".length());
temp.gid = gid.substring(0, gid.pos(">")-1);
// Get Guild Name
temp.gname = l.substring(l.pos(">")+1);
temp.gname = gname.substring(0, gname.pos("<"));
// Ignore Column Heading
if (gname == "Guild") continue;
// Record Data
onRecordGuild(gid, gname);
}
}
this.trigger("CompletedPhase", "");
}
function onRecordGuild(gid, gname) {
this.("guildid_" @ gid) = gname;
this.("guildname_" @ gname) = gid;
}
public function getGuildName(gid) {
return this.("guildid_" @ gid);
}
public function getGuildID(gname) {
return this.("guildname_" @ gname);
}
public function isGlobalGuild(gname) {
return (getGuildID(gname) > 0);
}
/*
Unused but nifty, Check if Guild Exists by Guild ID
*/
function onCheckGuild(temp.gid) {
this.sid = gid;
temp.link = http://www.graalonline.com/guilds/viewguild.php?full=0&gid=" @ gid;
temp.req = requesturl(link);
this.catchevent(temp.req, "onReceiveData", "onGuildData");
}
function onGuildData(obj) {
if (obj.fulldata.pos("<H3>Invalid Guild ID!</H3>") >= 0) {
echo("Invalid Guild ID");
return;
}
temp.idstr = "<B>Guild Tag</B></TD><TD class=\"aListBL\">";
temp.idpos = obj.fulldata.pos(idstr);
temp.idscp = obj.fulldata.substring(idpos+idstr.length());
temp.gname = idscp.substring(0, idscp.pos("</TD></TR>"));
onRecordGuild(this.sid, gname);
}