void main()
{
buffer page;
page.append(visit_url());
matcher m = create_matcher("<p><b>You have:</b>(.*?</table>)", page);
string [item] ingredients;
string [int] rows;
if (m.find()) {
page = page.replace_string(m.group(0), "");
string table = m.group(1);
m = create_matcher("<td><img.*? title=\"(.*?)\"></td>", table);
while (m.find() && m.group(1).to_item() != $item[none]) {
ingredients[m.group(1).to_item()] = m.group(0);
}
m = create_matcher("<input type=hidden name=action value=buyitem><br>(<table.*?</table>)", page);
if (m.find()) {
table = m.group(1);
buffer newtable;
newtable.append(table.substring(0,table.index_of("<tr rel")));
newtable.append("<tr><td colspan=3></td>");
foreach it in ingredients {
newtable.append(ingredients[it]);
}
newtable.append("</tr>");
newtable.append("<tr><td colspan=3><b>You have:</b></td>");
foreach it in ingredients {
newtable.append("<td><b>" + item_amount(it) + "</b></td>");
}
newtable.append("</tr>");
m = create_matcher("(<tr rel.*? </a></td>)(.*?)(<td valign=center><input class=\"button doit.*?</tr>)", table);
while (m.find()) {
int [item] ingred_count;
newtable.append(m.group(1));
matcher thing = create_matcher("title=\"(.*?)\"></td><td><b>(\\d+)</b>", m.group(2));
while (thing.find() && thing.group(1).to_item() != $item[none]) {
ingred_count[thing.group(1).to_item()] = thing.group(2).to_int();
}
foreach it in ingredients {
newtable.append("<td><b>" + ingred_count[it] + "</b></td>");
}
newtable.append(m.group(3));
}
newtable.append("</table>");
page.replace_string(table, newtable).write();
}
}
}