<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, July 28, 2011, 1:24 PM -->
<!-- MuClient version 4.72 -->

<!-- Plugin "avalon_trade_prices" generated by Plugin Wizard -->

<muclient>
<plugin
   name="avalon_trade_prices"
   author="Matt Adcock"
   id="676d79ae736b9365c7532dfb"
   language="Lua"
   purpose="Search villages for commodities that are for sale"
   save_state="y"
   date_written="2011-07-28 13:19:52"
   requires="4.72"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Use villageprices command in the trade centre to log the current prices in all villages.

Use villagesearch <commodity> to see where it is for sale.
]]>
</description>

</plugin>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Triggers  -->

<triggers>
  <trigger
   group="trade"
   match="^(\w+)\s+(\d+)gp\s+(\d+)gp\s+(\d+)\s+(\w+)\s+(\d+)gp\s+(\d+)gp\s+(\d+)$"
   name="village_prices"
   regexp="y"
   script="villagepricesTrigger"
   sequence="100"
  >
  </trigger>
</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   script="villagepricesAlias"
   match="^villageprices$"
   enabled="y"
   group="trade"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   script="villagesearchAlias"
   match="^villagesearch (.*?)$"
   enabled="y"
   group="trade"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>

<!--  Script  -->


<script>
<![CDATA[
-- village price checking script

require "wait"

villages = { "ELEUSIS", "LADAKH", "SNOWFOOT", "OGG", "ISABELLA", "KRISTANISTI", "NASSAU", "CORIONA", "OAKWOOD'S END", "GREENY'S EDGE", "PILGRIM'S REST", "ASTREA'S DELTA", "EASTPOINT", "MORGANA'S RISE", "ARCHANGEL", "KREMPTON", "ELEUTHERA", "MARSHTON"}

comms = {
    ['village'] = { ['comm1'] = { ['quantity'] = 0, ['buy'] = 0, ['sell'] = 0 }, ['comm2'] = { ['quantity'] = 0, ['buy'] = 0, ['sell'] = 0 }, },
}    

current_village = nil

function villagepricesAlias (name, line, wildcards)
    -- enable capture trigger
    EnableTrigger ("village_prices", true)
    -- clear all existing comms
    comms = {}
    wait.make (function ()
        for _,village in ipairs(villages) do
            current_village = village
            comms[current_village] = {}
            Send ("trade prices " .. current_village)
            wait.time (3.0)
        end
        EnableTrigger ("village_prices", false)
        ColourNote ("orangered", "", "Village prices saved.")
    end )
end

function villagesearchAlias (name, line, wildcards)
    -- wildcards[1] is commodity
    item = wildcards[1]:gsub("%a", string.upper, 1)
    ColourNote ("orangered", "", "Finding " .. item .. " for sale.")
    for village,v_data in pairs(comms) do
        for comm,c_data in pairs(v_data) do
            if comm == item and c_data['quantity'] > 0 and c_data['sell'] > 0 then
                ColourNote ("white", "", "  " .. c_data['quantity'] .. " " .. comm .. " for sale at " .. village .. " for " .. c_data['sell'] .. "gp.")
            end
        end
    end
    ColourNote ("orangered", "", "No more commodities found.")
end

function villagepricesTrigger (name, line, wildcards)
    -- wildcards:
        -- 1 Comm1
        -- 2 Comm1 buy
        -- 3 Comm1 sell
        -- 4 Comm1 quantity
        -- 5 Comm2
        -- 6 Comm2 buy
        -- 7 Comm2 sell
        -- 8 Comm2 quantity
    comms[current_village][wildcards[1]] = {}
    comms[current_village][wildcards[5]] = {}
    comms[current_village][wildcards[1]]['quantity'] = tonumber(wildcards[4]) or 0
    comms[current_village][wildcards[1]]['buy'] = tonumber(wildcards[2]) or 0
    comms[current_village][wildcards[1]]['sell'] = tonumber(wildcards[3]) or 0
    comms[current_village][wildcards[5]]['quantity'] = tonumber(wildcards[8]) or 0
    comms[current_village][wildcards[5]]['buy'] = tonumber(wildcards[6]) or 0
    comms[current_village][wildcards[5]]['sell'] = tonumber(wildcards[7]) or 0
end
]]>
</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="avalon_trade_prices:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>

