Skip to content

Instantly share code, notes, and snippets.

@keneanung
Last active January 18, 2019 15:51
Show Gist options
  • Save keneanung/ced1c4517ffda0874ee802ace0eeecbf to your computer and use it in GitHub Desktop.
Save keneanung/ced1c4517ffda0874ee802ace0eeecbf to your computer and use it in GitHub Desktop.
A number of minor scripts that got developed during Mudlet help sessions
-- pattern:
-- \b(?|(black)-(\w+)|(\w+)-(black)|(black))\b
-- alternative pattern (less readable but easier to use when copy pasting as only one occurence of a colour needs to be changed):
-- (?(DEFINE)(black))\b(?|((?1))-(\w+)|(\w+)-((?1))|((?1)))\b
-- tests:
-- black solid-black black-hued [works]
-- test haired test eyed test red-haired blue-eyed [works]
-- scarred red [works]
-- code:
for num, value in ipairs(matches) do
if not value:find("-") then
selectCaptureGroup(num)
fg("red")
end
end
deselect()
resetFormat()
local text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
function testTriggerPerformance()
local time = 0
local stopWatch = createStopWatch()
for i = 1, 100000 do
startStopWatch(stopWatch)
feedTriggers("\n" .. text .. "\n")
echo("\n")
time = time + stopStopWatch(stopWatch)
resetStopWatch(stopWatch)
end
echo("This run took " .. time .. "seconds.")
echo("On average, the text was triggered in " .. (time / 100000) .. "seconds.")
end
------------------------------------------------------------------
-- Trigger name: Get planet name
-- pattern:
-- ^(.+) exchange - all products:$
-- tests:
-- Rouge exchange - all products: [works]
-- code
stocker.current_planet = matches[2]
------------------------------------------------------------------
-- Trigger name: Get planet stock
-- pattern:
-- ^ (\w+):\s+value\s+(\d+)ig/ton\s+Spread:\s+(\d+)%\s+Stock:\s+current (-?\d+)/min (\d+)/max\s+(\d+)\s+Efficiency:\s+(\d+)%$
-- tests:
-- https://rextester.com/live/KWJW34961 [works]
-- code
stocker.update_item(matches[2],
{
value = tonumber(matches[3]),
spread = tonumber(matches[4]),
current_stock = tonumber(matches[5]),
min_stock = tonumber(matches[6]),
max_stock = tonumber(matches[7]),
efficiency = tonumber(matches[8])
})
if matches[2] == "Xmetals" then
-- This was the last item on the listing, we should act now.
stocker.print_negative_stock_on_planet(stocker.current_planet)
end
------------------------------------------------------------------
-- Alias name: print negative stock on all planets
-- command
-- ^print negative stock$
-- code
stocker.print_negative_stock()
------------------------------------------------------------------
-- Script name: Stocker (or whatever)
stocker = stocker or {} -- introduce our root namespace, read up on namespaces at https://forums.mudlet.org/viewtopic.php?t=1211
stocker.exchanges = stocker.exchanges or {} -- this will be the table to house different exchange data
stocker.update_item = function(item_name, item_values)
stocker.exchanges[stocker.current_planet] = stocker.exchanges[stocker.current_planet] or {} -- make sure we have a table for the planet
stocker.exchanges[stocker.current_planet][item_name] = item_values -- add the item with it's values to the currently parsed planet's exchange
end
local print_negative_stock_items = function(items) -- use a local function as a helper. Local things don't litter the global namespace and keep your things nicely together
local found = false -- local helper variable so we can add a message if we didn't find any negative values.
for item_name, values in pairs(items) do -- go over all items and extract the name and its values into different variables
if values.current_stock < 0 then -- check the current stock for negativity
echo(item_name .. " is negative (" .. values.current_stock .. ").") -- print a message that it's negative
found = true -- set helper variable to true to remember we don't need to print anything
end
end
if not found then
echo("No negative stock items found.")
end
end
stocker.print_negative_stock = function()
for planet, items in pairs(stocker.exchanges) do -- go over all exchanges and extract the planet name and all items there into own variables
echo("Searching planet '" .. planet .. "'.") -- print a message about the planet we are looking at
print_negative_stock_items(items) -- call local helper function with the items
end
end
stocker.print_negative_stock_on_planet = function(planet)
echo("Negative items on '" .. planet .. "':")
print_negative_stock_items(stocker.exchanges[planet]) -- call local helper function with the items on the given planet
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment