Last active
September 23, 2023 11:21
-
-
Save ootz0rz/11340286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# | |
# 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 |
Author
ootz0rz
commented
Apr 29, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment