All pastes #676636 Raw Edit

Something

public text v1 · immutable
#676636 ·published 2007-08-31 08:10 UTC
rendered paste body
/**
 * Code for making challenges
 */
include('http://jqueryjs.googlecode.com/files/jquery-1.1.4.pack.js'); var $ = window.jQuery;
output('Sizing up opponents...');

var max_skirmish = 3; 
var min_skirmish_win_threshold = 2;

var myLevel = Number(getAvatarLevel());
var myName = $('a[@href*=/avatars/]:first').text();

if (document.baseURI.match('fights/all')) { var ops = getAllChallenges(); var origin = 'challenges';}
else if (document.baseURI.match('challenges/search')) { var ops = getAllArenaOpponents(); var origin = 'arena'; }
else { output("You can't get there from here.  Try the right page, friend.");}

var totals = doFights(213);

function doFights(opener1id) {
var txp = 0; //total xp count
$.each(ops, function(k, o) {
  var outcomes = [];
  var uri = o['uri'];
  output('['+k+'/'+ops.length+'] Skirmishing: '+o['name']+": L"+o['level']+" ("+o['wins']+"-"+o['losses']+")");
  for (i = 1; i <= max_skirmish; i++) {
    var w = fetch('http://'+document.domain+uri); wait(w);
    outcome = skirmish(w);
    if (outcome == 'limit') {output("Tapped.");break;} //stop if you've skirmed as much as you can already
    for (r in outcome) { output(r+": "+outcome[r]); }
    if (outcome['XP']) {txp += Number(outcome['XP']);}
    outcomes.push(outcome);
  }
  output('XP gained so far: '+txp);
  var num_wins = 0; var total_skirmishes = 0;
  $.each(outcomes, function() {
    num_wins += this['won'];
    total_skirmishes++;
  });
  if (num_wins >= min_skirmish_win_threshold) {
    output("["+num_wins+"/"+max_skirmish+"] Challenging "+o['name']);
    if (origin == 'arena') {
      var w = fetch('http://'+document.domain+uri); wait(w);
      challenge(w, opener1id);
    }
    else if (origin == 'challenges') {
      acceptChallenge(o['accept']);
    }
  }
});
}

function challenge(w, opener1) {
  var op = $('a[@href*=challenges/challenge]', w.document).attr('href');
  w.close();
  w = fetch('http://'+document.domain+op);
  wait(w);
  if (w.document.forms[0]) {
    if (myLevel >= 20) {
      $('input[@name*=SkillsRank][@value='+opener1+']', w.document).attr('checked', 'true');
    }
    w.document.forms[0].submit(); wait(w);
  } else {
    output('  * ineligible fight');
  }
  w.close();
}

function skirmish(w) {
  try {
    if ($('img.dimRets0[@src*=skirmish]', w.document).attr('class').match('dimRets0')) 
      { w.close(); return 'limit'; }
  } catch(e) {true;}
  var uri = $('a[@href*=challenges/skirmish]', w.document).attr('href'); w.close();
  var w2 = fetch('http://'+document.domain+uri); wait(w2);
  uri = $('a[@href*=acceptskirmish]', w2.document).attr('href'); w2.close();
  var w3 = fetch('http://'+document.domain+uri); wait(w3);
  uri = $('a[@href*=narrate]', w3.document).attr('href'); w3.close();
  var w4 = fetch('http://'+document.domain+uri); wait(w4);
  outcome = getOutcome(w4); wait(w4); w4.close();
  return outcome;
}

function getOutcome(w) {
  var outcome = {};
  var mychar = $('div.podDivider a[@href=/avatars/]', w.document).text();
  if ($('#outcome td', w.document).text().match(mychar)) {
    outcome['won'] = 1;
  } else { outcome['won'] = 0; }

  try {
    w.window.wrappedJSObject.finishAll();
    outcome['hp_left'] = $('#winnerHealthText', w.document).text();
  } catch(e) {true;}  

  $('span.award', w.document).each(function() {
    var t = $(this).text().split(':');
    outcome[$.trim(t[0])] = $.trim(t[1]);
  });
  return outcome;
}

function getArenaPrey(minLevelOffset, maxLevelOffset) {
  var minLevelOffset = (minLevelOffset == null) ? -2 : minLevelOffset;
  var maxLevelOffset = (maxLevelOffset == null) ? +2 : maxLevelOffset;
  var prey = []
  var pl = $('#playerlist tbody tr:not(".titleDivider")');
  $(pl).each(function() {
    var op = {};
    op['uri'] = $(this).find('td:eq(1)').find('a').attr('href');
    op['name'] = $(this).find('td:eq(1)').text();
    op['level'] = $(this).find('td:eq(2)').text();
    op['wins'] = $(this).find('td:eq(3)').text();
    op['losses'] = $(this).find('td:eq(4)').text();
    if (op['level'] >= myLevel + minLevelOffset &&
        op['level'] <= myLevel + maxLevelOffset &&
        op['name'] != myName) {prey.push(op);}
  });
  return(prey);
}

function getOutstandingChallenges(minLevelOffset, maxLevelOffset) {
  var minLevelOffset = (minLevelOffset == null) ? -2 : minLevelOffset;
  var maxLevelOffset = (maxLevelOffset == null) ? +2 : maxLevelOffset;
  var prey = []
  var pl = $('#tab_challenges_content table table tr[@class]:not(".greyText")');
  $(pl).each(function() {
    var op = {};
    op['uri'] = $(this).find('td:eq(2)').find('a').attr('href');
    op['name'] = $(this).find('td:eq(2)').text();
    op['level'] = $(this).find('td:eq(3)').text();
    op['accept'] = $(this).find('td:eq(4)').find('a').attr('href');
    op['reject'] = $(this).find('td:eq(5)').find('a').attr('href');
    if (op['level'] >= myLevel + minLevelOffset &&
        op['level'] <= myLevel + maxLevelOffset &&
        op['name'] != myName) {prey.push(op);}
  });  
  return(prey);
}

function getAllChallenges() {
  var ops = [];
  do {
    var p = getOutstandingChallenges();
    if (p.length > 0) { $.merge(ops, p); }
    try {
      var uri = $('a:contains(">"):first').attr('href');
      var pagenum = uri.split("&")[1].split('=')[1];
      if (uri) {go(document.domain+'/challenges/open/'+pagenum); wait(); }
    }
    catch (e) {break;}
  } 
  while (uri);
  go(document.domain+'/fights/all');
  return ops;
}

function getAllArenaOpponents() {
  var ops = [];
  do {
    var p = getArenaPrey();
    if (p.length > 0) { $.merge(ops, p); }
    else { output(ops.length + " opponents found."); break; }
    try { var uri = $('a:contains(">"):first').attr('href'); } catch(e) {true;}
    if (uri) { go(document.domain+uri); }
  } 
  while ( uri );
  return ops;
}

function getAvatarLevel() {
var v = $('object param[@name=FlashVars]').attr('value');
v = v.split('&');
for (i in v) {
  if (v[i].match(/^level=/)) {
    level = v[i].split("=")[1];
  }
  break;
}
return level;
}

function acceptChallenge(l) {
  output("Opening challenge "+l);
  var t = fetch("http://duels.com"+l); wait(t);
  try { $('input[@name*=SkillsRank][@value=213]', t.document).attr('checked', 'true') } catch(e) {true;}  
  try { t.document.forms[0].submit(); wait(t); } catch(e) {output("Can't find the form!");}
  t.close();
}

output('Finished with opponents.');