//--------------------------------------------------
//RANKS
//--------------------------------------------------
$Prestige::Name[0] = ""; //none!
$Prestige::Name[1] = "Instructive ";
$Prestige::Name[2] = "Excelling ";
$Prestige::Name[3] = "Champion ";
$Prestige::Name[4] = "Prestigious ";
$Prestige::Name[5] = "Supreme ";
$Prestige::Name[6] = "Glorious ";
$Prestige::Name[7] = "Ultimate ";
$Prestige::Name[8] = "Shadowing ";
$Prestige::Name[9] = "Phantom ";
$Prestige::Name[10] = "(*) Phantom ";

$Ranks::MinPoints[0] = 0;
$Ranks::NewRank[0] = "Private";
$Ranks::RankTag[0] = "[Pvt]";

$Ranks::MinPoints[1] = 25;
$Ranks::NewRank[1] = "Private Grade I";
$Ranks::RankTag[1] = "[Pvt1]";

$Ranks::MinPoints[2] = 50;
$Ranks::NewRank[2] = "Private Grade II";
$Ranks::RankTag[2] = "[Pvt2]";

$Ranks::MinPoints[3] = 100;
$Ranks::NewRank[3] = "Private Grade III";
$Ranks::RankTag[3] = "[Pvt3]";

$Ranks::MinPoints[4] = 150;
$Ranks::NewRank[4] = "Gunnary Private";
$Ranks::RankTag[4] = "[GPvt]";

$Ranks::MinPoints[5] = 200;
$Ranks::NewRank[5] = "Gunnary Private Grade I";
$Ranks::RankTag[5] = "[GPvt1]";

$Ranks::MinPoints[6] = 250;
$Ranks::NewRank[6] = "Gunnary Private Grade II";
$Ranks::RankTag[6] = "[GPvt2]";

$Ranks::MinPoints[7] = 300;
$Ranks::NewRank[7] = "Gunnary Private Grade III";
$Ranks::RankTag[7] = "[GPvt3]";

$Ranks::MinPoints[8] = 400;
$Ranks::NewRank[8] = "Corporal";
$Ranks::RankTag[8] = "[Cpl]";

$Ranks::MinPoints[9] = 500;
$Ranks::NewRank[9] = "Corporal Grade I";
$Ranks::RankTag[9] = "[Cpl1]";

$Ranks::MinPoints[10] = 600;
$Ranks::NewRank[10] = "Corporal Grade II";
$Ranks::RankTag[10] = "[Cpl2]";

$Ranks::MinPoints[11] = 700;
$Ranks::NewRank[11] = "Corporal Grade III";
$Ranks::RankTag[11] = "[Cpl3]";

$Ranks::MinPoints[12] = 900;
$Ranks::NewRank[12] = "Corporal Grade IV";
$Ranks::RankTag[12] = "[Cpl4]";

$Ranks::MinPoints[13] = 1150;
$Ranks::NewRank[13] = "Sergeant";
$Ranks::RankTag[13] = "[Sgt]";

$Ranks::MinPoints[14] = 1400;
$Ranks::NewRank[14] = "Sergeant Grade I";
$Ranks::RankTag[14] = "[Sgt1]";

$Ranks::MinPoints[15] = 1700;
$Ranks::NewRank[15] = "Sergeant Grade II";
$Ranks::RankTag[15] = "[Sgt2]";

$Ranks::MinPoints[16] = 2000;
$Ranks::NewRank[16] = "Sergeant Grade III";
$Ranks::RankTag[16] = "[Sgt3]";

$Ranks::MinPoints[17] = 2500;
$Ranks::NewRank[17] = "Sergeant Grade IV";
$Ranks::RankTag[17] = "[Sgt4]";

$Ranks::MinPoints[18] = 3000;
$Ranks::NewRank[18] = "Gunnary Sergeant";
$Ranks::RankTag[18] = "[GSgt]";

$Ranks::MinPoints[19] = 3500;
$Ranks::NewRank[19] = "Gunnary Sergeant Grade I";
$Ranks::RankTag[19] = "[GSgt1]";

$Ranks::MinPoints[20] = 4000;
$Ranks::NewRank[20] = "Gunnary Sergeant Grade II";
$Ranks::RankTag[20] = "[GSgt2]";

$Ranks::MinPoints[21] = 4500;
$Ranks::NewRank[21] = "Gunnary Sergeant Grade III";
$Ranks::RankTag[21] = "[GSgt3]";

$Ranks::MinPoints[22] = 5000;
$Ranks::NewRank[22] = "Gunnary Sergeant Grade IV";
$Ranks::RankTag[22] = "[GSgt4]";

$Ranks::MinPoints[23] = 6000;
$Ranks::NewRank[23] = "Lieutenant";
$Ranks::RankTag[23] = "[Lt]";

$Ranks::MinPoints[24] = 7000;
$Ranks::NewRank[24] = "Lieutenant Grade I";
$Ranks::RankTag[24] = "[Lt1]";

$Ranks::MinPoints[25] = 8000;
$Ranks::NewRank[25] = "Lieutenant Grade II";
$Ranks::RankTag[25] = "[Lt2]";

$Ranks::MinPoints[26] = 9000;
$Ranks::NewRank[26] = "Lieutenant Grade III";
$Ranks::RankTag[26] = "[Lt3]";

$Ranks::MinPoints[27] = 10000;
$Ranks::NewRank[27] = "Lieutenant Grade IV";
$Ranks::RankTag[27] = "[Lt4]";

$Ranks::MinPoints[28] = 12500;
$Ranks::NewRank[28] = "Captain";
$Ranks::RankTag[28] = "[Cpt]";

$Ranks::MinPoints[29] = 15000;
$Ranks::NewRank[29] = "Captain Grade I";
$Ranks::RankTag[29] = "[Cpt1]";

$Ranks::MinPoints[30] = 20000;
$Ranks::NewRank[30] = "Captain Grade II";
$Ranks::RankTag[30] = "[Cpt2]";

$Ranks::MinPoints[31] = 25000;
$Ranks::NewRank[31] = "Captain Grade III";
$Ranks::RankTag[31] = "[Cpt3]";

$Ranks::MinPoints[32] = 30000;
$Ranks::NewRank[32] = "Major";
$Ranks::RankTag[32] = "[Maj]";

$Ranks::MinPoints[33] = 37500;
$Ranks::NewRank[33] = "Major Grade I";
$Ranks::RankTag[33] = "[Maj1]";

$Ranks::MinPoints[34] = 45000;
$Ranks::NewRank[34] = "Major Grade II";
$Ranks::RankTag[34] = "[Maj2]";

$Ranks::MinPoints[35] = 52500;
$Ranks::NewRank[35] = "Major Grade III";
$Ranks::RankTag[35] = "[Maj3]";

$Ranks::MinPoints[36] = 60000;
$Ranks::NewRank[36] = "Lieutenant Colonel";
$Ranks::RankTag[36] = "[LCol]";

$Ranks::MinPoints[37] = 70000;
$Ranks::NewRank[37] = "Lieutenant Colonel Grade I";
$Ranks::RankTag[37] = "[LCol1]";

$Ranks::MinPoints[38] = 80000;
$Ranks::NewRank[38] = "Lieutenant Colonel Grade II";
$Ranks::RankTag[38] = "[LCol2]";

$Ranks::MinPoints[39] = 90000;
$Ranks::NewRank[39] = "Lieutenant Colonel Grade III";
$Ranks::RankTag[39] = "[LCol3]";

$Ranks::MinPoints[40] = 100000;
$Ranks::NewRank[40] = "Colonel";
$Ranks::RankTag[40] = "[Col]";

$Ranks::MinPoints[41] = 125000;
$Ranks::NewRank[41] = "Colonel Grade I";
$Ranks::RankTag[41] = "[Col1]";

$Ranks::MinPoints[42] = 150000;
$Ranks::NewRank[42] = "Colonel Grade II";
$Ranks::RankTag[42] = "[Col2]";

$Ranks::MinPoints[43] = 175000;
$Ranks::NewRank[43] = "Brigadier";
$Ranks::RankTag[43] = "[Bri]";

$Ranks::MinPoints[44] = 200000;
$Ranks::NewRank[44] = "Brigadier Grade I";
$Ranks::RankTag[44] = "[Bri1]";

$Ranks::MinPoints[45] = 250000;
$Ranks::NewRank[45] = "Brigadier Grade II";
$Ranks::RankTag[45] = "[Bri2]";

$Ranks::MinPoints[46] = 300000;
$Ranks::NewRank[46] = "Brigadier General";
$Ranks::RankTag[46] = "[BriGn]";

$Ranks::MinPoints[47] = 350000;
$Ranks::NewRank[47] = "Brigadier General I";
$Ranks::RankTag[47] = "[BriGn1]";

$Ranks::MinPoints[48] = 425000;
$Ranks::NewRank[48] = "Brigadier General II";
$Ranks::RankTag[48] = "[BriGn2]";

$Ranks::MinPoints[49] = 500000;
$Ranks::NewRank[49] = "General";
$Ranks::RankTag[49] = "[Gen]";

$Ranks::MinPoints[50] = 575000;
$Ranks::NewRank[50] = "2 Star General";
$Ranks::RankTag[50] = "[2Gen]";

$Ranks::MinPoints[51] = 650000;
$Ranks::NewRank[51] = "3 Star General";
$Ranks::RankTag[51] = "[3Gen]";

$Ranks::MinPoints[52] = 800000;
$Ranks::NewRank[52] = "4 Star General";
$Ranks::RankTag[52] = "[4Gen]";

$Ranks::MinPoints[53] = 1000000;
$Ranks::NewRank[53] = "5 Star General";
$Ranks::RankTag[53] = "[5Gen]";

$Ranks::MinPoints[54] = 1250000;
$Ranks::NewRank[54] = "Master General";
$Ranks::RankTag[54] = "[MGen]";

$Ranks::MinPoints[55] = 1500000;
$Ranks::NewRank[55] = "Master General I";
$Ranks::RankTag[55] = "[MGen1]";

$Ranks::MinPoints[56] = 1750000;
$Ranks::NewRank[56] = "Master General II";
$Ranks::RankTag[56] = "[MGen2]";

$Ranks::MinPoints[57] = 2000000;
$Ranks::NewRank[57] = "Commanding General";
$Ranks::RankTag[57] = "[ComGen]";

$Ranks::MinPoints[58] = 2250000;
$Ranks::NewRank[58] = "Fleet Commander";
$Ranks::RankTag[58] = "[FltCom]";

$Ranks::MinPoints[59] = 2500000;
$Ranks::NewRank[59] = "Commanding Officer";
$Ranks::RankTag[59] = "[ComOcr]";

$Ranks::MinPoints[60] = 2750000;
$Ranks::NewRank[60] = "Commander";
$Ranks::RankTag[60] = "[Cmdr]";

$Ranks::MinPoints[61] = 3000000;
$Ranks::NewRank[61] = "Master Commander";
$Ranks::RankTag[61] = "[MCmdr]";
$Rank::RankCount = 61;

//TOP RANKS
function findTopRanks() {
   %server = ""@$PGDServer@":"@$PGDPort@"";
   %filename = "/public/Univ/Ranks/TWM2/top.php";
   if (!isObject(Top_Rank_Grab)) {
      %Downloader = new HTTPObject(Top_Rank_Grab){};
   }
   else {
      %Downloader = Top_Rank_Grab;
   }
   $TopRanks::LineCount = 0;
   //If the server crashes here, let everyone know why
   MessageAll('MsgAdminForce', "\c5SERVER: Downloading Top Ranks, Possible Lag.");
   %Downloader.get(%server, %filename);
   %Downloader.schedule(10000, 0, "Disconnect");
}

function Top_Rank_Grab::onLine(%this, %line) {
   %strPos = strStr(%line, " Registered Players*");
   %star1 = strStr(%line, "*");
   if(%this.count !$= "") {
      $TopRanks::LineCount++;
      $TopRanks::Line[$TopRanks::LineCount] = %line;
   }
   if(%strPos != -1 && %star1 != -1) {
      %this.count = getSubStr(%line, %star1+1, %strPos-(%star1+1));
   }
}

function Top_Rank_Grab::onConnectFailed(%this) {
   error("-- Could not connect to PGD.");
   error("Top Rank Download: fail (connection)");
}

function Top_Rank_Grab::onDisconnect(%this) {
   echo("Top Rank Download: OK, "@%this.count@" ranks");
   SortTopRanks(%this);
   %this.delete();
}

function SortTopRanks(%dlOBJ) {
   for(%i = 1; %i <= %dlOBJ.count; %i++) {
      %line = $TopRanks::Line[%i];
      //First, lets gather out the rank
      %RankName1 = strStr(%line, "title=\"");
      %RankName2 = strStr(%line, "width=") - 2;
      %RankName = getSubStr(%line, (%rankName1+7), (%rankName2-(%rankName1+7)));
      //Second, lets get the name and guid
      //GUID is not currently used, but hey, we may need it one day
      %GUID1 = strStr(%line, "guid=") + 5;
      %GUID2 = strStr(%line, "guid=") + 12;
      %GUID = getSubStr(%line, (%GUID1), (%GUID2-(%GUID1)));
      //
      %PlayerName1 = strStr(%line, "guid=") + 12;
      %PlayerName2 = strStr(%line, "</a>") - 1;
      %PlayerName = getSubStr(%line, (%PlayerName1)+1, (%PlayerName2-(%PlayerName1)));
      //Third, lets gather out the EXP
      %EXP1 = strStr(%line, "</a>:") + 5;
      %EXP2 = strStr(%line, "<p>") - 4;
      %EXP = getSubStr(%line, (%EXP1), (%EXP2-(%EXP1)));
      %EXP = Trim(stripChars(%EXP, ","));
      //Lastly, lets sort the needed data
      %rPos = %i;
      $Rank::Top[%i] = ""@%PlayerName@"";
      $Rank::TopXP[%i] = %EXP;
      $Rank::TopRank[%i] = ""@%RankName@"";
   }
}

function DoNameChangeChecks(%client) {
   if(!$TWM2::UseRankTags) {
      CheckGUID(%client);
      return;
   }
   %stillLooking = 1;
   %sO = %client.TWM2Core;
   //Obtain the rank number
   if(%sO.rankNumber $= "") {
      for(%j = 0; %j <= $Rank::RankCount; %j++) {
         if(%stillLooking) {
	        if(getCurrentEXP(%client) >= $Ranks::MinPoints[%j]) {
               %sO.rankNumber = %j;
            }
            else {
               %stillLooking = 0;
            }
         }
      }
   }
   //
   %tag = $Ranks::RankTag[%sO.rankNumber];
   //
   %name = "\cp\c9" @ %tag @ "\c6" @ %client.namebase @ "\co";
   MessageAll( 'MsgClientNameChanged', "", %client.name, %name, %client );
   removeTaggedString(%client.name);
   %client.name = addTaggedString(%name);
   setTargetName(%client.target, %client.name);
}
