Skip to content

Instantly share code, notes, and snippets.

@ootz0rz
Last active September 23, 2023 11:21
Show Gist options
  • Save ootz0rz/11340286 to your computer and use it in GitHub Desktop.
Save ootz0rz/11340286 to your computer and use it in GitHub Desktop.
###
#
# copy/paste me into your javascript console (as compiled JS)
#
###
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Load JQUERY (load this up first before doing anything else...)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
jq = window.document.createElement('script')
jq.src = "//code.jquery.com/jquery-2.1.0.min.js"
window.document.getElementsByTagName('head')[0].appendChild(jq)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Inventory
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class InventoryItem
constructor: (@origStr) ->
strSplit = @origStr.split(" ")
@value = 1
tryParse = parseInt(strSplit[0], 10)
if !isNaN(tryParse)
@value = tryParse
@name = strSplit[1..].join(" ")
else
@name = strSplit.join(" ")
@name = @name.trim()
isSameType: (other) ->
return @name == other.name
class Inventory
constructor: (@objID) ->
@refresh()
refresh: ->
@obj = $(@objID)
@inventory = @_parse_items(@obj)
@items = {}
for item in @inventory
@items[item.name] = item.value
_parse_items: (obj) ->
inventory = []
contents = obj.contents()
for item in contents[1..]
if item.length > 0
inventory.push(new InventoryItem(item.textContent))
return inventory
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Problems
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class AwardCostItem
reCostAward = /([+-]+)([\d]*)[\s]*(.*?)$/
constructor: (@origStr) ->
vals = @origStr.match(reCostAward)[1..]
@isAward = if (vals[0] == "+") then true else false;
@name = vals[2];
if @isAward
@value = if (vals[1].length == 0) then 1 else parseInt(vals[1], 10)
else
@value = if (vals[1].length == 0) then -1 else (-1 * parseInt(vals[1], 10))
@name = @name.trim()
equals: (other) ->
return @value == other.value && @isAward == other.isAward && @isSameType(other)
isSameType: (other) ->
return @name == other.name
class ProblemItem
reWeightSplit = /([-|+])/
constructor: (@objID) ->
@awards = []
@costs = []
@_parse(@objID)
_parse_item_list: (item_list) ->
awards = []
for itm, idx in item_list
$cur_item = $(itm)
_award_split = $cur_item.text().split(reWeightSplit)[1..]
award_list = []
for item, i in _award_split by 2
award_list.push(_award_split[i] + _award_split[i + 1])
for item, i in award_list
awards.push(new AwardCostItem(item))
return awards
_parse: (obj) ->
@_awards = $(".award", obj)
@_costs = $(".cost", obj)
@awards = @_parse_item_list(@_awards)
@costs = @_parse_item_list(@_costs)
curitem = null
class Problems
constructor: (@objID) ->
@refresh()
refresh: ->
@obj = $(@objID)
@_parse_actions(@obj)
_do_click: (links) ->
for link in links
link.click()
_parse_actions: (obj) ->
@tree = []
# parse all items
for item, id in $("div", @obj)
$item = $(item)
pItem = new ProblemItem($item)
@tree.push(pItem)
curitem = pItem
@min_requirements = {}
for node, idx in @tree
# for each cost, find a Problem with corresponding award
for cost in node.costs
if cost.name of @min_requirements
@min_requirements[cost.name] -= cost.value
else
@min_requirements[cost.name] = -cost.value
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Solver
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class Solver
constructor: ->
@inv = new Inventory("#equipment")
@probs = new Problems("#problems")
_do_click: (node) ->
if node.value != null
links = $("a", node.objID)
for link in links
link.click()
_doStuff: ->
@inv.refresh()
@probs.refresh()
# now that we know what depends on what...
for node in @probs.tree
if node.costs.length > 0
# if we have min_requirements[cost.name] >= inventory[cost.name],
# then click
for cost in node.costs
if cost.name of @probs.min_requirements
if cost.name of @inv.items && @inv.items[cost.name] >= @probs.min_requirements[cost.name]
# click
@_do_click(node)
else
# not a requirement, so click
@_do_click(node)
else
# no requirements
@_do_click(node)
intVal = null
s = new Solver()
start = ->
intVal = setInterval(
() ->
s._doStuff()
, 100);
stop = ->
clearInterval(intVal)
# execute start(); to begin the things
@ootz0rz
Copy link
Author

ootz0rz commented Apr 29, 2014

// Generated by CoffeeScript 1.7.1

/*
 *
 * copy/paste me into your javascript console (as compiled JS)
 *
 */
var AwardCostItem, Inventory, InventoryItem, ProblemItem, Problems, Solver, curitem, intVal, jq, s, start, stop;

jq = window.document.createElement('script');
jq.src = "//code.jquery.com/jquery-2.1.0.min.js";
window.document.getElementsByTagName('head')[0].appendChild(jq);

/*
 * Wait a few seconds before pasting rest...
 */

InventoryItem = (function() {
  function InventoryItem(origStr) {
    var strSplit, tryParse;
    this.origStr = origStr;
    strSplit = this.origStr.split(" ");
    this.value = 1;
    tryParse = parseInt(strSplit[0], 10);
    if (!isNaN(tryParse)) {
      this.value = tryParse;
      this.name = strSplit.slice(1).join(" ");
    } else {
      this.name = strSplit.join(" ");
    }
    this.name = this.name.trim();
  }

  InventoryItem.prototype.isSameType = function(other) {
    return this.name === other.name;
  };

  return InventoryItem;

})();

Inventory = (function() {
  function Inventory(objID) {
    this.objID = objID;
    this.refresh();
  }

  Inventory.prototype.refresh = function() {
    var item, _i, _len, _ref, _results;
    this.obj = $(this.objID);
    this.inventory = this._parse_items(this.obj);
    this.items = {};
    _ref = this.inventory;
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      item = _ref[_i];
      _results.push(this.items[item.name] = item.value);
    }
    return _results;
  };

  Inventory.prototype._parse_items = function(obj) {
    var contents, inventory, item, _i, _len, _ref;
    inventory = [];
    contents = obj.contents();
    _ref = contents.slice(1);
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      item = _ref[_i];
      if (item.length > 0) {
        inventory.push(new InventoryItem(item.textContent));
      }
    }
    return inventory;
  };

  return Inventory;

})();

AwardCostItem = (function() {
  var reCostAward;

  reCostAward = /([+-]+)([\d]*)[\s]*(.*?)$/;

  function AwardCostItem(origStr) {
    var vals;
    this.origStr = origStr;
    vals = this.origStr.match(reCostAward).slice(1);
    this.isAward = vals[0] === "+" ? true : false;
    this.name = vals[2];
    if (this.isAward) {
      this.value = vals[1].length === 0 ? 1 : parseInt(vals[1], 10);
    } else {
      this.value = vals[1].length === 0 ? -1 : -1 * parseInt(vals[1], 10);
    }
    this.name = this.name.trim();
  }

  AwardCostItem.prototype.equals = function(other) {
    return this.value === other.value && this.isAward === other.isAward && this.isSameType(other);
  };

  AwardCostItem.prototype.isSameType = function(other) {
    return this.name === other.name;
  };

  return AwardCostItem;

})();

ProblemItem = (function() {
  var reWeightSplit;

  reWeightSplit = /([-|+])/;

  function ProblemItem(objID) {
    this.objID = objID;
    this.awards = [];
    this.costs = [];
    this._parse(this.objID);
  }

  ProblemItem.prototype._parse_item_list = function(item_list) {
    var $cur_item, award_list, awards, i, idx, item, itm, _award_split, _i, _j, _k, _len, _len1, _len2;
    awards = [];
    for (idx = _i = 0, _len = item_list.length; _i < _len; idx = ++_i) {
      itm = item_list[idx];
      $cur_item = $(itm);
      _award_split = $cur_item.text().split(reWeightSplit).slice(1);
      award_list = [];
      for (i = _j = 0, _len1 = _award_split.length; _j < _len1; i = _j += 2) {
        item = _award_split[i];
        award_list.push(_award_split[i] + _award_split[i + 1]);
      }
      for (i = _k = 0, _len2 = award_list.length; _k < _len2; i = ++_k) {
        item = award_list[i];
        awards.push(new AwardCostItem(item));
      }
    }
    return awards;
  };

  ProblemItem.prototype._parse = function(obj) {
    this._awards = $(".award", obj);
    this._costs = $(".cost", obj);
    this.awards = this._parse_item_list(this._awards);
    return this.costs = this._parse_item_list(this._costs);
  };

  return ProblemItem;

})();

curitem = null;

Problems = (function() {
  function Problems(objID) {
    this.objID = objID;
    this.refresh();
  }

  Problems.prototype.refresh = function() {
    this.obj = $(this.objID);
    return this._parse_actions(this.obj);
  };

  Problems.prototype._do_click = function(links) {
    var link, _i, _len, _results;
    _results = [];
    for (_i = 0, _len = links.length; _i < _len; _i++) {
      link = links[_i];
      _results.push(link.click());
    }
    return _results;
  };

  Problems.prototype._parse_actions = function(obj) {
    var $item, cost, id, idx, item, node, pItem, _i, _j, _len, _len1, _ref, _ref1, _results;
    this.tree = [];
    _ref = $("div", this.obj);
    for (id = _i = 0, _len = _ref.length; _i < _len; id = ++_i) {
      item = _ref[id];
      $item = $(item);
      pItem = new ProblemItem($item);
      this.tree.push(pItem);
      curitem = pItem;
    }
    this.min_requirements = {};
    _ref1 = this.tree;
    _results = [];
    for (idx = _j = 0, _len1 = _ref1.length; _j < _len1; idx = ++_j) {
      node = _ref1[idx];
      _results.push((function() {
        var _k, _len2, _ref2, _results1;
        _ref2 = node.costs;
        _results1 = [];
        for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
          cost = _ref2[_k];
          if (cost.name in this.min_requirements) {
            _results1.push(this.min_requirements[cost.name] -= cost.value);
          } else {
            _results1.push(this.min_requirements[cost.name] = -cost.value);
          }
        }
        return _results1;
      }).call(this));
    }
    return _results;
  };

  return Problems;

})();

Solver = (function() {
  function Solver() {
    this.inv = new Inventory("#equipment");
    this.probs = new Problems("#problems");
  }

  Solver.prototype._do_click = function(node) {
    var link, links, _i, _len, _results;
    if (node.value !== null) {
      links = $("a", node.objID);
      _results = [];
      for (_i = 0, _len = links.length; _i < _len; _i++) {
        link = links[_i];
        _results.push(link.click());
      }
      return _results;
    }
  };

  Solver.prototype._doStuff = function() {
    var cost, node, _i, _len, _ref, _results;
    this.inv.refresh();
    this.probs.refresh();
    _ref = this.probs.tree;
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      node = _ref[_i];
      if (node.costs.length > 0) {
        _results.push((function() {
          var _j, _len1, _ref1, _results1;
          _ref1 = node.costs;
          _results1 = [];
          for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
            cost = _ref1[_j];
            if (cost.name in this.probs.min_requirements) {
              if (cost.name in this.inv.items && this.inv.items[cost.name] >= this.probs.min_requirements[cost.name]) {
                _results1.push(this._do_click(node));
              } else {
                _results1.push(void 0);
              }
            } else {
              _results1.push(this._do_click(node));
            }
          }
          return _results1;
        }).call(this));
      } else {
        _results.push(this._do_click(node));
      }
    }
    return _results;
  };

  return Solver;

})();

intVal = null;

s = new Solver();

start = function() {
  return intVal = setInterval(function() {
    return s._doStuff();
  }, 100);
};

stop = function() {
  return clearInterval(intVal);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment